Skip to content

Xpath fixes#19

Merged
ThomasWaldmann merged 3 commits intomoinwiki:masterfrom
ThomasWaldmann:xpath-fixes2
Mar 6, 2026
Merged

Xpath fixes#19
ThomasWaldmann merged 3 commits intomoinwiki:masterfrom
ThomasWaldmann:xpath-fixes2

Conversation

@ThomasWaldmann
Copy link
Copy Markdown
Member

No description provided.

Previously, when iter() encountered a non-Element child (like a text string), it would yield it unconditionally, even if the caller was searching for a specific tag (e.g., .//c).

```
    def test_Element_findall_dotslashslash():
        c1 = Element('c')
        c2 = Element('c')
        text = "text"
        b1 = Element('b', children=(c1, text, c2))
        b2 = Element('b')
        a1 = Element('a', children=(b1, b2, ))

        result = list(a1.findall('.//c'))
>       assert len(result) == 2
E       AssertionError: assert 3 == 2
E        +  where 3 = len([<Element 'c' at 1056c4450>, 'text', <Element 'c' at 1056c4810>])

src/emeraldtree/tests/test_tree.py:208: AssertionError
```

The fix ensures that non-Element children are only yielded if no tag filter is specified (tag is None).
- Implemented 1-based indexing for predicates (e.g. tag[1]).
- Raise SyntaxError for invalid indices (< 1) to match xml.etree behavior.
- Enabled test_Element_findall_position and added test_Element_findall_position_invalid.
@ThomasWaldmann ThomasWaldmann mentioned this pull request Feb 28, 2026
@ThomasWaldmann ThomasWaldmann changed the title path fixes xpath fixes Feb 28, 2026
@ThomasWaldmann
Copy link
Copy Markdown
Member Author

.// and [position] are the same as in #18.

.. parent selector is a simpler implementation that shares the same design limitation as the elementtree code in stdlib, tests adapted accordingly.

Build a temporary parent_map dynamically in prepare_dot_dot by
iterating from context.root, matching stdlib ElementTree's approach.

New: skip non-iterable children (text nodes) when building the map.

Like stdlib, ../tag returns empty when called from a leaf element
that has no ancestor context. Paths like b/../c work correctly
when called from a higher element that contains the full subtree.
@ThomasWaldmann ThomasWaldmann changed the title xpath fixes Xpath fixes Feb 28, 2026
@ThomasWaldmann ThomasWaldmann merged commit afbc232 into moinwiki:master Mar 6, 2026
5 checks passed
@ThomasWaldmann ThomasWaldmann deleted the xpath-fixes2 branch March 6, 2026 11:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant