Skip to content

Commit 7da5084

Browse files
fix(html): minify an XML <style> only when its type is CSS
Read `type` the way the HTML printer does: absent, empty or `text/css`, so a `text/less` sheet in an SVG is left as written. Claude-Session: https://claude.ai/code/session_01WzCD9CDRFzG1j2yQARWzLp Co-authored-by: Alexander Akait <[email protected]>
1 parent e1fc30b commit 7da5084

3 files changed

Lines changed: 13 additions & 0 deletions

File tree

‎lib/html/syntax-printer.js‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ const {
6666
_xmlOpenTag,
6767
collapseWhitespaceRuns,
6868
decodeEntities,
69+
decodeXmlAttribute,
6970
escapeXmlText,
7071
isAllWs,
7172
isAsciiAlphanumeric,
@@ -261,6 +262,14 @@ const _isXmlStylesheet = (path) => {
261262
if (parent === 0) return false;
262263
const name = path.tagName(parent);
263264
if (name !== "style" && !name.endsWith(":style")) return false;
265+
// As in HTML, the attribute is optional and only `text/css` keeps it CSS.
266+
for (const attribute of path.attributes(parent)) {
267+
if (attribute.name !== "type") continue;
268+
const type = _asciiLowerCase(
269+
_asciiTrim(decodeXmlAttribute(attribute.value))
270+
);
271+
if (type !== "" && type !== "text/css") return false;
272+
}
264273
// A comment or an element beside it splits the sheet, which is left alone.
265274
return (
266275
path.firstChild(parent) === path.node &&

‎test/unitCases/HtmlSyntax.unittest.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3866,6 +3866,8 @@ b&#10;c">
38663866
<x:style xmlns:x="urn:not-css">keep { this }</x:style>
38673867
<g xmlns="urn:not-css" style="keep : this"><style>keep { this }</style></g>
38683868
<style>a { b : c }<!-- split --></style>
3869+
<style type="text/less">@c: red; .b { fill: @c }</style>
3870+
<style type=" TEXT/CSS ">b { c : d }</style>
38693871
</svg>`;
38703872
expect(
38713873
/** @type {{ code: string }} */ (

‎test/unitCases/__snapshots__/HtmlSyntax.unittest.js.snap‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ exports[`parseHtml XML mode printing should hand SVG and XHTML stylesheets to th
7777
<x:style xmlns:x=\\"urn:not-css\\">keep { this }</x:style>
7878
<g xmlns=\\"urn:not-css\\" style=\\"keep : this\\"><style>keep { this }</style></g>
7979
<style>a { b : c }</style>
80+
<style type=\\"text/less\\">@c: red; .b { fill: @c }</style>
81+
<style type=\\" TEXT/CSS \\">b{c:d}</style>
8082
</svg>"
8183
`;
8284

0 commit comments

Comments
 (0)