diff --git a/lib/css-tools.js b/lib/css-tools.js index 101c093f3..a59aae6e7 100644 --- a/lib/css-tools.js +++ b/lib/css-tools.js @@ -189,7 +189,10 @@ function csstreeToStyleDeclaration(declaration) { * @return {string} CSS string or empty array if no styles are set */ function getCssStr(elem) { - if (elem.children[0].type === 'text' || elem.children[0].type === 'cdata') { + if ( + elem.children.length > 0 && + (elem.children[0].type === 'text' || elem.children[0].type === 'cdata') + ) { return elem.children[0].value; } return ''; @@ -203,10 +206,19 @@ function getCssStr(elem) { * @return {string} reference to field with CSS */ function setCssStr(elem, css) { - if (elem.children[0].type === 'text' || elem.children[0].type === 'cdata') { - elem.children[0].value = css; - return elem.children[0].value; + if (elem.children.length === 0) { + elem.children.push({ + type: 'text', + value: '', + }); + } + + if (elem.children[0].type !== 'text' && elem.children[0].type !== 'cdata') { + return css; } + + elem.children[0].value = css; + return css; } diff --git a/lib/svgo/config.js b/lib/svgo/config.js index f26e3ed88..edd6cf1d6 100644 --- a/lib/svgo/config.js +++ b/lib/svgo/config.js @@ -10,6 +10,7 @@ const pluginsOrder = [ 'removeXMLNS', 'removeEditorsNSData', 'cleanupAttrs', + 'mergeStyles', 'inlineStyles', 'minifyStyles', 'convertStyleToAttrs', diff --git a/plugins/mergeStyles.js b/plugins/mergeStyles.js new file mode 100644 index 000000000..48f2ba1d2 --- /dev/null +++ b/plugins/mergeStyles.js @@ -0,0 +1,87 @@ +'use strict'; + +const { querySelectorAll, closestByName } = require('../lib/xast.js'); +const { getCssStr, setCssStr } = require('../lib/css-tools'); + +exports.type = 'full'; +exports.active = true; +exports.description = 'merge multiple style elements into one'; + +/** + * Merge multiple style elements into one. + * + * @param {Object} document document element + * + * @author strarsis + */ +exports.fn = function (document) { + // collect + + + +@@@ + + + + + diff --git a/test/plugins/mergeStyles.02.svg b/test/plugins/mergeStyles.02.svg new file mode 100644 index 000000000..662e93931 --- /dev/null +++ b/test/plugins/mergeStyles.02.svg @@ -0,0 +1,20 @@ +Check whether plugin works with only one style element (no further merging needed, noop) and a media query. + +=== + + + + + + + +@@@ + + + + + diff --git a/test/plugins/mergeStyles.03.svg b/test/plugins/mergeStyles.03.svg new file mode 100644 index 000000000..a6b6977bb --- /dev/null +++ b/test/plugins/mergeStyles.03.svg @@ -0,0 +1,18 @@ +Check whether plugin works with merging styles of two style elements (no media queries). + +=== + + + + + + + +@@@ + + + + + diff --git a/test/plugins/mergeStyles.04.svg b/test/plugins/mergeStyles.04.svg new file mode 100644 index 000000000..9885f8eed --- /dev/null +++ b/test/plugins/mergeStyles.04.svg @@ -0,0 +1,19 @@ +Check whether plugin works with two style elements that contain styles that also uses media queries. + +=== + + + + + + + + +@@@ + + + + + diff --git a/test/plugins/mergeStyles.05.svg b/test/plugins/mergeStyles.05.svg new file mode 100644 index 000000000..cf27fe26a --- /dev/null +++ b/test/plugins/mergeStyles.05.svg @@ -0,0 +1,13 @@ +Check whether plugin works with no style elements at all (no merging needed, noop). + +=== + + + + + +@@@ + + + + diff --git a/test/plugins/mergeStyles.06.svg b/test/plugins/mergeStyles.06.svg new file mode 100644 index 000000000..04dd712f7 --- /dev/null +++ b/test/plugins/mergeStyles.06.svg @@ -0,0 +1,20 @@ +Check whether plugin removes empty + + + + +@@@ + + + + + diff --git a/test/plugins/mergeStyles.07.svg b/test/plugins/mergeStyles.07.svg new file mode 100644 index 000000000..f02a2705e --- /dev/null +++ b/test/plugins/mergeStyles.07.svg @@ -0,0 +1,16 @@ +Check whether plugin removes empty + + + + +@@@ + + + + diff --git a/test/plugins/mergeStyles.08.svg b/test/plugins/mergeStyles.08.svg new file mode 100644 index 000000000..42abf8c9e --- /dev/null +++ b/test/plugins/mergeStyles.08.svg @@ -0,0 +1,23 @@ +Check whether plugin removes empty + + + + + + + +@@@ + + + + + diff --git a/test/plugins/mergeStyles.09.svg b/test/plugins/mergeStyles.09.svg new file mode 100644 index 000000000..36011a0ca --- /dev/null +++ b/test/plugins/mergeStyles.09.svg @@ -0,0 +1,31 @@ +Check whether plugin removes empty + + + + + + +@@@ + + + + + + diff --git a/test/plugins/mergeStyles.10.svg b/test/plugins/mergeStyles.10.svg new file mode 100644 index 000000000..480abce29 --- /dev/null +++ b/test/plugins/mergeStyles.10.svg @@ -0,0 +1,15 @@ +Check whether plugin removes one empty + + + +@@@ + + + +