Commits

Luke Plant committed cd962c5

Fixed misc bugs/tests

Comments (0)

Files changed (2)

semanticeditor/tests.py

 
     def test_no_headings(self):
         html = "<p>Test</p>"
-        self.assertEqual(html, format_html(html, {}))
+        outh = "<div><p>Test</p></div>"
+        self.assertEqual(outh, format_html(html, {}))
 
     def test_creates_section_divs(self):
         html = "<h1>Hello</h1><p>P 1</p><h2>Heading 2</h2> trailing text<h1>Another</h1><p>So</p> trail"
-        outh = "<div><h1>Hello</h1><p>P 1</p><div><h2>Heading 2</h2> trailing text</div></div><div><h1>Another</h1><p>So</p> trail</div>"
+        outh = "<div><h1>Hello</h1><div><p>P 1</p></div><div><h2>Heading 2</h2> trailing text</div></div><div><h1>Another</h1><div><p>So</p> trail</div></div>"
         self.assertEqual(outh, format_html(html, {}))
 
     def test_existing_divs(self):
         html = "<div><foo><bar><fribble><div><div>Some text <p>para</p> some more</div><div> more <span> of </span> this stuff </div></div></fribble></bar></foo></div>"
-        outh = "<foo><bar><fribble>Some text <p>para</p> some more more <span> of </span> this stuff </fribble></bar></foo>"
+        outh = "<foo><bar><fribble>Some text <div><p>para</p> some more more </div><span> of </span> this stuff </fribble></bar></foo>"
         self.assertEqual(outh, format_html(html, {}))
 
     def test_add_css_classes(self):
 
     def test_columns_1(self):
         html = "<h1>1</h1><p>para 1</p><h1>2</h1><h1>3</h1>"
-        outh = "<div class=\"row2col\"><div class=\"col\"><div><h1>1</h1><p>para 1</p></div></div><div class=\"col\"><div><h1>2</h1></div><div><h1>3</h1></div></div></div>"
+        outh = "<div class=\"row2col\"><div class=\"col\"><div><h1>1</h1><div><p>para 1</p></div></div></div><div class=\"col\"><div><h1>2</h1></div><div><h1>3</h1></div></div></div>"
         self.assertEqual(outh, format_html(html, {'1':[NEWROW],
                                                   '2':[NEWCOL]}))
 

semanticeditor/utils/presentation.py

 
     section_nodes = {}
     headers = [(level,name,tag,h) for (level,name,tag,h) in structure
-               if tag in headingdef]
+               if tag.lower() in headingdef]
+
     # Cut the HTML up into sections
-    for idx, (level, name, tag, h) in enumerate(structure):
+    # First deal with headers only.  This makes life simple,
+    # as headers always produce nested structures, and the
+    # indexes passed to wrap_elements_in_tag don't need
+    # adjusting for the changes we have made.
+    for idx, (level, name, tag, node) in enumerate(headers):
         # We can no longer assume that parent = root, because the divs
         # we insert will change that.  However, the divs we insert
         # will keep sub-section headings on the same level.
-        parent = get_parent(root, h)
+        parent = get_parent(root, node)
 
-        thisidx = get_index(parent, h)
+        thisidx = get_index(parent, node)
         first_elem = thisidx
 
-        # 'scope' of each section is from heading node to before the next
-        # heading with a level the same or higher
-        nextnodes = [(l,n) for (l,nname,tag,n) in headers[idx+1:] if l <= level]
-        # Bug in elementtree - throws AssertionError if we try
-        # to set a slice with [something:None]. So we use len()
-        # instead of None
+        # if a heading, then the 'scope' of each section is from
+        # heading node to before the next heading with a level the
+        # same or higher
+        nextnodes = [(l,n) for (l,nname,t,n) in headers[idx+1:] if l <= level]
         if not nextnodes:
             # scope extends to end
+            # Bug in elementtree - throws AssertionError if we try
+            # to set a slice with [something:None]. So we use len()
+            # instead of None
             last_elem = len(parent)
         else:
             # scope extends to node before n
                 last_elem = len(parent)
 
         newdiv = wrap_elements_in_tag(parent, first_elem, last_elem, "div")
+        section_nodes[name] = newdiv
 
+    # Now deal with everything else
+    for idx, (level, name, tag, node) in enumerate(structure):
+        if tag.lower() not in headingdef:
+            # Normal block level - these simply get a div that wraps
+            # them.
+            parent = get_parent(root, node)
+            thisidx = get_index(parent, node)
+            newdiv = wrap_elements_in_tag(parent, thisidx, thisidx + 1, "div")
+            section_nodes[name] = newdiv
+
+    # Apply normal CSS classes.
+    for name, newdiv in section_nodes.items():
         # Apply css styles
         classes = [s.name for s in styleinfo[name] if s.prestype == "class"]
         classes.sort()
         if classes:
             newdiv.set("class", " ".join(classes))
 
-        section_nodes[name] = newdiv
-
+    # Apply row/column commands
     _apply_commands(root, section_nodes, styleinfo, structure)
 
     return _html_extract(root)
     # to cut the HTML into sections.
     for level, name, tag, h in structure:
         parent = get_parent(root, h)
-        if tag in headingdef and parent is not root:
+        if tag.lower() in headingdef and parent is not root:
             raise BadStructure("Section heading \"%(name)s\" is not at the top level of "
                                "the document. This interferes with the ability to "
                                "format the sections and apply columns. "