Skip to content

Commit 7173fe3

Browse files
committed
fix(treebuild): use Unicode compatibility API
Use the existing kind-aware constructor wrapper for borrowed text spans. PyPy requires its widening path to preserve surrogate code points; CPython expands it to the same constructor used by the measured optimization. Cover lone surrogates and surrogate pairs through tree-builder callbacks.
1 parent 83e6fe2 commit 7173fe3

2 files changed

Lines changed: 12 additions & 3 deletions

File tree

src/turbohtml/_c/dom/treebuild.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ static PyObject *node_str(th_tree *tree, th_node *node) {
8181
if (node->type != TH_NODE_DOCTYPE) {
8282
if (text_is_span(node)) {
8383
const char *data = (const char *)tree->data + text_span_offset(node) * tree->kind;
84-
return PyUnicode_FromKindAndData(tree->kind, data, node->text_len);
84+
return th_str_from_kind(tree->kind, data, node->text_len);
8585
}
8686
return PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, node->text, node->text_len);
8787
}

tests/dom/test_treebuild.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,17 @@ def test_element_carries_html_namespace_and_attribute_pairs() -> None:
105105

106106
@pytest.mark.parametrize(
107107
"data",
108-
["hello", "café", "水", "😀", "before\nnext", "a&b"],
109-
ids=["ascii", "latin1", "bmp", "astral", "newline", "entity"],
108+
[
109+
pytest.param("hello", id="ascii"),
110+
pytest.param("café", id="latin1"),
111+
pytest.param("水", id="bmp"),
112+
pytest.param("😀", id="astral"),
113+
pytest.param("before\nnext", id="newline"),
114+
pytest.param("a&b", id="entity"),
115+
pytest.param("\ud800", id="high-surrogate"),
116+
pytest.param("\udfff", id="low-surrogate"),
117+
pytest.param("\ud83d\ude00", id="surrogate-pair"),
118+
],
110119
)
111120
def test_text_node_payload(data: str) -> None:
112121
body = build(f"<p>{data.replace('&', '&amp;')}</p>").children[0].children[1]

0 commit comments

Comments
 (0)