Skip to content

Commit 336f0b2

Browse files
committed
#291 Made id also update attributes.
1 parent d2b69e2 commit 336f0b2

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

src/nodes/html.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ export default class HTMLElement extends Node {
139139
private _attrs: Attributes;
140140
private _rawAttrs: RawAttributes;
141141
private _parseOptions: Partial<Options>;
142+
private _id: string;
142143
public rawTagName: string; // there is not friend funciton in es
143-
public id: string;
144144
public classList: DOMTokenList;
145145

146146
/**
@@ -185,7 +185,7 @@ export default class HTMLElement extends Node {
185185
super(parentNode, range);
186186
this.rawTagName = tagName;
187187
this.rawAttrs = rawAttrs || '';
188-
this.id = keyAttrs.id || '';
188+
this._id = keyAttrs.id || '';
189189
this.childNodes = [];
190190
this._parseOptions = _parseOptions;
191191
this.classList = new DOMTokenList(
@@ -248,6 +248,13 @@ export default class HTMLElement extends Node {
248248
return this.voidTag.isVoidElement(this.localName);
249249
}
250250

251+
public get id() {
252+
return this._id;
253+
}
254+
public set id(newid: string) {
255+
this.setAttribute('id', newid);
256+
}
257+
251258
/**
252259
* Get escpaed (as-it) text value of current node and its children.
253260
* @return {string} text content
@@ -417,7 +424,7 @@ export default class HTMLElement extends Node {
417424
res.push(' '.repeat(indention) + str);
418425
}
419426
function dfs(node: HTMLElement) {
420-
const idStr = node.id ? `#${node.id}` : '';
427+
const idStr = node._id ? `#${node._id}` : '';
421428
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
422429
write(`${node.rawTagName}${idStr}${classStr}`);
423430
indention++;
@@ -565,7 +572,7 @@ export default class HTMLElement extends Node {
565572
}
566573

567574
if (child.nodeType === NodeType.ELEMENT_NODE) {
568-
if (child.id === id) {
575+
if (child._id === id) {
569576
return child;
570577
}
571578

@@ -716,9 +723,9 @@ export default class HTMLElement extends Node {
716723
return `${name}=${val}`;
717724
})
718725
.join(' ');
719-
// Update this.id
726+
// Update this._id
720727
if (key === 'id') {
721-
this.id = '';
728+
this._id = '';
722729
}
723730
return this;
724731
}
@@ -765,9 +772,9 @@ export default class HTMLElement extends Node {
765772
return `${name}=${val}`;
766773
})
767774
.join(' ');
768-
// Update this.id
775+
// Update this._id
769776
if (key === 'id') {
770-
this.id = value;
777+
this._id = value;
771778
}
772779
return this;
773780
}
@@ -793,6 +800,10 @@ export default class HTMLElement extends Node {
793800
return `${name}=${this.quoteAttribute(String(val))}`;
794801
})
795802
.join(' ');
803+
// Update this._id
804+
if ('id' in attributes) {
805+
this._id = attributes['id'];
806+
}
796807
return this;
797808
}
798809

0 commit comments

Comments
 (0)