Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mergeStyles plugin #1381

Merged
merged 49 commits into from
Mar 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
71bf368
Add mergeStyles plugin, tests and configuration.
strarsis Feb 27, 2021
37a0be2
Fix lint error (unused opts argument).
strarsis Mar 11, 2021
195bb9c
Adjust plugin to new xast in upstream.
strarsis Mar 19, 2021
57f5b8c
Merge branch 'master' of https://github.com/svg/svgo into mergeStyles…
strarsis Mar 19, 2021
bbd4087
Apply linter fix to mergeStyles plugin.
strarsis Mar 19, 2021
e25a9b0
Fix typecheck errors.
strarsis Mar 19, 2021
181884b
Improve for loop.
strarsis Mar 19, 2021
4f29d47
Downgrade lockfile version.
strarsis Mar 22, 2021
89bf746
Use const for non-mutated variables.
strarsis Mar 22, 2021
8e32212
Use better for loop.
strarsis Mar 22, 2021
f514792
Use xast.
strarsis Mar 22, 2021
5195c8b
Use direct attributes access.
strarsis Mar 22, 2021
28397f0
Use children property directly.
strarsis Mar 22, 2021
6bb980b
Run fix script.
strarsis Mar 22, 2021
eed2ccb
Move comment before condition.
strarsis Mar 22, 2021
a183e1c
Replace all vars with consts or lets.
strarsis Mar 22, 2021
9fb4781
Use xast for closest element by name.
strarsis Mar 22, 2021
fdb2e9f
Separate const for each require.
strarsis Mar 22, 2021
f696e92
Apply fix script.
strarsis Mar 22, 2021
d908384
Import each used method separetly.
strarsis Mar 22, 2021
b53cb93
Define unmutated variable as const.
strarsis Mar 22, 2021
8392db0
Add description to each plugin test.
strarsis Mar 22, 2021
97d152b
Improve import code.
strarsis Mar 22, 2021
fb6ce04
Simplify and improve checks for empty.
strarsis Mar 22, 2021
da06339
Move exports after commit.
strarsis Mar 22, 2021
7214c61
Fix native array splicing.
strarsis Mar 26, 2021
3a4d3eb
Add check in css-tools fpr empty <style> elements.
strarsis Mar 26, 2021
8033c40
Run fix script.
strarsis Mar 26, 2021
b44469d
Improve symbol names (full name).
strarsis Mar 26, 2021
d83a134
Ignore <style> with incorrect type.
strarsis Mar 26, 2021
27569c0
Use style type attribute check instead selector.
strarsis Mar 26, 2021
e9d5c25
Use two if conditions for filtering <style> elements.
strarsis Mar 26, 2021
2196b11
Improve variable order.
strarsis Mar 26, 2021
1bc6574
Fix setCssStr (initialize with text node).
strarsis Mar 26, 2021
bd21123
Fix lint.
strarsis Mar 26, 2021
eff25a0
Add mergeStyles plugin to config list.
strarsis Mar 27, 2021
e22779a
Revert package lock file version.
strarsis Mar 27, 2021
b0140d9
Adjust expected plugin count in test.
strarsis Mar 27, 2021
59605f5
Revert package-lock (branch).
strarsis Mar 27, 2021
5f23f5f
Use string literals/template string.
strarsis Mar 27, 2021
0c9b7b3
Beautify.
strarsis Mar 27, 2021
2163677
Fix comment.
strarsis Mar 27, 2021
a86c121
Use children length for checking set variable.
strarsis Mar 27, 2021
f044d6f
Change plugin order for mergeStyles.
strarsis Mar 27, 2021
4bf5911
Adjust test to plugin order.
strarsis Mar 27, 2021
50603c4
Use const where possible.
strarsis Mar 27, 2021
ddd2fd3
Use one line for style template.
strarsis Mar 27, 2021
8b791fc
Add test and fix for only one <style> in SVG that is also empty.
strarsis Mar 27, 2021
4b16224
Revert package lock.
strarsis Mar 27, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions lib/css-tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 '';
Expand All @@ -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;
}

Expand Down
1 change: 1 addition & 0 deletions lib/svgo/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const pluginsOrder = [
'removeXMLNS',
'removeEditorsNSData',
'cleanupAttrs',
'mergeStyles',
'inlineStyles',
'minifyStyles',
'convertStyleToAttrs',
Expand Down
87 changes: 87 additions & 0 deletions plugins/mergeStyles.js
Original file line number Diff line number Diff line change
@@ -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 <strarsis@gmail.com>
*/
exports.fn = function (document) {
// collect <style/>s with valid type attribute (preserve order)
const styleElements = querySelectorAll(document, 'style');

// no <styles/>s, nothing to do
if (styleElements.length === 0) {
return document;
}

const styles = [];
for (const styleElement of styleElements) {
if (
styleElement.attributes.type &&
styleElement.attributes.type !== 'text/css'
) {
// skip <style> with invalid type attribute
continue;
}

if (closestByName(styleElement, 'foreignObject')) {
// skip <foreignObject> content
continue;
}

const cssString = getCssStr(styleElement);

styles.push({
styleElement: styleElement,

mq: styleElement.attributes.media,
cssStr: cssString,
});
}

const collectedStyles = [];
for (let styleNo = 0; styleNo < styles.length; styleNo += 1) {
const style = styles[styleNo];

if (style.mq) {
const wrappedStyles = `@media ${style.mq}{${style.cssStr}}`;
collectedStyles.push(wrappedStyles);
} else {
collectedStyles.push(style.cssStr);
}

// remove all processed style elements – except the first one
if (styleNo > 0) {
removeFromParent(style.styleElement);
}
}
const collectedStylesString = collectedStyles.join('');

// combine collected styles in the first style element
const firstStyle = styles[0];
delete firstStyle.styleElement.attributes.media; // remove media mq attribute as CSS media queries are used
if (collectedStylesString.trim().length > 0) {
setCssStr(firstStyle.styleElement, collectedStylesString);
} else {
removeFromParent(firstStyle.styleElement);
}

return document;
};

function removeFromParent(element) {
const parentElement = element.parentNode;
return parentElement.children.splice(
parentElement.children.indexOf(element),
1
);
}
1 change: 1 addition & 0 deletions plugins/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ exports.convertPathData = require('./convertPathData.js');
exports.convertShapeToPath = require('./convertShapeToPath.js');
exports.convertStyleToAttrs = require('./convertStyleToAttrs.js');
exports.convertTransform = require('./convertTransform.js');
exports.mergeStyles = require('./mergeStyles.js');
strarsis marked this conversation as resolved.
Show resolved Hide resolved
exports.inlineStyles = require('./inlineStyles.js');
exports.mergePaths = require('./mergePaths.js');
exports.minifyStyles = require('./minifyStyles.js');
Expand Down
4 changes: 2 additions & 2 deletions test/config/_index.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ describe('config', function () {
(item) => item.name === 'cleanupIDs'
);
it('should preserve internal plugins order', () => {
expect(removeAttrsIndex).to.equal(40);
expect(cleanupIDsIndex).to.equal(10);
expect(removeAttrsIndex).to.equal(41);
expect(cleanupIDsIndex).to.equal(11);
});
it('should activate inactive by default plugins', () => {
const removeAttrsPlugin = resolvePluginConfig(
Expand Down
19 changes: 19 additions & 0 deletions test/plugins/mergeStyles.01.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions test/plugins/mergeStyles.02.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions test/plugins/mergeStyles.03.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions test/plugins/mergeStyles.04.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions test/plugins/mergeStyles.05.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions test/plugins/mergeStyles.06.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions test/plugins/mergeStyles.07.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions test/plugins/mergeStyles.08.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 31 additions & 0 deletions test/plugins/mergeStyles.09.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions test/plugins/mergeStyles.10.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.