This example shows how removeElement()
is used to specify an element to be "disallowed".
#log {
height: 420px;
overflow: scroll;
padding: 0.5rem;
border: 1px solid black;
}
const logElement = document.querySelector("#log");
function log(text) {
logElement.textContent = text;
}
JavaScript
The code first creates a new Sanitizer
object that initially allows <div>
and <script>
elements, and that replaces <span>
elements with their child elements.
The code then calls removeElement()
to add <p>
, <script>
and <span>
elements to the removeElements
list in the configuration. Note that adding <script>
and <span>
removes the elements from their original lists.
if ("Sanitizer" in window) {
// Create sanitizer using SanitizerConfig
const sanitizer = new Sanitizer({
elements: ["div", "script"],
replaceWithChildrenElements: ["span"],
});
// Disallow the <p> element
sanitizer.removeElement("p");
// Disallow the <script> element
sanitizer.removeElement("script");
// Disallow the <span> element
sanitizer.removeElement("span");
// Log the sanitizer configuration
let sanitizerConfig = sanitizer.get();
log(JSON.stringify(sanitizerConfig, null, 2));
} else {
log("The HTML Sanitizer API is NOT supported in this browser.");
}
Note: This configuration is provided for demonstration only. Sanitizer configurations should include either just the allowed elements (elements
) or just the disallowed elements (removeElements
), but not both. In this case only the <div>
element is allowed and all other elements will be removed from the input: so the removed elements have no effect.
The final configuration is logged below.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4