1 | // This extractor is used in level 2 optimizations
|
---|
2 | // IMPORTANT: Mind Token class and this code is not related!
|
---|
3 | // Properties will be tokenized in one step, see #429
|
---|
4 |
|
---|
5 | var Token = require('../../tokenizer/token');
|
---|
6 | var serializeRules = require('../../writer/one-time').rules;
|
---|
7 | var serializeValue = require('../../writer/one-time').value;
|
---|
8 |
|
---|
9 | function extractProperties(token) {
|
---|
10 | var properties = [];
|
---|
11 | var inSpecificSelector;
|
---|
12 | var property;
|
---|
13 | var name;
|
---|
14 | var value;
|
---|
15 | var i, l;
|
---|
16 |
|
---|
17 | if (token[0] == Token.RULE) {
|
---|
18 | inSpecificSelector = !/[.+>~]/.test(serializeRules(token[1]));
|
---|
19 |
|
---|
20 | for (i = 0, l = token[2].length; i < l; i++) {
|
---|
21 | property = token[2][i];
|
---|
22 |
|
---|
23 | if (property[0] != Token.PROPERTY) { continue; }
|
---|
24 |
|
---|
25 | name = property[1][1];
|
---|
26 | if (name.length === 0) { continue; }
|
---|
27 |
|
---|
28 | value = serializeValue(property, i);
|
---|
29 |
|
---|
30 | properties.push([
|
---|
31 | name,
|
---|
32 | value,
|
---|
33 | findNameRoot(name),
|
---|
34 | token[2][i],
|
---|
35 | name + ':' + value,
|
---|
36 | token[1],
|
---|
37 | inSpecificSelector
|
---|
38 | ]);
|
---|
39 | }
|
---|
40 | } else if (token[0] == Token.NESTED_BLOCK) {
|
---|
41 | for (i = 0, l = token[2].length; i < l; i++) {
|
---|
42 | properties = properties.concat(extractProperties(token[2][i]));
|
---|
43 | }
|
---|
44 | }
|
---|
45 |
|
---|
46 | return properties;
|
---|
47 | }
|
---|
48 |
|
---|
49 | function findNameRoot(name) {
|
---|
50 | if (name == 'list-style') { return name; }
|
---|
51 | if (name.indexOf('-radius') > 0) { return 'border-radius'; }
|
---|
52 | if (name == 'border-collapse' || name == 'border-spacing' || name == 'border-image') { return name; }
|
---|
53 | if (name.indexOf('border-') === 0 && /^border-\w+-\w+$/.test(name)) { return name.match(/border-\w+/)[0]; }
|
---|
54 | if (name.indexOf('border-') === 0 && /^border-\w+$/.test(name)) { return 'border'; }
|
---|
55 | if (name.indexOf('text-') === 0) { return name; }
|
---|
56 | if (name == '-chrome-') { return name; }
|
---|
57 |
|
---|
58 | return name.replace(/^-\w+-/, '').match(/([a-zA-Z]+)/)[0].toLowerCase();
|
---|
59 | }
|
---|
60 |
|
---|
61 | module.exports = extractProperties;
|
---|