Skip to content

Commit ce10cf8

Browse files
committed
Special case parsing of text nodes within content elements to avoid escaping markup.
1 parent 41aa486 commit ce10cf8

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

tdom/parser.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,16 @@
55

66
from markupsafe import Markup
77

8-
from .nodes import VOID_ELEMENTS, Comment, DocumentType, Element, Fragment, Node, Text
8+
from .nodes import (
9+
CONTENT_ELEMENTS,
10+
VOID_ELEMENTS,
11+
Comment,
12+
DocumentType,
13+
Element,
14+
Fragment,
15+
Node,
16+
Text,
17+
)
918

1019
_FRAGMENT_TAG = f"t🐍f-{''.join(random.choices(string.ascii_lowercase, k=4))}-"
1120

@@ -61,7 +70,7 @@ def handle_endtag(self, tag: str) -> None:
6170
self.append_element_child(element)
6271

6372
def handle_data(self, data: str) -> None:
64-
text = Text(Markup(data))
73+
text = Text(Markup(data) if self.in_content_element() else data)
6574
self.append_child(text)
6675

6776
def handle_comment(self, data: str) -> None:
@@ -76,6 +85,11 @@ def handle_decl(self, decl: str) -> None:
7685
# For simplicity, we ignore other declarations.
7786
pass
7887

88+
def in_content_element(self) -> bool:
89+
"""Return True if the current context is within a content element."""
90+
open_element = self.get_open_element()
91+
return open_element is not None and open_element.tag in CONTENT_ELEMENTS
92+
7993
def get_parent(self) -> Fragment | Element:
8094
"""Return the current parent node to which new children should be added."""
8195
return self.stack[-1] if self.stack else self.root

0 commit comments

Comments
 (0)