From 471aec5e250c9e3c70afc1d795996ca104321753 Mon Sep 17 00:00:00 2001 From: strarsis Date: Tue, 31 Oct 2017 11:50:20 +0100 Subject: [PATCH] Fix #820 (#821) * Add test for inlineStyles plugin/CSSStyleDeclaration API. * Adjust test. * Always initialize element attrs with empty object. Improve code. --- lib/svgo/css-style-declaration.js | 10 +++++----- lib/svgo/svg2js.js | 7 +++---- test/plugins/inlineStyles.17.svg | 18 ++++++++++++++++++ 3 files changed, 26 insertions(+), 9 deletions(-) create mode 100644 test/plugins/inlineStyles.17.svg diff --git a/lib/svgo/css-style-declaration.js b/lib/svgo/css-style-declaration.js index 43527fd10..5b2300b14 100644 --- a/lib/svgo/css-style-declaration.js +++ b/lib/svgo/css-style-declaration.js @@ -18,11 +18,6 @@ var CSSStyleDeclaration = function(node) { CSSStyleDeclaration.prototype.hasStyle = function() { - this.styleAttr = { // empty style attr - 'name': 'style', - 'value': null - }; - this.addStyleHandler(); }; @@ -33,6 +28,11 @@ CSSStyleDeclaration.prototype.hasStyle = function() { CSSStyleDeclaration.prototype.addStyleHandler = function() { + this.styleAttr = { // empty style attr + 'name': 'style', + 'value': null + }; + Object.defineProperty(this.parentNode.attrs, 'style', { get: this.getStyleAttr.bind(this), set: this.setStyleAttr.bind(this), diff --git a/lib/svgo/svg2js.js b/lib/svgo/svg2js.js index 0f7459417..6bba13e58 100644 --- a/lib/svgo/svg2js.js +++ b/lib/svgo/svg2js.js @@ -87,15 +87,14 @@ module.exports = function(data, callback) { var elem = { elem: data.name, prefix: data.prefix, - local: data.local + local: data.local, + attrs: {} }; + elem.class = new CSSClassList(elem); elem.style = new CSSStyleDeclaration(elem); if (Object.keys(data.attributes).length) { - elem.attrs = {}; - - for (var name in data.attributes) { if (name === 'class') { // has class attribute diff --git a/test/plugins/inlineStyles.17.svg b/test/plugins/inlineStyles.17.svg new file mode 100644 index 000000000..e5bce9cf8 --- /dev/null +++ b/test/plugins/inlineStyles.17.svg @@ -0,0 +1,18 @@ + + + +
hello, world
+
+
+ +@@@ + + + + +
+ hello, world +
+ +
+