Reference · Markup

escape

signature
escape(any) -> str

What it does

The value's text with & < > " ' written as entities (&amp; &lt; &gt; &quot; &#39;), safe to put inside an element or an attribute. Markup escapes text by itself, so this is for HTML built as a string: a piece you then mark with raw, or text written into a file. Any value is taken; its text is what str gives.

Examples

print(escape("<b>Tom & Jerry's \"show\"</b>"))print(escape(42), escape(true), escape("plain"))
output
&lt;b&gt;Tom &amp; Jerry&#39;s &quot;show&quot;&lt;/b&gt;42 true plain

Edge cases

print(escape(markdown("*a*")))print(escape(escape("<")))print(escape(null), escape(""))print(escape([1, "<"]), escape({ a: "<" }))print(escape("héllo\n😀"))
output
&lt;p&gt;&lt;em&gt;a&lt;/em&gt;&lt;/p&gt;&amp;lt;null [1, &quot;&lt;&quot;] { a: &quot;&lt;&quot; }héllo😀

Errors

None: every value has a text, so escape never fails and never gives an error value. An error value is shown as err(...), escaped.

print(escape(err("<bad>")))
output
err(&quot;&lt;bad&gt;&quot;)

Chaining

print(["<a>", "b & c"] | map(escape) | join(", "))let cell = "<script>" | escapeprint(raw(`<td>${cell}</td>`))
output
&lt;a&gt;, b &amp; c<td>&lt;script&gt;</td>

See also

raw, url_encode, markdown, str

Thread
7Useful? Like it, or add to its thread below.

Thread 0 posts

On escape(): questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.

    Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo