dperini / nwsapi

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

[XML] Selector with two elements, one capitalized #62

Closed regseb closed 1 year ago

regseb commented 2 years ago

I found a difference between nwsapi and browsers (Firefox and Chromium). For an XML document with a capitalized element, the querySelector() method doesn't work when searching by putting two elements in the selector.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Test case</title>
    <script src="https://cdn.jsdelivr.net/npm/nwsapi@2.2.1/src/nwsapi.js" onload="NW.Dom.install()"></script>
  </head>
  <body>
    <script>
        onload = function() {
            const xml = '<?xml version="1.0"?><Foo><bar></bar></Foo>';
            const dom = new window.DOMParser().parseFromString(xml, 'text/xml');
            console.log('0. Expected [Foo]', dom.querySelectorAll('Foo')); // Passed
            console.log('1. Expected []',    dom.querySelectorAll('foo')); // Passed
            console.log('2. Expected [bar]', dom.querySelectorAll('bar')); // Passed
            console.log('3. Expected []',    dom.querySelectorAll('foo bar')); // Passed
            console.log('4. Expected [bar]', dom.querySelectorAll('Foo bar')); // Failed!

            const xml2 = xml.toLowerCase();
            const dom2 = new window.DOMParser().parseFromString(xml2, 'text/xml');
            console.log('10. Expected [bar]', dom2.querySelectorAll('bar')); // Passed
            console.log('11. Expected []',    dom2.querySelectorAll('Bar')); // Passed
            console.log('12. Expected [bar]', dom2.querySelectorAll('foo bar')); // Passed
            console.log('13. Expected []',    dom2.querySelectorAll('Foo bar')); // Failed!
            console.log('14. Expected []',    dom2.querySelectorAll('FOO bar')); // Failed!
            console.log('15. Expected []',    dom2.querySelectorAll('foo BAR')); // Passed
            console.log('16. Expected []',    dom2.querySelectorAll('FOO BAR')); // Passed
        };
    </script>
  </body>
</html>
tinco commented 2 years ago

I can confirm upgrading from jsdom 16 to jsdom 19 broke our test suites due to this bug

tinco commented 2 years ago

Eh actually, this is probably fixed by https://github.com/dperini/nwsapi/commit/87fbfa9e3974571edc192689b0712d9536839246 which has not been released yet, right?

dperini commented 2 years ago

@tinco you can now check release 2.2.2 and see if this fixed it. Thank you !

regseb commented 2 years ago

I still have the difference between browsers and nwsapi.

dperini commented 2 years ago

@regseb please try the following on your environment ... replace the complete "tag" resolver case block at line 828 with the following:

      // tag name resolver
      case (/[_a-z]/i.test(symbol) ? symbol : undefined):
        match = selector.match(Patterns.tagName);
        source = 'if(' + N + '(e.localName' + '=="' + match[1] + '"' +
          ')){' + source + '}';
        break;

this seems to solve the problem you are reporting, however it removes the change I did for cases were the document contains "MIXED_CASE" syntax, due to presence of "svg" or other "xhtml" lowercase elements.

I would like to avoid that but if this is the only possible fix, I am going to release 2.2.3 and see if the recent changes to "localName" in place of "nodeName" is already covering the same fixes and do not introduce new regressions.

regseb commented 2 years ago

@dperini It works with your modification of the tag name resolver (on version 2.2.2). All results are equal to the browsers.