Enable str to be appended to Element via span#2447
Enable str to be appended to Element via span#2447WebReflection merged 6 commits intopyscript:mainfrom
Conversation
|
On the Web, Adding elements instead of just text can cause issues with both CSS and layout performance, because elements are reachable via CSS while raw text nodes aren't. I hence believe this is a great draft for some improvement, and I'd like to thank you for that, but I think we should have a It's /cc @ntoll |
|
Hey hey @iliketocode2 - good first draft. I agree with @WebReflection that adding elements rather than just a pure text node (see: https://developer.mozilla.org/en-US/docs/Web/API/Text) could create more complications. This is very much a case of finding the simplest most idiomatic solution - and that takes time and exploration (which is what's happening here - so you're doing exactly the right thing, and we're demonstrating why we have PRs for review!). I imagine (and I AM imagining - I've not checked), you could just do something like: But Take a look and let us know how you get on! 👍 |
|
@iliketocode2 if you're not doing anything at the time of our community technical call today, you could add discussion of this PR to our agenda? We could discuss the approach and any findings you have in the call. 👍 |
|
Thank you both for your feedback! Understood, it makes sense that this implementation could cause issues with applying CSS properties and also general HTML disruptions, thanks for pointing that out! @ntoll I tried the logic that you suggested (that was my first go-to based on the structure of the other cases in I can't make it to the community call today due to class :( but I'll play around some more today and tomorrow and also verify that I'm running the tests on an updated Thanks again for both of your feeback! |
|
@iliketocode2 I think all this MR requires is another # ...
if isinstance(item, Element):
self._dom_element.appendChild(item._dom_element)
elif isinstance(item, ElementCollection):
for element in item:
self._dom_element.appendChild(element._dom_element)
elif isinstance(item, (list, tuple)):
for child in item:
self.append(child)
# HERE 👇
elif isinstance(item, (str, int, float, bool)):
self._dom_element.append(item)
# ...after all, the DOM has a native there's not even any need to create a text node, the API does that for us ... deal? |
|
Wonderful, thank you @WebReflection! I agree, using the native DOM I realize now why all of my previous tests with similar code have been failing -- I missed this test at the very bottom of the test suite that was enforcing the old behavior. I'll make the appropriate changes and revise this PR. Thanks again! |
|
Bravo @iliketocode2 - you're now an open source contributor. We should be releasing the next version of PyScript today. 🎉 |
Description
Resolving Issue #2418
This PR updates the Element.append method to support strings. Previously, attempting to append a string would raise a TypeError (can be seen in this PyScript app created by jolaf). Now, strings are automatically wrapped in a span element, allowing them to be appended to dom elements.
Changes
Checklist
make buildworks locally.