Ticket #3262: mimeview-html-splitlines-close-order-fix.diff
| File mimeview-html-splitlines-close-order-fix.diff, 2.0 KB (added by Tim Hatch <trac@…>, 3 years ago) |
|---|
-
trac/mimeview/api.py
560 560 def _html_splitlines(lines): 561 561 """Tracks open and close tags in lines of HTML text and yields lines that 562 562 have no tags spanning more than one line.""" 563 open_tag_re = re.compile(r'<(\w+)(\s.*?)?[^/]?>') 564 close_tag_re = re.compile(r'</(\w+)>') 563 tag_re = re.compile(r'<(/?)(\w+)(.*?)(/?)>') 565 564 open_tags = [] 566 565 for line in lines: 566 new_line = line 567 567 # Reopen tags still open from the previous line 568 568 for tag in open_tags: 569 line = tag.group(0) + line 570 open_tags = [] 569 new_line = tag.group(0) + new_line 571 570 571 open_tags.reverse() 572 572 # Find all tags opened on this line 573 for tag in open_tag_re.finditer(line): 574 open_tags.append(tag) 573 for tag in tag_re.finditer(line): 574 if tag.group(4) != "/": 575 # We just flat out ignore selfclosing tags, since they resolve 576 # themselves 577 if tag.group(1) == "/": 578 # Closing tag, try to pop 579 if len(open_tags) and open_tags[-1].group(2) == tag.group(2): 580 del open_tags[-1] 581 else: 582 # Opening tag, add to stack 583 open_tags.append(tag) 575 584 576 585 open_tags.reverse() 577 586 578 # Find all tags closed on this line579 for ctag in close_tag_re.finditer(line):580 for otag in open_tags:581 if otag.group(1) == ctag.group(1):582 open_tags.remove(otag)583 break584 585 587 # Close all tags still open at the end of line, they'll get reopened at 586 588 # the beginning of the next line 587 589 for tag in open_tags: 588 line += '</%s>' % tag.group(1)590 new_line += '</%s>' % tag.group(2) 589 591 590 yield line592 yield new_line 591 593 592 594 593 595 # -- Default annotators
