Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parsed XML doc's XPath queries fail with upper case attributes #2530

Open
amfio opened this issue Mar 12, 2019 · 7 comments
Open

Parsed XML doc's XPath queries fail with upper case attributes #2530

amfio opened this issue Mar 12, 2019 · 7 comments
Labels

Comments

@amfio
Copy link

amfio commented Mar 12, 2019

Basic info:

  • Node.js version: 8.9.1
  • jsdom version: 14.0.0

When using XPath to query/evaluate an XML document generated by JSDOM's DOMParser, the attribute part of the query seems to be forced to lower case, making it impossible to query for attributes that contain upper cased characters.

For example, given the following document:

<?xml version="1.0" encoding="utf-8"?><example Foo="bar"></example>
                                               ^-- capital F

A query for //*[@Foo="bar"] (or //*[@foo="bar"] for that matter) returns no matches, however given this document instead with lower case attributes:

<?xml version="1.0" encoding="utf-8"?><example foo="bar"></example>
                                               ^-- lower case F

Now with this new document, //*[@foo="bar"] (and the upper case equivalent //*[@Foo="bar"]) successfully find the match.

I have found a similar issue from a long time ago (#651) that was fixed by introducing Saxes to parse XML documents separately from HTML documents, however currently at the parsing level everything seems to be parsed correctly (e.g. the attributes retain their case). It is at the XPath evaluation level that the query seems to be lower cased.

I have tried narrowing down where the error might be occurring, and have reached

shouldLowerCase = true, // TODO: give option

Setting that shouldLowerCase value to false fixes for my use case, but I am not aware of what implications that has for the rest of the XPath implementation.

Minimal reproduction case

const { JSDOM } = require("jsdom");

const dom = new JSDOM();

const domParser = new dom.window.DOMParser();
const doc = domParser.parseFromString('<?xml version="1.0" encoding="utf-8"?><example Foo="bar"></example>', 'text/xml');
const result = doc.evaluate('//*[@Foo="bar"]', doc, null, XPathResult.ANY_TYPE, null);
const exampleNode = result.iterateNext();
console.log('Result:', exampleNode); // exampleNode is null

How does similar code behave in browsers?

https://jsbin.com/qegifaqumi/2/edit?js,console

The XPath query is case sensitive and can match against nodes with attributes with upper case characters.

@domenic
Copy link
Member

domenic commented Mar 12, 2019

We've had a todo for a long time to replace our ancient hand-rolled xpath implementation with a maintained third-party one. That's probably the best route to fixing this, although a spot-fix PR with a test would also be acceptable.

@orihomie
Copy link

orihomie commented Oct 1, 2019

Hi, any updates?

@domenic
Copy link
Member

domenic commented Oct 1, 2019

https://mobile.twitter.com/slicknet/status/782274190451671040

@dprentis
Copy link

@domenic would it make sense for someone (me, if I can find the time) to try prepare a PR to use a third-party library such as https://github.com/google/wicked-good-xpath ?

@domenic
Copy link
Member

domenic commented Jan 11, 2022

Yes, anything that passes the existing test suite (and ideally adds new ones) would be welcome.

@Sebmaster
Copy link
Member

Unfortunately wicked-good-xpath currently fails our test suite from what I remember. I tried working on it in google/wicked-good-xpath#41, but introduced a different bug in that one blocking merge.

@dprentis
Copy link

Thanks for the quick replies - I thought there must be a catch. Probably won't have enough spare time for this, but I can take a look.

malte-laukoetter added a commit to digitalservicebund/ris-norms that referenced this issue May 14, 2024
While writing some unit tests using xpaths, I stumbled upon some
limitations of jsdom (the dom implementation used in the tests): It e.g.
does not support upper case characters within either the xpath, which is
something we need for e.g. the eIds (see also:
jsdom/jsdom#2530). Sadly the support for
xpaths is even worse with happy-dom :/

I therefore decided to mock the implementation of the method for
evaluating xpaths in the unit tests using the
 [xpath](https://www.npmjs.com/package/xpath) library.

RISDEV-3932
malte-laukoetter added a commit to digitalservicebund/ris-norms that referenced this issue May 14, 2024
While writing some unit tests using xpaths, I stumbled upon some
limitations of jsdom (the dom implementation used in the tests): It e.g.
does not support upper case characters within either the xpath, which is
something we need for e.g. the eIds (see also:
jsdom/jsdom#2530). Sadly the support for
xpaths is even worse with happy-dom :/

I therefore decided to mock the implementation of the method for
evaluating xpaths in the unit tests using the
 [xpath](https://www.npmjs.com/package/xpath) library.

RISDEV-3932
malte-laukoetter added a commit to digitalservicebund/ris-norms that referenced this issue May 15, 2024
While writing some unit tests using xpaths, I stumbled upon some
limitations of jsdom (the dom implementation used in the tests): It e.g.
does not support upper case characters within either the xpath, which is
something we need for e.g. the eIds (see also:
jsdom/jsdom#2530). Sadly the support for
xpaths is even worse with happy-dom :/

I therefore decided to mock the implementation of the method for
evaluating xpaths in the unit tests using the
 [xpath](https://www.npmjs.com/package/xpath) library.

RISDEV-3932
malte-laukoetter added a commit to digitalservicebund/ris-norms that referenced this issue May 15, 2024
While writing some unit tests using xpaths, I stumbled upon some
limitations of jsdom (the dom implementation used in the tests): It e.g.
does not support upper case characters within either the xpath, which is
something we need for e.g. the eIds (see also:
jsdom/jsdom#2530). Sadly the support for
xpaths is even worse with happy-dom :/

I therefore decided to mock the implementation of the method for
evaluating xpaths in the unit tests using the
 [xpath](https://www.npmjs.com/package/xpath) library.

RISDEV-3932
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants