Attribute selectors vs \n in values
See original GitHub issueHi! Thanks for the powerful library.
I use it via BeautifulSoup, and I find out this behavior:
from bs4 import BeautifulSoup
soup = BeautifulSoup("<p><span title='foo bar'>foo1</span><span title='foo\nbar'>foo1</span></p>", 'html.parser')
print(*soup.select('span[title*="bar"]'))
I expected this to print both spans, but the actual output is
<span title="foo bar">foo1</span>
It seems that *=
considers only the first line of multi-line attribute:
print(*soup.select('span[title*="foo"]'))
prints this:
<span title="foo bar">foo1</span> <span title="foo
bar">foo1</span>
Is there some bug, or some conscious limitation, or \n
in attribute values is against the standard?
Thanks!
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
Attribute selectors - Learn web development | MDN
Matches elements with an attr attribute whose value is exactly value, or contains value in its (space separated) list of values.
Read more >[attribute] | CSS-Tricks
Here, the selector matches a value that is either the only value or is the first in a dash-separated list of values. To...
Read more >CSS attribute selectors: how to escape newlines within ...
To include a newline in a string, use an escape representing the line feed character in ISO-10646 (U+000A), such as "\A" or "\00000a"....
Read more >Do You Use These 7 Attribute Selectors In Your CSS?
Attribute selectors — hooking into elements through html attributes; Combinators — combining selectors to be more specific and more powerful ...
Read more >[css-selectors] add numeric comparisons on attribute values
A flag doesn't really add anything anyway - attribute selectors currently require the value to be an ident or a string, so numbers...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
It’s actually not so peculiar. Yes, it is a bit odd to use newlines in attributes, but Soup Sieve’s goal is to match real-world CSS selector behavior, as much as is practical and possible in the scraping environment. Ideally, we’d like to limit surprises and have things operate as close as possible to what people experience using selectors in real browsers. Real-world browsers handle such cases, so we should too 🙂 .
Before I wrote Soup Sieve, BeautifulSoup’s selector behavior was quite limited and very quirky. Now, you can copy in most selectors and they should work pretty much as expected meaning you don’t have to think so hard about what this selector implementation supports and what it doesn’t or what it does differently.
I plan on cutting a release later today, so you should be able to pick the fix up soon.
@gir-bot remove S: triage @gir-bot add T: bug, S: confirmed