Skip to content

Commit 7cd06b2

Browse files
authored
Slight rework of setHTML to clarify removed elements (#41695)
1 parent b9acb53 commit 7cd06b2

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

files/en-us/web/api/element/sethtml/index.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ browser-compat: api.Element.setHTML
1010

1111
{{APIRef("HTML Sanitizer API")}}{{SeeCompatTable}}
1212

13-
The **`setHTML()`** method of the {{domxref("Element")}} interface provides an XSS-safe method to parse and sanitize a string of HTML into a {{domxref("DocumentFragment")}}, and then insert it into the DOM as a subtree of the element.
13+
The **`setHTML()`** method of the {{domxref("Element")}} interface provides an XSS-safe method to parse and sanitize a string of HTML and insert it into the DOM as a subtree of the element.
14+
15+
The method removes any elements and attributes that are considered XSS-unsafe, even if allowed by a passed sanitizer.
16+
Notably, the following elements are always removed: {{HTMLElement("script")}}, {{HTMLElement("frame")}}, {{HTMLElement("iframe")}}, {{HTMLElement("object")}}, {{SVGElement("use")}}, event handler attributes, and data attributes.
17+
18+
It is recommended (if supported) as a drop-in replacement for {{domxref("Element.innerHTML")}} when setting a user-provided string of HTML.
1419

1520
## Syntax
1621

@@ -27,9 +32,10 @@ setHTML(input, options)
2732
- : An options object with the following optional parameters:
2833
- `sanitizer`
2934
- : A {{domxref("Sanitizer")}} or {{domxref("SanitizerConfig")}} object which defines what elements of the input will be allowed or removed, or the string `"default"` for the default configuration.
35+
The method will remove any XSS-unsafe elements and attributes, even if allowed by the sanitizer.
36+
3037
Note that generally a `Sanitizer` is expected to be more efficient than a `SanitizerConfig` if the configuration is to be reused.
3138
If not specified, the default sanitizer configuration is used.
32-
The default configuration only allows known elements and attributes that are considered XSS-safe; notably, {{HTMLElement("script")}}, {{HTMLElement("frame")}}, {{HTMLElement("iframe")}}, {{HTMLElement("object")}}, {{SVGElement("use")}}, event handler attributes, and data attributes are all not in the allowlist.
3339

3440
### Return value
3541

files/en-us/web/api/shadowroot/sethtml/index.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ browser-compat: api.ShadowRoot.setHTML
88

99
{{APIRef("HTML Sanitizer API")}}
1010

11-
The **`setHTML()`** method of the {{domxref("ShadowRoot")}} interface provides an XSS-safe method to parse and sanitize a string of HTML into a {{domxref("DocumentFragment")}}, which then replaces the existing tree in the Shadow DOM.
11+
The **`setHTML()`** method of the {{domxref("ShadowRoot")}} interface provides an XSS-safe method to parse and sanitize a string of HTML, which then replaces the existing tree in the Shadow DOM.
12+
13+
It is recommended (if supported) as a drop-in replacement for {{domxref("ShadowRoot.innerHTML")}} when setting a user-provided string of HTML.
1214

1315
## Syntax
1416

@@ -25,9 +27,10 @@ setHTML(input, options)
2527
- : An options object with the following optional parameters:
2628
- `sanitizer`
2729
- : A {{domxref("Sanitizer")}} or {{domxref("SanitizerConfig")}} object which defines what elements of the input will be allowed or removed, or the string `"default"` for the default configuration.
30+
The method will remove any XSS-unsafe elements and attributes, even if allowed by the sanitizer.
31+
2832
Note that generally a `Sanitizer` is expected to be more efficient than a `SanitizerConfig` if the configuration is to be reused.
2933
If not specified, the default sanitizer configuration is used.
30-
The default configuration only allows known elements and attributes that are considered XSS-safe; notably, {{HTMLElement("script")}}, {{HTMLElement("frame")}}, {{HTMLElement("iframe")}}, {{HTMLElement("object")}}, {{SVGElement("use")}}, event handler attributes, and data attributes are all not in the allowlist.
3134

3235
### Return value
3336

0 commit comments

Comments
 (0)