Skip to content

Commit 1ecdbab

Browse files
test(html): cover the reflected attributes webref's data adds
Each case pairs the element the table names with one it does not, so a rewrite keyed on the attribute name alone fails the second half. Spell the generated entity table with `toJsStringLiteral`: the source is fetched, and a raw U+2028 would end the string literal it is written into.
1 parent f8c38d2 commit 1ecdbab

2 files changed

Lines changed: 54 additions & 1 deletion

File tree

‎test/HtmlSyntax.unittest.js‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10140,3 +10140,44 @@ describe("SourceProcessor — attribute rewrites as switches", () => {
1014010140
).toBe('<a ping=" b a ">x</a>');
1014110141
});
1014210142
});
10143+
10144+
describe("SourceProcessor — reflected attributes are read per element", () => {
10145+
const { SourceProcessor } = require("../lib/html/syntax");
10146+
10147+
/**
10148+
* @param {string} html input markup
10149+
* @returns {string} the minified serialization
10150+
*/
10151+
const minify = (html) =>
10152+
new SourceProcessor().process(html, { mode: "minify" }).code;
10153+
10154+
// Each case pairs the element the table names with one it does not, so a
10155+
// rewrite keyed on the attribute name alone fails the second half.
10156+
it("collapses a boolean attribute only where it is reflected", () => {
10157+
expect(minify('<marquee truespeed="truespeed">x</marquee>')).toBe(
10158+
"<marquee truespeed>x</marquee>"
10159+
);
10160+
expect(minify('<div truespeed="truespeed">x</div>')).toBe(
10161+
"<div truespeed=truespeed>x</div>"
10162+
);
10163+
expect(minify('<dir compact="compact"><li>x</li></dir>')).toBe(
10164+
"<dir compact><li>x</dir>"
10165+
);
10166+
expect(minify('<div compact="compact">x</div>')).toBe(
10167+
"<div compact=compact>x</div>"
10168+
);
10169+
});
10170+
10171+
it("normalizes an integer attribute only where it is reflected", () => {
10172+
expect(
10173+
minify('<marquee scrollamount="0010" scrolldelay="0085">x</marquee>')
10174+
).toBe("<marquee scrollamount=10 scrolldelay=85>x</marquee>");
10175+
expect(minify('<div scrollamount="0010">x</div>')).toBe(
10176+
"<div scrollamount=0010>x</div>"
10177+
);
10178+
expect(minify('<marquee hspace="007" vspace="008">x</marquee>')).toBe(
10179+
"<marquee hspace=7 vspace=8>x</marquee>"
10180+
);
10181+
expect(minify('<div hspace="007">x</div>')).toBe("<div hspace=007>x</div>");
10182+
});
10183+
});

‎tooling/generate-html-data.js‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
const fs = require("fs");
1717
const path = require("path");
1818
const prettier = require("prettier");
19+
const { toJsStringLiteral } = require("../lib/util/identifier");
1920

2021
const TARGET = path.resolve(__dirname, "../lib/html/data.js");
2122
const SYNTAX_TARGET = path.resolve(__dirname, "../lib/html/syntax.js");
@@ -2085,6 +2086,17 @@ const summary = `${booleans.length} boolean, ${urls.length} url, ${integers.leng
20852086
const REGION_REGEXP =
20862087
/\/\/ #region html entities\r?\n[\s\S]+?\/\/ #endregion\r?\n/;
20872088

2089+
/**
2090+
* Spell the entity table as an object literal. `JSON.stringify` would leave a
2091+
* U+2028 or U+2029 raw, and both end a JS string literal though not a JSON one.
2092+
* @param {Record<string, string>} map entity name to the characters it names
2093+
* @returns {string} the literal, on one line
2094+
*/
2095+
const entityLiteral = (map) =>
2096+
`{${Object.keys(map)
2097+
.map((name) => `${toJsStringLiteral(name)}:${toJsStringLiteral(map[name])}`)
2098+
.join(",")}}`;
2099+
20882100
/**
20892101
* Render the `// #region html entities` block. The table is emitted as a
20902102
* single-line frozen object literal so the engine builds it once at load.
@@ -2110,7 +2122,7 @@ const renderEntities = (entities) => {
21102122
21112123
// prettier-ignore
21122124
// cspell:disable-next-line
2113-
const HTML_ENTITIES = /** @type {Readonly<Record<string, string>>} */ (Object.freeze(Object.assign(Object.create(null), ${JSON.stringify(
2125+
const HTML_ENTITIES = /** @type {Readonly<Record<string, string>>} */ (Object.freeze(Object.assign(Object.create(null), ${entityLiteral(
21142126
map
21152127
)})));
21162128
// #endregion

0 commit comments

Comments
 (0)