CharacterData: beforeHTML() method
Limited availability
This feature is not Baseline because it does not work in some of the most widely-used browsers.
Want more browser support for this feature? Tell us why.
The beforeHTML() method of the CharacterData interface is an XSS-safe method to parse and sanitize a string of HTML into a DocumentFragment using the HTML Sanitizer API and insert it in the children list of the CharacterData's parent, just before the CharacterData.
Syntax
beforeHTML(input)
beforeHTML(input, options)
Parameters
input-
A string defining HTML to be sanitized and inserted.
optionsOptional-
An options object with the following optional parameters:
sanitizerOptional-
A
SanitizerorSanitizerConfigobject which defines which items in the input will be removed, or the string"default"for the default sanitizer configuration.If this option is omitted, the default sanitizer configuration is used. Note that if you're using the same configuration multiple times, it's likely to be more efficient to use a
Sanitizerthan aSanitizerConfig.
Return value
None (undefined).
Exceptions
TypeError-
This is thrown if
options.sanitizeris passed a:SanitizerConfigthat isn't valid. For example, a configuration that includes bothelementsandremoveElementsconfiguration settings.- string that does not have the value
"default". - value that is not a
Sanitizer,SanitizerConfig, or string.
HierarchyRequestErrorDOMException-
Thrown when the parent node is a
Document, an<html>element, a<template>element, or a hostlessDocumentFragment.
Description
beforeHTML() is an XSS-safe method to parse and sanitize a string of HTML into a DocumentFragment using the HTML Sanitizer API and insert it in the children list of the CharacterData's parent, just before the CharacterData.
It implements the same sanitization logic as all the other XSS-safe HTML setters.
Nothing happens if the node has no parent or the parent is a <script> element (and similar for SVG <script> elements).
It should be used instead of CharacterData.beforeHTMLUnsafe(), unless there is a specific need to allow unsafe elements and attributes.
Examples
Note:
The setHTML() page includes additional examples, including a live example, which are similar to how beforeHTML() can be used.
These examples show some of the ways you can use beforeHTML() to sanitize and inject a string of HTML.
Basic usage with default sanitizer
Using the default sanitizer:
const unsanitizedString = "<p><i>abc</i><script>alert(1)<" + "/script> def</p>";
const myText = document.getElementById("myText").firstChild;
myText.beforeHTML(unsanitizedString);
// Result is that the following is added before the current text:
// <p><i>abc</i> def</p>
Using a custom sanitizer
Using a custom Sanitizer, that allows only the <div>, <p>, <button>, and <script> elements. Note that <script> is XSS-unsafe, so it will be removed anyway:
const unsanitizedString = "<p><i>abc</i><script>alert(1)<" + "/script> def</p>";
const myText = document.getElementById("myText").firstChild;
const mySanitizer = new Sanitizer({
elements: ["div", "p", "button", "script"],
});
myText.beforeHTML(unsanitizedString, { sanitizer: mySanitizer });
// Result is that the following is added before the current text:
// <p> def</p>
Using a custom sanitizer with removeElements
Using a custom SanitizerConfig, that removes only the <i> element, as well as other XSS-unsafe elements and attributes that are always removed by this method.
const unsanitizedString = "<p><i>abc</i><script>alert(1)<" + "/script> def</p>";
const myText = document.getElementById("myText").firstChild;
myText.beforeHTML(unsanitizedString, {
sanitizer: { removeElements: ["i"] },
});
// Result is that the following is added before the current text:
// <p> def</p>
// The <i> is removed by the custom setting, and the <script> is removed by default.
Specifications
| Specification |
|---|
| HTML> # dom-nondocumenttypechildnode-beforehtml> |