diff --git a/lib/css-tools.js b/lib/css-tools.js index ba8708bc..b2a1f5ff 100644 --- a/lib/css-tools.js +++ b/lib/css-tools.js @@ -189,7 +189,10 @@ function csstreeToStyleDeclaration(declaration) { * @return {string|Array} CSS string or empty array if no styles are set */ function getCssStr(elem) { - return elem.content[0].text || elem.content[0].cdata || []; + if (elem.content[0].type === 'text' || elem.content[0].type === 'cdata') { + return elem.content[0].value; + } + return ''; } /** @@ -200,15 +203,11 @@ function getCssStr(elem) { * @return {Object} reference to field with CSS */ function setCssStr(elem, css) { - // in case of cdata field - if (elem.content[0].cdata) { - elem.content[0].cdata = css; - return elem.content[0].cdata; + if (elem.content[0].type === 'text' || elem.content[0].type === 'cdata') { + elem.content[0].value = css; + return elem.content[0].value; } - - // in case of text field + if nothing was set yet - elem.content[0].text = css; - return elem.content[0].text; + return css; } module.exports.flattenToSelectors = flattenToSelectors; diff --git a/lib/style.js b/lib/style.js index 9d95624d..ae9fb6c8 100644 --- a/lib/style.js +++ b/lib/style.js @@ -154,8 +154,9 @@ const computeStyle = (node) => { ) { const children = styleNode.content || []; for (const child of children) { - const css = child.text || child.cdata; - stylesheet.push(...parseStylesheet(css, dynamic)); + if (child.type === 'text' || child.type === 'cdata') { + stylesheet.push(...parseStylesheet(child.value, dynamic)); + } } } } diff --git a/lib/svgo/css-select-adapter.js b/lib/svgo/css-select-adapter.js index ffb5c0cb..d8fbd583 100644 --- a/lib/svgo/css-select-adapter.js +++ b/lib/svgo/css-select-adapter.js @@ -40,7 +40,10 @@ const getSiblings = (elem) => { }; const getText = (node) => { - return node.content[0].text || node.content[0].cdata || ''; + if (node.content[0].type === 'text' && node.content[0].type === 'cdata') { + return node.content[0].value; + } + return ''; }; const hasAttrib = (elem, name) => { diff --git a/lib/svgo/js2svg.js b/lib/svgo/js2svg.js index 5ba604ae..61279a8f 100644 --- a/lib/svgo/js2svg.js +++ b/lib/svgo/js2svg.js @@ -99,16 +99,16 @@ JS2SVG.prototype.convert = function (data) { data.content.forEach(function (item) { if (item.elem) { svg += this.createElem(item); - } else if (item.text) { - svg += this.createText(item.text); + } else if (item.type === 'text') { + svg += this.createText(item); } else if (item.type === 'doctype') { svg += this.createDoctype(item); } else if (item.type === 'instruction') { svg += this.createProcInst(item); } else if (item.type === 'comment') { svg += this.createComment(item); - } else if (item.cdata) { - svg += this.createCDATA(item.cdata); + } else if (item.type === 'cdata') { + svg += this.createCDATA(item); } }, this); } @@ -184,9 +184,10 @@ JS2SVG.prototype.createComment = function (node) { * * @return {String} */ -JS2SVG.prototype.createCDATA = function (cdata) { +JS2SVG.prototype.createCDATA = function (node) { + const { value } = node; return ( - this.createIndent() + this.config.cdataStart + cdata + this.config.cdataEnd + this.createIndent() + this.config.cdataStart + value + this.config.cdataEnd ); }; @@ -308,11 +309,12 @@ JS2SVG.prototype.createAttrs = function (elem) { * * @return {String} */ -JS2SVG.prototype.createText = function (text) { +JS2SVG.prototype.createText = function (node) { + const { value } = node; return ( this.createIndent() + this.config.textStart + - text.replace(this.config.regEntities, this.config.encodeEntity) + + value.replace(this.config.regEntities, this.config.encodeEntity) + (this.textContext ? '' : this.config.textEnd) ); }; diff --git a/lib/svgo/svg2js.js b/lib/svgo/svg2js.js index b77f3049..35e5d97a 100644 --- a/lib/svgo/svg2js.js +++ b/lib/svgo/svg2js.js @@ -74,7 +74,8 @@ module.exports = function (data) { sax.oncdata = function (cdata) { pushToContent({ - cdata: cdata, + type: 'cdata', + value: cdata, }); }; @@ -115,9 +116,15 @@ module.exports = function (data) { sax.ontext = function (text) { // prevent trimming of meaningful whitespace inside textual tags if (textElems.includes(current.elem) && !data.prefix) { - pushToContent({ text: text }); + pushToContent({ + type: 'text', + value: text, + }); } else if (/\S/.test(text)) { - pushToContent({ text: text.trim() }); + pushToContent({ + type: 'text', + value: text.trim(), + }); } }; diff --git a/plugins/minifyStyles.js b/plugins/minifyStyles.js index ef3e504a..9df19578 100755 --- a/plugins/minifyStyles.js +++ b/plugins/minifyStyles.js @@ -38,17 +38,18 @@ exports.fn = function (ast, options) { elems.forEach(function (elem) { if (elem.isElem('style')) { - //