Skip to content

Commit

Permalink
align to latest changes in csstree
Browse files Browse the repository at this point in the history
  • Loading branch information
lahmatiy committed Feb 4, 2017
1 parent 717ba5b commit 6920da5
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 82 deletions.
2 changes: 1 addition & 1 deletion lib/compressor/clean/Declaration.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = function cleanDeclartion(node, item, list) {
if (node.value.children.isEmpty()) {
if (node.value.children && node.value.children.isEmpty()) {
list.remove(item);
}
};
2 changes: 1 addition & 1 deletion lib/compressor/compress/Dimension.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module.exports = function compressDimension(node, item) {

node.value = value;

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

// only length values can be compressed
Expand Down
2 changes: 1 addition & 1 deletion lib/compressor/restructure/1-initialMergeRuleset.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function processRule(node, item, list) {
// try to join rulesets with equal pseudo signature
if (node.pseudoSignature === prev.pseudoSignature) {
// try to join by selectors
if (utils.isEqualLists(prevSelectors, selectors)) {
if (utils.isEqualSelectors(prevSelectors, selectors)) {
prevDeclarations.appendList(declarations);
list.remove(item);
return true;
Expand Down
157 changes: 88 additions & 69 deletions lib/compressor/restructure/6-restructBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var resolveProperty = require('css-tree').property;
var resolveKeyword = require('css-tree').keyword;
var walkRulesRight = require('css-tree').walkRulesRight;
var translate = require('css-tree').translate;
var fingerprintId = 1;
var dontRestructure = {
'src': 1 // https://github.com/afelix/csso/issues/50
};
Expand Down Expand Up @@ -71,90 +72,108 @@ function getPropertyFingerprint(propertyName, declaration, fingerprints) {
var fingerprint = fingerprints[declarationId];

if (!fingerprint) {
var vendorId = '';
var iehack = '';
var special = {};

declaration.value.children.each(function walk(node) {
switch (node.type) {
case 'Argument':
case 'Value':
case 'Parentheses':
node.children.each(walk);
break;

case 'Identifier':
var name = node.name;

if (!vendorId) {
vendorId = resolveKeyword(name).vendor;
}
switch (declaration.value.type) {
case 'Value':
var vendorId = '';
var iehack = '';
var special = {};
var raw = false;

declaration.value.children.each(function walk(node) {
switch (node.type) {
case 'Value':
case 'Brackets':
case 'Parentheses':
node.children.each(walk);
break;

if (/\\[09]/.test(name)) {
iehack = RegExp.lastMatch;
}
case 'Raw':
raw = true;
break;

if (realName === 'cursor') {
if (CURSOR_SAFE_VALUE.indexOf(name) === -1) {
special[name] = true;
}
} else if (DONT_MIX_VALUE.hasOwnProperty(realName)) {
if (DONT_MIX_VALUE[realName].test(name)) {
special[name] = true;
}
}
case 'Identifier':
var name = node.name;

break;
if (!vendorId) {
vendorId = resolveKeyword(name).vendor;
}

case 'Function':
var name = node.name;
if (/\\[09]/.test(name)) {
iehack = RegExp.lastMatch;
}

if (!vendorId) {
vendorId = resolveKeyword(name).vendor;
}
if (realName === 'cursor') {
if (CURSOR_SAFE_VALUE.indexOf(name) === -1) {
special[name] = true;
}
} else if (DONT_MIX_VALUE.hasOwnProperty(realName)) {
if (DONT_MIX_VALUE[realName].test(name)) {
special[name] = true;
}
}

if (name === 'rect') {
// there are 2 forms of rect:
// rect(<top>, <right>, <bottom>, <left>) - standart
// rect(<top> <right> <bottom> <left>) – backwards compatible syntax
// only the same form values can be merged
var hasComma = node.children.some(function(node) {
return node.type === 'Operator' && node.value === ',';
});
if (!hasComma) {
name = 'rect-backward';
}
}
break;

case 'Function':
var name = node.name;

special[name + '()'] = true;
if (!vendorId) {
vendorId = resolveKeyword(name).vendor;
}

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

break;
special[name + '()'] = true;

case 'Dimension':
var unit = node.unit;
// check nested tokens too
node.children.each(walk);

switch (unit) {
// is not supported until IE11
case 'rem':
break;

// v* units is too buggy across browsers and better
// don't merge values with those units
case 'vw':
case 'vh':
case 'vmin':
case 'vmax':
case 'vm': // IE9 supporting "vm" instead of "vmin".
special[unit] = true;
case 'Dimension':
var unit = node.unit;

switch (unit) {
// is not supported until IE11
case 'rem':

// v* units is too buggy across browsers and better
// don't merge values with those units
case 'vw':
case 'vh':
case 'vmin':
case 'vmax':
case 'vm': // IE9 supporting "vm" instead of "vmin".
special[unit] = true;
break;
}
break;
}
break;
}
});
});

fingerprint = '|' + Object.keys(special).sort() + '|' + iehack + vendorId;
fingerprint = raw
? '!' + fingerprintId++
: '!' + Object.keys(special).sort() + '|' + iehack + vendorId;
break;

case 'Raw':
fingerprint = '!' + declaration.value.value;
break;

default:
fingerprint = translate(declaration.value);
}

fingerprints[declarationId] = fingerprint;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/compressor/restructure/8-restructRuleset.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function processRule(node, item, list) {
}

// try to join by selectors
if (allowMergeUp && utils.isEqualLists(prevSelectors, selectors)) {
if (allowMergeUp && utils.isEqualSelectors(prevSelectors, selectors)) {
prevBlock.children.appendList(block.children);
list.remove(item);
return true;
Expand Down
7 changes: 7 additions & 0 deletions lib/compressor/restructure/prepare/specificity.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ module.exports = function specificity(simpleSelector) {
node.children.each(walk);
break;

case 'before':
case 'after':
case 'first-line':
case 'first-letter':
C++;
break;

// TODO: support for :nth-*(.. of <SelectorList>), :matches(), :has()

default:
Expand Down
4 changes: 2 additions & 2 deletions lib/compressor/restructure/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var hasOwnProperty = Object.prototype.hasOwnProperty;

function isEqualLists(a, b) {
function isEqualSelectors(a, b) {
var cursor1 = a.head;
var cursor2 = b.head;

Expand Down Expand Up @@ -130,7 +130,7 @@ function unsafeToSkipNode(node) {
}

module.exports = {
isEqualLists: isEqualLists,
isEqualSelectors: isEqualSelectors,
isEqualDeclarations: isEqualDeclarations,
compareDeclarations: compareDeclarations,
addSelectors: addSelectors,
Expand Down
4 changes: 2 additions & 2 deletions test/fixture/compress/atrules/empty.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
/* single bad declaration */
foo: ;
}
@media {
@media x {
/* empty */
}
@media {
@media x {
/* ruleset with single bad declaration */
a {
test: ;
Expand Down
2 changes: 1 addition & 1 deletion test/fixture/compress/atrules/import-2.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@import 'foo.css';
@media { }
@media all { }
@import 'bar.css';

2 changes: 1 addition & 1 deletion test/fixture/compress/css21/part7-2.css
Original file line number Diff line number Diff line change
@@ -1 +1 @@
@media screen, 3D { E { p: v } }
@media screen, print { E { p: v } }
2 changes: 1 addition & 1 deletion test/fixture/compress/css21/part7-2.min.css
Original file line number Diff line number Diff line change
@@ -1 +1 @@
@media screen,3D{E{p:v}}
@media screen,print{E{p:v}}
2 changes: 1 addition & 1 deletion test/fixture/compress/variables.css
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
:root {
--mainColor: #fb4c42;
--test-var: 5px;
/*--foo: if(x > 5) this.width = 10;*/
--foo: if(x > 5) this.width = 10;
}

.test {
Expand Down
2 changes: 1 addition & 1 deletion test/fixture/compress/variables.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6920da5

Please sign in to comment.