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
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
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
Polishing.
  • Loading branch information
mchilvers committed Jul 1, 2024
commit f1393b389ad218b51c9269ede0e5f52550ab825b
39 changes: 20 additions & 19 deletions pyscript.core/src/stdlib/pyscript/web/elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ def element_from_dom(dom_element):
attribute named `data-pyscript-type` that contains the name of the subclass
that created it. If the `data-pyscript-type` attribute *is* present we look up the
subclass by name and create an instance of that. Otherwise, we make a 'best-guess'
and look up the `Element` subclass by tag name (this is NOT fool-proof as many
subclasses might use a `<div>`, but close enough for jazz).
and look up the `Element` subclass by the DOM element's tag name (this is NOT
fool-proof as many subclasses might use a `<div>`, but close enough for jazz).
"""

# We use "getAttribute" here instead of `js_element.dataset.pyscriptType` as the
Expand All @@ -70,11 +70,12 @@ def element_from_dom(dom_element):
cls_name = dom_element.getAttribute("data-pyscript-type")
if cls_name:
cls = ELEMENT_CLASSES_BY_NAME.get(cls_name.lower())

else:
cls = ELEMENT_CLASSES_BY_TAG.get(dom_element.tagName.lower())

# For any unknown elements (custom tags etc.) we just create an instance of the
# 'Element' class.
# For any unknown elements (custom tags etc.) create an instance of the 'Element'
# class.
if not cls:
cls = Element

Expand Down Expand Up @@ -110,10 +111,10 @@ class Element:
virtualkeyboardpolicy = DOMProperty("virtualkeyboardpolicy")

def __init__(self, dom_element=None, style=None, classes=None, **kwargs):
"""Wrap or create a DOM element.
"""Create a new, or wrap an existing DOM element.

If `dom_element` is NOT None it means we are being called to *wrap* an existing
DOM element. Otherwise, we are being called to *create* a new element.
If `dom_element` is None we are being called to *create* a new element.
Otherwise, we are being called to *wrap* an existing DOM element.
"""

# Wrap or create a new DOM element.
Expand All @@ -126,7 +127,7 @@ def __init__(self, dom_element=None, style=None, classes=None, **kwargs):
self._parent = None
self._style = Style(self)

# Set any specified style properties.
# Set any specified styles.
if isinstance(style, dict):
self.style.set(**style)

Expand All @@ -139,7 +140,7 @@ def __init__(self, dom_element=None, style=None, classes=None, **kwargs):
if classes:
self.classes.add(classes)

# Set any DOM properties.
# Set any specified DOM properties.
self._init_properties(**kwargs)

def __eq__(self, obj):
Expand All @@ -164,6 +165,10 @@ def _init_properties(self, **kwargs):
print(f"Error setting {attr_name} to {kwargs[attr_name]}: {e}")
raise

@property
def children(self):
return [element_from_dom(el) for el in self._dom_element.children]

@property
def classes(self):
return self._classes
Expand All @@ -190,10 +195,6 @@ def content(self, value):

display(value, target=self.id)

@property
def children(self):
return [element_from_dom(el) for el in self._dom_element.children]

@property
def parent(self):
if self._parent:
Expand Down Expand Up @@ -239,11 +240,11 @@ def append(self, child):
f'Element "{child}" is a proxy object, but not a valid element or a NodeList.'
)

def clone(self, new_id=None):
def clone(self, clone_id=None):
"""Make a clone of the element (clones the underlying DOM object too)."""
el = element_from_dom(self._dom_element.cloneNode(True))
el.id = new_id
return el
clone = element_from_dom(self._dom_element.cloneNode(True))
clone.id = clone_id
return clone

def find(self, selector):
"""Return an ElementCollection representing all the child elements that
Expand All @@ -268,7 +269,7 @@ def show_me(self):


class Classes:
"""A 'more Pythonic' interface to an element's `classList`."""
"""A set-like interface to an element's `classList`."""

def __init__(self, element: Element):
self._element = element
Expand Down Expand Up @@ -424,7 +425,7 @@ def __getitem__(self, key):


class Style:
"""A dict-like interface to an elements css style."""
"""A dict-like interface to an element's css style."""

def __init__(self, element: Element) -> None:
self._element = element
Expand Down
0