diff --git a/lib/style.js b/lib/style.js index ae9fb6c8..68a74248 100644 --- a/lib/style.js +++ b/lib/style.js @@ -168,7 +168,7 @@ const computeStyle = (node) => { // collect inherited styles const computedStyles = computeOwnStyle(node, stylesheet); let parent = node; - while (parent.parentNode && parent.parentNode.elem !== '#document') { + while (parent.parentNode && parent.parentNode.type !== 'root') { const inheritedStyles = computeOwnStyle(parent.parentNode, stylesheet); for (const [name, computed] of Object.entries(inheritedStyles)) { if ( diff --git a/lib/svgo/css-select-adapter.js b/lib/svgo/css-select-adapter.js index d8fbd583..e7e5c394 100644 --- a/lib/svgo/css-select-adapter.js +++ b/lib/svgo/css-select-adapter.js @@ -5,7 +5,7 @@ * @return {node is any} */ const isTag = (node) => { - return node.isElem(); + return node.type === 'element'; }; const existsOne = (test, elems) => { @@ -27,7 +27,7 @@ const getChildren = (node) => { }; const getName = (elemAst) => { - return elemAst.elem; + return elemAst.name; }; const getParent = (node) => { diff --git a/lib/svgo/js2svg.js b/lib/svgo/js2svg.js index 61279a8f..c1a4997d 100644 --- a/lib/svgo/js2svg.js +++ b/lib/svgo/js2svg.js @@ -97,17 +97,22 @@ JS2SVG.prototype.convert = function (data) { this.indentLevel++; data.content.forEach(function (item) { - if (item.elem) { + if (item.type === 'element') { svg += this.createElem(item); - } else if (item.type === 'text') { + } + if (item.type === 'text') { svg += this.createText(item); - } else if (item.type === 'doctype') { + } + if (item.type === 'doctype') { svg += this.createDoctype(item); - } else if (item.type === 'instruction') { + } + if (item.type === 'instruction') { svg += this.createProcInst(item); - } else if (item.type === 'comment') { + } + if (item.type === 'comment') { svg += this.createComment(item); - } else if (item.type === 'cdata') { + } + if (item.type === 'cdata') { svg += this.createCDATA(item); } }, this); @@ -211,7 +216,7 @@ JS2SVG.prototype.createElem = function (data) { return ( this.createIndent() + this.config.tagShortStart + - data.elem + + data.name + this.createAttrs(data) + this.config.tagShortEnd ); @@ -219,11 +224,11 @@ JS2SVG.prototype.createElem = function (data) { return ( this.createIndent() + this.config.tagShortStart + - data.elem + + data.name + this.createAttrs(data) + this.config.tagOpenEnd + this.config.tagCloseStart + - data.elem + + data.name + this.config.tagCloseEnd ); } @@ -260,14 +265,14 @@ JS2SVG.prototype.createElem = function (data) { return ( openIndent + tagOpenStart + - data.elem + + data.name + this.createAttrs(data) + tagOpenEnd + processedData + dataEnd + closeIndent + tagCloseStart + - data.elem + + data.name + tagCloseEnd ); } diff --git a/lib/svgo/jsAPI.js b/lib/svgo/jsAPI.js index 972d2f22..b3cf989b 100644 --- a/lib/svgo/jsAPI.js +++ b/lib/svgo/jsAPI.js @@ -68,11 +68,16 @@ JSAPI.prototype.clone = function () { * @return {Boolean} */ JSAPI.prototype.isElem = function (param) { - if (!param) return !!this.elem; - - if (Array.isArray(param)) return !!this.elem && param.indexOf(this.elem) > -1; - - return !!this.elem && this.elem === param; + if (this.type !== 'element') { + return false; + } + if (param == null) { + return true; + } + if (Array.isArray(param)) { + return param.includes(this.name); + } + return this.name === param; }; /** @@ -82,7 +87,7 @@ JSAPI.prototype.isElem = function (param) { * @return {Object} element */ JSAPI.prototype.renameElem = function (name) { - if (name && typeof name === 'string') this.elem = name; + if (name && typeof name === 'string') this.name = name; return this; }; diff --git a/lib/svgo/svg2js.js b/lib/svgo/svg2js.js index 35e5d97a..d3eb3211 100644 --- a/lib/svgo/svg2js.js +++ b/lib/svgo/svg2js.js @@ -23,16 +23,14 @@ var config = { */ module.exports = function (data) { var sax = SAX.parser(config.strict, config), - root = new JSAPI({ elem: '#document', content: [] }), + root = new JSAPI({ type: 'root', content: [] }), current = root, stack = [root]; function pushToContent(content) { - content = new JSAPI(content, current); - - (current.content = current.content || []).push(content); - - return content; + const wrapped = new JSAPI(content, current); + current.content.push(wrapped); + return wrapped; } sax.ondoctype = function (doctype) { @@ -80,42 +78,44 @@ module.exports = function (data) { }; sax.onopentag = function (data) { - var elem = { - elem: data.name, + var element = { + type: 'element', + name: data.name, attrs: {}, + content: [], }; - elem.class = new CSSClassList(elem); - elem.style = new CSSStyleDeclaration(elem); + element.class = new CSSClassList(element); + element.style = new CSSStyleDeclaration(element); if (Object.keys(data.attributes).length) { for (const [name, attr] of Object.entries(data.attributes)) { if (name === 'class') { // has class attribute - elem.class.hasClass(); + element.class.hasClass(); } if (name === 'style') { // has style attribute - elem.style.hasStyle(); + element.style.hasStyle(); } - elem.attrs[name] = { + element.attrs[name] = { name: name, value: attr.value, }; } } - elem = pushToContent(elem); - current = elem; + element = pushToContent(element); + current = element; - stack.push(elem); + stack.push(element); }; sax.ontext = function (text) { // prevent trimming of meaningful whitespace inside textual tags - if (textElems.includes(current.elem) && !data.prefix) { + if (textElems.includes(current.name) && !data.prefix) { pushToContent({ type: 'text', value: text, diff --git a/plugins/cleanupAttrs.js b/plugins/cleanupAttrs.js index 6c325707..83657b88 100644 --- a/plugins/cleanupAttrs.js +++ b/plugins/cleanupAttrs.js @@ -27,7 +27,7 @@ var regNewlinesNeedSpace = /(\S)\r?\n(\S)/g, * @author Kir Belevich */ exports.fn = function (item, params) { - if (item.isElem()) { + if (item.type === 'element') { item.eachAttr(function (attr) { if (params.newlines) { // new line which requires a space instead of themselve diff --git a/plugins/cleanupIDs.js b/plugins/cleanupIDs.js index e840566a..83aedf4f 100644 --- a/plugins/cleanupIDs.js +++ b/plugins/cleanupIDs.js @@ -122,14 +122,14 @@ exports.fn = function (data, params) { // quit if