Skip to content

Commit 297ee37

Browse files
test: keep the XML printing tests with the XML printer
The beautify and minify output, the embedded and async renderers and _xmlCharacterData sat in the HTML parser suite; they move, with their snapshots, to xml/printer. Claude-Session: https://claude.ai/code/session_017TkBWTk3wcyUcJYDp5ZhaP
1 parent 67a62dc commit 297ee37

4 files changed

Lines changed: 221 additions & 219 deletions

File tree

‎test/unitCases/html/__snapshots__/parser.unittest.js.snap‎

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,5 @@
11
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
22

3-
exports[`parseHtml XML mode printing should defer an XML stylesheet to an asynchronous renderer 1`] = `"<svg xmlns=\\"http://www.w3.org/2000/svg\\"><style><![CDATA[a<b{}]]></style><rect style='fill:\\"a&lt;b\\"'/><style> </style></svg>"`;
4-
5-
exports[`parseHtml XML mode printing should hand SVG and XHTML stylesheets to the embedded renderer 1`] = `
6-
"<svg xmlns=\\"http://www.w3.org/2000/svg\\" xmlns:h=\\"http://www.w3.org/1999/xhtml\\">
7-
<style type=\\"text/css\\"><![CDATA[.a{fill:#fff}text>tspan{stroke:red}a[href=\\"x&y\\"]{fill:blue}]]></style>
8-
<rect style=\\"fill:red;stroke:none\\"/>
9-
<h:style>p{color:red}</h:style>
10-
<foreignObject><h:p style=\\"color:red\\">x</h:p></foreignObject>
11-
<x:style xmlns:x=\\"urn:not-css\\">keep { this }</x:style>
12-
<g xmlns=\\"urn:not-css\\" style=\\"keep : this\\"><style>keep { this }</style></g>
13-
<style>a { b : c }</style>
14-
<style type=\\"text/less\\">@c: red; .b { fill: @c }</style>
15-
<style type=\\" TEXT/CSS \\">b{c:d}</style>
16-
</svg>"
17-
`;
18-
19-
exports[`parseHtml XML mode printing should write XML back as written when beautifying 1`] = `
20-
"<?xml version=\\"1.0\\"?><!DOCTYPE svg [ <!ENTITY ns \\"http://www.w3.org/2000/svg\\"> <!ATTLIST svg version CDATA \\"1.1\\"> ]><!-- drawn by hand --><svg xmlns=\\"&ns;\\" viewBox = \\"0 0 10 10\\" data-q='say \\"hi\\"' data-both=\\"it's &quot;q&quot;\\" data-ws=\\"a
21-
b&#10;c\\">
22-
<title>A &amp; B &gt; C</title>
23-
<g><!-- inert --></g>
24-
<path d=\\"M0 0L10 10\\" ></path>
25-
<br></br>
26-
<text>&ns;</text>
27-
<style><![CDATA[a > b { fill: red } a < b && c]]></style>
28-
<script><![CDATA[x]]></script>
29-
<?render mode=\\"fast\\"?>
30-
</svg>"
31-
`;
32-
33-
exports[`parseHtml XML mode printing should write only what the target browsers read: every ability 1`] = `"<svg xmlns=\\"http://www.w3.org/2000/svg\\"><style>g>.b{fill:#0000ff80}@media (width>=0px){.e{fill:red}}.p{user-select:none}</style><rect style=\\"fill:#ff000080\\"/></svg>"`;
34-
35-
exports[`parseHtml XML mode printing should write only what the target browsers read: legacy browsers 1`] = `"<svg xmlns=\\"http://www.w3.org/2000/svg\\"><style>g>.b{fill:rgba(0,0,255,.5)}@media (min-width:0px){.e{fill:red}}.p{-webkit-user-select:none;-ms-user-select:none;user-select:none}</style><rect style=\\"fill:rgba(255,0,0,.5)\\"/></svg>"`;
36-
37-
exports[`parseHtml XML mode printing should write the shortest well-formed XML when minifying 1`] = `
38-
"<?xml version=\\"1.0\\"?><!DOCTYPE svg [ <!ENTITY ns \\"http://www.w3.org/2000/svg\\"> <!ATTLIST svg version CDATA \\"1.1\\"> ]><svg xmlns=\\"&ns;\\" viewBox=\\"0 0 10 10\\" data-q='say \\"hi\\"' data-both='it&apos;s \\"q\\"' data-ws=\\"a b&#10;c\\">
39-
<title>A &amp; B &gt; C</title>
40-
<g/>
41-
<path d=\\"M0 0L10 10\\"/>
42-
<br/>
43-
<text>&ns;</text>
44-
<style><![CDATA[a > b { fill: red } a < b && c]]></style>
45-
<script>x</script>
46-
<?render mode=\\"fast\\"?>
47-
</svg>"
48-
`;
49-
503
exports[`parseHtml — insertion modes appends a comment after the body to the html element 1`] = `
514
"| <!DOCTYPE html>
525
| <html>

‎test/unitCases/html/parser.unittest.js‎

Lines changed: 0 additions & 172 deletions
Original file line numberDiff line numberDiff line change
@@ -799,178 +799,6 @@ describe("parseHtml", () => {
799799
]);
800800
expect(tokenizeXml("<a>")).toBe(3);
801801
});
802-
803-
describe("printing", () => {
804-
/**
805-
* @param {string} source XML source
806-
* @param {"minify" | "beautify"} mode print mode
807-
* @returns {string} printed XML
808-
*/
809-
const printXml = (source, mode) =>
810-
/** @type {{ code: string }} */ (
811-
new SourceProcessor().process(source, { xml: true, mode })
812-
).code;
813-
const source = `<?xml version="1.0"?>
814-
<!DOCTYPE svg [ <!ENTITY ns "http://www.w3.org/2000/svg"> <!ATTLIST svg version CDATA "1.1"> ]>
815-
<!-- drawn by hand -->
816-
<svg xmlns="&ns;" viewBox = "0 0 10 10" data-q='say "hi"' data-both="it's &quot;q&quot;" data-ws="a
817-
b&#10;c">
818-
<title>A &amp; B &gt; C</title>
819-
<g><!-- inert --></g>
820-
<path d="M0 0L10 10" ></path>
821-
<br></br>
822-
<text>&ns;</text>
823-
<style><![CDATA[a > b { fill: red } a < b && c]]></style>
824-
<script><![CDATA[x]]></script>
825-
<?render mode="fast"?>
826-
</svg>
827-
`;
828-
829-
it("should write XML back as written when beautifying", () => {
830-
expect(printXml(source, "beautify")).toMatchSnapshot();
831-
});
832-
833-
it("should write the shortest well-formed XML when minifying", () => {
834-
const minified = printXml(source, "minify");
835-
expect(minified).toMatchSnapshot();
836-
expect(printXml(minified, "minify")).toBe(minified);
837-
});
838-
839-
it("should hand SVG and XHTML stylesheets to the embedded renderer", () => {
840-
const { builtinEmbeddedRenderer } = require("../../../lib/html/builtinEmbeddedRenderer");
841-
const document = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:h="http://www.w3.org/1999/xhtml">
842-
<style type="text/css"><![CDATA[
843-
.a { fill : #FFFFFF ; }
844-
text > tspan { stroke: red }
845-
a[href="x&y"] { fill: blue }
846-
]]></style>
847-
<rect style="fill: #ff0000 ; stroke : none"/>
848-
<h:style>p { color : red }</h:style>
849-
<foreignObject><h:p style="color : red">x</h:p></foreignObject>
850-
<x:style xmlns:x="urn:not-css">keep { this }</x:style>
851-
<g xmlns="urn:not-css" style="keep : this"><style>keep { this }</style></g>
852-
<style>a { b : c }<!-- split --></style>
853-
<style type="text/less">@c: red; .b { fill: @c }</style>
854-
<style type=" TEXT/CSS ">b { c : d }</style>
855-
</svg>`;
856-
expect(
857-
/** @type {{ code: string }} */ (
858-
new SourceProcessor().process(document, {
859-
xml: true,
860-
mode: "minify",
861-
renderEmbeddedSource: builtinEmbeddedRenderer()
862-
})
863-
).code
864-
).toMatchSnapshot();
865-
});
866-
867-
it("should write only what the target browsers read", () => {
868-
const { builtinEmbeddedRenderer } = require("../../../lib/html/builtinEmbeddedRenderer");
869-
const document =
870-
'<svg xmlns="http://www.w3.org/2000/svg"><style>g > .b { fill: rgba(0,0,255,0.5) } @media (min-width: 0px) { .e { fill: red } } .p { user-select: none }</style><rect style="fill: rgba(255,0,0,0.5)"/></svg>';
871-
/**
872-
* @param {string[]=} browsers the browserslist selection
873-
* @returns {string} the minified document
874-
*/
875-
const minify = (browsers) =>
876-
/** @type {{ code: string }} */ (
877-
new SourceProcessor().process(document, {
878-
xml: true,
879-
mode: "minify",
880-
renderEmbeddedSource: builtinEmbeddedRenderer(
881-
browsers === undefined ? undefined : { environment: { browsers } }
882-
)
883-
})
884-
).code;
885-
expect(minify()).toMatchSnapshot("every ability");
886-
expect(minify(["chrome 60", "safari 10", "ie 11"])).toMatchSnapshot(
887-
"legacy browsers"
888-
);
889-
});
890-
891-
it("should defer an XML stylesheet to an asynchronous renderer", async () => {
892-
/** @type {string[]} */
893-
const offered = [];
894-
const { code } = await new SourceProcessor().processAsync(
895-
'<svg xmlns="http://www.w3.org/2000/svg"><style>a { b : c }</style><rect style="fill: red"/><style> </style></svg>',
896-
{
897-
xml: true,
898-
mode: "minify",
899-
renderEmbeddedSource: (
900-
/** @type {string} */ source,
901-
/** @type {{ as?: string }} */ hole
902-
) => {
903-
offered.push(source);
904-
return Promise.resolve(hole.as ? 'fill:"a<b"' : "a<b{}");
905-
}
906-
}
907-
);
908-
expect(offered).toEqual(["a { b : c }", "fill: red"]);
909-
expect(code).toMatchSnapshot();
910-
});
911-
912-
it("should write a rendered stylesheet in the shortest character data", () => {
913-
const { _xmlCharacterData } = require("../../../lib/html/syntax-parser");
914-
expect(_xmlCharacterData("a>b{}")).toBe("a>b{}");
915-
expect(_xmlCharacterData("a]]>b\r")).toBe("a]]&gt;b&#13;");
916-
expect(_xmlCharacterData("a<b&c")).toBe("<![CDATA[a<b&c]]>");
917-
expect(_xmlCharacterData("a<b]]>")).toBe("a&lt;b]]&gt;");
918-
});
919-
920-
it("should leave a stylesheet as written when nothing renders it", () => {
921-
const document =
922-
'<svg xmlns="http://www.w3.org/2000/svg"><style>a { b : c }</style><rect style="fill : red"/></svg>';
923-
expect(printXml(document, "minify")).toBe(
924-
'<svg xmlns="http://www.w3.org/2000/svg"><style>a { b : c }</style><rect style="fill : red"/></svg>'
925-
);
926-
expect(
927-
/** @type {{ code: string }} */ (
928-
new SourceProcessor().process(document, {
929-
xml: true,
930-
mode: "minify",
931-
renderEmbeddedSource: () => undefined
932-
})
933-
).code
934-
).toBe(document);
935-
});
936-
937-
it("should resolve a prefixed stylesheet's namespace through its own prefix", () => {
938-
const { builtinEmbeddedRenderer } = require("../../../lib/html/builtinEmbeddedRenderer");
939-
expect(
940-
/** @type {{ code: string }} */ (
941-
new SourceProcessor().process(
942-
'<s:svg xmlns:s="http://www.w3.org/2000/svg"><s:style>a { b : c }</s:style><style>d { e : f }</style></s:svg>',
943-
{
944-
xml: true,
945-
mode: "minify",
946-
renderEmbeddedSource: builtinEmbeddedRenderer()
947-
}
948-
)
949-
).code
950-
).toBe(
951-
'<s:svg xmlns:s="http://www.w3.org/2000/svg"><s:style>a{b:c}</s:style><style>d { e : f }</style></s:svg>'
952-
);
953-
});
954-
955-
it("should keep a stylesheet an asynchronous renderer declines", async () => {
956-
const document =
957-
'<svg xmlns="http://www.w3.org/2000/svg"><style>a { b : c }</style><rect style="fill : red"/></svg>';
958-
const { code } = await new SourceProcessor().processAsync(document, {
959-
xml: true,
960-
mode: "minify",
961-
renderEmbeddedSource: () => Promise.resolve(undefined)
962-
});
963-
expect(code).toBe(document);
964-
});
965-
966-
it("should close what the input ran out in", () => {
967-
expect(printXml("<r><!-- open", "beautify")).toBe("<r><!-- open--></r>");
968-
expect(printXml("<r><![CDATA[a<b", "beautify")).toBe("<r>a&lt;b</r>");
969-
expect(printXml("<r><![CDATA[]]>]]&gt;&#13;", "minify")).toBe(
970-
"<r>]]&gt;&#13;</r>"
971-
);
972-
});
973-
});
974802
});
975803

976804
it("should auto-close <p> when a block element opens", () => {

‎test/unitCases/xml/__snapshots__/printer.unittest.js.snap‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,50 @@ exports[`xmlMinify should minify the document and the stylesheets it holds 1`] =
1515
<circle class=\\"dot\\" cx=\\"5\\" cy=\\"5\\" r=\\"4\\" style=\\"stroke:#0f0\\"/>
1616
</svg>"
1717
`;
18+
19+
exports[`SourceProcessor — XML printing should defer an XML stylesheet to an asynchronous renderer 1`] = `"<svg xmlns=\\"http://www.w3.org/2000/svg\\"><style><![CDATA[a<b{}]]></style><rect style='fill:\\"a&lt;b\\"'/><style> </style></svg>"`;
20+
21+
exports[`SourceProcessor — XML printing should hand SVG and XHTML stylesheets to the embedded renderer 1`] = `
22+
"<svg xmlns=\\"http://www.w3.org/2000/svg\\" xmlns:h=\\"http://www.w3.org/1999/xhtml\\">
23+
<style type=\\"text/css\\"><![CDATA[.a{fill:#fff}text>tspan{stroke:red}a[href=\\"x&y\\"]{fill:blue}]]></style>
24+
<rect style=\\"fill:red;stroke:none\\"/>
25+
<h:style>p{color:red}</h:style>
26+
<foreignObject><h:p style=\\"color:red\\">x</h:p></foreignObject>
27+
<x:style xmlns:x=\\"urn:not-css\\">keep { this }</x:style>
28+
<g xmlns=\\"urn:not-css\\" style=\\"keep : this\\"><style>keep { this }</style></g>
29+
<style>a { b : c }</style>
30+
<style type=\\"text/less\\">@c: red; .b { fill: @c }</style>
31+
<style type=\\" TEXT/CSS \\">b{c:d}</style>
32+
</svg>"
33+
`;
34+
35+
exports[`SourceProcessor — XML printing should write XML back as written when beautifying 1`] = `
36+
"<?xml version=\\"1.0\\"?><!DOCTYPE svg [ <!ENTITY ns \\"http://www.w3.org/2000/svg\\"> <!ATTLIST svg version CDATA \\"1.1\\"> ]><!-- drawn by hand --><svg xmlns=\\"&ns;\\" viewBox = \\"0 0 10 10\\" data-q='say \\"hi\\"' data-both=\\"it's &quot;q&quot;\\" data-ws=\\"a
37+
b&#10;c\\">
38+
<title>A &amp; B &gt; C</title>
39+
<g><!-- inert --></g>
40+
<path d=\\"M0 0L10 10\\" ></path>
41+
<br></br>
42+
<text>&ns;</text>
43+
<style><![CDATA[a > b { fill: red } a < b && c]]></style>
44+
<script><![CDATA[x]]></script>
45+
<?render mode=\\"fast\\"?>
46+
</svg>"
47+
`;
48+
49+
exports[`SourceProcessor — XML printing should write only what the target browsers read: every ability 1`] = `"<svg xmlns=\\"http://www.w3.org/2000/svg\\"><style>g>.b{fill:#0000ff80}@media (width>=0px){.e{fill:red}}.p{user-select:none}</style><rect style=\\"fill:#ff000080\\"/></svg>"`;
50+
51+
exports[`SourceProcessor — XML printing should write only what the target browsers read: legacy browsers 1`] = `"<svg xmlns=\\"http://www.w3.org/2000/svg\\"><style>g>.b{fill:rgba(0,0,255,.5)}@media (min-width:0px){.e{fill:red}}.p{-webkit-user-select:none;-ms-user-select:none;user-select:none}</style><rect style=\\"fill:rgba(255,0,0,.5)\\"/></svg>"`;
52+
53+
exports[`SourceProcessor — XML printing should write the shortest well-formed XML when minifying 1`] = `
54+
"<?xml version=\\"1.0\\"?><!DOCTYPE svg [ <!ENTITY ns \\"http://www.w3.org/2000/svg\\"> <!ATTLIST svg version CDATA \\"1.1\\"> ]><svg xmlns=\\"&ns;\\" viewBox=\\"0 0 10 10\\" data-q='say \\"hi\\"' data-both='it&apos;s \\"q\\"' data-ws=\\"a b&#10;c\\">
55+
<title>A &amp; B &gt; C</title>
56+
<g/>
57+
<path d=\\"M0 0L10 10\\"/>
58+
<br/>
59+
<text>&ns;</text>
60+
<style><![CDATA[a > b { fill: red } a < b && c]]></style>
61+
<script>x</script>
62+
<?render mode=\\"fast\\"?>
63+
</svg>"
64+
`;

0 commit comments

Comments
 (0)