Commits

Luke Plant committed ebd7615

Fixed tests for bad structure

Comments (0)

Files changed (3)

semanticeditor/media/semanticeditor/javascript/wymeditor/plugins/semantic/wymeditor.semantic.js

         }
     }
 
-    // If after this process we don't have newrow and newcol commands
-    // at the beginning of the heading list, we add them.
-    if (this.stored_headings[0].tag != 'newrow') {
-        // FIXME - what if first_sect_id is '' ?
-        this.insert_row_command(this.stored_headings, 0, first_sect_id);
-    }
-    if (this.stored_headings[1].tag != 'newcol') {
-        // FIXME - what if first_sect_id is '' ?
-        this.insert_column_command(this.stored_headings, 1, first_sect_id);
+    if (this.stored_headings.length > 0) {
+        // If after this process we don't have newrow and newcol commands
+        // at the beginning of the heading list, we add them.
+        if (this.stored_headings[0].tag != 'newrow') {
+            this.insert_row_command(this.stored_headings, 0, first_sect_id);
+        }
+        if (this.stored_headings[1].tag != 'newcol') {
+            this.insert_column_command(this.stored_headings, 1, first_sect_id);
+        }
     }
 
 };

semanticeditor/tests.py

         self.assertEqual(outh, format_html(html, {'h1_1':[PC('myclass')],
                                                   'h2_1':[PC('c1'), PC('c2')]}))
 
-    def test_sanity_check_sections(self):
-        html = "<h1>Hello</h1><blockquote><h2>Hi</h2></blockquote>"
-        self.assertRaises(BadStructure, format_html, html, {})
+    def test_sanity_check_columns(self):
+        """
+        Check that user is not allowed to add column structure
+        to blocks that aren't 'top level' in document structure
+        """
+        html = "<blockquote><p>How are you</p></blockquote>"
+        pres = {'newrow_p_1': [NEWROW]}
+        self.assertRaises(BadStructure, format_html, html, pres)
+
+        html2 = "<blockquote><p>How are you</p></blockquote>"
+        pres2 = {'newcol_p_1': [NEWROW]}
+        self.assertRaises(BadStructure, format_html, html2, pres2)
 
     def test_columns_1(self):
         html = "<h1>1</h1><p>para 1</p><h1>2</h1><h1>3</h1>"

semanticeditor/utils/presentation.py

     # raise BadStructure later, but divs have no semantics so can just
     # be removed.
     _strip_presentation(root)
-    _assert_sane_sections(root, structure)
 
     # Apply normal CSS classes.
     for si in structure:
 
     return out
 
-def _assert_sane_sections(root, structure):
-    # First, all h1, h2 etc tags will be children of the root.
-    # remove_tag should have ensured that, otherwise we will be unable
-    # to cut the HTML into sections.
-    for si in structure:
-        parent = get_parent(root, si.node)
-        if si.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. "
-                               "Please move the heading out of the '%(element)s'"
-                               " element that contains it." % dict(name=si.name, element=parent.tag.upper()))
-
 #### Layout related ####
 
 Layout = struct("Layout", (object,), dict(rows=list))
 _NEWROW_PREFIX = 'newrow_'
 _NEWCOL_PREFIX = 'newcol_'
 
-def _find_layout_commands(structure, styleinfo):
+def _find_layout_commands(root, structure, styleinfo):
     # Layout commands are not stored against normal sections,
     # but have their own entry in the section list, using an id
     # of 'newrow_' or 'newcol_' + id of block they preceed.
             real_sect_id = sect_id[len(_NEWROW_PREFIX):]
             sect = sect_dict.get(real_sect_id)
             if sect is not None:
+                parent = get_parent(root, sect.node)
+                if parent is not root:
+                    raise BadStructure("Section \"%(name)s\" is not at the top level of "
+                                       "the document, and therefore cannot have a column "
+                                       "structure applied to it.  Please move the 'New row' "
+                                       "command to a top level element." %
+                                       dict(name=sect.name))
+
                 row_info[real_sect_id] = presinfo
 
         if sect_id.startswith(_NEWCOL_PREFIX):
             real_sect_id = sect_id[len(_NEWCOL_PREFIX):]
             sect = sect_dict.get(real_sect_id)
             if sect is not None:
+                parent = get_parent(root, sect.node)
+                if parent is not root:
+                    raise BadStructure("Section \"%(name)s\" is not at the top level of "
+                                       "the document, and therefore cannot have a column "
+                                       "structure applied to it.  Please move the 'New column' "
+                                       "command to a top level element." %
+                                       dict(name=sect.name))
                 col_info[real_sect_id] = presinfo
 
     return row_info, col_info
 
 def _create_layout(root, styleinfo, structure):
     # Find the layout commands
-    row_info, col_info = _find_layout_commands(structure, styleinfo)
+    row_info, col_info = _find_layout_commands(root, structure, styleinfo)
 
     # Build a Layout structure