8000 Cleanup pyscript web elements by fpliger · Pull Request #2094 · pyscript/pyscript · GitHub
[go: up one dir, main page]

Skip to content

Cleanup pyscript web elements #2094

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 93 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
93 commits
Select commit Hold shift + click to select a range
af7092b
change pydom example to use new pyscript.web namespace
fpliger Jun 4, 2024
12f1619
change tests to use new pyscript.web namespace
fpliger Jun 4, 2024
2351d45
create new pyscript.web package and move pydom to pyscript.web.dom
fpliger Jun 4, 2024
426da5a
add __init__ to pyscript.web and expose the dom instance instead of t…
fpliger Jun 4, 2024
74c8ede
move elements from pyweb.ui to pyscript.web and temp fix pydom import
fpliger Jun 4, 2024
a9254e6
moved of elements file completed
fpliger Jun 4, 2024
5cba331
moved media from pyweb to pyscript.web
fpliger Jun 4, 2024
02cc87b
RIP pyweb
fpliger Jun 4, 2024
0fb0985
move JSProperty from pyscript.web.dom to pyscript.web.elements
fpliger Jun 4, 2024
5fd5e9b
move element classes from pyscript.web.dom to pyscript.web.elements
fpliger Jun 4, 2024
903a63f
first round of fixes while running tests
fpliger Jun 4, 2024
009aeda
fix test typo
8000 fpliger Jun 4, 2024
676a500
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 4, 2024
e4e2378
restore right type type returned for Element.parent. ALL TESTS PASS L…
fpliger Jun 5, 2024
dd44759
lint
fpliger Jun 5, 2024
56991d9
Merge branch 'pydom_pyweb_to_stdlib' of github.com:pyscript/pyscript …
fpliger Jun 5, 2024
e399663
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 5, 2024
a944199
clean up dom.py from dead commented code and osbolete comments
fpliger Jun 5, 2024
490d23b
bugfix: dom shouldn't return None when it can't find any element for …
fpliger Jun 5, 2024
27f107a
additional cleanup in tests
fpliger Jun 5, 2024
d2aa10b
lint
fpliger Jun 5, 2024
8804401
Merge branch 'pydom_pyweb_to_stdlib' of github.com:pyscript/pyscript …
fpliger Jun 5, 2024
e914b1b
initial cleaning up of unused modules
fpliger Jun 5, 2024
6824617
change element.append to not consider types anymore and add tests for…
fpliger Jun 5, 2024
11f624f
add Element.append tests for append JS elements directly and appendin…
fpliger Jun 5, 2024
edf4cdc
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 5, 2024
4784405
Tag and create the correct subclass of Element.
mchilvers Jun 7, 2024
6b4ad33
Move: Element.snap -> video.snap
mchilvers Jun 7, 2024
a599e30
Move: Element.download and draw to canvas.download and draw.
mchilvers Jun 7, 2024
c6aa88e
Minor cleanups.
mchilvers Jun 7, 2024
965fe00
Commenting.
mchilvers Jun 7, 2024
c7d13b2
Allow css classes to be passed to Element constructor.
mchilvers Jun 7, 2024
12baf1e
Commenting.
mchilvers Jun 7, 2024
17e05e4
Typo fix.
mchilvers Jun 7, 2024
a7cbe52
Make html, id and text JSProperties.
mchilvers Jun 7, 2024
d72f20b
Commenting.
mchilvers Jun 7, 2024
9f4fd2e
Remove unnecessary selected attribute on BaseElement.
mchilvers Jun 7, 2024
ca66a1a
Extract: BaseElement.from_js -> element_from_js
mchilvers Jun 7, 2024
796b038
Pass *args and **kwargs to element_from_js and remove BaseElement.create
mchilvers Jun 7, 2024
458bea9
Move value attribute to specific Element subclasses.
mchilvers Jun 7, 2024
863f64b
fix: wrapping of existing js elements.
mchilvers Jun 7, 2024
568b306
Add body and head elements so parent and children work everywhere.
mchilvers Jun 7, 2024
bc3844b
Revert order of HasOptions mixin for the select element.
mchilvers Jun 7, 2024
49af9d5
Comment out tests that are no longer relevant (see comment).
mchilvers Jun 7, 2024
66b267e
Use correct super args in mixin.
mchilvers Jun 7, 2024
f12c051
Have to use element_from_js when returning options from OptionsProxy.
mchilvers Jun 7, 2024
899ccc2
rename: StyleProxy -> Style, OptionsProxy -> Options and added Classes.
mchilvers Jun 10, 2024
3ec40dc
Remove cached_property.
mchilvers Jun 10, 2024
5635922
Remove list-y methods from Classes collection.
mchilvers Jun 11, 2024
8096e72
Allow explicit children or *args for containers.
mchilvers Jun 25, 2024
73dc1fa
controversial: fix tests to use find rather than dom
mchilvers Jun 26, 2024
e6bd691
Add html element so (say) body.parent does what is expected.
mchilvers Jun 26, 2024
b28b98a
Collapse Element class hierarchy.
mchilvers Jun 27, 2024
7e218f7
rename: js_element -> dom_element
mchilvers Jun 27, 2024
ac17b48
rename: element_from_js -> element_from_dom
mchilvers Jun 27, 2024
fdb4ae1
replace: JS with DOM
mchilvers Jun 27, 2024
8483ca5
rename: _js -> _dom_element
mchilvers Jun 27, 2024
f7a67cf
fix dom tests.
mchilvers Jun 27, 2024
1e7f80d
Complete element list with void elements derived from Element.
mchilvers Jun 27, 2024
e2ea500
Added attributes to the newly added Element subclasses.
mchilvers Jun 27, 2024
5a0494d
remove dom module, replace with instance.
mchilvers Jun 27, 2024
df507e5
fix: typo in test for 'b' element.
mchilvers Jun 27, 2024
a86b07b
Merge branch 'main' into cleanup_pyscript_web_elements
mchilvers Jun 27, 2024
ed0a6de
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 27, 2024
bf7a74c
Remove dom and media modules.
mchilvers Jun 27, 2024
77b2e3a
Merge branch 'cleanup_pyscript_web_elements' of github.com:pyscript/p…
mchilvers Jun 27, 2024
2958ba5
fix up ts definitions.
mchilvers Jun 27, 2024
0776a16
Added missing import (used in content property).
mchilvers Jun 27, 2024
58d9a38
Added TODO :)
mchilvers Jun 27, 2024
141ee2e
wip: Add ClassesCollection
mchilvers Jun 30, 2024
b7a6f33
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 30, 2024
390a584
Attempt to ask black to leave class list alone.
mchilvers Jun 30, 2024
8e692fa
Merge branch 'cleanup_pyscript_web_elements' of github.com:pyscript/p…
mchilvers Jun 30, 2024
6f377cd
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 30, 2024
2107def
Add classes attribute to ElementCollection
mchilvers Jul 1, 2024
088f86e
Merge branch 'cleanup_pyscript_web_elements' of github.com:pyscript/p…
mchilvers Jul 1, 2024
308d070
wip: work on classes collection
mchilvers Jul 1, 2024
4c44c71
Extract code to get set of all class names in ClassesCollection.
mchilvers Jul 1, 2024
bee9824
Update elements.py
mchilvers Jul 1, 2024
f1393b3
Polishing.
mchilvers Jul 1, 2024
babb6a9
Make children return an ElementCollection
mchilvers Jul 1, 2024
fd0d94c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 1, 2024
f73bbc6
wip: Add the ability to set multiple properties.
mchilvers Jul 1, 2024
551723e
Merge branch 'cleanup_pyscript_web_elements' of github.com:pyscript/p…
mchilvers Jul 1, 2024
07b11cc
Add __getitem__ back to the dom object.
mchilvers Jul 2, 2024
81bf2fa
Put validation when setting DOM properties back in.
mchilvers Jul 2, 2024
0f324d6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 2, 2024
8c0b73a
All tests green.
mchilvers Jul 3, 2024
89e30ff
Merge branch 'cleanup_pyscript_web_elements' of github.com:pyscript/p…
mchilvers Jul 3, 2024
4644f20
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 3, 2024
86d1f42
Remove unnecessary comment.
mchilvers Jul 3, 2024
f3c3a5f
Merge branch 'cleanup_pyscript_web_elements' of github.com:pyscript/p…
mchilvers Jul 3, 2024
75fd534
Merge branch 'main' into cleanup_pyscript_web_elements
fpliger Jul 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
All tests green.
  • Loading branch information
mchilvers committed Jul 3, 2024
commit 8c0b73a4cf6fdc6317e186d9eceb655ed110a392
80 changes: 33 additions & 47 deletions pyscript.core/src/stdlib/pyscript/web/elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def warn(*args, **kwargs):
print("WARNING: ", *args, **kwargs)


from pyscript import display, document
from pyscript import document


#: A flag to show if MicroPython is the current Python interpreter.
Expand Down Expand Up @@ -98,7 +98,7 @@ class Element:
draggable = DOMProperty("draggable")
enterkeyhint = DOMProperty("enterkeyhint")
hidden = DOMProperty("hidden")
html = DOMProperty("innerHTML")
innerHTML = DOMProperty("innerHTML")
id = DOMProperty("id")
lang = DOMProperty("lang")
nonce = DOMProperty("nonce")
Expand Down Expand Up @@ -192,28 +192,6 @@ def children(self):
def classes(self):
return self._classes

@property
def content(self):
# TODO: This breaks with with standard template elements. Define how to best
# handle this specific use case. Just not support for now?
if self._dom_element.tagName == "TEMPLATE":
warnings.warn(
"Content attribute not supported for template elements.", stacklevel=2
)
return None
return self._dom_element.innerHTML

@content.setter
def content(self, value):
# TODO: (same comment as above)
if self._dom_element.tagName == "TEMPLATE":
warnings.warn(
"Content attribute not supported for template elements.", stacklevel=2
)
return

display(value, target=self.id)

@property
def parent(self):
if self._parent:
Expand Down Expand Up @@ -489,7 +467,7 @@ def __init__(
self.append(child)

else:
self.html += child
self.innerHTML += child


# IMPORTANT: For all HTML components defined below, we are not mapping all possible
Expand Down Expand Up @@ -1579,8 +1557,36 @@ def remove(self, key):
class ElementCollection:
def __init__(self, elements: [Element]) -> None:
self._elements = elements
self.classes = ClassesCollection(self)
self.style = StyleCollection(self)
self._classes = ClassesCollection(self)
self._style = StyleCollection(self)

@property
def children(self):
return self._elements

@property
def classes(self):
return self._classes

@property
def style(self):
return self._style

@property
def innerHTML(self):
return self._get_attribute("innerHTML")

@innerHTML.setter
def innerHTML(self, value):
self._set_attribute("innerHTML", value)

@property
def value(self):
return self._get_attribute("value")

@value.setter
def value(self, value):
self._set_attribute("value", value)

def __eq__(self, obj):
"""Check if the element is the same as the other element by comparing
Expand Down Expand Up @@ -1621,26 +1627,6 @@ def _set_attribute(self, attr, value):
for el in self._elements:
setattr(el, attr, value)

@property
def children(self):
return self._elements

@property
def html(self):
return self._get_attribute("html")

@html.setter
def html(self, value):
self._set_attribute("html", value)

@property
def value(self):
return self._get_attribute("value")

@value.setter
def value(self, value):
self._set_attribute("value", value)


# fmt: off
ELEMENT_CLASSES = [
Expand Down
56 changes: 28 additions & 28 deletions pyscript.core/test/pyscript_dom/tests/test_dom.py
ED7A
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_getitem_by_id():
div = result[0]
# EXPECT the element text value to match what we expect and what
# the JS document.querySelector API would return
assert document.querySelector(selector).innerHTML == div.html == txt
assert document.querySelector(selector).innerHTML == div.innerHTML == txt
# EXPECT the results to be of the right types
assert isinstance(div, el.Element)
assert isinstance(result, ElementCollection)
Expand All @@ -47,12 +47,12 @@ def test_read_n_write_collection_elements():
elements = dom.find(".multi-elems")

for element in elements:
assert element.html == f"Content {element.id.replace('#', '')}"
assert element.innerHTML == f"Content {element.id.replace('#', '')}"

new_content = "New Content"
elements.html = new_content
elements.innerHTML = new_content
for element in elements:
assert element.html == new_content
assert element.innerHTML == new_content


class TestElement:
Expand All @@ -69,7 +69,7 @@ def test_query(self):
# EXPECT the new element to be a el.Element
assert isinstance(div, el.Element)
# EXPECT the div attributes to be == to how they are configured in the page
assert div.html == "Child 1"
assert div.innerHTML == "Child 1"
assert div.id == "test_selector_w_children_child_1"

def test_equality(self):
Expand All @@ -85,10 +85,10 @@ def test_equality(self):
assert div is not div2

# EXPECT their value to always be equal
assert div.html == div2.html
div.html = "some value"
assert div.innerHTML == div2.innerHTML
div.innerHTML = "some value"

assert div.html == div2.html == "some value"
assert div.innerHTML == div2.innerHTML == "some value"

def test_append_element(self):
id_ = "element-append-tests"
Expand Down Expand Up @@ -158,11 +158,11 @@ def test_html_attribute(self):
div = dom.find("#element_attribute_tests")[0]

# WHEN we set the html attribute
div.html = "<b>New Content</b>"
div.innerHTML = "<b>New Content</b>"

# EXPECT the element html and underlying JS Element innerHTML property
# to match what we expect and what
assert div.html == div._dom_element.innerHTML == "<b>New Content</b>"
assert div.innerHTML == div._dom_element.innerHTML == "<b>New Content</b>"
assert div.text == div._dom_element.textContent == "New Content"

def test_text_attribute(self):
Expand All @@ -175,7 +175,7 @@ def test_text_attribute(self):
# EXPECT the element html and underlying JS Element innerHTML property
# to match what we expect and what
assert (
div.html == div._dom_element.innerHTML == "&lt;b&gt;New Content&lt;/b&gt;"
div.innerHTML == div._dom_element.innerHTML == "&lt;b&gt;New Content&lt;/b&gt;"
)
assert div.text == div._dom_element.textContent == "<b>New Content</b>"

Expand Down Expand Up @@ -251,7 +251,7 @@ def test_create_element_child(self):

# Creating an element from another element automatically creates that element
# as a child of the original element
new_el = el.p("a div", classes=["code-description"], html="Ciao PyScripters!")
new_el = el.p("a div", classes=[" F438 ;code-description"], innerHTML="Ciao PyScripters!")
parent_div.append(new_el)

assert isinstance(new_el, el.Element)
Expand Down Expand Up @@ -320,7 +320,7 @@ def test_select_options_iter(self):

for i, option in enumerate(select.options, 1):
assert option.value == f"{i}"
assert option.html == f"Option {i}"
assert option.innerHTML == f"Option {i}"

def test_select_options_len(self):
select = dom.find(f"#test_select_element_w_options")[0]
Expand Down Expand Up @@ -348,7 +348,7 @@ def test_select_element_add(self):
# we passed in
assert len(select.options) == 1
assert select.options[0].value == "1"
assert select.options[0].html == "Option 1"
assert select.options[0].innerHTML == "Option 1"

# WHEN we add another option (blank this time)
select.options.add("")
Expand All @@ -358,7 +358,7 @@ def test_select_element_add(self):

# EXPECT the last option to have an empty value and html
assert select.options[1].value == ""
assert select.options[1].html == ""
assert select.options[1].innerHTML == ""

# WHEN we add another option (this time adding it in between the other 2
# options by using an integer index)
Expand All @@ -369,11 +369,11 @@ def test_select_element_add(self):

# EXPECT the middle option to have the value and html we passed in
assert select.options[0].value == "1"
assert select.options[0].html == "Option 1"
assert select.options[0].innerHTML == "Option 1"
assert select.options[1].value == "2"
assert select.options[1].html == "Option 2"
assert select.options[1].innerHTML == "Option 2"
assert select.options[2].value == ""
assert select.options[2].html == ""
assert select.options[2].innerHTML == ""

# WHEN we add another option (this time adding it in between the other 2
# options but using the option itself)
Expand All @@ -386,23 +386,23 @@ def test_select_element_add(self):

# EXPECT the middle option to have the value and html we passed in
assert select.options[0].value == "1"
assert select.options[0].html == "Option 1"
assert select.options[0].innerHTML == "Option 1"
assert (
select.options[0].selected
== select.options[0]._dom_element.selected
== False
)
assert select.options[1].value == "2"
assert select.options[1].html == "Option 2"
assert select.options[1].innerHTML == "Option 2"
assert select.options[2].value == "3"
assert select.options[2].html == "Option 3"
assert select.options[2].innerHTML == "Option 3"
assert (
select.options[2].selected
== select.options[2]._dom_element.selected
== True
)
assert select.options[3].value == ""
assert select.options[3].html == ""
assert select.options[3].innerHTML == ""

# WHEN we add another option (this time adding it in between the other 2
# options but using the JS element of the option itself)
Expand All @@ -415,15 +415,15 @@ def test_select_element_add(self):

# EXPECT the middle option to have the value and html we passed in
assert select.options[0].value == "1"
assert select.options[0].html == "Option 1"
assert select.options[0].innerHTML == "Option 1"
assert select.options[1].value == "2"
assert select.options[1].html == "Option 2"
assert select.options[1].innerHTML == "Option 2"
assert select.options[2].value == "2a"
assert select.options[2].html == "Option 2a"
assert select.options[2].innerHTML == "Option 2a"
assert select.options[3].value == "3"
assert select.options[3].html == "Option 3"
assert select.options[3].innerHTML == "Option 3"
assert select.options[4].value == ""
assert select.options[4].html == ""
assert select.options[4].innerHTML == ""

def test_select_options_remove(self):
# GIVEN the existing select element with 3 options
Expand Down Expand Up @@ -456,5 +456,5 @@ def test_select_get_selected_option(self):

# EXPECT the selected option to be correct
assert selected_option.value == "2"
assert selected_option.html == "Option 2"
assert selected_option.innerHTML == "Option 2"
assert selected_option.selected == selected_option._dom_element.selected == True
17 changes: 1 addition & 16 deletions pyscript.core/types/exceptions.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,4 @@ export class InstallError extends UserError {
/**
* Keys of the ErrorCode object
*/
export type ErrorCodes = keyof {
GENERIC: string;
CONFLICTING_CODE: string;
BAD_CONFIG: string;
MICROPIP_INSTALL_ERROR: string;
BAD_PLUGIN_FILE_EXTENSION: string;
NO_DEFAULT_EXPORT: string;
TOP_LEVEL_AWAIT: string;
FETCH_ERROR: string;
FETCH_NAME_ERROR: string;
FETCH_UNAUTHORIZED_ERROR: string;
FETCH_FORBIDDEN_ERROR: string;
FETCH_NOT_FOUND_ERROR: string;
FETCH_SERVER_ERROR: string;
FETCH_UNAVAILABLE_ERROR: string;
};
export type ErrorCodes = "GENERIC" | "CONFLICTING_CODE" | "BAD_CONFIG" | "MICROPIP_INSTALL_ERROR" | "BAD_PLUGIN_FILE_EXTENSION" | "NO_DEFAULT_EXPORT" | "TOP_LEVEL_AWAIT" | "FETCH_ERROR" | "FETCH_NAME_ERROR" | "FETCH_UNAUTHORIZED_ERROR" | "FETCH_FORBIDDEN_ERROR" | "FETCH_NOT_FOUND_ERROR" | "FETCH_SERVER_ERROR" | "FETCH_UNAVAILABLE_ERROR";
0