Sanitizer: removeProcessingInstruction() method

The removeProcessingInstruction() method of the Sanitizer interface removes a previously allowed processing instruction from a sanitizer configuration.

Syntax

js
removeProcessingInstruction(processingInstruction)

Parameters

processingInstruction

A string indicating the target of the processing instruction to remove, 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 disallow the processing instruction, and false if the element was already disallowed.

Note that false might be returned if the internal configuration:

  • defines an processingInstructions array and the processing instruction is already omitted (it does not need to be removed)
  • instead defines the removeElements array and the specified processing instruction is already present (and is hence already filtered)

Examples

How to remove processing instructions

This example shows how removeProcessingInstruction() is used to remove a processing instruction from the sanitizer's processingInstructions configuration (the list of allowed processing instructions).

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

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

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

const untrustedString =
  '<?start name="placeholder">Loading...<?end><?marker name="placeholder2">';
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-removeprocessinginstruction

Browser compatibility