Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
The allowAttribute()
method of the Sanitizer
interface sets an attribute to be allowed on all elements.
The specified attribute is added to the list of attributes
in this sanitizer's configuration. The attribute is removed from the removeAttributes
list if present.
Note that to allow/disallow attributes only on specific elements use Sanitizer.allowElement()
.
allowAttribute(attribute)
Parameters
attribute
A string indicating the name of the attribute to be allowed globally on elements, or an object with the following properties:
name
A string containing the name of the attribute.
namespace
Optional
A string containing the namespace of the attribute, which defaults to null
.
None (undefined
).
This example shows how allowAttribute()
is used to specify that an attribute is allowed on elements.
#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 no attributes. We then call allowAttribute()
with the attributes title
and mathcolor
.
if ("Sanitizer" in window) {
// Create an allow sanitizer
const sanitizer = new Sanitizer({
attributes: [],
});
// Allow the "title" attribute
sanitizer.allowAttribute("title");
// Allow the "mathcolor" attribute
sanitizer.allowAttribute("mathcolor");
// 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.");
}
Results
The final configuration is logged below. Note how both attributes are now added to the attributes
list (other attributes will not be allowed on elements when the sanitizer is used).
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