question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Attribute selectors vs \n in values

See original GitHub issue

Hi! 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:closed
  • Created 2 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
facelessusercommented, Nov 11, 2021

Wow. I don’t know what to say. To handle such a peculiar request so fast and gracious—it is absolutely awesome 😍 Thank you!

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.

1reaction
facelessusercommented, Nov 10, 2021

@gir-bot remove S: triage @gir-bot add T: bug, S: confirmed

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found