html_escape

    Escaping/unescaping HTML entities.

    html
    escape
    Download zip
    Author
    Version
    0.3.1
    License
    MIT
    Last updated
    3 months ago
    Downloads
    413

    #html_escape

    A HTML escaping and unescaping library for MoonBit.

    #Examples

    Escaping:
    assert_eq(escape_quoted("\"'&<>"), "&quot;&#x27;&amp;&lt;&gt;")
    assert_eq(escape_safe("<script>alert('xss')</script>"), "&lt;script&gt;alert(&#x27;xss&#x27;)&lt;&#x2F;script&gt;")

    Unescaping:
    assert_eq(unescape("&lt;div&gt;hello&amp;world&lt;/div&gt;"), "<div>hello&world</div>")

    #License

    MIT License

    EscapeMode

    pub(all) enum EscapeMode {
    TextMinimal
    Text
    DoubleQuoted
    SingleQuoted
    Quoted
    Safe
    }

    Defines the HTML escaping strategy.

    Different contexts in HTML require different escaping rules. For example, plain text only needs to escape < and &, while quoted attributes must also escape quotation marks.

    UnescapeState

    type UnescapeState derive(Eq)

    escape

    fn escape(text : String, mode : EscapeMode) -> String

    Escapes a string according to the specified HTML escaping mode.

    escape_double_quoted

    fn escape_double_quoted(text : String) -> String

    Escapes a string for use inside a double-quoted HTML attribute.

    The following characters are escaped:

    • &&amp;
    • <&lt;
    • >&gt;
    • "&quot;

    escape_quoted

    fn escape_quoted(text : String) -> String

    Escapes a string for use inside a quoted HTML attribute.

    The following characters are escaped:

    • &&amp;
    • <&lt;
    • >&gt;
    • "&quot;
    • '&#x27;

    escape_safe

    fn escape_safe(text : String) -> String

    Escapes a string using the safest HTML escaping strategy.

    The following characters are escaped:

    • &&amp;
    • <&lt;
    • >&gt;
    • "&quot;
    • '&#x27;
    • / → `/

    escape_single_quoted

    fn escape_single_quoted(text : String) -> String

    Escapes a string for use inside a single-quoted HTML attribute.

    The following characters are escaped:

    • &&amp;
    • <&lt;
    • >&gt;
    • '&#x27;

    escape_text

    fn escape_text(text : String) -> String

    Escapes a string for use in normal HTML text content.

    The following characters are escaped:

    • &&amp;
    • <&lt;
    • >&gt;

    escape_text_minimal

    fn escape_text_minimal(text : String) -> String

    Escapes a string for use in HTML text content using the minimal escaping rules.

    The following characters are escaped:

    • &&amp;
    • <&lt;

    html_entities

    let html_entities : Array[(String, String)]

    Auto-generated from gen_entities.py Source: https://html.spec.whatwg.org/entities.json DO NOT EDIT MANUALLY.

    parse_decimal

    fn parse_decimal(input : String, begin : Int, end : Int) -> Int?

    parse_hex

    fn parse_hex(input : String, begin : Int, end : Int) -> Int?

    unescape

    fn unescape(text : String) -> String

    Decodes HTML entities contained in a string.

    Supported entity forms:

    Named entities:
    • &amp;
    • &lt;
    • &gt;
    • and all entries contained in html_entities

    Decimal numeric entities:
    • &#38;
    • &#160;

    Hexadecimal numeric entities:
    • &#x26;
    • &#XA0;

    Legacy entities without semicolons:
    • &amp
    • &lt
    • &gt
    • &quot
    • &apos
    • &nbsp