Skip to content

Commit

Permalink
fix: All elements should have type: tag in xmlMode
Browse files Browse the repository at this point in the history
  • Loading branch information
fb55 committed Apr 1, 2021
1 parent c3e1e2b commit 3977aab
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/__fixtures__/23-dom-lvl1.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "DOM level 1",
"options": { "withDomLvl1": true },
"options": {},
"html": "<div>some stray text<h1>Hello, world.</h1><!-- comment node -->more stray text</div>",
"expected": [
{
Expand Down
29 changes: 29 additions & 0 deletions src/__fixtures__/27-xml-no-special-tags.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "XML mode: All tags should have `type: tag`",
"options": {
"xmlMode": true
},
"html": "<script><style><div>",
"expected": [
{
"type": "tag",
"name": "script",
"attribs": {},
"children": [
{
"type": "tag",
"name": "style",
"attribs": {},
"children": [
{
"type": "tag",
"name": "div",
"attribs": {},
"children": []
}
]
}
]
}
]
}
10 changes: 9 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ export interface DomHandlerOptions {
* @deprecated
*/
normalizeWhitespace?: boolean;

/**
* Treat the markup as XML.
*
* @default false
*/
xmlMode?: boolean;
}

// Default options
Expand Down Expand Up @@ -151,7 +158,8 @@ export class DomHandler {
}

public onopentag(name: string, attribs: { [key: string]: string }): void {
const element = new Element(name, attribs);
const type = this.options.xmlMode ? ElementType.Tag : undefined;
const element = new Element(name, attribs, undefined, type);
this.addNode(element);
this.tagStack.push(element);
}
Expand Down
20 changes: 10 additions & 10 deletions src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,17 +184,17 @@ export class Element extends NodeWithChildren {
constructor(
public name: string,
public attribs: { [name: string]: string },
children: Node[] = []
children: Node[] = [],
type:
| ElementType.Tag
| ElementType.Script
| ElementType.Style = name === "script"
? ElementType.Script
: name === "style"
? ElementType.Style
: ElementType.Tag
) {
super(
name === "script"
? ElementType.Script
: name === "style"
? ElementType.Style
: ElementType.Tag,
children
);
this.attribs = attribs;
super(type, children);
}

// DOM Level 1 aliases
Expand Down

0 comments on commit 3977aab

Please sign in to comment.