Skip to content

Commit

Permalink
feat(index): Add parseDocument method
Browse files Browse the repository at this point in the history
  • Loading branch information
fb55 committed Dec 2, 2020
1 parent 4dd4233 commit 4653f23
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 5 deletions.
35 changes: 35 additions & 0 deletions src/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,38 @@ Array [
</a>,
]
`;

exports[`Index parseDocument 1`] = `
Document {
"children": Array [
<a
foo=""
>
<b>
<c>
ProcessingInstruction {
"data": "?foo",
"endIndex": null,
"name": "?foo",
"next": Yay!,
"parent": <c>
[Circular]
Yay!
</c>,
"prev": null,
"startIndex": null,
"type": "directive",
}
Yay!
</c>
</b>
</a>,
],
"endIndex": null,
"next": null,
"parent": null,
"prev": null,
"startIndex": null,
"type": "root",
}
`;
6 changes: 6 additions & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
parseDocument,
parseDOM,
createDomStream,
DomHandler,
Expand All @@ -21,6 +22,11 @@ Object.defineProperty(Element.prototype, "attributes", {
});

describe("Index", () => {
test("parseDocument", () => {
const dom = parseDocument("<a foo><b><c><?foo>Yay!");
expect(dom).toMatchSnapshot();
});

test("parseDOM", () => {
const dom = parseDOM("<a foo><b><c><?foo>Yay!");
expect(dom).toMatchSnapshot();
Expand Down
29 changes: 24 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { Parser, ParserOptions } from "./Parser";
export { Parser, ParserOptions };

import { DomHandler, DomHandlerOptions, Node, Element } from "domhandler";
import {
DomHandler,
DomHandlerOptions,
Node,
Element,
Document,
} from "domhandler";

export { DomHandler, DomHandlerOptions };

Expand All @@ -10,15 +16,28 @@ type Options = ParserOptions & DomHandlerOptions;
// Helper methods

/**
* Parses data, returns the resulting DOM.
* Parses the data, returns the resulting document.
*
* @param data The data that should be parsed.
* @param options Optional options for the parser and DOM builder.
*/
export function parseDOM(data: string, options?: Options): Node[] {
const handler = new DomHandler(void 0, options);
export function parseDocument(data: string, options?: Options): Document {
const handler = new DomHandler(undefined, options);
new Parser(handler, options).end(data);
return handler.dom;
return handler.root;
}
/**
* Parses data, returns an array of the root nodes.
*
* Note that the root nodes still have a `Document` node as their parent.
* Use `parseDocument` to get the `Document` node instead.
*
* @param data The data that should be parsed.
* @param options Optional options for the parser and DOM builder.
* @deprecated Use `parseDocument` instead.
*/
export function parseDOM(data: string, options?: Options): Node[] {
return parseDocument(data, options).children;
}
/**
* Creates a parser instance, with an attached DOM handler.
Expand Down

0 comments on commit 4653f23

Please sign in to comment.