Skip to content

Commit

Permalink
revert options and reset default values
Browse files Browse the repository at this point in the history
  • Loading branch information
taoqf committed Oct 27, 2020
1 parent 0d930bb commit d7a8c29
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ Parse given data, and return root of the generated DOM.
```js
{
lowerCaseTagName: false, // convert tag name to lower case (hurt performance heavily)
script: true, // retrieve content in <script> (hurt performance slightly)
style: true, // retrieve content in <style> (hurt performance slightly)
pre: true, // retrieve content in <pre> (hurt performance slightly)
comment: false // retrieve comments (hurt performance slightly)
}
```
Expand Down
29 changes: 17 additions & 12 deletions src/nodes/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -660,8 +660,11 @@ const kBlockTextElements = {
};

export interface Options {
lowerCaseTagName?: boolean;
comment?: boolean;
lowerCaseTagName: boolean;
script: boolean;
style: boolean;
pre: boolean;
comment: boolean;
}

const frameflag = 'documentfragmentcontainer';
Expand All @@ -675,7 +678,7 @@ const frameflag = 'documentfragmentcontainer';
export function parse(data: string, options?: Options): HTMLElement & { valid: boolean };
export function parse(data: string, options?: Options & { noFix: false }): HTMLElement & { valid: boolean };
export function parse(data: string, options?: Options & { noFix: true }): (HTMLElement | TextNode) & { valid: boolean };
export function parse(data: string, options = {} as Options & { noFix?: boolean }) {
export function parse(data: string, options = { pre: true, style: true, script: true, lowerCaseTagName: false, comment: false } as Options & { noFix?: boolean }) {
const root = new HTMLElement(null, {});
let currentParent = root;
const stack = [root];
Expand Down Expand Up @@ -734,15 +737,17 @@ export function parse(data: string, options = {} as Options & { noFix?: boolean
}
return data.indexOf(closeMarkup, kMarkupPattern.lastIndex);
})();
let text: string;
if (index === -1) {
// there is no matching ending for the text element.
text = data.substr(kMarkupPattern.lastIndex);
} else {
text = data.substring(kMarkupPattern.lastIndex, index);
}
if (text.length > 0) {
currentParent.appendChild(new TextNode(text));
if (options[match[2]]) {
let text: string;
if (index === -1) {
// there is no matching ending for the text element.
text = data.substr(kMarkupPattern.lastIndex);
} else {
text = data.substring(kMarkupPattern.lastIndex, index);
}
if (text.length > 0) {
currentParent.appendChild(new TextNode(text));
}
}
if (index === -1) {
lastTextPos = kMarkupPattern.lastIndex = data.length + 1;
Expand Down

0 comments on commit d7a8c29

Please sign in to comment.