Edgewall Software

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)

First attempt at fix, diff against trunk@3423

  • trac/mimeview/api.py

     
    560560def _html_splitlines(lines): 
    561561    """Tracks open and close tags in lines of HTML text and yields lines that 
    562562    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+)(.*?)(/?)>') 
    565564    open_tags = [] 
    566565    for line in lines: 
     566        new_line = line 
    567567        # Reopen tags still open from the previous line 
    568568        for tag in open_tags: 
    569             line = tag.group(0) + line 
    570         open_tags = [] 
     569            new_line = tag.group(0) + new_line 
    571570 
     571        open_tags.reverse() 
    572572        # 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) 
    575584 
    576585        open_tags.reverse() 
    577586 
    578         # Find all tags closed on this line 
    579         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                     break 
    584  
    585587        # Close all tags still open at the end of line, they'll get reopened at 
    586588        # the beginning of the next line 
    587589        for tag in open_tags: 
    588             line += '</%s>' % tag.group(1) 
     590            new_line += '</%s>' % tag.group(2) 
    589591 
    590         yield line 
     592        yield new_line 
    591593 
    592594 
    593595# -- Default annotators