Skip to content

Commit 7cc5533

Browse files
committed
refactor: override type of _docNode in subclass
1 parent 3582618 commit 7cc5533

1 file changed

Lines changed: 41 additions & 45 deletions

File tree

src/InspectorDOM.ts

Lines changed: 41 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,7 @@ export class InspectorNode {
156156
}
157157

158158
export class InspectorElement extends InspectorNode {
159-
private get element(): Element {
160-
return this._docNode as Element;
161-
}
159+
declare readonly _docNode: Element;
162160

163161
static get(element: Element): InspectorElement {
164162
const cached = InspectorElement.nodeMap.get(element);
@@ -173,128 +171,128 @@ export class InspectorElement extends InspectorNode {
173171
}
174172

175173
get tagName() {
176-
return this.element.tagName;
174+
return this._docNode.tagName;
177175
}
178176

179177
get id() {
180-
return this.element.id;
178+
return this._docNode.id;
181179
}
182180

183181
get className() {
184-
return this.element.className;
182+
return this._docNode.className;
185183
}
186184

187185
get children(): InspectorElement[] {
188-
return Array.from(this.element.children).map(InspectorElement.get);
186+
return Array.from(this._docNode.children).map(InspectorElement.get);
189187
}
190188

191189
get attributes(): NamedNodeMap {
192-
return this.element.attributes;
190+
return this._docNode.attributes;
193191
}
194192

195193
get classList(): DOMTokenList {
196-
return this.element.classList;
194+
return this._docNode.classList;
197195
}
198196

199197
querySelector(selector: string): InspectorElement | null {
200-
const el = this.element.querySelector(selector);
198+
const el = this._docNode.querySelector(selector);
201199
return el ? InspectorElement.get(el) : null;
202200
}
203201

204202
querySelectorAll(selector: string): InspectorElement[] {
205-
return Array.from(this.element.querySelectorAll(selector)).map(
203+
return Array.from(this._docNode.querySelectorAll(selector)).map(
206204
InspectorElement.get,
207205
);
208206
}
209207

210208
get textContent(): string | null {
211-
return this.element.textContent;
209+
return this._docNode.textContent;
212210
}
213211

214212
get innerHTML(): string {
215-
return this.element.innerHTML;
213+
return this._docNode.innerHTML;
216214
}
217215

218216
get outerHTML(): string {
219-
return this.element.outerHTML;
217+
return this._docNode.outerHTML;
220218
}
221219

222220
get parentNode(): InspectorNode | null {
223-
const parent = this.element.parentNode;
221+
const parent = this._docNode.parentNode;
224222
if (!parent) return null;
225223
return parent instanceof Element
226224
? InspectorElement.get(parent)
227225
: InspectorNode.get(parent);
228226
}
229227

230228
get parentElement(): InspectorElement | null {
231-
return this.element.parentElement
232-
? InspectorElement.get(this.element.parentElement)
229+
return this._docNode.parentElement
230+
? InspectorElement.get(this._docNode.parentElement)
233231
: null;
234232
}
235233

236234
get nextSibling(): InspectorNode | null {
237-
const next = this.element.nextSibling;
235+
const next = this._docNode.nextSibling;
238236
if (!next) return null;
239237
return next instanceof Element
240238
? InspectorElement.get(next)
241239
: InspectorNode.get(next);
242240
}
243241

244242
get nextElementSibling(): InspectorElement | null {
245-
return this.element.nextElementSibling
246-
? InspectorElement.get(this.element.nextElementSibling)
243+
return this._docNode.nextElementSibling
244+
? InspectorElement.get(this._docNode.nextElementSibling)
247245
: null;
248246
}
249247

250248
get previousSibling(): InspectorNode | null {
251-
const prev = this.element.previousSibling;
249+
const prev = this._docNode.previousSibling;
252250
if (!prev) return null;
253251
return prev instanceof Element
254252
? InspectorElement.get(prev)
255253
: InspectorNode.get(prev);
256254
}
257255

258256
get previousElementSibling(): InspectorElement | null {
259-
return this.element.previousElementSibling
260-
? InspectorElement.get(this.element.previousElementSibling)
257+
return this._docNode.previousElementSibling
258+
? InspectorElement.get(this._docNode.previousElementSibling)
261259
: null;
262260
}
263261

264262
get childNodes(): InspectorNode[] {
265-
return Array.from(this.element.childNodes).map((child) =>
263+
return Array.from(this._docNode.childNodes).map((child) =>
266264
child instanceof Element
267265
? InspectorElement.get(child)
268266
: InspectorNode.get(child),
269267
);
270268
}
271269

272270
get firstChild(): InspectorNode | null {
273-
const first = this.element.firstChild;
271+
const first = this._docNode.firstChild;
274272
if (!first) return null;
275273
return first instanceof Element
276274
? InspectorElement.get(first)
277275
: InspectorNode.get(first);
278276
}
279277

280278
get lastChild(): InspectorNode | null {
281-
const last = this.element.lastChild;
279+
const last = this._docNode.lastChild;
282280
if (!last) return null;
283281
return last instanceof Element
284282
? InspectorElement.get(last)
285283
: InspectorNode.get(last);
286284
}
287285

288286
getAttribute(name: string) {
289-
return this.element.getAttribute(name);
287+
return this._docNode.getAttribute(name);
290288
}
291289

292290
matches(selector: string): boolean {
293-
return this.element.matches(selector);
291+
return this._docNode.matches(selector);
294292
}
295293

296294
closest(selector: string): InspectorElement | null {
297-
const el = this.element.closest(selector);
295+
const el = this._docNode.closest(selector);
298296
return el ? InspectorElement.get(el) : null;
299297
}
300298

@@ -355,9 +353,7 @@ export class InspectorElement extends InspectorNode {
355353
}
356354

357355
export class InspectorDocument extends InspectorNode {
358-
private get document(): Document {
359-
return this._docNode as Document;
360-
}
356+
declare readonly _docNode: Document;
361357

362358
static get(document: Document): InspectorDocument {
363359
const cached = InspectorDocument.nodeMap.get(document);
@@ -380,43 +376,43 @@ export class InspectorDocument extends InspectorNode {
380376
}
381377

382378
get body(): InspectorElement | null {
383-
return this.document.body ? InspectorElement.get(this.document.body) : null;
379+
return this._docNode.body ? InspectorElement.get(this._docNode.body) : null;
384380
}
385381

386382
get head(): InspectorElement | null {
387-
return this.document.head ? InspectorElement.get(this.document.head) : null;
383+
return this._docNode.head ? InspectorElement.get(this._docNode.head) : null;
388384
}
389385

390386
get documentElement(): InspectorElement | null {
391-
return this.document.documentElement
392-
? InspectorElement.get(this.document.documentElement)
387+
return this._docNode.documentElement
388+
? InspectorElement.get(this._docNode.documentElement)
393389
: null;
394390
}
395391

396392
querySelector(selector: string): InspectorElement | null {
397-
const el = this.document.querySelector(selector);
393+
const el = this._docNode.querySelector(selector);
398394
return el ? InspectorElement.get(el) : null;
399395
}
400396

401397
querySelectorAll(selector: string): InspectorElement[] {
402-
return Array.from(this.document.querySelectorAll(selector)).map(
398+
return Array.from(this._docNode.querySelectorAll(selector)).map(
403399
InspectorElement.get,
404400
);
405401
}
406402

407403
getElementById(id: string): InspectorElement | null {
408-
const el = this.document.getElementById(id);
404+
const el = this._docNode.getElementById(id);
409405
return el ? InspectorElement.get(el) : null;
410406
}
411407

412408
getElementsByClassName(className: string): InspectorElement[] {
413-
return Array.from(this.document.getElementsByClassName(className)).map(
409+
return Array.from(this._docNode.getElementsByClassName(className)).map(
414410
InspectorElement.get,
415411
);
416412
}
417413

418414
getElementsByTagName(tagName: string): InspectorElement[] {
419-
return Array.from(this.document.getElementsByTagName(tagName)).map(
415+
return Array.from(this._docNode.getElementsByTagName(tagName)).map(
420416
InspectorElement.get,
421417
);
422418
}
@@ -425,9 +421,9 @@ export class InspectorDocument extends InspectorNode {
425421
* @experimental
426422
*/
427423
queryXPath(xpath: string): InspectorNode | null {
428-
const result = this.document.evaluate(
424+
const result = this._docNode.evaluate(
429425
xpath,
430-
this.document,
426+
this._docNode,
431427
this.nsResolver,
432428
9, //XPathResult.FIRST_ORDERED_NODE_TYPE
433429
null,
@@ -440,9 +436,9 @@ export class InspectorDocument extends InspectorNode {
440436
* @experimental
441437
*/
442438
queryXPathAll(xpath: string): InspectorNode[] {
443-
const result = this.document.evaluate(
439+
const result = this._docNode.evaluate(
444440
xpath,
445-
this.document,
441+
this._docNode,
446442
this.nsResolver,
447443
7, //XPathResult.ORDERED_NODE_SNAPSHOT_TYPE
448444
null,

0 commit comments

Comments
 (0)