WebReflection / linkedom

A triple-linked lists based DOM implementation.
https://webreflection.medium.com/linkedom-a-jsdom-alternative-53dd8f699311
ISC License
1.62k stars 78 forks source link

Attributes are case sensitive #235

Closed asportnoy closed 9 months ago

asportnoy commented 9 months ago

Linkedom currently treates attributes as case sensitive. It is currently impossible to use querySelector attribute selectors if the attribute is uppercase. In addition, getAttribute is currently case sensitive and will only return if the cases match. I'm guessing these two issues are related.

Live demo: https://runkit.com/embed/vz4doq4cyr3v

const linkedom = require('linkedom');

const { document } = linkedom.parseHTML(`
    <!DOCTYPE html>
    <html>
    <head>
    </head>
    <body>
        <div SOME-ATTRIBUTE="test" another-attribute="test" id="el"></div>
    </body>
    </html>
`);

console.log(!!document.querySelector('div[SOME-ATTRIBUTE]')); // No match
console.log(!!document.querySelector('div[some-attribute]')); // No match
console.log(!!document.querySelector('div[ANOTHER-ATTRIBUTE]')); // Matches
console.log(!!document.querySelector('div[another-attribute]')); // Matches

const el = document.getElementById('el');
console.log(!!el.getAttribute('SOME-ATTRIBUTE')) // Matches
console.log(!!el.getAttribute('some-attribute'))  // No match
console.log(!!el.getAttribute('ANOTHER-ATTRIBUTE'))  // No match
console.log(!!el.getAttribute('another-attribute')) // Matches

In a real browser, these all work: https://jsfiddle.net/7vp3m2r4/

Thanks!

WebReflection commented 9 months ago

to be honest I have zero time or interest in this and the PR would likely penalize perf for everyone for something nobody cared for years at this point ... I am afraid this is a won't fix.

ugudango commented 8 months ago

Sorry for leaving a comment on a closed issue, but this should probably be mentioned in the README, or at least get a tag with "good first issue". I agree with you that it's pointless to introduce string manipulation that just slows this down. It can take a while to debug the source of a problem with this caveat, though.

In my case HTML gets passed around, and at some certain points the case changes for the attributes. The solution was to make these uniform everywhere, but I wouldn't have known this without this issue.

WebReflection commented 8 months ago

the thing is that with both SVG and XML parser stuff would break if matches are case-insensitive so that for this weird case I need to change and check the document type before matching against a lower cased attribute and all this because you can't write document.querySelector('div[SOME-ATTRIBUTE],div[some-attribute]') when that's really strictly needed?

this project goal is to work but if I need to take care of all the possible legacy quirks then I say: use JSDOM instead!

I don't want to destroy performance and become another JSDOM for sloppy stuff easy to workaround, I hope this makes at least sense, even if it's not super welcomed by everyone (I suppose).

WebReflection commented 8 months ago

P.S. agreed this might be in the README, happy to land a PR, if any, or I'll work on this when I'll find some time, thanks!