---
title: "tinymce.html.DomParser"
description: ""
canonical_url: "https://www.tiny.cloud/docs/tinymce/latest/apis/tinymce.html.domparser/"
md_url: "https://www.tiny.cloud/docs/tinymce/6/apis/tinymce.html.domparser/index.md"
version: "6"
last_updated: "2022-07-13T05:38:26Z"
tokens: 1027
---
Important This page documents TinyMCE 6. For new projects, Tiny recommends using the latest, most up-to-date version. [See TinyMCE 8 documentation](../../../latest/apis/tinymce.html.domparser/).
# tinymce.html.DomParser

This class parses HTML code into a DOM like structure of nodes it will remove redundant whitespace and make sure that the node tree is valid according to the specified schema. So for example: `<p>a<p>b</p>c</p>` will become `<p>a</p><p>b</p><p>c</p>`.

## Examples

```javascript
const parser = tinymce.html.DomParser({ validate: true }, schema);
const rootNode = parser.parse('<h1>content</h1>');
```

## Summary

### Methods

| Name | Summary | Defined by |
| --- | --- | --- |
| [addAttributeFilter()](#addAttributeFilter) | Adds an attribute filter function to the parser, the parser will collect nodes that has the specified attributes and then execute the callback once it has finished parsing the document. | `DomParser` |
| [addNodeFilter()](#addNodeFilter) | Adds a node filter function to the parser, the parser will collect the specified nodes by name and then execute the callback once it has finished parsing the document. | `DomParser` |
| [parse()](#parse) | Parses the specified HTML string into a DOM like node tree and returns the result. | `DomParser` |
| [removeAttributeFilter()](#removeAttributeFilter) | Removes an attribute filter function or removes all filter functions from the parser for the attribute names provided. | `DomParser` |
| [removeNodeFilter()](#removeNodeFilter) | Removes a node filter function or removes all filter functions from the parser for the node names provided. | `DomParser` |

## Methods

### addAttributeFilter()

```javascript
addAttributeFilter(name: String, callback: Function)
```
Adds an attribute filter function to the parser, the parser will collect nodes that has the specified attributes
and then execute the callback once it has finished parsing the document.

#### Examples

```javascript
parser.addAttributeFilter('src,href', (nodes, name) => {
  for (let i = 0; i < nodes.length; i++) {
    console.log(nodes[i].name);
  }
});
```

#### Parameters

- `name (String)` - Comma separated list of attributes to collect.
- `callback (Function)` - Callback function to execute once it has collected nodes.

### addNodeFilter()

```javascript
addNodeFilter(name: String, callback: Function)
```
Adds a node filter function to the parser, the parser will collect the specified nodes by name
and then execute the callback once it has finished parsing the document.

#### Examples

```javascript
parser.addNodeFilter('p,h1', (nodes, name) => {
  for (var i = 0; i < nodes.length; i++) {
    console.log(nodes[i].name);
  }
});
```

#### Parameters

- `name (String)` - Comma separated list of nodes to collect.
- `callback (Function)` - Callback function to execute once it has collected nodes.

### parse()

```javascript
parse(html: String, args: Object): tinymce.html.Node
```
Parses the specified HTML string into a DOM like node tree and returns the result.

#### Examples

```javascript
const rootNode = tinymce.html.DomParser({...}).parse('<b>text</b>');
```

#### Parameters

- `html (String)` - Html string to sax parse.
- `args (Object)` - Optional args object that gets passed to all filter functions.

#### Return value

- `Node` - Root node containing the tree.

### removeAttributeFilter()

```javascript
removeAttributeFilter(name: String, callback: Function)
```
Removes an attribute filter function or removes all filter functions from the parser for the attribute names provided.

#### Examples

```javascript
// Remove a single filter
parser.removeAttributeFilter('src,href', someCallback);

// Remove all filters
parser.removeAttributeFilter('src,href');
```

#### Parameters

- `name (String)` - Comma separated list of attribute names to remove filters for.
- `callback (Function)` - Optional callback function to only remove a specific callback.

### removeNodeFilter()

```javascript
removeNodeFilter(name: String, callback: Function)
```
Removes a node filter function or removes all filter functions from the parser for the node names provided.

#### Examples

```javascript
// Remove a single filter
parser.removeNodeFilter('p,h1', someCallback);

// Remove all filters
parser.removeNodeFilter('p,h1');
```

#### Parameters

- `name (String)` - Comma separated list of node names to remove filters for.
- `callback (Function)` - Optional callback function to only remove a specific callback.
