Skip to content

Commit

Permalink
align to last changes in css-tree
Browse files Browse the repository at this point in the history
  • Loading branch information
lahmatiy committed Feb 7, 2017
1 parent a42433c commit 6aa9e69
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 50 deletions.
11 changes: 7 additions & 4 deletions lib/compressor/clean/Rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
});

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
12 changes: 6 additions & 6 deletions lib/compressor/clean/index.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions lib/compressor/compress/atrule/keyframes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion lib/compressor/compress/color.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
22 changes: 11 additions & 11 deletions lib/compressor/compress/index.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion lib/compressor/compress/property/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ module.exports = function compressBackground(node) {
value: '0'
},
{
type: 'Space'
type: 'Space',
value: ' '
},
{
type: 'Number',
Expand Down
4 changes: 2 additions & 2 deletions lib/compressor/restructure/4-restructShorthand.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
32 changes: 16 additions & 16 deletions lib/compressor/restructure/prepare/processSelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down
19 changes: 13 additions & 6 deletions lib/compressor/restructure/prepare/specificity.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;

}
});

Expand Down

0 comments on commit 6aa9e69

Please sign in to comment.