Skip to content

Commit

Permalink
fix: parse comments
Browse files Browse the repository at this point in the history
  • Loading branch information
milahu committed Sep 19, 2022
1 parent a8e4e69 commit 82b68ff
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/nodes/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ class DOMTokenList {
export default class HTMLElement extends Node {
private _attrs: Attributes;
private _rawAttrs: RawAttributes;
private _parseOptions: Partial<Options>;
public rawTagName: string; // there is not friend funciton in es
public id: string;
public classList: DOMTokenList;
Expand Down Expand Up @@ -173,13 +174,15 @@ export default class HTMLElement extends Node {
public rawAttrs = '',
parentNode: HTMLElement | null,
range: [number, number],
private voidTag = new VoidTag()
private voidTag = new VoidTag(),
_parseOptions = {} as Partial<Options>
) {
super(parentNode, range);
this.rawTagName = tagName;
this.rawAttrs = rawAttrs || '';
this.id = keyAttrs.id || '';
this.childNodes = [];
this._parseOptions = _parseOptions;
this.classList = new DOMTokenList(
keyAttrs.class ? keyAttrs.class.split(/\s+/) : [],
(classList) => this.setAttribute('class', classList.toString()) // eslint-disable-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
Expand Down Expand Up @@ -327,8 +330,7 @@ export default class HTMLElement extends Node {
}

public set innerHTML(content: string) {
//const r = parse(content, global.options); // TODO global.options ?
const r = parse(content);
const r = parse(content, this._parseOptions);
const nodes = r.childNodes.length ? r.childNodes : [new TextNode(content, this)];
resetParent(nodes, this);
resetParent(this.childNodes, null);
Expand All @@ -339,6 +341,7 @@ export default class HTMLElement extends Node {
if (content instanceof Node) {
content = [content];
} else if (typeof content == 'string') {
options = { ...this._parseOptions, ...options };
const r = parse(content, options);
content = r.childNodes.length ? r.childNodes : [new TextNode(content, this)];
}
Expand All @@ -355,8 +358,7 @@ export default class HTMLElement extends Node {
if (node instanceof Node) {
return [node];
} else if (typeof node == 'string') {
// const r = parse(content, global.options); // TODO global.options ?
const r = parse(node);
const r = parse(node, this._parseOptions);
return r.childNodes.length ? r.childNodes : [new TextNode(node, this)];
}
return [];
Expand Down Expand Up @@ -802,7 +804,7 @@ export default class HTMLElement extends Node {
if (arguments.length < 2) {
throw new Error('2 arguments required');
}
const p = parse(html);
const p = parse(html, this._parseOptions);
if (where === 'afterend') {
const idx = this.parentNode.childNodes.findIndex((child) => {
return child === this;
Expand Down Expand Up @@ -903,7 +905,7 @@ export default class HTMLElement extends Node {
* Clone this Node
*/
public clone() {
return parse(this.toString()).firstChild;
return parse(this.toString(), this._parseOptions).firstChild;
}
}

Expand Down Expand Up @@ -1033,7 +1035,7 @@ export function base_parse(data: string, options = { lowerCaseTagName: false, co
}

const createRange = (startPos: number, endPos: number): [number, number] => [startPos - frameFlagOffset, endPos - frameFlagOffset];
const root = new HTMLElement(null, {}, '', null, [0, data.length], voidTag);
const root = new HTMLElement(null, {}, '', null, [0, data.length], voidTag, options);

let currentParent = root;
const stack = [root];
Expand Down Expand Up @@ -1116,7 +1118,7 @@ export function base_parse(data: string, options = { lowerCaseTagName: false, co

currentParent = currentParent.appendChild(
// Initialize range (end position updated later for closed tags)
new HTMLElement(tagName, attrs, attributes.slice(1), null, createRange(tagStartPos, tagEndPos), voidTag)
new HTMLElement(tagName, attrs, attributes.slice(1), null, createRange(tagStartPos, tagEndPos), voidTag, options)
);
stack.push(currentParent);

Expand Down

0 comments on commit 82b68ff

Please sign in to comment.