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

attribute selector ignores attributes with dashes in their name (e.g., data-* attributes) #17

Open
rodw opened this issue Apr 7, 2013 · 1 comment

Comments

@rodw
Copy link

rodw commented Apr 7, 2013

Given an HTML snippet such as

<span id="eg" foo="bar" data-foo="bar">something</span>

The selector span[foo] works as expected, returning the span element with the id eg.

But no variation of span[data-foo] seems to do the trick. It always selects an empty array.

More generally, any attribute name containing a dash character (-) is not recognized as an attribute selector by node-soupselect. Although I can't find a definitive reference that states attribute names can contain hyphens, they are all over HTML5 and this is contrary to the behavior of CSS on any browser I've tested. (I.e., a selector like:

span[data-foo] { background: green; }

will accurately select the element.)

I haven't tested the change extensively, but I think this is easily addressed by changing line 22 of soupselect.js from:

var attrSelectRe = /^(\w+)?\[(\w+)([=~\|\^\$\*]?)=?"?([^\]"]*)"?\]$/;

to

var attrSelectRe = /^(\w+)?\[([\w-]+)([=~\|\^\$\*]?)=?"?([^\]"]*)"?\]$/;
// the change is here---------^--^^

since \w matches letters, numbers or underscores ([a-zA-Z0-9_]) but not the dash character (-). Changing the second instance of (\w+) to ([\w-]+) adds dashes into the mix.

It may be sensible to do the same thing to the first instance of \w in that pattern so that tag names can contain hyphens as well.

I can provide pull request for this if desired.

@sbrodehl
Copy link

Seems like id's containing a dash character are broken, too.
Looks like this project is dead, anyway.

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

No branches or pull requests

2 participants