Skip to content

Commit ad4ff56

Browse files
authored
fix(xml): properly initialize '~name' property in xml '#instructions' (#104)
1 parent 74c1c6a commit ad4ff56

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

xml/stringify.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,11 @@ export function stringify(document: stringifyable, options?: options): string {
7878
text += xml_prolog(document as xml_document, _options)
7979
// Add processing instructions
8080
if (document["#instructions"]) {
81-
for (const nodes of Object.values(document["#instructions"])) {
81+
for (const [name, nodes] of Object.entries(document["#instructions"])) {
8282
for (const node of [nodes].flat()) {
83+
if (!("~name" in node)) {
84+
Object.defineProperties(node, { ["~name"]: { enumerable: false, writable: false, value: name } })
85+
}
8386
text += xml_instruction(node, _options)
8487
}
8588
}

xml/stringify_test.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,75 @@ test("`stringify()` xml syntax doctype", () =>
6262
},
6363
))
6464

65+
test("`stringify()` lume example issue #96", () => {
66+
expect(stringify({
67+
"@version": "1.0",
68+
"@encoding": "UTF-8",
69+
urlset: {
70+
"@xmlns": "http://www.sitemaps.org/schemas/sitemap/0.9",
71+
"@xmlns:xhtml": "http://www.w3.org/1999/xhtml",
72+
url: [
73+
{
74+
loc: "https://example.com/overrided-page2/",
75+
lastmod: "2020-06-21T00:00:00.000Z",
76+
},
77+
{
78+
loc: "https://example.com/page5/",
79+
lastmod: "1979-06-21T23:45:00.000Z",
80+
},
81+
{
82+
loc: "https://example.com/page_3/",
83+
lastmod: "2020-01-01T00:00:00.000Z",
84+
},
85+
{
86+
loc: "https://example.com/pages/new-name/page7/",
87+
lastmod: "2022-01-02T00:00:00.000Z",
88+
},
89+
{
90+
loc: "https://example.com/pages/page4/",
91+
lastmod: "2021-01-02T18:32:00.000Z",
92+
},
93+
{
94+
loc: "https://example.com/pages/page6/",
95+
lastmod: "2022-01-01T00:00:00.000Z",
96+
},
97+
],
98+
},
99+
"#instructions": {
100+
"xml-stylesheet": { "@href": "/sitemap-style.xml", "@type": "text/xsl" },
101+
},
102+
})).toEqual(
103+
`<?xml version="1.0" encoding="UTF-8"?>
104+
<?xml-stylesheet href="/sitemap-style.xml" type="text/xsl"?>
105+
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
106+
<url>
107+
<loc>https://example.com/overrided-page2/</loc>
108+
<lastmod>2020-06-21T00:00:00.000Z</lastmod>
109+
</url>
110+
<url>
111+
<loc>https://example.com/page5/</loc>
112+
<lastmod>1979-06-21T23:45:00.000Z</lastmod>
113+
</url>
114+
<url>
115+
<loc>https://example.com/page_3/</loc>
116+
<lastmod>2020-01-01T00:00:00.000Z</lastmod>
117+
</url>
118+
<url>
119+
<loc>https://example.com/pages/new-name/page7/</loc>
120+
<lastmod>2022-01-02T00:00:00.000Z</lastmod>
121+
</url>
122+
<url>
123+
<loc>https://example.com/pages/page4/</loc>
124+
<lastmod>2021-01-02T18:32:00.000Z</lastmod>
125+
</url>
126+
<url>
127+
<loc>https://example.com/pages/page6/</loc>
128+
<lastmod>2022-01-01T00:00:00.000Z</lastmod>
129+
</url>
130+
</urlset>`,
131+
)
132+
})
133+
65134
for (const indent of [" ", ""]) {
66135
test(`\`stringify()\` xml example w3schools.com#3 (indent = "${indent}")`, () =>
67136
expect(

0 commit comments

Comments
 (0)