Skip to content

Commit

Permalink
feat(index): Add createDocumentStream (#1510)
Browse files Browse the repository at this point in the history
  • Loading branch information
fb55 committed May 10, 2023
1 parent 19edfbc commit c0b1372
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
16 changes: 16 additions & 0 deletions src/__snapshots__/index.spec.ts.snap
@@ -1,5 +1,21 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Index createDocumentStream 1`] = `
Document {
"children": [
&This is text,
<!-- and comments -->,
<tags />,
],
"endIndex": null,
"next": null,
"parent": null,
"prev": null,
"startIndex": null,
"type": "root",
}
`;

exports[`Index createDomStream 1`] = `
[
&This is text,
Expand Down
16 changes: 16 additions & 0 deletions src/index.spec.ts
@@ -1,6 +1,7 @@
import {
parseDocument,
parseDOM,
createDocumentStream,
createDomStream,
DomHandler,
DefaultHandler,
Expand Down Expand Up @@ -30,6 +31,21 @@ describe("Index", () => {
expect(dom).toMatchSnapshot();
});

test("createDocumentStream", (done) => {
const domStream = createDocumentStream((error, dom) => {
expect(error).toBeNull();
expect(dom).toMatchSnapshot();

done();
});

for (const c of "&amp;This is text<!-- and comments --><tags>") {
domStream.write(c);
}

domStream.end();
});

test("createDomStream", (done) => {
const domStream = createDomStream((error, dom) => {
expect(error).toBeNull();
Expand Down
28 changes: 24 additions & 4 deletions src/index.ts
Expand Up @@ -24,7 +24,7 @@ export type Options = ParserOptions & DomHandlerOptions;
* 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.
* @param options Optional options for the parser and DOM handler.
*/
export function parseDocument(data: string, options?: Options): Document {
const handler = new DomHandler(undefined, options);
Expand All @@ -38,7 +38,7 @@ export function parseDocument(data: string, options?: Options): Document {
* 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.
* @param options Optional options for the parser and DOM handler.
* @deprecated Use `parseDocument` instead.
*/
export function parseDOM(data: string, options?: Options): ChildNode[] {
Expand All @@ -47,10 +47,30 @@ export function parseDOM(data: string, options?: Options): ChildNode[] {
/**
* Creates a parser instance, with an attached DOM handler.
*
* @param callback A callback that will be called once parsing has been completed.
* @param options Optional options for the parser and DOM builder.
* @param callback A callback that will be called once parsing has been completed, with the resulting document.
* @param options Optional options for the parser and DOM handler.
* @param elementCallback An optional callback that will be called every time a tag has been completed inside of the DOM.
*/
export function createDocumentStream(
callback: (error: Error | null, document: Document) => void,
options?: Options,
elementCallback?: (element: Element) => void
): Parser {
const handler: DomHandler = new DomHandler(
(error: Error | null) => callback(error, handler.root),
options,
elementCallback
);
return new Parser(handler, options);
}
/**
* Creates a parser instance, with an attached DOM handler.
*
* @param callback A callback that will be called once parsing has been completed, with an array of root nodes.
* @param options Optional options for the parser and DOM handler.
* @param elementCallback An optional callback that will be called every time a tag has been completed inside of the DOM.
* @deprecated Use `createDocumentStream` instead.
*/
export function createDomStream(
callback: (error: Error | null, dom: ChildNode[]) => void,
options?: Options,
Expand Down

0 comments on commit c0b1372

Please sign in to comment.