Edgewall Software

Opened 14 years ago

Closed 13 years ago

#399 closed defect (fixed)

"." expression in Stream.select/Path.select botches end tags

Reported by: Kyle Alan Hale <kylealanhale@…> Owned by: hodgestar
Priority: major Milestone: 0.6.1
Component: Expression evaluation Version: 0.6
Keywords: Cc:

Description

Try this out:

from genshi.template import MarkupTemplate
from genshi.core import Stream

def filter(stream):
    for event in stream:
        print event
        yield event

template = MarkupTemplate('''<rhyme xmlns:py="http://genshi.edgewall.org/">
    <py:match path="*[@type]">
        ${select('.') | filter}
    </py:match>
    
    <lines>
        <first type="one">fish</first>
        <second type="two">fish</second>
        <third type="red">fish</third>
        <fourth type="blue">fish</fourth>
    </lines>
</rhyme>
''')
stream = template.generate(filter=filter)
print stream.render('xml')

As a summary, this uses a py:match to find all elements with an attribute named type, selects the whole found node, and runs it through a filter to print each event in the selected stream to stdout. When I run it, I get the following output:

('START', (QName('first'), Attrs([(QName('type'), u'one')])), (None, 7, 8))
('TEXT', u'fish', (None, 7, 26))
('END', QName('first'), (None, 7, 30))
('START', (QName('second'), Attrs([(QName('type'), u'two')])), (None, 8, 8))
('TEXT', u'fish', (None, 8, 27))
('END', QName('first'), (None, 7, 30))
('START', (QName('third'), Attrs([(QName('type'), u'red')])), (None, 9, 8))
('TEXT', u'fish', (None, 9, 26))
('END', QName('first'), (None, 7, 30))
('START', (QName('fourth'), Attrs([(QName('type'), u'blue')])), (None, 10, 8))
('TEXT', u'fish', (None, 10, 28))
('END', QName('first'), (None, 7, 30))
<rhyme>
    <lines>
        <first type="one">fish</first>
        <second type="two">fish</first>
        <third type="red">fish</first>
        <fourth type="blue">fish</first>
    </lines>
</rhyme>

You'll see that the output keeps the ending tag of the first child element of <lines> for the following three lines. If you remove the child content of each element, the generated markup is correct, but the printed events still show incorrect END events.

This is not caused by the filtering; if you remove the filter and keep the child content, the same invalid markup is generated. The same behavior occurs no matter how the stream is rendered (html, xhtml, whatever).

Also, the same behavior occurs when the expression is changed to the equivalent "//*".

Attachments (1)

select-tail-fix-r1135.diff (941 bytes) - added by hodgestar 14 years ago.
Fix selected tail.

Download all attachments as: .zip

Change History (10)

Changed 14 years ago by hodgestar

Fix selected tail.

comment:1 Changed 14 years ago by anonymous

The attached patch fixes the problem for me without breaking the test suite. It looks like a simple mistake. The matching code has to work around Python generators not being able to return values when they finish iterating by passing in a list append function to catch the last tag of the matched XML. A single local list was being used to catch all closing tags so the list grew indefinitely.

comment:2 Changed 14 years ago by Kyle Alan Hale <kylealanhale@…>

That seems to solve the issue for both my test example and the original code in which I discovered the issue. Well done.

comment:3 Changed 13 years ago by anonymous

Fix committed to py3k branch in r1146.

comment:4 Changed 13 years ago by hodgestar

  • Owner changed from cmlenz to hodgestar
  • Status changed from new to assigned

comment:5 Changed 13 years ago by anonymous

  • Milestone changed from 0.6.1 to 0.8
  • Type changed from defect to enhancement

comment:6 Changed 13 years ago by kylealanhale

  • Milestone changed from 0.8 to 0.6.1
  • Type changed from enhancement to defect

This is not an enhancement; it is a defect. If it will not be incorporated into a 0.6.x release, fine, but 0.8 seems like a far shot for an easily-patched defect.

comment:7 Changed 13 years ago by hodgestar

Not sure why that got changed to 'enhancement'. Note that I haven't committed to trunk only because I don't have commit rights for it.

comment:8 Changed 13 years ago by hodgestar

Fix committed to trunk by asmodai in r1149. Awaiting merging to 0.6.x branch.

comment:9 Changed 13 years ago by hodgestar

  • Resolution set to fixed
  • Status changed from assigned to closed

Fix committed to 0.6.x branch in r1152.

Note: See TracTickets for help on using tickets.