From c75d83607a79b131cb36d44bece0744a06585972 Mon Sep 17 00:00:00 2001 From: Seth Falco Date: Sun, 12 Nov 2023 17:33:02 +0000 Subject: [PATCH] fix(inlineStyles): inline styles in order of priority --- plugins/inlineStyles.js | 13 ++++++++++++- test/plugins/inlineStyles.03.svg | 2 +- test/plugins/inlineStyles.05.svg | 2 +- test/plugins/inlineStyles.16.svg | 2 +- test/plugins/inlineStyles.19.svg | 2 +- test/plugins/inlineStyles.22.svg | 2 +- 6 files changed, 17 insertions(+), 6 deletions(-) diff --git a/plugins/inlineStyles.js b/plugins/inlineStyles.js index db6bd2350..cd06d0ca3 100644 --- a/plugins/inlineStyles.js +++ b/plugins/inlineStyles.js @@ -203,9 +203,17 @@ exports.fn = (root, params) => { continue; } const styleDeclarationItems = new Map(); + + /** @type {csstree.ListItem} */ + let firstListItem; + csstree.walk(styleDeclarationList, { visit: 'Declaration', enter(node, item) { + if (firstListItem == null) { + firstListItem = item; + } + styleDeclarationItems.set(node.property.toLowerCase(), item); }, }); @@ -232,7 +240,10 @@ exports.fn = (root, params) => { const ruleDeclarationItem = styleDeclarationList.children.createItem(ruleDeclaration); if (matchedItem == null) { - styleDeclarationList.children.append(ruleDeclarationItem); + styleDeclarationList.children.insert( + ruleDeclarationItem, + firstListItem + ); } else if ( matchedItem.data.important !== true && ruleDeclaration.important === true diff --git a/test/plugins/inlineStyles.03.svg b/test/plugins/inlineStyles.03.svg index f0a98c4c1..0f8dabccf 100644 --- a/test/plugins/inlineStyles.03.svg +++ b/test/plugins/inlineStyles.03.svg @@ -20,5 +20,5 @@ - + diff --git a/test/plugins/inlineStyles.05.svg b/test/plugins/inlineStyles.05.svg index 1d8f86040..c6fedc586 100644 --- a/test/plugins/inlineStyles.05.svg +++ b/test/plugins/inlineStyles.05.svg @@ -13,5 +13,5 @@ @@@ - + diff --git a/test/plugins/inlineStyles.16.svg b/test/plugins/inlineStyles.16.svg index 0f73f776c..31f548af2 100644 --- a/test/plugins/inlineStyles.16.svg +++ b/test/plugins/inlineStyles.16.svg @@ -27,7 +27,7 @@ button - + diff --git a/test/plugins/inlineStyles.19.svg b/test/plugins/inlineStyles.19.svg index cb847f958..1ed0b46bf 100644 --- a/test/plugins/inlineStyles.19.svg +++ b/test/plugins/inlineStyles.19.svg @@ -39,7 +39,7 @@ .cls-2,.cls-3{fill:#f5f5f5;stroke:gray}.cls-2{stroke-width:1px}.cls-2{fill-rule:evenodd}.cls-3{stroke-width:2px} - + diff --git a/test/plugins/inlineStyles.22.svg b/test/plugins/inlineStyles.22.svg index 3f37fc375..3bc24003e 100644 --- a/test/plugins/inlineStyles.22.svg +++ b/test/plugins/inlineStyles.22.svg @@ -21,6 +21,6 @@ element, and further selectors of those classes should still apply. - +