Skip to content

Commit 8c38087

Browse files
AllanZhengYPNitzan Uziely
authored and
Nitzan Uziely
committed
fix(xml-builder): xml entity encoding for more characters (aws#2309)
XML entity encoding for carriage return, line feed, next line, line separator characters, quote and single quote
1 parent 9801c49 commit 8c38087

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

packages/xml-builder/src/XmlText.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ import { XmlText } from "./XmlText";
33
describe("XmlText", () => {
44
it("escapes element text", () => {
55
const text = new XmlText('this & that are < or > "most"');
6-
expect(text.toString()).toBe('this &amp; that are &lt; or &gt; "most"');
6+
expect(text.toString()).toBe("this &amp; that are &lt; or &gt; &quot;most&quot;");
77
});
88
});
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { escapeElement } from "./escape-element";
22

33
describe("escape-element", () => {
4-
it("escapes: & < >", () => {
5-
const value = 'abc 123 &<>"%';
6-
expect(escapeElement(value)).toBe('abc 123 &amp;&lt;&gt;"%');
4+
it("escapes: & < > \" ' \\n \\r \\u0085 \\u2028", () => {
5+
const value = "abc 123 &<>\"'%\n\r\u0085\u2028";
6+
expect(escapeElement(value)).toBe("abc 123 &amp;&lt;&gt;&quot;&apos;%&#x0A;&#x0D;&#x85;&#x2028;");
77
});
88
});

packages/xml-builder/src/escape-element.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,14 @@
22
* Escapes characters that can not be in an XML element.
33
*/
44
export function escapeElement(value: string): string {
5-
return value.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
5+
return value
6+
.replace(/&/g, "&amp;")
7+
.replace(/"/g, "&quot;")
8+
.replace(/'/g, "&apos;")
9+
.replace(/</g, "&lt;")
10+
.replace(/>/g, "&gt;")
11+
.replace(/\r/g, "&#x0D;")
12+
.replace(/\n/g, "&#x0A;")
13+
.replace(/\u0085/g, "&#x85;")
14+
.replace(/\u2028/, "&#x2028;");
615
}

0 commit comments

Comments
 (0)