Skip to content

Commit

Permalink
Move stack events to tree adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
fb55 committed Feb 7, 2022
1 parent 2409e2a commit 9c60b20
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
9 changes: 6 additions & 3 deletions packages/parse5/lib/parser/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,16 @@ describe('parser', () => {
expect(doctype).toHaveProperty('systemId', '');
});

describe('Options', () => {
describe('Tree adapters', () => {
it('should support onItemPush and onItemPop', () => {
const onItemPush = jest.fn();
const onItemPop = jest.fn();
const document = parse5.parse('<p><p>', {
onItemPush,
onItemPop,
treeAdapter: {
...treeAdapters.default,
onItemPush,
onItemPop,
},
});

const htmlElement = document.childNodes[0];
Expand Down
20 changes: 2 additions & 18 deletions packages/parse5/lib/parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,22 +109,6 @@ export interface ParserOptions<T extends TreeAdapterTypeMap> {
* @default `null`
*/
onParseError?: ParserErrorHandler | null;

/**
* Callback for elements being pushed to the stack of open elements.
*
* @default `null`
* @param element The element being pushed to the stack of open elements.
*/
onItemPush?: ((item: T['element']) => void) | null;

/**
* Callback for elements being popped from the stack of open elements.
*
* @default `null`
* @param item The element being popped.
*/
onItemPop?: ((item: T['element']) => void) | null;
}

//Parser
Expand Down Expand Up @@ -333,7 +317,7 @@ export class Parser<T extends TreeAdapterTypeMap> {

//Text parsing
private onItemPush(node: T['parentNode'], tid: number, isTop: boolean): void {
this.options.onItemPush?.(node);
this.treeAdapter.onItemPush?.(node);
if (isTop && this.openElements.stackTop > 0) this._setContextModes(node, tid);
}

Expand All @@ -342,7 +326,7 @@ export class Parser<T extends TreeAdapterTypeMap> {
this._setEndLocation(node, this.currentToken!);
}

this.options.onItemPop?.(node);
this.treeAdapter.onItemPop?.(node);

if (isTop) {
let current;
Expand Down
14 changes: 14 additions & 0 deletions packages/parse5/lib/tree-adapters/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,18 @@ export interface TreeAdapter<T extends TreeAdapterTypeMap = TreeAdapterTypeMap>
* @param contentElement - Content element.
*/
setTemplateContent(templateElement: T['template'], contentElement: T['documentFragment']): void;

/**
* Optional callback for elements being pushed to the stack of open elements.
*
* @param element The element being pushed to the stack of open elements.
*/
onItemPush?: (item: T['element']) => void;

/**
* Optional callback for elements being popped from the stack of open elements.
*
* @param item The element being popped.
*/
onItemPop?: (item: T['element']) => void;
}

0 comments on commit 9c60b20

Please sign in to comment.