Skip to content

Commit 7361bfe

Browse files
scr-oathsparkprime
authored andcommitted
[Fixes 1037] Add escapeStringXML to support unsafe strings in manifestXmlJsonml
1 parent 813c741 commit 7361bfe

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

stdlib/std.jsonnet

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,17 @@ limitations under the License.
10121012
ch;
10131013
std.foldl(function(a, b) a + trans(b), std.stringChars(str), ''),
10141014

1015+
escapeStringXML(str_)::
1016+
local str = std.toString(str_);
1017+
local escapes = {
1018+
'<': '&lt;',
1019+
'>': '&gt;',
1020+
'&': '&amp;',
1021+
'"': '&quot;',
1022+
"'": '&apos;',
1023+
};
1024+
std.join('', [std.get(escapes, ch, ch) for ch in std.stringChars(str)]),
1025+
10151026
manifestJson(value):: std.manifestJsonEx(value, ' '),
10161027

10171028
manifestJsonMinified(value):: std.manifestJsonEx(value, '', '', ':'),

test_suite/stdlib.jsonnet

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,11 @@ std.assertEqual(std.escapeStringJson('he"llo'), '"he\\"llo"') &&
339339
std.assertEqual(std.escapeStringJson('he"llo'), '"he\\"llo"') &&
340340
std.assertEqual(std.escapeStringBash("he\"l'lo"), "'he\"l'\"'\"'lo'") &&
341341
std.assertEqual(std.escapeStringDollars('The path is ${PATH}.'), 'The path is $${PATH}.') &&
342+
std.assertEqual(std.escapeStringXML('2 < 3'), '2 &lt; 3') &&
343+
std.assertEqual(std.escapeStringXML('3 > 2'), '2 &gt; 3') &&
344+
std.assertEqual(std.escapeStringXML('"foo"'), '&quot;foo&quot;') &&
345+
std.assertEqual(std.escapeStringXML("don't believe the hype"), 'don&apos;t believe the hype') &&
346+
std.assertEqual(std.escapeStringXML('PB&J'), 'PB&amp;J') &&
342347
std.assertEqual(std.escapeStringJson('!~'), '"!~"') &&
343348

344349
std.assertEqual(std.manifestPython({

0 commit comments

Comments
 (0)