aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-09-03 20:48:11 -0400
committerBruce Hill <bruce@bruce-hill.com>2024-09-03 20:48:11 -0400
commitc14ed3e3e71535e73352ead0ecaedaa46dc5aeee (patch)
tree53b1c82def67ef82f57a10337621df4322056c98 /test
parent3c2c1a308b1e0ff24bba4d407b4d04e9ef496a94 (diff)
Add Text.replace_all({Pattern:Text}) and tweak API for replacement to
support placeholders
Diffstat (limited to 'test')
-rw-r--r--test/lang.tm13
-rw-r--r--test/text.tm8
2 files changed, 16 insertions, 5 deletions
diff --git a/test/lang.tm b/test/lang.tm
index e2093a6e..e23410e7 100644
--- a/test/lang.tm
+++ b/test/lang.tm
@@ -1,11 +1,14 @@
lang HTML:
HEADER := $HTML"<!DOCTYPE HTML>"
func escape(t:Text)->HTML:
- t = t:replace($/&/, "&amp;")
- t = t:replace($/</, "&lt;")
- t = t:replace($/>/, "&gt;")
- t = t:replace($/"/, "&quot;")
- t = t:replace($/'/, "&#39;")
+ t = t:replace_all({
+ $/&/: "&amp;",
+ $/</: "&lt;",
+ $/>/: "&gt;",
+ $/"/: "&quot",
+ $/'/: "&#39;",
+ })
+
return HTML.from_unsafe_text(t)
func escape_int(i:Int)->HTML:
diff --git a/test/text.tm b/test/text.tm
index 52c74afd..d75a56a7 100644
--- a/test/text.tm
+++ b/test/text.tm
@@ -227,4 +227,12 @@ func main():
>> $/$malicious/
= $/{1{}xxx}/
+ >> "Hello":replace($/{lower}/, "(@)", $/@/)
+ = "H(ello)"
+
+ >> " foo(xyz) foo(yyy) foo(z()) ":replace_chain([$/foo(/, $/?/, $/)/], ["baz[", "@", "]"], $/@/)
+ = " baz[xyz] baz[yyy] baz[z()] "
+
+ >> "<tag>":replace_all({$/</:"&lt;", $/>/:"&gt;"})
+ = "&lt;tag&gt;"