Skip to content

Commit d7324e5

Browse files
committed
Fix bug with child node order when there are pattern nodes
1 parent 727a41e commit d7324e5

File tree

5 files changed

+37
-21
lines changed

5 files changed

+37
-21
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# 0.11.3
2+
3+
- Include new index in added and moved modification events
4+
- Fix bug where pattern nodes caused nodes to be inserted in the incorrect order
5+
16
# 0.11.2
27

38
- Fix error when `Raw` node source is empty string.

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
setup(
1717
name='enaml-web',
18-
version='0.11.2',
18+
version='0.11.3',
1919
author='CodeLV',
2020
author_email='[email protected]',
2121
url='https://github.com/codelv/enaml-web',

tests/test_html.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,12 @@ def on_modified(change):
678678
r = [li.text for li in view.proxy.widget.xpath("/html/body/ul/li")]
679679
assert r == ["1", "2", "4", "5"]
680680

681+
# Trigger condition
681682
view.render(menu=["3", "4"])
682683
r = [li.text for li in view.proxy.widget.xpath("/html/body/ul/li")]
683684
assert r == ["1", "2", "3", "4", "5"]
685+
686+
# Remove condition
687+
view.render(menu=["4", "3"])
688+
r = [li.text for li in view.proxy.widget.xpath("/html/body/ul/li")]
689+
assert r == ["1", "4", "3", "5"]

web/components/html.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ def child_added(self, child: Declarative):
212212
"type": "added",
213213
"name": "children",
214214
"value": child.render(),
215+
"index": self._child_index(child),
215216
"before": self._next_child_id(child),
216217
},
217218
)
@@ -231,6 +232,7 @@ def child_moved(self, child: Declarative):
231232
"type": "moved",
232233
"name": "children",
233234
"value": child.id,
235+
"index": self._child_index(child),
234236
"before": self._next_child_id(child),
235237
},
236238
)
@@ -266,6 +268,16 @@ def _notify_modified(self, root: Optional[Tag], change: ChangeDict):
266268
if root is not None:
267269
root.modified(change)
268270

271+
def _child_index(self, child: Tag) -> int:
272+
"""Find the index of the child ignoring any pattern nodes"""
273+
i = 0
274+
for c in self.children:
275+
if c is child:
276+
return i
277+
elif isinstance(c, Tag):
278+
i += 1
279+
raise ValueError("Child not found")
280+
269281
def _next_child_id(self, child: Tag) -> Optional[str]:
270282
"""Find the id of the node after this child."""
271283
# Indicate where it was added

web/impl/lxml_toolkit_object.py

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -154,41 +154,34 @@ def destroy(self):
154154
def child_added(self, child: WebComponent):
155155
"""Handle the child added event from the declaration.
156156
157-
This handler will insert the child toolkit widget in the correct.
158-
position. Subclasses which need more control should reimplement this
159-
method.
157+
This handler will insert the child into the tree at the
158+
appropriate index.
160159
161160
"""
161+
w = self.widget
162+
if w is None:
163+
return
164+
162165
# Use insert to put in the correct spot
163166
d = self.declaration
164167
assert d is not None
165-
i = d.children.index(child.declaration)
166-
widget = self.widget
167-
if widget is None:
168-
return False
169-
widget.insert(i, child.widget)
168+
i = d._child_index(child.declaration)
169+
w.insert(i, child.widget)
170170

171-
def child_moved(self, child: WebComponent):
171+
def child_moved(self, child: WebComponent) -> bool:
172172
"""Handle the child moved event from the declaration.
173173
174174
This handler will pop the child and insert it in the correct position
175175
if it isn't already there.
176176
177-
Subclasses which need more control should reimplement this method.
178-
179-
Returns
180-
-------
181-
was_moved: Bool
182-
Whether a move was performed or not
183-
184177
"""
185-
# Determine the new index
186-
d = self.declaration
187-
assert d is not None
188-
i = d.children.index(child.declaration)
189178
w = self.widget
190179
if w is None:
191180
return False
181+
# Determine the new index
182+
d = self.declaration
183+
assert d is not None
184+
i = d._child_index(child.declaration)
192185
j = w.index(child.widget)
193186
if j == i:
194187
return False # Already in the correct spot

0 commit comments

Comments
 (0)