WebTextEscaper

Package: Libraries.Web · Inherits from: Libraries.Language.Object

WebTextEscaper makes ordinary text safe to put in a web page. A few characters have a special meaning in HTML: "<" starts a tag and "&" starts a character name, so text such as "x < y" or "Q&A" could break the page or be misread. This class writes those characters in a form the browser shows as the characters themselves. Inside an attribute's value, a double quote mark would end the value, so it is written the same way there too. Pages never use this directly: RichText escapes every piece of text it writes. It is a shared class, so there is only one.

Example Code

use Libraries.Web.WebTextEscaper

WebTextEscaper escaper
output escaper:Escape("if x < y and y > z")

Actions

Escape(text value)

Returns text that is safe to show inside an element: "&", "<" and ">" are written so the browser shows them as themselves.

Parameters

  • value text: The text, as a reader should see it.

Returns

text: The text, safe to put in a page.

Example

use Libraries.Web.WebTextEscaper

WebTextEscaper escaper
output escaper:Escape("Q&A")

EscapeAttribute(text value)

Returns text that is safe to use as an attribute's value, such as a link's address or an abbreviation's full form: the same as Escape, and double quote marks too, since one would end the value.

Parameters

  • value text: The value.

Returns

text: The value, safe to put in an attribute.

Example

use Libraries.Web.WebTextEscaper

WebTextEscaper escaper
output escaper:EscapeAttribute("https://example.com/?a=1&b=2")

EscapeForScript(text value)

Returns text that is safe inside a quoted value in a script, such as the structured data that describes a page to search engines: backslashes, quote marks and line breaks are written as escapes, and "<" is written so it can never end the script early.

Parameters

  • value text: The text.

Returns

text: The text, safe inside a quoted value in a script.

Example

use Libraries.Web.WebTextEscaper

WebTextEscaper escaper
output escaper:EscapeForScript("A lesson about x < y")

Inherited Actions

3 actions from Libraries.Language.Object