From abde4ee786e77a44a3b08dd356fd855a51ba5c3a Mon Sep 17 00:00:00 2001 From: William Goldman <60369048+iliketocode2@users.noreply.github.com> Date: Wed, 4 Feb 2026 11:36:14 -0500 Subject: [PATCH] Enable str to be appended to Element via span (#2447) * Enable str, bool, int and float to be appended to Element via native DOM append --- core/src/stdlib/pyscript/web.py | 4 +++- core/tests/python/tests/test_web.py | 34 +++++++++++++++++++++++++---- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/core/src/stdlib/pyscript/web.py b/core/src/stdlib/pyscript/web.py index af464086..b4c2fde1 100644 --- a/core/src/stdlib/pyscript/web.py +++ b/core/src/stdlib/pyscript/web.py @@ -642,7 +642,7 @@ class Element: Append items to this element's `children`. Accepts `Element` instances, `ElementCollection` instances, lists, - tuples, raw DOM elements, and NodeLists. + tuples, raw DOM elements, NodeLists, str, int, float, and bool. """ for item in items: if isinstance(item, Element): @@ -660,6 +660,8 @@ class Element: # NodeList or similar iterable. for element in item: self._dom_element.appendChild(element) + elif isinstance(item, (str, int, float, bool)): + self._dom_element.append(item) else: raise TypeError(f"Cannot append {type(item).__name__} to element.") diff --git a/core/tests/python/tests/test_web.py b/core/tests/python/tests/test_web.py index 1084e2b7..cfbdf936 100644 --- a/core/tests/python/tests/test_web.py +++ b/core/tests/python/tests/test_web.py @@ -290,6 +290,32 @@ class TestElement: for i in range(len(collection)): assert div.children[-1 - i].id == collection[-1 - i].id + """ + Ensure that appending a string adds a text node. + """ + def test_append_string(self): + id_ = "element-append-tests" + div = web.page[f"#{id_}"] + len_children_before = len(div.children) + text_to_append = "a simple string" + div.append(text_to_append) + assert len(div.children) == len_children_before + assert div._dom_element.textContent.endswith(text_to_append) + + """ + Test that appending a string does not overwrite existing children. + """ + def test_append_multiple_mixed_types(self): + id_ = "element-append-tests" + div = web.page[f"#{id_}"] + new_el = web.p("First item", id="first-p") + div.append(new_el) + text_to_append = "Second item" + div.append(text_to_append) + assert div.children[-1].id == "first-p" + assert "First item" in div._dom_element.textContent + assert div._dom_element.textContent.endswith(text_to_append) + def test_read_classes(self): id_ = "test_class_selector" expected_class = "a-test-class" @@ -1490,11 +1516,11 @@ class TestErrorCases: with upytest.raises(ValueError): div.on_nonexistent_event - def test_invalid_append_type(self): - """Test that appending invalid types raises TypeError.""" + def test_append_number(self): + """Test that appending numbers is supported and renders as text.""" div = web.div() - with upytest.raises(TypeError): - div.append(12345) # Numbers can't be appended + div.append(12345) + assert div._dom_element.textContent == "12345" def test_event_name_without_on_prefix(self): """Test that get_event requires on_ prefix."""