Skip to content

Commit 0313608

Browse files
authored
Merge pull request #257 from wdzeng/main
fix: nullable return types of HTMLElement
2 parents 7fa5002 + fe83c3e commit 0313608

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

README.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ Note: Full range of CSS3 selectors supported since v3.0.0.
202202

203203
### querySelector(selector)
204204

205-
Query CSS Selector to find matching node.
205+
Query CSS Selector to find matching node. `null` if not found.
206206

207207
### getElementsByTagName(tagName)
208208

@@ -212,7 +212,7 @@ Note: Use * for all elements.
212212

213213
### closest(selector)
214214

215-
Query closest element by css selector.
215+
Query closest element by css selector. `null` if not found.
216216

217217
### appendChild(node)
218218

@@ -236,7 +236,7 @@ Remove `key` attribute.
236236

237237
### getAttribute(key: string)
238238

239-
Get `key` attribute.
239+
Get `key` attribute. `undefined` if not set.
240240

241241
### exchangeChild(oldNode: Node, newNode: Node)
242242

@@ -292,7 +292,7 @@ Get class names.
292292

293293
Clone a node.
294294

295-
#### getElementById(id: string): HTMLElement
295+
#### getElementById(id: string): HTMLElement | null
296296

297297
Get element by it's ID.
298298

@@ -322,11 +322,11 @@ Get DOM structure.
322322

323323
### firstChild
324324

325-
Get first child node.
325+
Get first child node. `undefined` if no child.
326326

327327
### lastChild
328328

329-
Get last child node.
329+
Get last child node. `undefined` if no child
330330

331331
### innerHTML
332332

@@ -338,19 +338,19 @@ Get outerHTML.
338338

339339
### nextSibling
340340

341-
Returns a reference to the next child node of the current element's parent.
341+
Returns a reference to the next child node of the current element's parent. `null` if not found.
342342

343343
### nextElementSibling
344344

345-
Returns a reference to the next child element of the current element's parent.
345+
Returns a reference to the next child element of the current element's parent. `null` if not found.
346346

347347
### previousSibling
348348

349-
Returns a reference to the previous child node of the current element's parent.
349+
Returns a reference to the previous child node of the current element's parent. `null` if not found.
350350

351351
### previousElementSibling
352352

353-
Returns a reference to the previous child element of the current element's parent.
353+
Returns a reference to the previous child element of the current element's parent. `null` if not found.
354354

355355
### textContent
356356

src/nodes/html.ts

+13-11
Original file line numberDiff line numberDiff line change
@@ -528,8 +528,9 @@ export default class HTMLElement extends Node {
528528
/**
529529
* find element by it's id
530530
* @param {string} id the id of the element to select
531+
* @returns {HTMLElement | null} the element with the given id or null if not found
531532
*/
532-
public getElementById(id: string) {
533+
public getElementById(id: string): HTMLElement | null {
533534
const stack: Array<number> = [];
534535

535536
let currentNodeReference = this as Node;
@@ -572,8 +573,9 @@ export default class HTMLElement extends Node {
572573
/**
573574
* traverses the Element and its parents (heading toward the document root) until it finds a node that matches the provided selector string. Will return itself or the matching ancestor. If no such element exists, it returns null.
574575
* @param selector a DOMString containing a selector list
576+
* @returns {HTMLElement | null} the element with the given id or null if not found
575577
*/
576-
public closest(selector: string) {
578+
public closest(selector: string): HTMLElement | null {
577579
type Predicate = (node: Node) => node is HTMLElement;
578580

579581
const mapChild = new Map<Node, Node>();
@@ -642,17 +644,17 @@ export default class HTMLElement extends Node {
642644

643645
/**
644646
* Get first child node
645-
* @return {Node} first child node
647+
* @return {Node | undefined} first child node; or undefined if none
646648
*/
647-
public get firstChild() {
649+
public get firstChild(): Node | undefined {
648650
return this.childNodes[0];
649651
}
650652

651653
/**
652654
* Get last child node
653-
* @return {Node} last child node
655+
* @return {Node | undefined} last child node; or undefined if none
654656
*/
655-
public get lastChild() {
657+
public get lastChild(): Node | undefined {
656658
return arr_back(this.childNodes);
657659
}
658660

@@ -737,7 +739,7 @@ export default class HTMLElement extends Node {
737739

738740
/**
739741
* Get an attribute
740-
* @return {string} value of the attribute
742+
* @return {string | undefined} value of the attribute; or undefined if not exist
741743
*/
742744
public getAttribute(key: string): string | undefined {
743745
return this.attrs[key.toLowerCase()];
@@ -839,7 +841,7 @@ export default class HTMLElement extends Node {
839841
// }
840842
}
841843

842-
public get nextSibling() {
844+
public get nextSibling(): Node | null {
843845
if (this.parentNode) {
844846
const children = this.parentNode.childNodes;
845847
let i = 0;
@@ -851,7 +853,7 @@ export default class HTMLElement extends Node {
851853
}
852854
}
853855

854-
public get nextElementSibling(): HTMLElement {
856+
public get nextElementSibling(): HTMLElement | null {
855857
if (this.parentNode) {
856858
const children = this.parentNode.childNodes;
857859
let i = 0;
@@ -870,7 +872,7 @@ export default class HTMLElement extends Node {
870872
}
871873
}
872874

873-
public get previousSibling() {
875+
public get previousSibling(): Node | null {
874876
if (this.parentNode) {
875877
const children = this.parentNode.childNodes;
876878
let i = children.length;
@@ -882,7 +884,7 @@ export default class HTMLElement extends Node {
882884
}
883885
}
884886

885-
public get previousElementSibling(): HTMLElement {
887+
public get previousElementSibling(): HTMLElement | null {
886888
if (this.parentNode) {
887889
const children = this.parentNode.childNodes;
888890
let i = children.length;

0 commit comments

Comments
 (0)