Skip to content

Commit fe4a4e1

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

File tree

7 files changed

+35
-5
lines changed

7 files changed

+35
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## 3.3.1 (2025-11-21)
11+
12+
- [uploadChangeset] fix tag values with linebreaks (`\n`) not handled properly
13+
1014
## 3.3.0 (2025-09-18)
1115

1216
- [uploadChangeset] add an `onProgress` callback, so that apps can show a progress bar while uploading

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "osm-api",
3-
"version": "3.3.0",
3+
"version": "3.3.1",
44
"contributors": [
55
"Kyle Hensel (https://github.com/k-yle)"
66
],

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 \t" },
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; &#10; &#13; &#9;"/>
111111
<nd ref="3005"/>
112112
<nd ref="3006"/>
113113
<nd ref="3007"/>

src/api/_xml.ts

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

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; &#10; &#13; &#9;"/>
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 \t",
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/g, val: "&#9;" },
18+
{ regex: /\n/g, val: "&#10;" },
19+
{ regex: /\r/g, val: "&#13;" },
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)