Skip to content

#291 Made id also update attributes. #292

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions src/nodes/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ export default class HTMLElement extends Node {
private _attrs: Attributes;
private _rawAttrs: RawAttributes;
private _parseOptions: Partial<Options>;
private _id: string;
public rawTagName: string; // there is not friend funciton in es
public id: string;
public classList: DOMTokenList;

/**
Expand Down Expand Up @@ -185,7 +185,7 @@ export default class HTMLElement extends Node {
super(parentNode, range);
this.rawTagName = tagName;
this.rawAttrs = rawAttrs || '';
this.id = keyAttrs.id || '';
this._id = keyAttrs.id || '';
this.childNodes = [];
this._parseOptions = _parseOptions;
this.classList = new DOMTokenList(
Expand Down Expand Up @@ -248,6 +248,13 @@ export default class HTMLElement extends Node {
return this.voidTag.isVoidElement(this.localName);
}

public get id() {
return this._id;
}
public set id(newid: string) {
this.setAttribute('id', newid);
}

/**
* Get escpaed (as-it) text value of current node and its children.
* @return {string} text content
Expand Down Expand Up @@ -417,7 +424,7 @@ export default class HTMLElement extends Node {
res.push(' '.repeat(indention) + str);
}
function dfs(node: HTMLElement) {
const idStr = node.id ? `#${node.id}` : '';
const idStr = node._id ? `#${node._id}` : '';
const classStr = node.classList.length ? `.${node.classList.value.join('.')}` : ''; // eslint-disable-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/restrict-template-expressions, @typescript-eslint/no-unsafe-call
write(`${node.rawTagName}${idStr}${classStr}`);
indention++;
Expand Down Expand Up @@ -565,7 +572,7 @@ export default class HTMLElement extends Node {
}

if (child.nodeType === NodeType.ELEMENT_NODE) {
if (child.id === id) {
if (child._id === id) {
return child;
}

Expand Down Expand Up @@ -716,9 +723,9 @@ export default class HTMLElement extends Node {
return `${name}=${val}`;
})
.join(' ');
// Update this.id
// Update this._id
if (key === 'id') {
this.id = '';
this._id = '';
}
return this;
}
Expand Down Expand Up @@ -765,9 +772,9 @@ export default class HTMLElement extends Node {
return `${name}=${val}`;
})
.join(' ');
// Update this.id
// Update this._id
if (key === 'id') {
this.id = value;
this._id = value;
}
return this;
}
Expand All @@ -793,6 +800,10 @@ export default class HTMLElement extends Node {
return `${name}=${this.quoteAttribute(String(val))}`;
})
.join(' ');
// Update this._id
if ('id' in attributes) {
this._id = attributes['id'];
}
return this;
}

Expand Down