Skip to content

Commit 6920da5

Browse files
committedFeb 4, 2017
align to latest changes in csstree
1 parent 717ba5b commit 6920da5

File tree

13 files changed

+108
-82
lines changed

13 files changed

+108
-82
lines changed
 

‎lib/compressor/clean/Declaration.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module.exports = function cleanDeclartion(node, item, list) {
2-
if (node.value.children.isEmpty()) {
2+
if (node.value.children && node.value.children.isEmpty()) {
33
list.remove(item);
44
}
55
};

‎lib/compressor/compress/Dimension.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ module.exports = function compressDimension(node, item) {
2727

2828
node.value = value;
2929

30-
if (value === '0' && this.declaration) {
30+
if (value === '0' && this.declaration !== null && this.atruleExpression === null) {
3131
var unit = node.unit.toLowerCase();
3232

3333
// only length values can be compressed

‎lib/compressor/restructure/1-initialMergeRuleset.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function processRule(node, item, list) {
1717
// try to join rulesets with equal pseudo signature
1818
if (node.pseudoSignature === prev.pseudoSignature) {
1919
// try to join by selectors
20-
if (utils.isEqualLists(prevSelectors, selectors)) {
20+
if (utils.isEqualSelectors(prevSelectors, selectors)) {
2121
prevDeclarations.appendList(declarations);
2222
list.remove(item);
2323
return true;

‎lib/compressor/restructure/6-restructBlock.js

+88-69
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var resolveProperty = require('css-tree').property;
22
var resolveKeyword = require('css-tree').keyword;
33
var walkRulesRight = require('css-tree').walkRulesRight;
44
var translate = require('css-tree').translate;
5+
var fingerprintId = 1;
56
var dontRestructure = {
67
'src': 1 // https://github.com/afelix/csso/issues/50
78
};
@@ -71,90 +72,108 @@ function getPropertyFingerprint(propertyName, declaration, fingerprints) {
7172
var fingerprint = fingerprints[declarationId];
7273

7374
if (!fingerprint) {
74-
var vendorId = '';
75-
var iehack = '';
76-
var special = {};
77-
78-
declaration.value.children.each(function walk(node) {
79-
switch (node.type) {
80-
case 'Argument':
81-
case 'Value':
82-
case 'Parentheses':
83-
node.children.each(walk);
84-
break;
85-
86-
case 'Identifier':
87-
var name = node.name;
88-
89-
if (!vendorId) {
90-
vendorId = resolveKeyword(name).vendor;
91-
}
75+
switch (declaration.value.type) {
76+
case 'Value':
77+
var vendorId = '';
78+
var iehack = '';
79+
var special = {};
80+
var raw = false;
81+
82+
declaration.value.children.each(function walk(node) {
83+
switch (node.type) {
84+
case 'Value':
85+
case 'Brackets':
86+
case 'Parentheses':
87+
node.children.each(walk);
88+
break;
9289

93-
if (/\\[09]/.test(name)) {
94-
iehack = RegExp.lastMatch;
95-
}
90+
case 'Raw':
91+
raw = true;
92+
break;
9693

97-
if (realName === 'cursor') {
98-
if (CURSOR_SAFE_VALUE.indexOf(name) === -1) {
99-
special[name] = true;
100-
}
101-
} else if (DONT_MIX_VALUE.hasOwnProperty(realName)) {
102-
if (DONT_MIX_VALUE[realName].test(name)) {
103-
special[name] = true;
104-
}
105-
}
94+
case 'Identifier':
95+
var name = node.name;
10696

107-
break;
97+
if (!vendorId) {
98+
vendorId = resolveKeyword(name).vendor;
99+
}
108100

109-
case 'Function':
110-
var name = node.name;
101+
if (/\\[09]/.test(name)) {
102+
iehack = RegExp.lastMatch;
103+
}
111104

112-
if (!vendorId) {
113-
vendorId = resolveKeyword(name).vendor;
114-
}
105+
if (realName === 'cursor') {
106+
if (CURSOR_SAFE_VALUE.indexOf(name) === -1) {
107+
special[name] = true;
108+
}
109+
} else if (DONT_MIX_VALUE.hasOwnProperty(realName)) {
110+
if (DONT_MIX_VALUE[realName].test(name)) {
111+
special[name] = true;
112+
}
113+
}
115114

116-
if (name === 'rect') {
117-
// there are 2 forms of rect:
118-
// rect(<top>, <right>, <bottom>, <left>) - standart
119-
// rect(<top> <right> <bottom> <left>) – backwards compatible syntax
120-
// only the same form values can be merged
121-
var hasComma = node.children.some(function(node) {
122-
return node.type === 'Operator' && node.value === ',';
123-
});
124-
if (!hasComma) {
125-
name = 'rect-backward';
126-
}
127-
}
115+
break;
116+
117+
case 'Function':
118+
var name = node.name;
128119

129-
special[name + '()'] = true;
120+
if (!vendorId) {
121+
vendorId = resolveKeyword(name).vendor;
122+
}
130123

131-
// check nested tokens too
132-
node.children.each(walk);
124+
if (name === 'rect') {
125+
// there are 2 forms of rect:
126+
// rect(<top>, <right>, <bottom>, <left>) - standart
127+
// rect(<top> <right> <bottom> <left>) – backwards compatible syntax
128+
// only the same form values can be merged
129+
var hasComma = node.children.some(function(node) {
130+
return node.type === 'Operator' && node.value === ',';
131+
});
132+
if (!hasComma) {
133+
name = 'rect-backward';
134+
}
135+
}
133136

134-
break;
137+
special[name + '()'] = true;
135138

136-
case 'Dimension':
137-
var unit = node.unit;
139+
// check nested tokens too
140+
node.children.each(walk);
138141

139-
switch (unit) {
140-
// is not supported until IE11
141-
case 'rem':
142+
break;
142143

143-
// v* units is too buggy across browsers and better
144-
// don't merge values with those units
145-
case 'vw':
146-
case 'vh':
147-
case 'vmin':
148-
case 'vmax':
149-
case 'vm': // IE9 supporting "vm" instead of "vmin".
150-
special[unit] = true;
144+
case 'Dimension':
145+
var unit = node.unit;
146+
147+
switch (unit) {
148+
// is not supported until IE11
149+
case 'rem':
150+
151+
// v* units is too buggy across browsers and better
152+
// don't merge values with those units
153+
case 'vw':
154+
case 'vh':
155+
case 'vmin':
156+
case 'vmax':
157+
case 'vm': // IE9 supporting "vm" instead of "vmin".
158+
special[unit] = true;
159+
break;
160+
}
151161
break;
152162
}
153-
break;
154-
}
155-
});
163+
});
156164

157-
fingerprint = '|' + Object.keys(special).sort() + '|' + iehack + vendorId;
165+
fingerprint = raw
166+
? '!' + fingerprintId++
167+
: '!' + Object.keys(special).sort() + '|' + iehack + vendorId;
168+
break;
169+
170+
case 'Raw':
171+
fingerprint = '!' + declaration.value.value;
172+
break;
173+
174+
default:
175+
fingerprint = translate(declaration.value);
176+
}
158177

159178
fingerprints[declarationId] = fingerprint;
160179
}

‎lib/compressor/restructure/8-restructRuleset.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ function processRule(node, item, list) {
5656
}
5757

5858
// try to join by selectors
59-
if (allowMergeUp && utils.isEqualLists(prevSelectors, selectors)) {
59+
if (allowMergeUp && utils.isEqualSelectors(prevSelectors, selectors)) {
6060
prevBlock.children.appendList(block.children);
6161
list.remove(item);
6262
return true;

‎lib/compressor/restructure/prepare/specificity.js

+7
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ module.exports = function specificity(simpleSelector) {
2525
node.children.each(walk);
2626
break;
2727

28+
case 'before':
29+
case 'after':
30+
case 'first-line':
31+
case 'first-letter':
32+
C++;
33+
break;
34+
2835
// TODO: support for :nth-*(.. of <SelectorList>), :matches(), :has()
2936

3037
default:

‎lib/compressor/restructure/utils.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var hasOwnProperty = Object.prototype.hasOwnProperty;
22

3-
function isEqualLists(a, b) {
3+
function isEqualSelectors(a, b) {
44
var cursor1 = a.head;
55
var cursor2 = b.head;
66

@@ -130,7 +130,7 @@ function unsafeToSkipNode(node) {
130130
}
131131

132132
module.exports = {
133-
isEqualLists: isEqualLists,
133+
isEqualSelectors: isEqualSelectors,
134134
isEqualDeclarations: isEqualDeclarations,
135135
compareDeclarations: compareDeclarations,
136136
addSelectors: addSelectors,

‎test/fixture/compress/atrules/empty.css

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
/* single bad declaration */
77
foo: ;
88
}
9-
@media {
9+
@media x {
1010
/* empty */
1111
}
12-
@media {
12+
@media x {
1313
/* ruleset with single bad declaration */
1414
a {
1515
test: ;
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
@import 'foo.css';
2-
@media { }
2+
@media all { }
33
@import 'bar.css';
44

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
@media screen, 3D { E { p: v } }
1+
@media screen, print { E { p: v } }
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
@media screen,3D{E{p:v}}
1+
@media screen,print{E{p:v}}

‎test/fixture/compress/variables.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
:root {
22
--mainColor: #fb4c42;
33
--test-var: 5px;
4-
/*--foo: if(x > 5) this.width = 10;*/
4+
--foo: if(x > 5) this.width = 10;
55
}
66

77
.test {

‎test/fixture/compress/variables.min.css

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
Please sign in to comment.