Index: trac/mimeview/api.py
===================================================================
--- trac/mimeview/api.py	(revision 3423)
+++ trac/mimeview/api.py	(working copy)
@@ -560,34 +560,36 @@
 def _html_splitlines(lines):
     """Tracks open and close tags in lines of HTML text and yields lines that
     have no tags spanning more than one line."""
-    open_tag_re = re.compile(r'<(\w+)(\s.*?)?[^/]?>')
-    close_tag_re = re.compile(r'</(\w+)>')
+    tag_re = re.compile(r'<(/?)(\w+)(.*?)(/?)>')
     open_tags = []
     for line in lines:
+	new_line = line
         # Reopen tags still open from the previous line
         for tag in open_tags:
-            line = tag.group(0) + line
-        open_tags = []
+            new_line = tag.group(0) + new_line
 
+        open_tags.reverse()
         # Find all tags opened on this line
-        for tag in open_tag_re.finditer(line):
-            open_tags.append(tag)
+        for tag in tag_re.finditer(line):
+            if tag.group(4) != "/":
+                # We just flat out ignore selfclosing tags, since they resolve
+                # themselves
+                if tag.group(1) == "/":
+                    # Closing tag, try to pop
+                    if len(open_tags) and open_tags[-1].group(2) == tag.group(2):
+                        del open_tags[-1]
+                else:
+                    # Opening tag, add to stack
+                    open_tags.append(tag)
 
         open_tags.reverse()
 
-        # Find all tags closed on this line
-        for ctag in close_tag_re.finditer(line):
-            for otag in open_tags:
-                if otag.group(1) == ctag.group(1):
-                    open_tags.remove(otag)
-                    break
-
         # Close all tags still open at the end of line, they'll get reopened at
         # the beginning of the next line
         for tag in open_tags:
-            line += '</%s>' % tag.group(1)
+            new_line += '</%s>' % tag.group(2)
 
-        yield line
+        yield new_line
 
 
 # -- Default annotators

