Class HTMLSanitizer

This class contains methods to sanitize url and string, filtering html tags and attributes present in the string.

See theHTMLSanitizer for the one and only one instance of this class

Source : file core/htmlSanitizer/HTMLSanitizer.js at line 45

Constructor

new HTMLSanitizer ( )

The constructor

Source : file core/htmlSanitizer/HTMLSanitizer.js at line 293

Public methods

clone ( htmlElement )

This method clone a DOM node, removing all invalid childs and attributes

Source : file core/htmlSanitizer/HTMLSanitizer.js at line 325

Parameters

Name Type Description
htmlElement HTMLElement

The node to clone

Returns

The cloned node
Type : HTMLElement

sanitizeToColor ( colorString )

This method verify that a string describe a css color. A valid css color must start with a hash followed by 6 hex numbers

Source : file core/htmlSanitizer/HTMLSanitizer.js at line 524

Parameters

Name Type Description
colorString String

The string to test

Returns

The verified color or null if the given color is invalid
Type : String

sanitizeToHtmlElement ( htmlString, targetNode )

This method transform a string containing html and svg tags into html and svg elements and copy these elements as child nodes of the targetNode. Only tags and attributes present in the HTMLSanitizerData.#validityMap variable are copied in the targetNode. Url in the href and src attributes must be valid url (see sanitizeToUrl method)

Source : file core/htmlSanitizer/HTMLSanitizer.js at line 305

Parameters

Name Type Description
htmlString String

The string to transform

targetNode HTMLElement

The node in witch the created elements are placed

sanitizeToHtmlString ( htmlString )

This method transform a string containing html and svg tags. Tags and attributes not present in the HTMLSanitizerData.#validityMap variable are removed. Invalid Url in the href and src attributes are also removed (see sanitizeToUrl method)

Source : file core/htmlSanitizer/HTMLSanitizer.js at line 340

Parameters

Name Type Description
htmlString String

The string to transform

Returns

A HtmlStringValidationResult with the result of the validation
Type : HtmlStringValidationResult

sanitizeToJsString ( stringToSanitize )

Remove all html tags from a string and replace htmlEntities and < > ' " and nbsp chars with others similar unicode chars

Source : file core/htmlSanitizer/HTMLSanitizer.js at line 486

Parameters

Name Type Description
stringToSanitize String

The string to transform

Returns

A string with html tags removed and htmlEntities and < >' " and nbsp chars replaced
Type : String

sanitizeToUrl ( urlString, attributeName )

This method verify that a string contains a valid url.

A valid url must not contains html tags or html entities or invalid characters and must start with a valid protocol.

Valid protocols are http: and https:. For href attributes mailto:, sms: and tel: are also valid and for src attributes, data: is also valid.

sms: and tel: url's must start with a + and contains only digits, *, # or space

Source : file core/htmlSanitizer/HTMLSanitizer.js at line 372

Parameters

Name Type Description
urlString String

The url to validate

attributeName String

The attribute name in witch the url will be placed. must be 'src' or null (in this case 'href' is used as default)

Returns

A UrlValidationResult with the result of the validation
Type : UrlValidationResult

Private properties

static #htmlSanitizerData : HTMLSanitizerData

An instance of the HTMLSanitizerData

Source : file core/htmlSanitizer/HTMLSanitizer.js at line 66

#stringifiedHTML : String

The results of the #stringify method

Source : file core/htmlSanitizer/HTMLSanitizer.js at line 52

#stringifyErrors : String

The errors detected by the #stringify method

Source : file core/htmlSanitizer/HTMLSanitizer.js at line 59

Private methods

#addHtmlEntities ( htmlString )

Replace < >' " and nbsp chars with htmlEntities

Source : file core/htmlSanitizer/HTMLSanitizer.js at line 74

Parameters

Name Type Description
htmlString String

The string to transform

Returns

A string with htmlEntities
Type : String

#addStringifyErrors ( currentNode )

Helper method for the #stringify method. Add the removed attributes to the error string

Source : file core/htmlSanitizer/HTMLSanitizer.js at line 153

Parameters

Name Type Description
currentNode HTMLElement

The HTML node for witch the attributes are stringified.

#cloneHTML ( currentNode, nodeName )

Helper function for the #cloneNode method. Clone a HTML node

Source : file core/htmlSanitizer/HTMLSanitizer.js at line 230

Parameters

Name Type Description
currentNode HTMLElement

The html node to clone

nodeName String

The name of the currentNode

#cloneNode ( clonedNode, newNode )

Deep clone the contains of an HTML node into another node. Only valid tags, valid attributes, valid url's and valid texts are cloned

Source : file core/htmlSanitizer/HTMLSanitizer.js at line 267

Parameters

Name Type Description
clonedNode HTMLElement

The node to clone

newNode HTMLElement

The destination node

#cloneSvg ( currentNode, nodeName )

Helper function for the #cloneNode method. Clone a svg node

Source : file core/htmlSanitizer/HTMLSanitizer.js at line 207

Parameters

Name Type Description
currentNode SVGElement

The svg node to clone

nodeName String

The name of the currentNode

#stringify ( sourceNode )

Transform a node and it's descendants into a string, removing all the invalid tags, invalid atrributes, invalid texts and invalid url's

Source : file core/htmlSanitizer/HTMLSanitizer.js at line 172

Parameters

Name Type Description
sourceNode HTMLElement

The node to stringify

#stringifyHTMLAttributes ( currentNode, nodeName )

Helper method for the #stringify method. Validate and stringify the attributes of a HTML node

Source : file core/htmlSanitizer/HTMLSanitizer.js at line 127

Parameters

Name Type Description
currentNode HTMLElement

The HTML node for witch the attributes are stringified.

nodeName String

The name of the currentNode

#stringifySvgAttributes ( currentNode, nodeName )

Helper method for the #stringify method. Validate and stringify the attributes of a svg node

Source : file core/htmlSanitizer/HTMLSanitizer.js at line 108

Parameters

Name Type Description
currentNode SVGElement

The svg node for witch the attributes are stringified.

nodeName String

The name of the currentNode

#stringifyUrl ( url, attributeName )

Helper method for the #stringify method. Validate an url present in a htmlString

Source : file core/htmlSanitizer/HTMLSanitizer.js at line 91

Parameters

Name Type Description
url String

The url to validate

attributeName String

The attribute name in witch the url was found