From 6aa9e69862b2791aab33476abf251b44b02feeeb Mon Sep 17 00:00:00 2001 From: Roman Dvornov Date: Tue, 7 Feb 2017 23:35:00 +0300 Subject: [PATCH] align to last changes in css-tree --- lib/compressor/clean/Rule.js | 11 ++++--- .../clean/{Universal.js => TypeSelector.js} | 9 +++++- lib/compressor/clean/index.js | 12 +++---- .../{Attribute.js => AttributeSelector.js} | 0 lib/compressor/compress/atrule/keyframes.js | 4 +-- lib/compressor/compress/color.js | 3 +- lib/compressor/compress/index.js | 22 ++++++------- .../compress/property/background.js | 3 +- .../restructure/4-restructShorthand.js | 4 +-- .../restructure/prepare/processSelector.js | 32 +++++++++---------- .../restructure/prepare/specificity.js | 19 +++++++---- 11 files changed, 69 insertions(+), 50 deletions(-) rename lib/compressor/clean/{Universal.js => TypeSelector.js} (51%) rename lib/compressor/compress/{Attribute.js => AttributeSelector.js} (100%) diff --git a/lib/compressor/clean/Rule.js b/lib/compressor/clean/Rule.js index c4c37f1e..ea013aaf 100644 --- a/lib/compressor/clean/Rule.js +++ b/lib/compressor/clean/Rule.js @@ -4,15 +4,18 @@ function cleanUnused(node, usageData) { return node.selector.children.each(function(selector, item, list) { var hasUnused = selector.children.some(function(node) { switch (node.type) { - case 'Class': + case 'ClassSelector': return usageData.classes && !hasOwnProperty.call(usageData.classes, node.name); - case 'Id': + case 'IdSelector': return usageData.ids && !hasOwnProperty.call(usageData.ids, node.name); - case 'Type': + case 'TypeSelector': // TODO: remove toLowerCase when type selectors will be normalized - return usageData.tags && !hasOwnProperty.call(usageData.tags, node.name.toLowerCase()); + // ignore universal selectors + if (node.name.charAt(node.name.length - 1) !== '*') { + return usageData.tags && !hasOwnProperty.call(usageData.tags, node.name.toLowerCase()); + } } }); diff --git a/lib/compressor/clean/Universal.js b/lib/compressor/clean/TypeSelector.js similarity index 51% rename from lib/compressor/clean/Universal.js rename to lib/compressor/clean/TypeSelector.js index d20718e2..84bbab78 100644 --- a/lib/compressor/clean/Universal.js +++ b/lib/compressor/clean/TypeSelector.js @@ -1,5 +1,12 @@ // remove useless universal selector -module.exports = function cleanIdentifier(node, item, list) { +module.exports = function cleanType(node, item, list) { + var name = item.data.name; + + // check it's a universal selector + if (name.charAt(name.length - 1) !== '*') { + return; + } + // remove when universal selector isn't last or before combinator if (item.next !== null && item.next.data.type !== 'Combinator') { list.remove(item); diff --git a/lib/compressor/clean/index.js b/lib/compressor/clean/index.js index a4981fbd..0ca05ff0 100644 --- a/lib/compressor/clean/index.js +++ b/lib/compressor/clean/index.js @@ -1,11 +1,11 @@ var walk = require('css-tree').walkUp; var handlers = { - Space: require('./Space.js'), - Atrule: require('./Atrule.js'), - Rule: require('./Rule.js'), - Declaration: require('./Declaration.js'), - Universal: require('./Universal.js'), - Comment: require('./Comment.js') + Space: require('./Space'), + Atrule: require('./Atrule'), + Rule: require('./Rule'), + Declaration: require('./Declaration'), + TypeSelector: require('./TypeSelector'), + Comment: require('./Comment') }; module.exports = function(ast, usageData) { diff --git a/lib/compressor/compress/Attribute.js b/lib/compressor/compress/AttributeSelector.js similarity index 100% rename from lib/compressor/compress/Attribute.js rename to lib/compressor/compress/AttributeSelector.js diff --git a/lib/compressor/compress/atrule/keyframes.js b/lib/compressor/compress/atrule/keyframes.js index 94975d55..47956f21 100644 --- a/lib/compressor/compress/atrule/keyframes.js +++ b/lib/compressor/compress/atrule/keyframes.js @@ -4,11 +4,11 @@ module.exports = function(node) { simpleselector.children.each(function(data, item) { if (data.type === 'Percentage' && data.value === '100') { item.data = { - type: 'Type', + type: 'TypeSelector', loc: data.loc, name: 'to' }; - } else if (data.type === 'Type' && data.name === 'from') { + } else if (data.type === 'TypeSelector' && data.name === 'from') { item.data = { type: 'Percentage', loc: data.loc, diff --git a/lib/compressor/compress/color.js b/lib/compressor/compress/color.js index 6334c222..ce99268a 100644 --- a/lib/compressor/compress/color.js +++ b/lib/compressor/compress/color.js @@ -416,7 +416,8 @@ function compressFunction(node, item, list) { var next = item.next; if (next && next.data.type !== 'Space') { list.insert(list.createItem({ - type: 'Space' + type: 'Space', + value: ' ' }), next); } diff --git a/lib/compressor/compress/index.js b/lib/compressor/compress/index.js index a44189b5..521f00fc 100644 --- a/lib/compressor/compress/index.js +++ b/lib/compressor/compress/index.js @@ -1,16 +1,16 @@ var walk = require('css-tree').walkUp; var handlers = { - Atrule: require('./Atrule.js'), - Attribute: require('./Attribute.js'), - Value: require('./Value.js'), - Dimension: require('./Dimension.js'), - Percentage: require('./Number.js'), - Number: require('./Number.js'), - String: require('./String.js'), - Url: require('./Url.js'), - Hash: require('./color.js').compressHex, - Identifier: require('./color.js').compressIdent, - Function: require('./color.js').compressFunction + Atrule: require('./Atrule'), + AttributeSelector: require('./AttributeSelector'), + Value: require('./Value'), + Dimension: require('./Dimension'), + Percentage: require('./Number'), + Number: require('./Number'), + String: require('./String'), + Url: require('./Url'), + Hash: require('./color').compressHex, + Identifier: require('./color').compressIdent, + Function: require('./color').compressFunction }; module.exports = function(ast) { diff --git a/lib/compressor/compress/property/background.js b/lib/compressor/compress/property/background.js index 563e8558..f99c70cb 100644 --- a/lib/compressor/compress/property/background.js +++ b/lib/compressor/compress/property/background.js @@ -20,7 +20,8 @@ module.exports = function compressBackground(node) { value: '0' }, { - type: 'Space' + type: 'Space', + value: ' ' }, { type: 'Number', diff --git a/lib/compressor/restructure/4-restructShorthand.js b/lib/compressor/restructure/4-restructShorthand.js index c41da9e3..018c7537 100644 --- a/lib/compressor/restructure/4-restructShorthand.js +++ b/lib/compressor/restructure/4-restructShorthand.js @@ -296,14 +296,14 @@ TRBL.prototype.getValue = function() { for (var i = 0; i < values.length; i++) { if (i) { - result.appendData({ type: 'Space' }); + result.appendData({ type: 'Space', value: ' ' }); } result.appendData(values[i].node); } if (this.iehack) { - result.appendData({ type: 'Space' }); + result.appendData({ type: 'Space', value: ' ' }); result.appendData({ type: 'Identifier', loc: null, diff --git a/lib/compressor/restructure/prepare/processSelector.js b/lib/compressor/restructure/prepare/processSelector.js index 24535824..de5f8b9b 100644 --- a/lib/compressor/restructure/prepare/processSelector.js +++ b/lib/compressor/restructure/prepare/processSelector.js @@ -28,7 +28,7 @@ module.exports = function freeze(node, usageData) { simpleSelector.children.some(function(node) { switch (node.type) { - case 'Class': + case 'ClassSelector': if (usageData && usageData.scopes) { var classScope = usageData.scopes[node.name] || 0; @@ -40,36 +40,36 @@ module.exports = function freeze(node, usageData) { } break; - case 'PseudoClass': - if (!nonFreezePseudoClasses.hasOwnProperty(node.name)) { - pseudos[node.name] = true; + case 'PseudoClassSelector': + var name = node.name.toLowerCase(); + + if (!nonFreezePseudoClasses.hasOwnProperty(name)) { + pseudos[name] = true; hasPseudo = true; } break; - case 'PseudoElement': - if (!nonFreezePseudoElements.hasOwnProperty(node.name)) { - pseudos[node.name] = true; + case 'PseudoElementSelector': + var name = node.name.toLowerCase(); + + if (!nonFreezePseudoElements.hasOwnProperty(name)) { + pseudos[name] = true; hasPseudo = true; } break; - case 'Negation': - pseudos.not = true; - hasPseudo = true; - break; - - case 'Type': - tagName = node.name; + case 'TypeSelector': + tagName = node.name.toLowerCase(); break; - case 'Attribute': + case 'AttributeSelector': if (node.flags) { - pseudos['[' + node.flags + ']'] = true; + pseudos['[' + node.flags.toLowerCase() + ']'] = true; hasPseudo = true; } break; + case 'Space': case 'Combinator': tagName = '*'; break; diff --git a/lib/compressor/restructure/prepare/specificity.js b/lib/compressor/restructure/prepare/specificity.js index 60f52bc9..555de1eb 100644 --- a/lib/compressor/restructure/prepare/specificity.js +++ b/lib/compressor/restructure/prepare/specificity.js @@ -10,16 +10,16 @@ module.exports = function specificity(simpleSelector) { node.children.each(walk); break; - case 'Id': + case 'IdSelector': A++; break; - case 'Class': - case 'Attribute': + case 'ClassSelector': + case 'AttributeSelector': B++; break; - case 'PseudoClass': + case 'PseudoClassSelector': switch (node.name.toLowerCase()) { case 'not': node.children.each(walk); @@ -39,10 +39,17 @@ module.exports = function specificity(simpleSelector) { } break; - case 'Type': - case 'PseudoElement': + case 'PseudoElementSelector': C++; break; + + case 'TypeSelector': + // ignore universal selector + if (node.name.charAt(node.name.length - 1) !== '*') { + C++; + } + break; + } });