Skip to content

Commit 0647e5d

Browse files
test(html): cover the shapes a sheet does not end cleanly in
The scan that decides whether a `<style>` run may absorb the next sheet reads escapes, comments and strings, and none of those branches had a test: a build never reaches them, since a sheet a document embeds is normally well formed.
1 parent 1534397 commit 0647e5d

1 file changed

Lines changed: 42 additions & 5 deletions

File tree

‎test/HtmlSyntax.unittest.js‎

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4033,11 +4033,8 @@ describe("SourceProcessor — collapseWhitespace", () => {
40334033
});
40344034
});
40354035

4036-
// The two conditions that decline a fold cannot be reached through a build:
4037-
// webpack's CSS pipeline resolves an `@import` away long before the HTML
4038-
// minifier sees the sheet, and the minifier only refuses text that overflows
4039-
// its own parser. The rest of the transform is covered by the
4040-
// `configCases/html/minimize-merge-styles` case.
4036+
// The shapes a fold is declined for, which a build does not reach: webpack
4037+
// resolves an `@import` away long before the HTML minifier sees the sheet.
40414038
describe("SourceProcessor — merging adjacent <style>", () => {
40424039
const { SourceProcessor } = require("../lib/html/syntax");
40434040

@@ -4085,6 +4082,46 @@ describe("SourceProcessor — merging adjacent <style>", () => {
40854082
).toBe("<style>@import url(x.css);a{color:red}b{color:#00f}</style>");
40864083
});
40874084

4085+
it("declines a sheet ending inside a comment", () => {
4086+
// Appending to text the comment swallows would comment the next sheet out.
4087+
expect(
4088+
minify("<style>a{color:red}/*x</style><style>b{color:#00f}</style>")
4089+
).toBe("<style>a{color:red}</style><style>b{color:#00f}</style>");
4090+
expect(
4091+
minify("<style>a{color:red}/*x*/</style><style>b{color:#00f}</style>")
4092+
).toBe("<style>a{color:red}b{color:#00f}</style>");
4093+
});
4094+
4095+
it("declines a sheet ending inside a string", () => {
4096+
expect(
4097+
minify('<style>a{content:"x</style><style>b{color:#00f}</style>')
4098+
).toBe('<style>a{content:"x"}</style><style>b{color:#00f}</style>');
4099+
// A newline ends a bad string, so this one is over where the sheet is.
4100+
expect(
4101+
minify('<style>a{content:"x\n}</style><style>b{color:#00f}</style>')
4102+
).toBe('<style>a{content:"x\n}b{color:#00f}</style>');
4103+
// An escaped quote is a character of the string, not its end.
4104+
expect(
4105+
minify('<style>a{content:"x\\"y"}</style><style>b{color:#00f}</style>')
4106+
).toBe("<style>a{content:'x\"y'}b{color:#00f}</style>");
4107+
});
4108+
4109+
it("reads an escape outside a string as one too", () => {
4110+
// Were the quote not escaped it would open a string running to the end.
4111+
expect(
4112+
minify('<style>a{color:red}\\"</style><style>b{color:#00f}</style>')
4113+
).toBe("<style>a{color:red}b{color:#00f}</style>");
4114+
});
4115+
4116+
it("declines a sheet left inside a bracket", () => {
4117+
expect(
4118+
minify("<style>a{color:red</style><style>b{color:#00f}</style>")
4119+
).toBe("<style>a{color:red}</style><style>b{color:#00f}</style>");
4120+
expect(
4121+
minify("<style>a{color:rgb(1,2,3}</style><style>b{color:#00f}</style>")
4122+
).toBe("<style>a{color:rgb(1,2,3})}</style><style>b{color:#00f}</style>");
4123+
});
4124+
40884125
it("declines a sheet the CSS minifier could not read", () => {
40894126
// Deep enough to overflow the CSS parser's stack. Text it never parsed may
40904127
// be unterminated, and appending to that makes the next sheet part of it.

0 commit comments

Comments
 (0)