@@ -15,7 +15,42 @@ bringing back these behaviors.
1515For more details on specific changes, and the reasoning why a specific change is considered a good change, or simply a
1616feature 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.
0 commit comments