Closed IAkumaI closed 2 years ago
Oh, I see. getAttributes
do exists in Node.
But TypeScript think is don't, because of node.d.ts:
export default abstract class Node {
parentNode: HTMLElement;
abstract nodeType: NodeType;
childNodes: Node[];
range: readonly [number, number];
abstract text: string;
abstract rawText: string;
abstract toString(): string;
abstract clone(): Node;
constructor(parentNode?: HTMLElement, range?: [number, number]);
/**
* Remove current node
*/
remove(): this;
get innerText(): string;
get textContent(): string;
set textContent(val: string);
}
Is it bug or old version or what?
Well. It's just cause childNodes can be Node or HTMLElement and there is no way to know what is got. So, this is not issue. Closed.
By the way,
const root = parse('<div class="item">Some text</div>');
(root.firstChild as HTMLElement).getAttribute('class'); // should be `item`
should works.
Yeah, it works. It's just TypeScript showing the error "getAttribute does not exists on Node" without casting to HTMLElement.
But. In production you have to check firstChild
is real HTMLElement, but not text-Node.
Indeed.
But. In production you have to check
firstChild
is real HTMLElement, but not text-Node.
Well. It's just cause childNodes can be Node or HTMLElement and there is no way to know what is got. So, this is not issue. Closed.
If the childNodes can be either Node
or HTMLElement
, does that not mean the typing for .firstChild
(and others) is incorrect?
/**
* Get first child node
* @return {Node} first child node <- should this not be `Node | HTMLElement`?
*/
public get firstChild() {
return this.childNodes[0];
}
Hi. How can i get attribute from first root element? I found https://github.com/taoqf/node-html-parser/blob/main/test/tests/quoteattributes.js#L26 is tests, but root.firstChild is Node, so it does not has getAttribute method.
I am using version 5.3.3 (latests from npm).
What am I doing wrong?