1 | 'use strict';
|
2 |
|
3 | var csstree = require('css-tree'),
|
4 | List = csstree.List,
|
5 | stable = require('stable'),
|
6 | specificity = require('csso/lib/restructure/prepare/specificity');
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | function flattenToSelectors(cssAst) {
|
16 | var selectors = [];
|
17 |
|
18 | csstree.walk(cssAst, {visit: 'Rule', enter: function(node) {
|
19 | if (node.type !== 'Rule') {
|
20 | return;
|
21 | }
|
22 |
|
23 | var atrule = this.atrule;
|
24 | var rule = node;
|
25 |
|
26 | node.prelude.children.each(function(selectorNode, selectorItem) {
|
27 | var selector = {
|
28 | item: selectorItem,
|
29 | atrule: atrule,
|
30 | rule: rule,
|
31 | pseudos: []
|
32 | };
|
33 |
|
34 | selectorNode.children.each(function(selectorChildNode, selectorChildItem, selectorChildList) {
|
35 | if (selectorChildNode.type === 'PseudoClassSelector' ||
|
36 | selectorChildNode.type === 'PseudoElementSelector') {
|
37 | selector.pseudos.push({
|
38 | item: selectorChildItem,
|
39 | list: selectorChildList
|
40 | });
|
41 | }
|
42 | });
|
43 |
|
44 | selectors.push(selector);
|
45 | });
|
46 | }});
|
47 |
|
48 | return selectors;
|
49 | }
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | function filterByMqs(selectors, useMqs) {
|
59 | return selectors.filter(function(selector) {
|
60 | if (selector.atrule === null) {
|
61 | return ~useMqs.indexOf('');
|
62 | }
|
63 |
|
64 | var mqName = selector.atrule.name;
|
65 | var mqStr = mqName;
|
66 | if (selector.atrule.expression &&
|
67 | selector.atrule.expression.children.first().type === 'MediaQueryList') {
|
68 | var mqExpr = csstree.generate(selector.atrule.expression);
|
69 | mqStr = [mqName, mqExpr].join(' ');
|
70 | }
|
71 |
|
72 | return ~useMqs.indexOf(mqStr);
|
73 | });
|
74 | }
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 | function filterByPseudos(selectors, usePseudos) {
|
84 | return selectors.filter(function(selector) {
|
85 | var pseudoSelectorsStr = csstree.generate({
|
86 | type: 'Selector',
|
87 | children: new List().fromArray(selector.pseudos.map(function(pseudo) {
|
88 | return pseudo.item.data;
|
89 | }))
|
90 | });
|
91 | return ~usePseudos.indexOf(pseudoSelectorsStr);
|
92 | });
|
93 | }
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 | function cleanPseudos(selectors) {
|
102 | selectors.forEach(function(selector) {
|
103 | selector.pseudos.forEach(function(pseudo) {
|
104 | pseudo.list.remove(pseudo.item);
|
105 | });
|
106 | });
|
107 | }
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 | function compareSpecificity(aSpecificity, bSpecificity) {
|
119 | for (var i = 0; i < 4; i += 1) {
|
120 | if (aSpecificity[i] < bSpecificity[i]) {
|
121 | return -1;
|
122 | } else if (aSpecificity[i] > bSpecificity[i]) {
|
123 | return 1;
|
124 | }
|
125 | }
|
126 |
|
127 | return 0;
|
128 | }
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 | function compareSimpleSelectorNode(aSimpleSelectorNode, bSimpleSelectorNode) {
|
139 | var aSpecificity = specificity(aSimpleSelectorNode),
|
140 | bSpecificity = specificity(bSimpleSelectorNode);
|
141 | return compareSpecificity(aSpecificity, bSpecificity);
|
142 | }
|
143 |
|
144 | function _bySelectorSpecificity(selectorA, selectorB) {
|
145 | return compareSimpleSelectorNode(selectorA.item.data, selectorB.item.data);
|
146 | }
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 | function sortSelectors(selectors) {
|
156 | return stable(selectors, _bySelectorSpecificity);
|
157 | }
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 | function csstreeToStyleDeclaration(declaration) {
|
167 | var propertyName = declaration.property,
|
168 | propertyValue = csstree.generate(declaration.value),
|
169 | propertyPriority = (declaration.important ? 'important' : '');
|
170 | return {
|
171 | name: propertyName,
|
172 | value: propertyValue,
|
173 | priority: propertyPriority
|
174 | };
|
175 | }
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 | function getCssStr(elem) {
|
185 | return elem.content[0].text || elem.content[0].cdata || [];
|
186 | }
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 | function setCssStr(elem, css) {
|
196 |
|
197 | if(elem.content[0].cdata) {
|
198 | elem.content[0].cdata = css;
|
199 | return elem.content[0].cdata;
|
200 | }
|
201 |
|
202 |
|
203 | elem.content[0].text = css;
|
204 | return elem.content[0].text;
|
205 | }
|
206 |
|
207 |
|
208 | module.exports.flattenToSelectors = flattenToSelectors;
|
209 |
|
210 | module.exports.filterByMqs = filterByMqs;
|
211 | module.exports.filterByPseudos = filterByPseudos;
|
212 | module.exports.cleanPseudos = cleanPseudos;
|
213 |
|
214 | module.exports.compareSpecificity = compareSpecificity;
|
215 | module.exports.compareSimpleSelectorNode = compareSimpleSelectorNode;
|
216 |
|
217 | module.exports.sortSelectors = sortSelectors;
|
218 |
|
219 | module.exports.csstreeToStyleDeclaration = csstreeToStyleDeclaration;
|
220 |
|
221 | module.exports.getCssStr = getCssStr;
|
222 | module.exports.setCssStr = setCssStr;
|