taoqf / node-html-parser

A very fast HTML parser, generating a simplified DOM, with basic element query support.
MIT License
1.11k stars 107 forks source link

Cannot convert Node to HTMLElement? #217

Closed YuMao233 closed 1 year ago

YuMao233 commented 2 years ago

I know that "Node.parentNode" can get an HTMLElement type, but this is an parent element, not itself.

Did I do something wrong?

function forEachAttr(root: HTMLElement) {
  // code...
}

function walk(root: HTMLElement | Node) {
  for (const node of root.childNodes) {
   // To get or set HTML attributes, I have to get the HTMLElement object
   // But I can only get its parent elements.
    forEachAttr(node.parentNode).    // I want: node.currentElement
  }
}
taoqf commented 1 year ago

Sorry for replay so late. Would this help you?

function walk(root) {
    let count = 0;
    for (const node of root.childNodes) {
        // To get or set HTML attributes, I have to get the HTMLElement object
        // But I can only get its parent elements.
        if (node instanceof HTMLElement) {
            count+=forEachAttr(node as HTMLElement)    // I want: node.currentElement
        }
    }
}

Am I understood?

YuMao233 commented 1 year ago

Thanks for your reply, the problem has been solved.