Sanitizer: allowProcessingInstruction() method

The allowProcessingInstruction() method of the Sanitizer interface sets that the specified processing instruction is allowed in the output when the sanitizer is used.

Syntax

js
allowProcessingInstruction(processingInstruction)

Parameters

processingInstruction

A string indicating the target of the allowed processing instruction, or an object with the following properties:

target

A string containing the target of the processing instruction.

Return value

true if the operation changed the configuration to allow the processing instruction, and false if the configuration was not changed (usually because the processing instruction was already allowed, but potentially because the change could not be made).

Description

The allowProcessingInstruction() method sets that the specified processing instruction is allowed in the output 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 processing instruction is added to the processingInstructions array. If used with a remove configuration, the processing instruction is removed from the processingInstructions array (if present).

For example, the following code creates an allow Sanitizer that allows <?start> processing instructions and then calls allowProcessingInstruction() to additionally allow <?end> processing instructions.

js
const sanitizer = new Sanitizer({ processingInstructions: ["start"] });
sanitizer.allowElement("end");

Examples

How to allow processing instructions

This example shows how allowProcessingInstruction() is used to add processing instruction targets to the sanitizer's processingInstructions configuration (the list of allowed processing instructions).

js
const sanitizer = new Sanitizer({
  processingInstructions: ["start", "end"],
});

console.log(JSON.stringify(sanitizer.get().processingInstructions));
sanitizer.allowProcessingInstruction("marker");
console.log(JSON.stringify(sanitizer.get().processingInstructions));

// Output:
// [{"target":"end"},{"target":"start"}]
// [{"target":"end"},{"target":"marker"},{"target":"start"}]

const untrustedString = '<?start name="placeholder">Loading...<?end><?other>';
const element = document.querySelector("h1");
element.setHTMLUnsafe(untrustedString, { sanitizer });
console.log(document.querySelector("h1").innerHTML);

// Output:
// <?start name="placeholder"?>Loading...<?end ?>

Specifications

Specification
HTML
# dom-sanitizer-allowprocessinginstruction

Browser compatibility