Commits

Luke Plant committed f5539e4

Fixed bug #18

  • Participants
  • Parent commits e7812ae

Comments (0)

Files changed (2)

File semanticeditor/tests.py

         self.assertEqual(structure[0].sect_id, "h1_10")
         self.assertEqual(structure[1].sect_id, "h1_1")
 
+    def test_regression_1(self):
+        # A bug in using existing section ids
+        html = '<h1 id="h1_1">heading 1</h1><h1>A new heading</h1><h1 id="h1_2">heading 2</h1><h1 id="h1_3">heading 3</h1>'
+        structure = get_structure(parse(html))
+        self.assertEqual(["h1_1", "h1_4", "h1_2", "h1_3"], [s.sect_id for s in structure])
+
 class TestPresentationInfo(TestCase):
     def test_equality(self):
         p1 = PresentationInfo(prestype="command", name="foo", verbose_name="blah")

File semanticeditor/utils/presentation.py

     cur_level = 1
     last_heading_num = 0
     first_heading_level = 1
+
+    # Pre-pass to get existing ids.
+    for n in root.getiterator():
+        if n.tag in blockdef:
+            sect_id = n.get('id')
+            if sect_id is not None:
+                if not sect_id.startswith(n.tag) or sect_id in sect_ids:
+                    # don't use invalid or duplicate ids.
+                    # remove
+                    del n.attrib['id']
+                else:
+                    # reserve
+                    sect_ids.add(sect_id)
+
     for n in root.getiterator():
         if n.tag in blockdef:
             text = flatten(n)
-            # Section id - use existing if it is their, but don't duplicate
-            sect_id = n.get('id', '')
-            if sect_id == '' or not sect_id.startswith(n.tag) or sect_id in sect_ids:
+            sect_id = n.get('id')
+            if sect_id is None:
                 sect_id = make_sect_id(n.tag, sect_ids)
             sect_ids.add(sect_id)
             if n.tag in headingdef: