aboutsummaryrefslogtreecommitdiffstats
path: root/theme-compiler/src/com/vaadin/sass/tree/ListRemoveNode.java
blob: 114e095a9cb8bd1b55dd40b976f4d3f49915270d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package com.vaadin.sass.tree;

import java.util.ArrayList;

import com.vaadin.sass.parser.LexicalUnitImpl;
import com.vaadin.sass.util.DeepCopy;

public class ListRemoveNode extends Node implements ListModifyNode,
        IVariableNode {

    private ArrayList<String> list;
    private ArrayList<String> remove;
    private String separator;

    public ListRemoveNode(ArrayList<String> list, ArrayList<String> remove,
            String separator) {
        this.list = list;
        this.remove = remove;
        this.separator = separator;
    }

    @Override
    public boolean isModifyingVariable() {
        if (list != null) {
            return list.size() == 1 && list.get(0).startsWith("$");
        }
        return false;
    }

    @Override
    public String getVariable() {
        if (list != null && list.size() == 1) {
            String string = list.get(0);
            return string.substring(1, string.length());
        }
        return null;
    }

    @Override
    public VariableNode getModifiedList(VariableNode variableNode) {

        VariableNode clone = (VariableNode) DeepCopy.copy(variableNode);

        LexicalUnitImpl first = null;
        LexicalUnitImpl current = clone.getExpr();
        LexicalUnitImpl lastAccepted = null;
        while (current != null) {

            if (shouldInclude(current, lastAccepted)) {
                LexicalUnitImpl temp = current.clone();
                temp.setNextLexicalUnit(null);

                if (lastAccepted != null) {
                    lastAccepted.setNextLexicalUnit(temp);
                }

                lastAccepted = temp;

                if (first == null) {
                    first = lastAccepted;
                }
            }
            current = current.getNextLexicalUnit();
        }

        clone.setExpr(first);

        return clone;
    }

    private boolean shouldInclude(LexicalUnitImpl current,
            LexicalUnitImpl lastAccepted) {

        if (lastAccepted != null
                && lastAccepted.getLexicalUnitType() == LexicalUnitImpl.SAC_OPERATOR_COMMA
                && current.getLexicalUnitType() == LexicalUnitImpl.SAC_OPERATOR_COMMA) {
            return false;
        }

        String string = current.getValue().toString();
        for (final String s : remove) {
            if (s.equals(string)) {
                return false;
            }
        }
        return true;
    }

    @Override
    public void replaceVariables(ArrayList<VariableNode> variables) {
        ArrayList<String> newList = new ArrayList<String>();

        for (final String removeVar : remove) {
            if (!removeVar.startsWith("$")) {
                continue;
            }

            for (final VariableNode var : variables) {
                if (removeVar.equals("$" + var.getName())) {
                    LexicalUnitImpl expr = var.getExpr();
                    while (expr != null) {
                        newList.add(expr.getValue().toString());
                        expr = expr.getNextLexicalUnit();
                    }

                }
            }
        }
        if (newList.size() > 0) {
            remove = newList;
        }

    }
}