Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
18 changes: 12 additions & 6 deletions plugins/inlineStyles.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const preservedPseudos = [
*/
export const fn = (root, params) => {
const {
disableIfAtRulesPresent = true,
onlyMatchedOnce = true,
removeMatchedSelectors = true,
useMqs = ['', 'screen'],
Expand All @@ -58,11 +59,12 @@ export const fn = (root, params) => {
* }[]}
*/
let selectors = [];
let deoptimized = false;

return {
element: {
enter: (node, parentNode) => {
if (node.name === 'foreignObject') {
if (deoptimized || node.name === 'foreignObject') {
return visitSkip;
}
if (node.name !== 'style' || node.children.length === 0) {
Expand Down Expand Up @@ -103,14 +105,18 @@ export const fn = (root, params) => {
const atrule = this.atrule;

// skip media queries not included into useMqs param
let mediaQuery = '';
let atRuleText = '';
if (atrule != null) {
mediaQuery = atrule.name;
atRuleText = atrule.name;
if (atrule.prelude != null) {
mediaQuery += ` ${csstree.generate(atrule.prelude)}`;
atRuleText += ` ${csstree.generate(atrule.prelude)}`;
}
}
if (!useMqs.includes(mediaQuery)) {
if (disableIfAtRulesPresent && atRuleText !== '') {
deoptimized = true;
return;
}
if (!useMqs.includes(atRuleText)) {
return;
}

Expand Down Expand Up @@ -167,7 +173,7 @@ export const fn = (root, params) => {

root: {
exit: () => {
if (styles.length === 0) {
if (deoptimized || styles.length === 0) {
return;
}
const sortedSelectors = selectors
Expand Down
2 changes: 2 additions & 0 deletions plugins/plugins-types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ type DefaultPlugins = {
};
mergeStyles: void;
inlineStyles: {
/** If true, do not inline styles if any CSS at rules are present. */
disableIfAtRulesPresent?: boolean;
/**
* Inlines selectors that match once only.
*
Expand Down
4 changes: 4 additions & 0 deletions test/plugins/inlineStyles.13.svg.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,7 @@
</defs>
<rect width="100" height="100" style="fill:blue"/>
</svg>

@@@

{"disableIfAtRulesPresent":false}
2 changes: 1 addition & 1 deletion test/plugins/inlineStyles.14.svg.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@

@@@

{"useMqs": ["media only screen and (min-device-width:320px) and (max-device-width:480px) and (-webkit-min-device-pixel-ratio:2)"]}
{"useMqs": ["media only screen and (min-device-width:320px) and (max-device-width:480px) and (-webkit-min-device-pixel-ratio:2)"],"disableIfAtRulesPresent":false}
35 changes: 35 additions & 0 deletions test/plugins/inlineStyles.29.svg.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
Styles should not be inlined in the presence of media queries.

See: https://github.com/svg/svgo/issues/1834

===

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 24 24">
<style>
.fill {
fill: red
}
@media (max-width:500px) {
.fill {
fill: blue
}
}
</style>
<rect class="fill" x="1" y="1" width="10" height="10"/>
</svg>

@@@

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 24 24">
<style>
.fill {
fill: red
}
@media (max-width:500px) {
.fill {
fill: blue
}
}
</style>
<rect class="fill" x="1" y="1" width="10" height="10"/>
</svg>