# How to parse HTML raw text with ProseMirror?

**URL:** <https://discuss.prosemirror.net/t/how-to-parse-html-raw-text-with-prosemirror/3618>\
**Category:** Uncategorized\
**Created:** [March 23, 2021, 5:14am UTC](https://discuss.prosemirror.net/t/how-to-parse-html-raw-text-with-prosemirror/3618 "2021-03-23T05:14:04Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ccorcos](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/ccorcos/32/1786_2.png) [@ccorcos](https://discuss.prosemirror.net/u/ccorcos)\
**Post date:** [March 23, 2021, 5:14am UTC](https://discuss.prosemirror.net/t/how-to-parse-html-raw-text-with-prosemirror/3618/1 "2021-03-23T05:14:04Z")

</div>

I want to open edit an HTML file with ProseMirror, but I don’t want to load the html into the DOM for security reasons. Preferably ProseMirror would parse the HTML text into its JSON representation and thus only render secure content.

Is this possible?

I’m seeing that [`DOMParser`](https://prosemirror.net/docs/ref/#model.DOMParser) only parses HTML nodes.

It looks like [`prosemirror-markdown`](https://github.com/ProseMirror/prosemirror-markdown/) uses `markdown-it` to parse directly into the JSON format… So I guess I should mention that I’m just using the [`prosemirror-example-setup`](https://prosemirror.net/examples/basic/). Perhaps this doesn’t exist yet?

Thanks!

---

<div class="post-metadata">

**Author:** ![marijn](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/marijn/32/15_2.png) [@marijn](https://discuss.prosemirror.net/u/marijn)\
**Post date:** [March 23, 2021, 9:53am UTC](https://discuss.prosemirror.net/t/how-to-parse-html-raw-text-with-prosemirror/3618/2 "2021-03-23T09:53:18Z")

</div>

Browsers can parse HTML securely. Look into [`createHTMLDocument`](https://developer.mozilla.org/en-US/docs/Web/API/DOMImplementation/createHTMLDocument) for creating a detached DOM document.

---

<div class="post-metadata">

**Author:** ![ccorcos](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/ccorcos/32/1786_2.png) [@ccorcos](https://discuss.prosemirror.net/u/ccorcos)\
**Post date:** [April 1, 2021, 9:14pm UTC](https://discuss.prosemirror.net/t/how-to-parse-html-raw-text-with-prosemirror/3618/3 "2021-04-01T21:14:45Z")

</div>

Hmm. I don’t think this prevents from writing a `<script>` that evaluates within that dom though…

---

<div class="post-metadata">

**Author:** ![marijn](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/marijn/32/15_2.png) [@marijn](https://discuss.prosemirror.net/u/marijn)\
**Post date:** [April 2, 2021, 7:04am UTC](https://discuss.prosemirror.net/t/how-to-parse-html-raw-text-with-prosemirror/3618/4 "2021-04-02T07:04:37Z")

</div>

It would have taken you about 10 seconds to experimentally find out that, yes, it does prevent that.

---

<div class="post-metadata">

**Author:** ![ccorcos](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/ccorcos/32/1786_2.png) [@ccorcos](https://discuss.prosemirror.net/u/ccorcos)\
**Post date:** [April 2, 2021, 7:08pm UTC](https://discuss.prosemirror.net/t/how-to-parse-html-raw-text-with-prosemirror/3618/5 "2021-04-02T19:08:36Z")

</div>

Touché.

I didn’t see any mention of security in that documentation though.

```
const doc = document.implementation.createHTMLDocument()
const div = doc.createElement("div")
div.innerHTML = `<div>
<p>Hello world</p>
<script>
console.log("hello")
</script>
<div>`

```

![image](https://discuss.prosemirror.net/uploads/secondsite/original/2X/3/3286a8aa8ec40255d3767eaec27e90fc67c9cfd3.png)

The script doesn’t appear to be evaluated, but the script tag is there.

[It looks like DOMParser is also an option](https://security.stackexchange.com/questions/50970/is-it-safe-to-use-createhtmldocument-to-sanitize-html).

```
(new DOMParser).parseFromString(`<div>
<p>Hello world</p>
<script>
alert("hello")
</script>
<div>`, "text/html")

```

Thanks for the help

---

<div class="post-metadata">

**Author:** ![ccorcos](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/ccorcos/32/1786_2.png) [@ccorcos](https://discuss.prosemirror.net/u/ccorcos)\
**Post date:** [June 30, 2021, 11:21pm UTC](https://discuss.prosemirror.net/t/how-to-parse-html-raw-text-with-prosemirror/3618/6 "2021-06-30T23:21:04Z")

</div>

I’m running into this issue again for writing unit tests in Node.js (no browser document) 😕

---

<div class="post-metadata">

**Author:** ![marijn](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/marijn/32/15_2.png) [@marijn](https://discuss.prosemirror.net/u/marijn)\
**Post date:** [July 1, 2021, 6:22am UTC](https://discuss.prosemirror.net/t/how-to-parse-html-raw-text-with-prosemirror/3618/7 "2021-07-01T06:22:04Z")

</div>

The prosemirror-model tests use jsdom (see [here](https://github.com/ProseMirror/prosemirror-model/blob/master/test/test-dom.js#L8)) for this.

---

<div class="post-metadata">

**Author:** ![ccorcos](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/ccorcos/32/1786_2.png) [@ccorcos](https://discuss.prosemirror.net/u/ccorcos)\
**Post date:** [October 25, 2021, 2:45am UTC](https://discuss.prosemirror.net/t/how-to-parse-html-raw-text-with-prosemirror/3618/8 "2021-10-25T02:45:31Z")

</div>

Causing some pain again – `HTMLElement` is not defined… It appears `node: string | Node`. So I cant just check for `typeof node !== "string"`. Since HTMLElement inherits from Node, I need to actually check the instanceof for TypeScript to be happy…

```auto
		checkbox: {
			noBlockSelect: "first-child",
			content: "paragraph block*",
			defining: true,
			attrs: {
				checked: { default: false },
			},
			parseDOM: [
				{
					tag: `li[data-type="checkbox"]`,
					contentElement: ":scope > div",
					priority: 51,
					getAttrs(node) {
						if (node instanceof HTMLElement) {
							const child = node.querySelector(
								":scope > input"
							) as HTMLInputElement | null

							return { checked: child && child.checked }
						}
					},
				},
			],

```

Any ideas? I’m thinking I might just patch into the node.js global 😕

---

<div class="post-metadata">

**Author:** ![marijn](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/marijn/32/15_2.png) [@marijn](https://discuss.prosemirror.net/u/marijn)\
**Post date:** [October 25, 2021, 6:29am UTC](https://discuss.prosemirror.net/t/how-to-parse-html-raw-text-with-prosemirror/3618/9 "2021-10-25T06:29:32Z")

</div>

Maybe check `node.nodeType == 1` instead?
