Skip to content

getElementById does not support root nodes #1665

@missannil

Description

@missannil
const dom: Domhandler.Document = htmlparser2.parseDocument(wxmlText, {
  xmlMode: true,
});

When I use domUtils.getElementsByTagName('tagName', dom), I can pass in the dom and get the expected element. However, when I use domUtils.getElementsById('eleId', dom), TypeScript allows me to pass in the dom, but it doesn't return the correct result (null).

Upon checking the getElementsByTagName source code, I found that it first makes an isTag check on the passed parameter (dom). This causes the dom to not enter the subsequent logic because the dom tag is 'root'. This is obviously confusing, and I hope TypeScript will be more rigorous about the types of parameters passed in.

I now use domUtils.getElementsById('eleId', dom.children) as a workaround.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions