From e7b8a6ce7d4467dee981b43cf25fd01468b5ab03 Mon Sep 17 00:00:00 2001 From: Stephanie Miller Date: Sun, 18 Mar 2018 22:19:17 -0400 Subject: [PATCH] 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 --- lib/css-tools.js | 10 +++++----- lib/svgo/css-style-declaration.js | 10 ++++++++-- package.json | 2 +- plugins/inlineStyles.js | 12 ++++++------ plugins/prefixIds.js | 2 +- test/plugins/inlineStyles.03.svg | 2 +- test/plugins/inlineStyles.13.svg | 2 +- test/plugins/inlineStyles.14.svg | 2 +- test/plugins/inlineStyles.15.svg | 2 +- test/plugins/inlineStyles.19.svg | 2 +- 10 files changed, 26 insertions(+), 20 deletions(-) diff --git a/lib/css-tools.js b/lib/css-tools.js index 4b1e77fb..6517892b 100644 --- a/lib/css-tools.js +++ b/lib/css-tools.js @@ -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, diff --git a/lib/svgo/css-style-declaration.js b/lib/svgo/css-style-declaration.js index 4bb987cd..fb6d7811 100644 --- a/lib/svgo/css-style-declaration.js +++ b/lib/svgo/css-style-declaration.js @@ -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; + } + } }); }; diff --git a/package.json b/package.json index 8b689f6c..676171fd 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/plugins/inlineStyles.js b/plugins/inlineStyles.js index 8573649a..e4f65e00 100644 --- a/plugins/inlineStyles.js +++ b/plugins/inlineStyles.js @@ -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 diff --git a/test/plugins/inlineStyles.13.svg b/test/plugins/inlineStyles.13.svg index 5c7b3961..6bb9b8bd 100644 --- a/test/plugins/inlineStyles.13.svg +++ b/test/plugins/inlineStyles.13.svg @@ -61,7 +61,7 @@ diff --git a/test/plugins/inlineStyles.14.svg b/test/plugins/inlineStyles.14.svg index b89d3b36..4dd4a0fb 100644 --- a/test/plugins/inlineStyles.14.svg +++ b/test/plugins/inlineStyles.14.svg @@ -19,7 +19,7 @@ diff --git a/test/plugins/inlineStyles.15.svg b/test/plugins/inlineStyles.15.svg index eb9dabfc..50618df1 100644 --- a/test/plugins/inlineStyles.15.svg +++ b/test/plugins/inlineStyles.15.svg @@ -58,7 +58,7 @@ diff --git a/test/plugins/inlineStyles.19.svg b/test/plugins/inlineStyles.19.svg index 2d7e429b..cb847f95 100644 --- a/test/plugins/inlineStyles.19.svg +++ b/test/plugins/inlineStyles.19.svg @@ -36,7 +36,7 @@