Skip to content

Commit d09de1f

Browse files
committed
Fix and simplify parse5 example using the dom-treeadapter lib
1 parent 98e0303 commit d09de1f

File tree

5 files changed

+10
-251
lines changed

5 files changed

+10
-251
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ The following features have not yet been implemented:
6060

6161
- No XML parsing (no `DOMParser`, `innerHTML` and `outerHTML` are read-only, no `insertAdjacentHTML` on `Element`, no `createContextualFragment` on `Range`). If you need to parse XML, consider using [slimdom-sax-parser][slimdom-sax-parser].
6262
- No CSS selectors, so no `querySelector` / `querySelectorAll` on `ParentNode`, no `closest` / `matches` / `webkitMatchesSelector` on `Element`. The older non-CSS query methods (`getElementById` for interface `NonElementParentNode`, and `getElementsByTagName` / `getElementsByTagNameNS` / `getElementsByClassName` on `Document`) have not yet been implemented either. To query the DOM, consider using [FontoXPath][fontoxpath].
63-
- No HTML parsing / serialization, but see [this example][parse5-example] which shows how to connect the [parse5][parse5] HTML parser.
63+
- No HTML parsing / serialization, but see [this example][parse5-example] which shows how to connect the [parse5][parse5] HTML parser with the help of the [dom-treeadapter][dom-treeadapter] library.
6464
- No special treatment of HTML documents, including `HTMLElement` and its subclasses. This also includes HTML casing behavior for attributes and tagNames, as well as the `id` / `className` / `classList` properties on `Element` and `compatMode` / `contentType` on `Document`.
6565
- No events, no `createEvent` on `Document`.
6666
- No support for shadow DOM, `Slotable` / `ShadowRoot`, no `slot` / `attachShadow` / `shadowRoot` on `Element`.
@@ -77,6 +77,7 @@ The following features have not yet been implemented:
7777
[fontoxpath]: https://github.com/FontoXML/fontoxpath/
7878
[parse5-example]: https://github.com/bwrrp/slimdom.js/tree/master/test/examples/parse5
7979
[parse5]: https://github.com/inikulin/parse5
80+
[dom-treeadapter]: https://github.com/RReverser/dom-treeadapter
8081

8182
Do not rely on the behavior or presence of any methods and properties not specified in the DOM standard. For example, do not use JavaScript array methods exposed on properties that should expose a NodeList and do not use Element as a constructor. This behavior is _not_ considered public API and may change without warning in a future release.
8283

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"@types/jest": "^26.0.20",
3737
"@types/parse5": "^6.0.0",
3838
"copyfiles": "^2.4.1",
39+
"dom-treeadapter": "^0.2.1",
3940
"jest": "^26.6.3",
4041
"npm-run-all": "^4.1.5",
4142
"parse5": "^6.0.1",

test/examples/parse5/SlimdomTreeAdapter.ts

Lines changed: 0 additions & 248 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
declare module 'dom-treeadapter';

test/examples/parse5/parse5.tests.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import * as parse5 from 'parse5';
22
import * as slimdom from '../../../src/index';
3-
import SlimdomTreeAdapter from './SlimdomTreeAdapter';
3+
4+
// DomTreeAdapter needs to be imported this way
5+
import DomTreeAdapter = require('dom-treeadapter');
46

57
function parseHTML(html: string): slimdom.Document {
6-
return parse5.parse(html, { treeAdapter: new SlimdomTreeAdapter() }) as slimdom.Document;
8+
const document = new slimdom.Document();
9+
const treeAdapter = DomTreeAdapter(document);
10+
return parse5.parse(html, { treeAdapter }) as slimdom.Document;
711
}
812

913
describe('Example: parse5 integration', () => {

0 commit comments

Comments
 (0)