Skip to content

Commit d305d43

Browse files
authored
Generally fix template handling (#349)
* Generally fix template handling * Bump version
1 parent cedc978 commit d305d43

8 files changed

Lines changed: 657 additions & 99 deletions

File tree

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,19 @@ icon: lucide/scroll-text
33
---
44
# Changelog
55

6-
## 3.1
6+
## 4.0
77

8+
- **BREAK**: The contents of an HTML `template` are no longer treated as part of the document. Selectors do not
9+
descend into a `template`, and elements inside one can't see beyond it, which also applies to pseudo-classes such
10+
as `:lang()`, `:disabled`, and `:default`. `select` and `filter` will still reach the contents of a `template` when
11+
they are called on the `template` itself, for example `soup.template.select('p')`. See the
12+
[FAQ](../faq.md#how-does-template-handling-work) for more information.
813
- **NEW**: Update `:disabled` and `:enabled` to treat `option` and `optgroup` elements inside a disabled `select` as
914
disabled, following a recent HTML specification update.
15+
- **FIX**: Fix the subsequent-sibling combinator and `:has(~ ...)` never matching top level elements of a fragment
16+
that has no root element, unless caching was disabled.
17+
- **FIX**: `:indeterminate` now treats a radio button group without a form as extending across the whole document
18+
instead of the top level element that contains it, and it no longer ignores radio buttons at the top level.
1019
- **FIX**: Fix `:default` to treat a `button` with a missing or invalid `type`, and an `input` with `type="image"`,
1120
as submit buttons.
1221
- **FIX**: Fix `:in-range` and `:out-of-range` raising `TypeError` when the document contains an `input` without a

‎docs/src/markdown/faq.md‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,42 @@ bringing back these behaviors.
1515
For more details on specific changes, and the reasoning why a specific change is considered a good change, or simply a
1616
feature that Soup Sieve cannot/will not support, see [Beautiful Soup Differences](./differences.md).
1717

18-
## How does `#!html <iframe>` handling work?
18+
## How does `#!html <template>` handling work? {#how-does-template-handling-work}
19+
20+
The contents of an HTML `#!html <template>` are not part of the document. They are held for later use, and in a web
21+
browser, they are kept in a separate document fragment that is available from the `content` property of the element.
22+
Soup Sieve treats them the same way: selectors do not descend into a `#!html <template>`, and elements inside one can't
23+
see anything outside of it. This is true of everything, including combinators and pseudo-classes like
24+
[`#!css :lang()`](./selectors/pseudo-classes.md#:lang), [`#!css :disabled`](./selectors/pseudo-classes.md#:disabled),
25+
and [`#!css :default`](./selectors/pseudo-classes.md#:default). A `#!html <template>` itself is an element like any
26+
other that can be selected, but it is considered to have no children.
27+
28+
Beautiful Soup has no `content` property, so to select the contents, call `select` on the `#!html <template>`.
29+
30+
```py play
31+
from bs4 import BeautifulSoup as bs
32+
html = """
33+
<html>
34+
<head></head>
35+
<body>
36+
<div>
37+
<template><p>Template content</p></template>
38+
</div>
39+
</body>
40+
</html>
41+
"""
42+
soup = bs(html, 'html5lib')
43+
print(soup.select('p'))
44+
print(soup.template.select('p'))
45+
print(soup.template.select('div p'))
46+
```
47+
48+
Any `#!html <template>` within the contents is not entered, select from that `#!html <template>` to get its contents.
49+
50+
This only applies to HTML elements. Something like `#!html <svg><template>` is not an HTML `#!html <template>`, so it
51+
has normal children, but only parsers that provide namespaces can make that distinction.
52+
53+
## How does `#!html <iframe>` handling work? {#how-does-iframe-handling-work}
1954

2055
> [!note]
2156
> Recent versions of Python's `html.parser`, `lxml`, and `html5lib` all seem to escape `iframe` content.

‎soupsieve/__meta__.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,5 +193,5 @@ def parse_version(ver: str) -> Version:
193193
return Version(major, minor, micro, release, pre, post, dev)
194194

195195

196-
__version_info__ = Version(3, 1, 0, "final")
196+
__version_info__ = Version(4, 0, 0, "final")
197197
__version__ = __version_info__._get_canonical()

0 commit comments

Comments
 (0)