HTML - Crystal 1.19.1

# module HTML

##
Overview

Provides HTML escaping and unescaping methods.

For HTML parsing see module XML, especially `XML.parse_html`.

NOTE To use `HTML`, you must explicitly import it with `require "html"`

##
Defined in:

html.cr
html/entities.cr

##
Class Method Summary

- .escape(string : String, io : IO) : Nil

  Same as `.escape(string)` but outputs the result to the given io.

- .escape(string : Bytes, io : IO) : Nil

  Same as `.escape(String, IO)` but accepts `Bytes` instead of `String`.

- .escape(string : String) : String

  Escapes special characters in HTML, namely `&`, `<`, `>`, `"` and `'`.

- .unescape(string : String) : String

  Returns a string where named and numeric character references (e.g.

##
Class Method Detail

def self.escape(string : String, io : IO) : Nil #

Same as `.escape(string)` but outputs the result to the given io.

    require "html"

    io = IO::Memory.new
    HTML.escape("Crystal & You", io) # => nil
    io.to_s                          # => "Crystal &amp; You"

def self.escape(string : Bytes, io : IO) : Nil #

Same as `.escape(String, IO)` but accepts `Bytes` instead of `String`.

The slice is assumed to be valid UTF-8.

def self.escape(string : String) : String #

Escapes special characters in HTML, namely `&`, `<`, `>`, `"` and `'`.

    require "html"

    HTML.escape("Crystal & You") # => "Crystal &amp; You"

def self.unescape(string : String) : String #

Returns a string where named and numeric character references (e.g. &gt;, &#62;, &#x3e;) in string are replaced with the corresponding unicode characters. This method decodes all HTML5 entities including those without a trailing semicolon (such as "&copy").

    require "html"

    HTML.unescape("Crystal &amp; You") # => "Crystal & You"