Skip to content

Commit f78ac28

Browse files
committed
Various spec fixes
- `:read-write` (and `:read-only`) now ignore `contenteditable` on non-HTML elements, such as an SVG `foreignObject`, as the attribute only applies to HTML elements. Such elements inherit the state of their parent. - `:checked` now only matches the last radio button with a `checked` attribute in a radio button group, as in a browser, where a group is the radio buttons with the same `name` and form, including the `form` attribute. `:default` still matches all of them, and the contents of a `template` are unaffected as they are not connected. - `:dir()` and `:defined` no longer affect the rest of the selector list they are in. Previously, a selector list that used either of them could not have its combinators cross an `iframe`, ignored the specified namespaces, and matched nothing in XML, not even the parts that didn't use them. They still do not match in XML, but `:not(:dir(ltr))` and `:not(:defined)` now do. - `:default` and `:indeterminate` now honor the `form` attribute, which can associate a submit button or radio button with a form it is not in, or leave it without a form. It is ignored in a `template`, as in the HTML specification. Submit buttons outside of a form can now be its default button. - `:default`, `:checked`, and `:indeterminate` now handle a form inside a form like a browser. In HTML, a browser ignores the nested form and takes its end tag for that of the parent, which matters for parsers that keep the nested form, such as `html.parser`. In XHTML and in a `template`, nested forms are real forms. - `:checked` now matches the `option` that a browser would select. Without `multiple`, that is only the last `option` with a `selected` attribute in a `select`, and if there are none and the `select` shows a single option, the first `option` that is not disabled. `:default` still only considers the `selected` attribute. - `:read-write` (and `:read-only`) no longer let the contents of an `iframe` inherit `contenteditable` from outside of the `iframe`, as they are in another document.
1 parent d305d43 commit f78ac28

20 files changed

Lines changed: 1684 additions & 151 deletions

‎docs/src/dictionary/en-custom.txt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ pytest
8181
recurse
8282
recursing
8383
regex
84+
selectedness
8485
sublicense
8586
substring
8687
subtag
@@ -91,6 +92,7 @@ traceback
9192
tuple
9293
tuples
9394
un
95+
unchecks
9496
unclosed
9597
unescaped
9698
unmatchable

‎docs/src/markdown/about/changelog.md‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,26 @@ icon: lucide/scroll-text
2222
`type` attribute.
2323
- **FIX**: `:read-write` (and `:read-only`) now treat `contenteditable="plaintext-only"` elements as editing hosts and
2424
match the editable elements inside an editing host, following the nearest `contenteditable` state.
25+
- **FIX**: `:read-write` (and `:read-only`) now ignore `contenteditable` on non-HTML elements, such as an SVG
26+
`foreignObject`, as the attribute only applies to HTML elements. Such elements inherit the state of their parent.
27+
- **FIX**: `:checked` now only matches the last radio button with a `checked` attribute in a radio button group, as
28+
in a browser, where a group is the radio buttons with the same `name` and form, including the `form` attribute.
29+
`:default` still matches all of them, and the contents of a `template` are unaffected as they are not connected.
30+
- **FIX**: `:dir()` and `:defined` no longer affect the rest of the selector list they are in. Previously, a selector
31+
list that used either of them could not have its combinators cross an `iframe`, ignored the specified namespaces,
32+
and matched nothing in XML, not even the parts that didn't use them. They still do not match in XML, but
33+
`:not(:dir(ltr))` and `:not(:defined)` now do.
34+
- **FIX**: `:default` and `:indeterminate` now honor the `form` attribute, which can associate a submit button or
35+
radio button with a form it is not in, or leave it without a form. It is ignored in a `template`, as in the HTML
36+
specification. Submit buttons outside of a form can now be its default button.
37+
- **FIX**: `:default`, `:checked`, and `:indeterminate` now handle a form inside a form like a browser. In HTML, a
38+
browser ignores the nested form and takes its end tag for that of the parent, which matters for parsers that keep
39+
the nested form, such as `html.parser`. In XHTML and in a `template`, nested forms are real forms.
40+
- **FIX**: `:checked` now matches the `option` that a browser would select. Without `multiple`, that is only the
41+
last `option` with a `selected` attribute in a `select`, and if there are none and the `select` shows a single
42+
option, the first `option` that is not disabled. `:default` still only considers the `selected` attribute.
43+
- **FIX**: `:read-write` (and `:read-only`) no longer let the contents of an `iframe` inherit `contenteditable` from
44+
outside of the `iframe`, as they are in another document.
2545
- **FIX**: Fix `[attribute!=value]` excluding values that only start with the specified value, and in XML documents,
2646
`type` values that contain a newline.
2747
- **FIX**: Match the values of every attribute that the HTML specification lists as ASCII case insensitive (`rel`,

‎docs/src/markdown/faq.md‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,15 @@ Soup Sieve isn't entirely unaware of `#!html <iframe>` elements though. In Soup
6767
pseudo-classes behaved in unexpected ways without awareness to `#!html <iframe>`, this was fixed in 1.9.1.
6868
Pseudo-classes such as [`#!css :default`](./selectors/pseudo-classes.md#:default),
6969
[`#!css :indeterminate`](./selectors/pseudo-classes.md#:indeterminate), [`#!css :dir()`](./selectors/pseudo-classes.md#:dir),
70-
[`:lang()`](./selectors/pseudo-classes.md#:lang), [`#!css :root`](./selectors/pseudo-classes.md#:root), and
70+
[`:lang()`](./selectors/pseudo-classes.md#:lang), [`#!css :read-write`](./selectors/pseudo-classes.md#:read-write),
71+
[`#!css :root`](./selectors/pseudo-classes.md#:root), and
7172
[`#!css :contains()`](./selectors/pseudo-classes.md#:-soup-contains) were given awareness of `#!html <iframe>` elements
7273
to ensure they behaved properly and returned the expected elements. This doesn't mean that `select` won't return
7374
elements in an `#!html <iframe>`, but it won't allow something like `#!css :default` to select a `#!html <button>` in an
7475
`#!html <iframe>` whose parent `#!html <form>` is outside the `#!html <iframe>`. Or better put, a default
75-
`#!html <button>` will be evaluated in the context of the document it is in.
76+
`#!html <button>` will be evaluated in the context of the document it is in. Combinators in the rest of the selector are
77+
not affected, so `#!css iframe > p:dir(ltr)` will still cross the `#!html <iframe>` boundary to find the `#!html <p>`,
78+
but the `#!html <p>` will get its direction from the document it is in.
7679

7780
With all of this said, if your selectors have issues with `#!html <iframe>` elements, it is most likely because
7881
`#!html <iframe>` elements are handled differently by different parsers. `html.parser` will usually parse

‎docs/src/markdown/selectors/pseudo-classes.md‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ soup.select(':any-link')
4747
Selects any `#!html <input type="radio"/>`, `#!html <input type="checkbox"/>`, or `#!html <option>` element (in a
4848
`#!html <select>` element) that is checked or toggled to an on state.
4949

50+
Like in a browser, only one radio button can be checked in a radio button group, so if more than one has the `checked`
51+
attribute, only the last one matches. A group is made up of the radio buttons with the same `name` and the same form.
52+
53+
The same is true of `#!html <option>` elements with the `selected` attribute in a `#!html <select>`, unless it has the
54+
`multiple` attribute. If none are selected, and the `#!html <select>` shows a single option, the first `#!html <option>`
55+
that is not disabled is matched.
56+
5057
/// tab | Syntax
5158
```css
5259
:checked
@@ -91,6 +98,9 @@ soup.select(':checked')
9198
Selects any form element that is the default among a group of related elements, including: `#!html <button>`,
9299
`#!html <input type="checkbox">`, `#!html <input type="radio">`, `#!html <option>` elements.
93100

101+
The default `#!html <button>` of a form is the first submit button that belongs to it, which can be outside of the form
102+
if it refers to the form with the `form` attribute.
103+
94104
/// tab | Syntax
95105
```css
96106
:default
@@ -256,6 +266,10 @@ soup.select('input:disabled')
256266

257267
Selects elements that have no children and no text (whitespace is ignored).
258268

269+
> [!note]
270+
> Ignoring whitespace is what the Selectors Level 4 draft specifies, and the behavior is intentional, but browsers still
271+
> follow the older definition in which whitespace prevents an element from being empty.
272+
259273
/// tab | Syntax
260274
```css
261275
:empty
@@ -530,8 +544,8 @@ Selects all form elements whose are in an indeterminate state.
530544
An element is considered indeterminate if:
531545
532546
- The element is of type `#!html <input type="checkbox"/>` and the `indeterminate` attribute is set.
533-
- The element is of type `#!html <input type="radio"/>` and all other radio controls with the same name are not
534-
selected.
547+
- The element is of type `#!html <input type="radio"/>` and all other radio controls with the same name and form
548+
(including the form specified by the `form` attribute) are not selected.
535549
- The element is of type `#!html <progress>` with no value.
536550
537551
/// tab | Syntax
@@ -1271,7 +1285,8 @@ soup.select('details:open')
12711285
## `#!css :optional`:material-language-html5:{: title="HTML" data-md-color-primary="orange" .icon} {:#:optional}
12721286
12731287
Selects any `#!html <input>`, `#!html <select>`, or `#!html <textarea>` element that does not have the `required`
1274-
attribute set on it.
1288+
attribute set on it. As the HTML specification does not list `#!html <button>`, it is not matched, even though some
1289+
browsers do.
12751290
12761291
/// tab | Syntax
12771292
```css
@@ -1457,6 +1472,8 @@ soup.select(':placeholder-shown')
14571472
14581473
Selects elements (such as `#!html <input>` or `#!html <textarea>`) that are *not* editable by the user. This does not
14591474
just apply to form elements with `readonly` set, but it applies to **any** element that cannot be edited by the user.
1475+
As the HTML specification states, this is only for HTML elements, so SVG and MathML elements are not matched, even
1476+
though some browsers do.
14601477
14611478
/// tab | Syntax
14621479
```css
@@ -1497,7 +1514,8 @@ soup.select('body :read-only')
14971514
Selects elements (such as `#!html <input>` or `#!html <textarea>`) that are editable by the user. This does not just
14981515
apply to form elements as it applies to **any** element that can be edited by the user, such as a `#!html <p>` element
14991516
with `contenteditable` set on it, and the elements inside it unless a nearer `contenteditable="false"` turns editing
1500-
off.
1517+
off. `contenteditable` only applies to HTML elements, so it is ignored on elements such as those in SVG and MathML, but
1518+
this requires a parser that provides namespaces.
15011519
15021520
/// tab | Syntax
15031521
```css

0 commit comments

Comments
 (0)