domutils icon indicating copy to clipboard operation
domutils copied to clipboard

`getElementById` does not support root nodes

Open missannil opened this issue 10 months ago • 0 comments

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.

missannil avatar Apr 07 '24 08:04 missannil