Skip to content

Commit

Permalink
Upgrade css-tree from alpha25 to alpha.28
Browse files Browse the repository at this point in the history
API changed walkFoo(ast, fun) to walk(ast,{visit: 'Rule',enter: fun)
csstree.translate renamed to csstree.generate
csstree.generate throws an error on invalid style where translate silently ignored it - update style processing to catch and ignore instead
Updated tests - csstree now omits more whitespace
  • Loading branch information
shysteph authored and GreLI committed Sep 15, 2018
1 parent e928c1f commit e7b8a6c
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 20 deletions.
10 changes: 5 additions & 5 deletions lib/css-tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var csstree = require('css-tree'),
function flattenToSelectors(cssAst) {
var selectors = [];

csstree.walkRules(cssAst, function(node) {
csstree.walk(cssAst, {visit: 'Rule', enter: function(node) {
if (node.type !== 'Rule') {
return;
}
Expand Down Expand Up @@ -43,7 +43,7 @@ function flattenToSelectors(cssAst) {

selectors.push(selector);
});
});
}});

return selectors;
}
Expand All @@ -65,7 +65,7 @@ function filterByMqs(selectors, useMqs) {
var mqStr = mqName;
if (selector.atrule.expression &&
selector.atrule.expression.children.first().type === 'MediaQueryList') {
var mqExpr = csstree.translate(selector.atrule.expression);
var mqExpr = csstree.generate(selector.atrule.expression);
mqStr = [mqName, mqExpr].join(' ');
}

Expand All @@ -82,7 +82,7 @@ function filterByMqs(selectors, useMqs) {
*/
function filterByPseudos(selectors, usePseudos) {
return selectors.filter(function(selector) {
var pseudoSelectorsStr = csstree.translate({
var pseudoSelectorsStr = csstree.generate({
type: 'Selector',
children: new List().fromArray(selector.pseudos.map(function(pseudo) {
return pseudo.item.data;
Expand Down Expand Up @@ -165,7 +165,7 @@ function sortSelectors(selectors) {
*/
function csstreeToStyleDeclaration(declaration) {
var propertyName = declaration.property,
propertyValue = csstree.translate(declaration.value),
propertyValue = csstree.generate(declaration.value),
propertyPriority = (declaration.important ? 'important' : '');
return {
name: propertyName,
Expand Down
10 changes: 8 additions & 2 deletions lib/svgo/css-style-declaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,14 @@ CSSStyleDeclaration.prototype._loadCssText = function() {

var self = this;
declarations.children.each(function(declaration) {
var styleDeclaration = csstools.csstreeToStyleDeclaration(declaration);
self.setProperty(styleDeclaration.name, styleDeclaration.value, styleDeclaration.priority);
try {
var styleDeclaration = csstools.csstreeToStyleDeclaration(declaration);
self.setProperty(styleDeclaration.name, styleDeclaration.value, styleDeclaration.priority);
} catch(styleError) {
if(styleError.message !== 'Unknown node type: undefined') {
self.parseError = styleError;
}
}
});
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"colors": "~1.1.2",
"css-select": "~1.3.0-rc0",
"css-select-base-adapter": "~0.1.0",
"css-tree": "1.0.0-alpha25",
"css-tree": "1.0.0-alpha.28",
"css-url-regex": "^1.1.0",
"csso": "^3.5.0",
"js-yaml": "~3.10.0",
Expand Down
12 changes: 6 additions & 6 deletions plugins/inlineStyles.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ exports.fn = function(document, opts) {

// match selectors
for (selector of sortedSelectors) {
var selectorStr = csstree.translate(selector.item.data),
var selectorStr = csstree.generate(selector.item.data),
selectedEls = null;

try {
Expand Down Expand Up @@ -143,7 +143,7 @@ exports.fn = function(document, opts) {
}

// merge declarations
csstree.walkDeclarations(selector.rule, function(styleCsstreeDeclaration) {
csstree.walk(selector.rule, {visit: 'Declaration', enter: function(styleCsstreeDeclaration) {

// existing inline styles have higher priority
// no inline styles, external styles, external styles used
Expand All @@ -155,7 +155,7 @@ exports.fn = function(document, opts) {
return;
}
selectedEl.style.setProperty(styleDeclaration.name, styleDeclaration.value, styleDeclaration.priority);
});
}});
}

if (opts.removeMatchedSelectors && selector.selectedEls !== null && selector.selectedEls.length > 0) {
Expand Down Expand Up @@ -202,7 +202,7 @@ exports.fn = function(document, opts) {

// clean up now empty elements
for (var style of styles) {
csstree.walkRules(style.cssAst, function(node, item, list) {
csstree.walk(style.cssAst, {visit: 'Rule', enter: function(node, item, list) {
// clean up <style/> atrules without any rulesets left
if (node.type === 'Atrule' &&
// only Atrules containing rulesets
Expand All @@ -217,7 +217,7 @@ exports.fn = function(document, opts) {
node.prelude.children.isEmpty()) {
list.remove(item);
}
});
}});


if (style.cssAst.children.isEmpty()) {
Expand All @@ -237,7 +237,7 @@ exports.fn = function(document, opts) {


// update existing, left over <style>s
cssTools.setCssStr(style.styleEl, csstree.translate(style.cssAst));
cssTools.setCssStr(style.styleEl, csstree.generate(style.cssAst));
}


Expand Down
2 changes: 1 addition & 1 deletion plugins/prefixIds.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ exports.fn = function(node, opts, extra) {
});

// update <style>s
node.content[0].text = csstree.translate(cssAst);
node.content[0].text = csstree.generate(cssAst);
return node;
}

Expand Down
2 changes: 1 addition & 1 deletion test/plugins/inlineStyles.03.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test/plugins/inlineStyles.13.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test/plugins/inlineStyles.14.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test/plugins/inlineStyles.15.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test/plugins/inlineStyles.19.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit e7b8a6c

Please sign in to comment.