# Handling malformed HTML

**URL:** <https://discuss.prosemirror.net/t/handling-malformed-html/3898>\
**Category:** Uncategorized\
**Created:** [July 14, 2021, 3:13am UTC](https://discuss.prosemirror.net/t/handling-malformed-html/3898 "2021-07-14T03:13:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![chance](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/chance/32/2240_2.png) [@chance](https://discuss.prosemirror.net/u/chance)\
**Post date:** [July 14, 2021, 3:13am UTC](https://discuss.prosemirror.net/t/handling-malformed-html/3898/1 "2021-07-14T03:13:22Z")

</div>

First off, thank you for ProseMirror. The amount of work that went into it is quite impressive. While complex, it is thoroughly comprehensive.

Is there a way to detect parsing errors? For example, initializing PM with the following code:

```auto

		try {
			const doc = DOMParser.fromSchema(schema).parse(inputElement);
			const state = EditorState.create({
				doc,
				plugins: []
			});

			state.doc.check();

			const view = new EditorView({ mount: container }, { state });
		} catch (e) {
			console.error("error parsing", e);
		}

```

where `inputElement` is set to `<h1>` does not throw an error in either of the `try/catch` blocks. However, focusing on the PM editor results in the following error:

```auto
dom.js:42 Uncaught TypeError: Cannot read property 'nodeType' of null
    at scanFor (dom.js:42)
    at isEquivalentPosition (dom.js:31)
    at NodeViewDesc2.setSelection (viewdesc.js:401)
    at NodeViewDesc2.setSelection (viewdesc.js:359)
    at selectionToDOM (selection.js:57)
    at DOMObserver2.flush (domobserver.js:178)
    at DOMObserver2.onSelectionChange (domobserver.js:122)
scanFor @ dom.js:42
isEquivalentPosition @ dom.js:31
setSelection @ viewdesc.js:401
setSelection @ viewdesc.js:359
selectionToDOM @ selection.js:57
flush @ domobserver.js:178
onSelectionChange @ domobserver.js:122
dom.js:42 Uncaught TypeError: Cannot read property 'nodeType' of null
    at scanFor (dom.js:42)
    at isEquivalentPosition (dom.js:31)
    at NodeViewDesc2.setSelection (viewdesc.js:401)
    at NodeViewDesc2.setSelection (viewdesc.js:359)
    at selectionToDOM (selection.js:57)
    at DOMObserver2.flush (domobserver.js:178)
    at MutationObserver.DOMObserver2.observer (domobserver.js:50)
scanFor @ dom.js:42
isEquivalentPosition @ dom.js:31
setSelection @ viewdesc.js:401
setSelection @ viewdesc.js:359
selectionToDOM @ selection.js:57
flush @ domobserver.js:178
DOMObserver2.observer @ domobserver.js:50
attributes (async)
selectionToDOM @ selection.js:63
flush @ domobserver.js:178
onSelectionChange @ domobserver.js:122
dom.js:42 Uncaught TypeError: Cannot read property 'nodeType' of null
    at scanFor (dom.js:42)
    at isEquivalentPosition (dom.js:31)
    at NodeViewDesc2.setSelection (viewdesc.js:401)
    at NodeViewDesc2.setSelection (viewdesc.js:359)
    at selectionToDOM (selection.js:57)
    at input.js:664

```

Am I missing a method that’ll validate the content? [state.doc.check()](https://prosemirror.net/docs/ref/#model.Node.check) seems like it would suffice but I guess it doesn’t function if the content is malformed.

**edit**

Actually, the error I’m seeing may not be related to parsing errors at all.

I refactored the code to:

```auto
		try {
			const el = document.createElement("div");
			el.innerHTML = content;
			console.log(content);
			const doc = DOMParser.fromSchema(schema).parse(el);
			const state = EditorState.create({
				doc,
				plugins: []
			});
			state.doc.check();
			container.innerHTML = "";
			const view = new EditorView({ mount: container }, { state });
			el.remove();
		} catch (e) {
			console.error("error parsing", e);
		}

```

even with valid input (below) I’m getting the same errors when I focus on the editor.

```auto
<h1>title</h1>
<p>paragraph</p>

```

As of right now, I am recreating the editor every time the content changes. The output of PM is correct as well:

```auto
<div class="s-Hsmovd_is9MC ProseMirror" contenteditable="true">
  <h1>title</h1>
  <p>paragraph</p>
</div>

```

 ![Screen Shot 2021-07-14 at 12.29.20 AM](https://discuss.prosemirror.net/uploads/secondsite/original/2X/b/bce899a4fdeee28e6f99919dec3e29b38e8e2916.png)

---

<div class="post-metadata">

**Author:** ![chance](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/chance/32/2240_2.png) [@chance](https://discuss.prosemirror.net/u/chance)\
**Post date:** [July 14, 2021, 2:50pm UTC](https://discuss.prosemirror.net/t/handling-malformed-html/3898/2 "2021-07-14T14:50:08Z")

</div>

Ahh, this was related to the fact that I was recreating the view. Destroying the previous view resolved the errors.

---

<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 14, 2021, 4:35pm UTC](https://discuss.prosemirror.net/t/handling-malformed-html/3898/3 "2021-07-14T16:35:53Z")

</div>

Right. The DOM parser never emits parsing errors—in effect, it consumes everything you throw at it.

---

<div class="post-metadata">

**Author:** ![chance](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/chance/32/2240_2.png) [@chance](https://discuss.prosemirror.net/u/chance)\
**Post date:** [July 14, 2021, 5:41pm UTC](https://discuss.prosemirror.net/t/handling-malformed-html/3898/4 "2021-07-14T17:41:35Z")

</div>

The fact that you pass it a DOM element was somewhat confusing to me at first. However, I’m guessing that ensures the HTML is valid which is clever.

Thanks man. PM is truly awesome. A bit of a beast to get a comprehensive view of the API so I can only imagine crafting it.
