1
0
mirror of https://github.com/svg/svgo.git synced 2025-07-29 20:21:14 +03:00

Upgrade css-tree from alpha25 to alpha.28

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
This commit is contained in:
Stephanie Miller
2018-03-18 22:19:17 -04:00
committed by Lev Solntsev
parent e928c1f050
commit e7b8a6ce7d
10 changed files with 26 additions and 20 deletions

View File

@ -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;
}
@ -43,7 +43,7 @@ function flattenToSelectors(cssAst) {
selectors.push(selector);
});
});
}});
return selectors;
}
@ -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(' ');
}
@ -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;
@ -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,

View File

@ -127,8 +127,14 @@ CSSStyleDeclaration.prototype._loadCssText = function() {
var self = this;
declarations.children.each(function(declaration) {
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;
}
}
});
};

View File

@ -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",

View File

@ -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 {
@ -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
@ -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) {
@ -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
@ -217,7 +217,7 @@ exports.fn = function(document, opts) {
node.prelude.children.isEmpty()) {
list.remove(item);
}
});
}});
if (style.cssAst.children.isEmpty()) {
@ -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));
}

View File

@ -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;
}