Skip to content

Commit efda043

Browse files
committed
Merge branch 'main' of github.com:taoqf/node-html-parser
2 parents 5a6a614 + 0313608 commit efda043

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

@@ -735,7 +737,7 @@ export default class HTMLElement extends Node {
735737

736738
/**
737739
* Get an attribute
738-
* @return {string} value of the attribute
740+
* @return {string | undefined} value of the attribute; or undefined if not exist
739741
*/
740742
public getAttribute(key: string): string | undefined {
741743
return this.attrs[key.toLowerCase()];
@@ -837,7 +839,7 @@ export default class HTMLElement extends Node {
837839
// }
838840
}
839841

840-
public get nextSibling() {
842+
public get nextSibling(): Node | null {
841843
if (this.parentNode) {
842844
const children = this.parentNode.childNodes;
843845
let i = 0;
@@ -849,7 +851,7 @@ export default class HTMLElement extends Node {
849851
}
850852
}
851853

852-
public get nextElementSibling(): HTMLElement {
854+
public get nextElementSibling(): HTMLElement | null {
853855
if (this.parentNode) {
854856
const children = this.parentNode.childNodes;
855857
let i = 0;
@@ -868,7 +870,7 @@ export default class HTMLElement extends Node {
868870
}
869871
}
870872

871-
public get previousSibling() {
873+
public get previousSibling(): Node | null {
872874
if (this.parentNode) {
873875
const children = this.parentNode.childNodes;
874876
let i = children.length;
@@ -880,7 +882,7 @@ export default class HTMLElement extends Node {
880882
}
881883
}
882884

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

0 commit comments

Comments
 (0)