Skip to content

Commit 725e308

Browse files
committed
handle line breaks in tag values
1 parent b1a176c commit 725e308

4 files changed

Lines changed: 27 additions & 2 deletions

File tree

src/__tests__/_createOsmChangeXml.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ describe("createOsmChangeXml", () => {
6060
type: "way",
6161
id: 4002,
6262
version: 2,
63-
tags: { highway: "path", surface: "< & \" ' >" },
63+
tags: { highway: "path", surface: "< & \" ' > \n \r" },
6464
nodes: [3005, 3006, 3007, -3, 3008, 3009, 3010],
6565
},
6666
{
@@ -107,7 +107,7 @@ describe("createOsmChangeXml", () => {
107107
</way>
108108
<way id="4002" version="2" changeset="6001">
109109
<tag k="highway" v="path"/>
110-
<tag k="surface" v="&lt; &amp; &quot; &apos; &gt;"/>
110+
<tag k="surface" v="&lt; &amp; &quot; &apos; &gt; &xA; &xD;"/>
111111
<nd ref="3005"/>
112112
<nd ref="3006"/>
113113
<nd ref="3007"/>

src/api/_xml.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,9 @@ export const xmlParser = new XMLParser({
66
attributesGroupName: "$",
77
attributeNamePrefix: "",
88
isArray: (tagName) => tagName !== "$",
9+
attributeValueProcessor(_name, value) {
10+
return value
11+
.replaceAll(/&(xA|#10);/g, "\n")
12+
.replaceAll(/&(xD|#13);/g, "\r");
13+
},
914
});

src/api/changesets/__tests__/_parseOsmChangeXml.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ describe("_parseOsmChangeXml", () => {
1818
<modify>
1919
<way id="-2" version="0" changeset="123">
2020
<tag k="building" v="yes"/>
21+
<tag k="inscription" v="a&#10;b&#13;&#10;c"/>
22+
<tag k="surface" v="&lt; &amp; &quot; &apos; &gt; &xA; &xD;"/>
2123
<nd ref="10"/>
2224
<nd ref="11"/>
2325
<nd ref="12"/>
@@ -55,6 +57,8 @@ describe("_parseOsmChangeXml", () => {
5557
nodes: [10, 11, 12, 13, 10],
5658
tags: {
5759
building: "yes",
60+
inscription: "a\nb\r\nc",
61+
surface: "< & \" ' > \n \r",
5862
},
5963
timestamp: undefined,
6064
type: "way",

src/api/changesets/_createOsmChangeXml.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,22 @@ const builder = new XMLBuilder({
77
format: true,
88
suppressEmptyNode: true,
99
suppressBooleanAttributes: false,
10+
// @ts-expect-error -- typedefs are wrong
11+
entities: [
12+
{ regex: /&/g, val: "&amp;" },
13+
{ regex: />/g, val: "&gt;" },
14+
{ regex: /</g, val: "&lt;" },
15+
{ regex: /'/g, val: "&apos;" },
16+
{ regex: /"/g, val: "&quot;" },
17+
{ regex: /\t/u, val: "&x9;" },
18+
{ regex: /\n/u, val: "&xA;" },
19+
{ regex: /\r/u, val: "&xD;" },
20+
// we need this because the library only defines a subset of
21+
// the characters that need to be escaped. compare:
22+
// https://github.com/NaturalIntelligence/fast-xml-parser/blob/e0769f/src/xmlbuilder/json2xml.js#L27-L31
23+
// with
24+
// https://github.com/jsdom/w3c-xmlserializer/blob/83115f/lib/attributes.js#L30-L36
25+
],
1026
});
1127

1228
/** @internal */

0 commit comments

Comments
 (0)