Sanitizer: allowAttribute() method

The allowAttribute() method of the Sanitizer interface sets an attribute to be allowed on all elements when the sanitizer is used.

The method can be used with either an allow configuration or a remove configuration. If used with an allow configuration, the specified attribute is added to the attributes array. If used with a remove configuration, the attribute is removed from the removeAttributes array (if present).

Note that to allow/disallow attributes only on specific elements use Sanitizer.allowElement().

Syntax

js
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.

Return value

true if the operation changed the configuration to allow the attribute, and false if the configuration already allowed the attribute.

Note that false might be returned if the internal configuration:

Examples

undefined

How to allow specific attributes on elements

This example shows how allowAttribute() is used to specify that an attribute is allowed on elements.

JavaScript

The code first creates a new Sanitizer object that initially allows no attributes. We then call allowAttribute() with the attributes title and mathcolor.

js
// 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));

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).

Specifications