1 | var generate = require('css-tree').generate;
|
---|
2 | var specificity = require('./specificity');
|
---|
3 |
|
---|
4 | var nonFreezePseudoElements = {
|
---|
5 | 'first-letter': true,
|
---|
6 | 'first-line': true,
|
---|
7 | 'after': true,
|
---|
8 | 'before': true
|
---|
9 | };
|
---|
10 | var nonFreezePseudoClasses = {
|
---|
11 | 'link': true,
|
---|
12 | 'visited': true,
|
---|
13 | 'hover': true,
|
---|
14 | 'active': true,
|
---|
15 | 'first-letter': true,
|
---|
16 | 'first-line': true,
|
---|
17 | 'after': true,
|
---|
18 | 'before': true
|
---|
19 | };
|
---|
20 |
|
---|
21 | module.exports = function freeze(node, usageData) {
|
---|
22 | var pseudos = Object.create(null);
|
---|
23 | var hasPseudo = false;
|
---|
24 |
|
---|
25 | node.prelude.children.each(function(simpleSelector) {
|
---|
26 | var tagName = '*';
|
---|
27 | var scope = 0;
|
---|
28 |
|
---|
29 | simpleSelector.children.each(function(node) {
|
---|
30 | switch (node.type) {
|
---|
31 | case 'ClassSelector':
|
---|
32 | if (usageData && usageData.scopes) {
|
---|
33 | var classScope = usageData.scopes[node.name] || 0;
|
---|
34 |
|
---|
35 | if (scope !== 0 && classScope !== scope) {
|
---|
36 | throw new Error('Selector can\'t has classes from different scopes: ' + generate(simpleSelector));
|
---|
37 | }
|
---|
38 |
|
---|
39 | scope = classScope;
|
---|
40 | }
|
---|
41 | break;
|
---|
42 |
|
---|
43 | case 'PseudoClassSelector':
|
---|
44 | var name = node.name.toLowerCase();
|
---|
45 |
|
---|
46 | if (!nonFreezePseudoClasses.hasOwnProperty(name)) {
|
---|
47 | pseudos[':' + name] = true;
|
---|
48 | hasPseudo = true;
|
---|
49 | }
|
---|
50 | break;
|
---|
51 |
|
---|
52 | case 'PseudoElementSelector':
|
---|
53 | var name = node.name.toLowerCase();
|
---|
54 |
|
---|
55 | if (!nonFreezePseudoElements.hasOwnProperty(name)) {
|
---|
56 | pseudos['::' + name] = true;
|
---|
57 | hasPseudo = true;
|
---|
58 | }
|
---|
59 | break;
|
---|
60 |
|
---|
61 | case 'TypeSelector':
|
---|
62 | tagName = node.name.toLowerCase();
|
---|
63 | break;
|
---|
64 |
|
---|
65 | case 'AttributeSelector':
|
---|
66 | if (node.flags) {
|
---|
67 | pseudos['[' + node.flags.toLowerCase() + ']'] = true;
|
---|
68 | hasPseudo = true;
|
---|
69 | }
|
---|
70 | break;
|
---|
71 |
|
---|
72 | case 'WhiteSpace':
|
---|
73 | case 'Combinator':
|
---|
74 | tagName = '*';
|
---|
75 | break;
|
---|
76 | }
|
---|
77 | });
|
---|
78 |
|
---|
79 | simpleSelector.compareMarker = specificity(simpleSelector).toString();
|
---|
80 | simpleSelector.id = null; // pre-init property to avoid multiple hidden class
|
---|
81 | simpleSelector.id = generate(simpleSelector);
|
---|
82 |
|
---|
83 | if (scope) {
|
---|
84 | simpleSelector.compareMarker += ':' + scope;
|
---|
85 | }
|
---|
86 |
|
---|
87 | if (tagName !== '*') {
|
---|
88 | simpleSelector.compareMarker += ',' + tagName;
|
---|
89 | }
|
---|
90 | });
|
---|
91 |
|
---|
92 | // add property to all rule nodes to avoid multiple hidden class
|
---|
93 | node.pseudoSignature = hasPseudo && Object.keys(pseudos).sort().join(',');
|
---|
94 | };
|
---|