Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

document DOMParser.parseFromString includeShadowRoots option #31968

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion files/en-us/web/api/domparser/parsefromstring/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ The **`parseFromString()`** method of the {{domxref("DOMParser")}} interface par
## Syntax

```js-nolint
parseFromString(string, mimeType)
parseFromString(string, mimeType, options)
```

### Parameters
Expand All @@ -38,6 +38,16 @@ parseFromString(string, mimeType)

Any other value is invalid and will cause a [`TypeError`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypeError) to be thrown.

- `options` (Optional)

- : An object. This object allows for additional configuration of the `parseFromString` method. Supported properties are:

- `includeShadowRoots` {{Non-standard_Inline}}

A value of `includeShadowRoots` enables parsing of HTML with Declarative Shadow Roots applied. This avoids some important security considerations as fragment parsing APIs like `innerHTML` or `insertAdjacentHTML()` don't support creation of Declarative Shadow Roots.

> As `includeShadowRoots` is non-standard, once available [`setHTMLUnsafe` and `parseHTMLUnsafe`](https://github.com/mdn/mdn/issues/459) should be used instead.

### Return value

An {{domxref("HTMLDocument")}} or an {{domxref("XMLDocument")}}, depending on the
Expand Down Expand Up @@ -93,6 +103,24 @@ if (errorNode) {

Additionally, the parsing error may be reported to the browser's JavaScript console.

### includeShadowRoots

The below shows an example of passing `includeShadowRoots` as an option to `parseFromString`.

```js
<script>
const html = `
<div>
<template shadowrootmode="open"></template>
</div>
`;

const fragment = new DOMParser().parseFromString(html, 'text/html', {
includeShadowRoots: true
});
</script>
```

## Specifications

{{Specifications}}
Expand Down
Loading