dperini / nwsapi

Fast CSS Selectors API Engine
MIT License
105 stars 36 forks source link

Tag with an uppercase letter is not working as expected in XML #64

Closed sasensi closed 1 year ago

sasensi commented 1 year ago

This was originally reported as part of the jsdom repo here. I decided to try to fix it (because I had the exact same issue) and was able to track down the issue until here.

So here's the case: given the following XML DOM tree:

<root>
  <aB>
    <c></c>
  </aB>
  <cd>
    <e></e>
  </cd>
</root>

This query: aB * doesn't return anything while it should match the <c> element.

Here's the full reproduction code:

NW.Dom.install();

const xml = `
<root>
  <aB>
    <c></c>
  </aB>
  <cd>
    <e></e>
  </cd>
</root>
`;

const dom = new window.DOMParser().parseFromString(xml, 'text/xml');
const ko = dom.querySelectorAll('aB *').length; // 0 (should be 1)
const ok = dom.querySelectorAll('cd *').length; // 1
console.log({ko,ok});

And here's a fiddle hosting it.


I don't know this library codebase well, but I investigated a bit and I have the feeling that this is caused by the fact that the selector is converted to lower case while the element localName is not.
Hence the missing match in the compiled resolver code.

What I also noticed is that if you parse the same code as HTML, with:

const dom = new window.DOMParser().parseFromString(xml, 'text/xml');

It works as expected, I guess because all the tags are automatically converted to lower case.

regseb commented 1 year ago

I think this is the same problem as the issue: [XML] Selector with two elements, one capitalized #62

dperini commented 1 year ago

@regseb Yes it seems the same issue so I am closing this too.