domutils
domutils copied to clipboard
`getElementById` does not support root nodes
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.