Commits

Luke Plant committed 64cb48f

Added 'get_parent' utility

Comments (0)

Files changed (2)

semanticeditor/tests.py

 # -*- coding: utf-8 -*-
 
 from django.test import TestCase
-from semanticeditor.utils import extract_headings, InvalidHtml, IncorrectHeadings, format_html
+from semanticeditor.utils import extract_headings, InvalidHtml, IncorrectHeadings, format_html, parse, get_parent
 
 class TestExtract(TestCase):
     def test_extract_headings(self):
     def test_rejects_duplicate_headings(self):
         self.assertRaises(IncorrectHeadings, extract_headings, "<h1>Hello</h1><h2>Hello</h2>")
 
-class TestCombine(TestCase):
+class TestFormat(TestCase):
     def test_no_headings(self):
         html = "<p>Test</p>"
         self.assertEqual(html, format_html(html, {}))
         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>"
         self.assertEqual(outh, format_html(html, {}))
+
+class TestElementTreeUtils(TestCase):
+    def test_get_parent(self):
+        """
+        Tests that get_parent works
+        """
+        t = parse("<a><b1></b1><b2></b2></a>")
+        n = t.find(".//b2")
+        p = get_parent(t, n)
+        self.assertEqual(p, t.find(".//a"))

semanticeditor/utils.py

             text += e.tail
     return text
 
+def get_parent(topnode, elem):
+    for n in topnode.getiterator():
+        if elem in n.getchildren():
+            return n
+    return None
+
 def format_html(html, styleinfo):
     """
     Formats the XHTML given using a dictionary of style information.
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.