module HTML

Overview

Provides HTML escaping and unescaping methods.

For HTMLparsing see module XML, especiallyXML.parse_html.

NOTE To useHTML, you must explicitly import it withrequire "html"

Defined in:

html.cr
html/entities.cr

Class Method Summary

Class Method Detail

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

Same as.escape(string) but outputs the result to the givenio.

require "html"

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

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

Same as.escape(String, IO) but acceptsBytes instead ofString.

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.&amp;gt;,&amp;#62;,&amp;#x3e;) instring are replaced with the corresponding unicode characters. This method decodes all HTML5 entities including those without a trailing semicolon (such as&quot;&amp;copy&quot;).

require "html"

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