Skip to content

Commit

Permalink
Merge pull request #43 from martin-honnen/master
Browse files Browse the repository at this point in the history
First stab at fixing and testing preceding-sibling::text() and following-sibling::text() issue raised as issue 42
  • Loading branch information
brunato committed May 15, 2022
2 parents 6d8a9e8 + 085450a commit 5a4bb2e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
9 changes: 9 additions & 0 deletions elementpath/xpath_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,9 @@ def iter_siblings(self, axis: Optional[str] = None) \
self.axis = axis or 'following-sibling'

if axis == 'preceding-sibling':
if parent.text is not None:
self.item = TextNode(parent.text, parent)
yield self.item
for child in parent: # pragma: no cover
if child is item:
break
Expand All @@ -564,6 +567,9 @@ def iter_siblings(self, axis: Optional[str] = None) \
yield self.item
else:
follows = False
if parent.text is not None:
self.item = TextNode(parent.text, parent)
yield self.item
for child in parent:
if follows:
self.item = child
Expand All @@ -573,6 +579,9 @@ def iter_siblings(self, axis: Optional[str] = None) \
yield self.item
elif child is item:
follows = True
if child.tail is not None:
self.item = TextNode(child.tail, child, True)
yield self.item

self.item, self.axis = status

Expand Down
11 changes: 11 additions & 0 deletions tests/test_selectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ def test_issue_001(self):
root = ElementTree.XML('<FullPath>High Temp</FullPath>')
self.assertListEqual(selector.select(root), [root])

def test_issue_042(self):
selector1 = Selector('text()')
selector2 = Selector('sup[last()]/preceding-sibling::text()')
root = ElementTree.XML('<root>a<sup>1</sup>b<sup>2</sup>c<sup>3</sup></root>')
self.assertListEqual(selector1.select(root), selector2.select(root))

selector2 = Selector('sup[1]/following-sibling::text()')
root = ElementTree.XML('<root><sup>1</sup>b<sup>2</sup>c<sup>3</sup>d</root>')
self.assertListEqual(selector1.select(root), selector2.select(root))



if __name__ == '__main__':
unittest.main()

0 comments on commit 5a4bb2e

Please sign in to comment.