Skip to content

Commit 5e44445

Browse files
committed
Make attrs optional
1 parent ed6f610 commit 5e44445

File tree

4 files changed

+43
-11
lines changed

4 files changed

+43
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
the `attrs` dict to define these instead.
77
- The `alt` attribute was moved from `Tag` to `A` and `Img`, for other nodes use the `attrs` dict.
88
- The `cls` attributes now only accept strings. These values are now internd to save memory
9+
- The `attrs` is now `None` by default and can be set to `None` to clear any old attrs set
910
- Tags now have a `find_by_id` method to retrieve a node by it's `id` field
1011
- Fix bug where an item removed attrs dict was not removed from the html
1112
- Any proxy `set_{attr}` methods now take a second argument that includes the oldvalue.

tests/test_html.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,28 @@ def test_html(app, tag, attr, query):
125125
("Source", "type", '"a"', "b", '//source[@type="b"]'),
126126
("P", "text", '"a"', "b", '//p[text()="b"]'),
127127
("P", "tail", '"a"', "b", '//body[text()="b"]'),
128-
("A", "attrs", {"data-x": "a"}, {"data-x": "b"}, '//a[@data-x="b"]'),
129128
(
129+
"A",
130+
"attrs",
131+
{"data-x": "a"},
132+
{"data-x": "b"},
133+
'//a[@data-x="b"]',
134+
), # attrs value changed
135+
(
136+
"A",
137+
"attrs",
138+
None,
139+
{"data-x": "b"},
140+
'//a[@data-x="b"]',
141+
), # attrs from none to new
142+
(
143+
"A",
144+
"attrs",
145+
{"data-x": "b"},
146+
None,
147+
"//a[not(@data-x)]",
148+
), # attrs from old to none
149+
( # Test attrs key change
130150
"A",
131151
"attrs",
132152
{"data-x": "a"},

web/components/html.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
from atom.api import (
1717
Event,
1818
Enum,
19-
Dict,
2019
Instance,
2120
Value,
2221
Str,
@@ -96,7 +95,7 @@ class Tag(ToolkitObject):
9695
tail = d_(Str()).tag(attr=False)
9796

9897
#: Custom attributes not explicitly defined
99-
attrs = d_(Dict()).tag(attr=False)
98+
attrs = d_(Typed(dict)).tag(attr=False)
10099

101100
#: Event triggered on click
102101
clicked = d_(Event())

web/impl/lxml_toolkit_object.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,9 @@ def init_widget(self):
9797
if "dragstart" in engine._handlers:
9898
set_attr("draggable", "true")
9999

100-
for k, v in d.attrs.items():
101-
set_attr(k, v)
100+
if attrs := d.attrs:
101+
for k, v in attrs.items():
102+
set_attr(k, v)
102103

103104
for m in get_fields(d.__class__):
104105
name = m.name
@@ -262,16 +263,27 @@ def set_tag(self, tag: str, oldvalue: str):
262263
assert w is not None
263264
w.tag = tag
264265

265-
def set_attrs(self, attrs: dict[str, str], oldattrs: dict[str, str]):
266+
def set_attrs(
267+
self, attrs: Optional[dict[str, str]], oldattrs: Optional[dict[str, str]]
268+
):
266269
"""Set any attributes not explicitly defined"""
267270
w = self.widget
268271
assert w is not None
269-
set_attr = w.set
270-
for k in oldattrs:
271-
if k not in attrs:
272+
if attrs and oldattrs:
273+
# Attrs changed
274+
for k in oldattrs:
275+
if k not in attrs:
276+
w.attrib.pop(k, None)
277+
for k, v in attrs.items():
278+
w.set(k, v)
279+
elif not attrs and oldattrs:
280+
# All attrs removed
281+
for k in oldattrs:
272282
w.attrib.pop(k, None)
273-
for k, v in attrs.items():
274-
set_attr(k, v)
283+
elif attrs and not oldattrs:
284+
# Only new attrs
285+
for k, v in attrs.items():
286+
w.set(k, v)
275287

276288
def set_cls(self, cls: str, oldvalue: str):
277289
w = self.widget

0 commit comments

Comments
 (0)