fb55 / domutils

Utilities for working with htmlparser2's DOM
https://domutils.js.org
BSD 2-Clause "Simplified" License
202 stars 56 forks source link

`getElementById` does not support root nodes #1665

Open missannil opened 6 months ago

missannil commented 6 months ago
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.