source: trip-planner-front/node_modules/csso/lib/restructure/prepare/processSelector.js@ 188ee53

Last change on this file since 188ee53 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.9 KB
Line 
1var generate = require('css-tree').generate;
2var specificity = require('./specificity');
3
4var nonFreezePseudoElements = {
5 'first-letter': true,
6 'first-line': true,
7 'after': true,
8 'before': true
9};
10var 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
21module.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};
Note: See TracBrowser for help on using the repository browser.