-
Notifications
You must be signed in to change notification settings - Fork 22.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial content for setHTMLUnsafe and parseHTMLUnsafe (#33492)
* Add initial content for setHTMLUnsafe and parseHTMLUnsafe * Remove erroneous secure context banner * Apply suggestions from (my own) code review * Updates to parseHTMLUnsafe(), parent topic, cross link * Apply suggestions from code review * Element.setHTMLUnsafe * ShadowRoot.setHTMLUnsafe() * Apply suggestions from code review - remove untrusted Co-authored-by: Luke Warlow <[email protected]> --------- Co-authored-by: Hamish Willee <[email protected]>
- Loading branch information
1 parent
f7856d2
commit 3c386c3
Showing
7 changed files
with
177 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
files/en-us/web/api/document/parsehtmlunsafe_static/index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
--- | ||
title: "Document: parseHTMLUnsafe() static method" | ||
short-title: parseHTMLUnsafe() | ||
slug: Web/API/Document/parseHTMLUnsafe_static | ||
page-type: web-api-static-method | ||
browser-compat: api.Document.parseHTMLUnsafe_static | ||
--- | ||
|
||
{{APIRef("DOM")}} | ||
|
||
The **`parseHTMLUnsafe()`** static method of the {{domxref("Document")}} object is used to parse a string of HTML, which may contain [declarative shadow roots](/en-US/docs/Web/HTML/Element/template#declarative_shadow_dom), in order to create a new {{domxref("Document")}} instance. | ||
|
||
The suffix "Unsafe" in the method name indicates that, while `<script>` elements are not evaluated during parsing, the method does not sanitize other potentially unsafe XSS-relevant input. | ||
|
||
The resulting `Document` will have a [content type](/en-US/docs/Web/API/Document/contentType) of "text/html", a [character set](/en-US/docs/Web/API/Document/characterSet) of UTF-8, and a URL of "about:blank" | ||
|
||
## Syntax | ||
|
||
```js-nolint | ||
Document.parseHTMLUnsafe(input) | ||
``` | ||
|
||
### Parameters | ||
|
||
- `html` | ||
- : A string of HTML to be parsed. | ||
|
||
### Return value | ||
|
||
A {{domxref("Document")}}. | ||
|
||
### Exceptions | ||
|
||
None. | ||
|
||
## Specifications | ||
|
||
{{Specifications}} | ||
|
||
## Browser compatibility | ||
|
||
{{Compat}} | ||
|
||
## See also | ||
|
||
- {{domxref("DOMParser.parseFromString()")}} for parsing HTML or XML into a DOM tree | ||
- {{domxref("Element.setHTMLUnsafe")}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
--- | ||
title: "Element: setHTMLUnsafe() method" | ||
short-title: setHTMLUnsafe() | ||
slug: Web/API/Element/setHTMLUnsafe | ||
page-type: web-api-instance-method | ||
browser-compat: api.Element.setHTMLUnsafe | ||
--- | ||
|
||
{{APIRef("DOM")}} | ||
|
||
The **`setHTMLUnsafe()`** method of the {{domxref("Element")}} interface is used to parse a string of HTML into a {{domxref("DocumentFragment")}}, which then replaces the element's subtree in the DOM. | ||
The input HTML may include [declarative shadow roots](/en-US/docs/Web/HTML/Element/template#declarative_shadow_dom). | ||
|
||
The suffix "Unsafe" in the method name indicates that the method does not sanitize or remove potentially unsafe XSS-relevant input, such as `<script>` elements, and script or event handler content attributes. | ||
|
||
If the string of HTML defines more than one [declarative shadow root](/en-US/docs/Web/HTML/Element/template#declarative_shadow_dom) in a particular shadow host then only the first {{domxref("ShadowRoot")}} is created — subsequent declarations are parsed as `<template>` elements within that shadow root. | ||
|
||
> **Note:** This method should be used instead of {{domxref("Element.innerHTML")}} when a string of HTML may contain declarative shadow roots. | ||
## Syntax | ||
|
||
```js-nolint | ||
setHTMLUnsafe(html) | ||
``` | ||
|
||
### Parameters | ||
|
||
- `html` | ||
- : A string defining HTML to be parsed. | ||
|
||
### Return value | ||
|
||
None (`undefined`). | ||
|
||
### Exceptions | ||
|
||
None. | ||
|
||
## Examples | ||
|
||
The code below demonstrates how to parse a string of HTML and insert it into the `Element` with an id of `target`. | ||
|
||
```js | ||
const value = "<p>This is a string of text</p>"; // string of HTML | ||
|
||
// Get the Element with id "target" and set it with the string. | ||
document.getElementById("target").setHTMLUnsafe(value); | ||
|
||
// Result (as a string): "<p>This is a string of text</p>" | ||
``` | ||
|
||
## Specifications | ||
|
||
{{Specifications}} | ||
|
||
## Browser compatibility | ||
|
||
{{Compat}} | ||
|
||
## See also | ||
|
||
- {{domxref("ShadowRoot.setHTMLUnsafe()")}} | ||
- {{domxref("Element.innerHTML")}} | ||
- {{domxref("Document.parseHTMLUnsafe_static", "Document.parseHTMLUnsafe()")}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
--- | ||
title: "ShadowRoot: setHTMLUnsafe() method" | ||
short-title: setHTMLUnsafe() | ||
slug: Web/API/ShadowRoot/setHTMLUnsafe | ||
page-type: web-api-instance-method | ||
browser-compat: api.ShadowRoot.setHTMLUnsafe | ||
--- | ||
|
||
{{APIRef("Shadow DOM")}} | ||
|
||
The **`setHTMLUnsafe()`** method of the {{domxref("ShadowRoot")}} interface is used to parse a string of HTML into a {{domxref("DocumentFragment")}}, which then replaces the element's subtree in the DOM. | ||
The input HTML may include [declarative shadow roots](/en-US/docs/Web/HTML/Element/template#declarative_shadow_dom). | ||
|
||
The suffix "Unsafe" in the method name indicates that the method does not sanitize or remove potentially unsafe XSS-relevant input, such as `<script>` elements, and script or event handler content attributes. | ||
|
||
If the string of HTML defines more than one [declarative shadow root](/en-US/docs/Web/HTML/Element/template#declarative_shadow_dom) in a particular shadow host then only the first {{domxref("ShadowRoot")}} is created — subsequent declarations are parsed as `<template>` elements within that shadow root. | ||
|
||
> **Note:** This method should be used instead of {{domxref("ShadowRoot.innerHTML")}} when a string of HTML may contain declarative shadow roots. | ||
## Syntax | ||
|
||
```js-nolint | ||
setHTMLUnsafe(html) | ||
``` | ||
|
||
### Parameters | ||
|
||
- `html` | ||
- : A string defining HTML to be parsed. | ||
|
||
### Return value | ||
|
||
None (`undefined`). | ||
|
||
### Exceptions | ||
|
||
None. | ||
|
||
## Specifications | ||
|
||
{{Specifications}} | ||
|
||
## Browser compatibility | ||
|
||
{{Compat}} | ||
|
||
## See also | ||
|
||
- {{domxref("Element.setHTMLUnsafe()")}} | ||
- {{domxref("ShadowRoot.innerHTML")}} | ||
- {{domxref("Document.parseHTMLUnsafe_static", "Document.parseHTMLUnsafe()")}} |