aboutsummaryrefslogtreecommitdiffstats
path: root/theme-compiler/src/com/vaadin/sass/internal/visitor/MixinNodeHandler.java
blob: 4e54416522b8c64d4560ff9c2375c19c99393e60 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
 * Copyright 2000-2013 Vaadin Ltd.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.vaadin.sass.internal.visitor;

import java.util.ArrayList;

import com.vaadin.sass.internal.ScssStylesheet;
import com.vaadin.sass.internal.parser.LexicalUnitImpl;
import com.vaadin.sass.internal.tree.IVariableNode;
import com.vaadin.sass.internal.tree.MixinDefNode;
import com.vaadin.sass.internal.tree.MixinNode;
import com.vaadin.sass.internal.tree.Node;
import com.vaadin.sass.internal.tree.VariableNode;
import com.vaadin.sass.internal.util.DeepCopy;

public class MixinNodeHandler {

    public static void traverse(MixinNode node) throws Exception {
        replaceMixins(node);
    }

    private static void replaceMixins(MixinNode node) throws Exception {
        MixinDefNode mixinDef = ScssStylesheet.getMixinDefinition(node
                .getName());
        if (mixinDef == null) {
            throw new Exception("Mixin Definition: " + node.getName()
                    + " not found");
        }
        replaceMixinNode(node, mixinDef);
    }

    private static void replaceMixinNode(MixinNode mixinNode,
            MixinDefNode mixinDef) {
        Node pre = mixinNode;

        MixinDefNode defClone = (MixinDefNode) DeepCopy.copy(mixinDef);
        defClone.traverse();

        if (mixinDef.getArglist().isEmpty()) {
            for (Node child : new ArrayList<Node>(defClone.getChildren())) {
                mixinNode.getParentNode().appendChild(child, pre);
                pre = child;
            }
        } else {

            replacePossibleArguments(mixinNode, defClone);

            Node previous = mixinNode;
            for (final Node child : new ArrayList<Node>(defClone.getChildren())) {
                replaceChildVariables(defClone, child);
                mixinNode.getParentNode().appendChild(child, previous);
                previous = child;
            }

        }

        mixinNode.getParentNode().removeChild(mixinNode);
    }

    /**
     * We have to replace all the mixin parameters. This is done in two phases.
     * First phase replaces all the named parameters while the second replaces
     * in order of remaining unmodified parameters.
     * 
     * @param mixinNode
     * @param def
     */
    private static void replacePossibleArguments(MixinNode mixinNode,
            MixinDefNode def) {

        if (mixinNode.getArglist().size() > 0) {
            ArrayList<VariableNode> remainingNodes = new ArrayList<VariableNode>(
                    def.getArglist());
            ArrayList<LexicalUnitImpl> remainingUnits = new ArrayList<LexicalUnitImpl>(
                    mixinNode.getArglist());

            for (final LexicalUnitImpl unit : mixinNode.getArglist()) {
                if (unit.getLexicalUnitType() == LexicalUnitImpl.SCSS_VARIABLE
                        && unit.getNextLexicalUnit() != null) {
                    for (final VariableNode node : def.getArglist()) {
                        if (node.getName().equals(unit.getValue().toString())) {
                            node.setExpr((LexicalUnitImpl) DeepCopy.copy(unit
                                    .getNextLexicalUnit()));
                            remainingNodes.remove(node);
                            remainingUnits.remove(unit);
                            break;
                        }
                    }
                }
            }
            checkExtraParameters(mixinNode, remainingNodes.size(),
                    remainingUnits.size());
            for (int i = 0; i < remainingNodes.size()
                    && i < remainingUnits.size(); i++) {
                LexicalUnitImpl unit = remainingUnits.get(i);
                remainingNodes.get(i).setExpr(
                        (LexicalUnitImpl) DeepCopy.copy(unit));
            }
        }

    }

    protected static void checkExtraParameters(MixinNode mixinNode,
            int remainingNodesSize, int remainingUnitsSize) {
        if (remainingUnitsSize > remainingNodesSize) {
            String fileName = null;
            Node root = mixinNode.getParentNode();
            while (root != null && !(root instanceof ScssStylesheet)) {
                root = root.getParentNode();
            }
            if (root != null) {
                fileName = ((ScssStylesheet) root).getFileName();
            }
            StringBuilder builder = new StringBuilder();
            builder.append("More parameters than expected, in Mixin ").append(
                    mixinNode.getName());
            if (fileName != null) {
                builder.append(", in file ").append(fileName);
            }
            throw new RuntimeException(builder.toString());
        }
    }

    private static void replaceChildVariables(MixinDefNode mixinDef, Node node) {
        for (final Node child : node.getChildren()) {
            replaceChildVariables(mixinDef, child);
        }
        if (node instanceof IVariableNode) {
            ((IVariableNode) node).replaceVariables(mixinDef.getArglist());
        }
    }
}