def test_extract_1(self): html = \ "<h1>1</h1>" \ "<h1>2</h1>" \ "<h2>2.1</h2>" \ "<h2>2.2</h2>" \ "<h2>2.3</h2>" \ "<h2>2.4</h2>" \ "<h1>3</h1>" \ "<h1>4</h1>" presentation = {'h1_1':set([PC('myclass1')]), 'h1_2':set([]), 'h1_3':set([]), 'h1_4':set([]), 'h2_1':set([]), 'h2_2':set([]), 'h2_3':set([]), 'h2_4':set([]), 'newrow_h1_1':set([NEWROW]), 'newcol_h1_1':set([NEWCOL]), 'newrow_h2_1':set([NEWROW]), 'newcol_h2_1':set([NEWCOL]), 'newcol_h2_2':set([NEWCOL]), 'newrow_h2_3':set([NEWROW]), 'newcol_h2_3':set([NEWCOL]), 'newcol_h2_4':set([NEWCOL, PC('myclass2')]), 'newrow_h1_3':set([NEWROW]), 'newcol_h1_3':set([NEWCOL]), 'newcol_h1_4':set([NEWCOL]), } combined = format_html(html, presentation) pres2, html2 = extract_presentation(combined) self.assertEqual(presentation, pres2)
def test_extract_2(self): # Full featured, proper test html = """ <div class="row columns3"><div class="column firstcolumn"><div class="myclass"><h1>Hello Jane</h1><p>Some fancy content, entered using WYMeditor</p><p>Another paragraph</p><p>Hello</p></div></div><div class="column doublewidth"><div><h1>Another <heading></h1><h2>this is a test</h2><h2>hello1</h2><h3>hello2</h3><h3>hello3</h3><h3>hello4</h3></div></div><div class="column lastcolumn"><div><h1>hello5</h1><h2>hello6</h2><p>asdasd</p><p>asdxx</p></div></div></div> """ pres = {'newrow_h1_1':set([NEWROW]), 'newcol_h1_1':set([NEWCOL, PC('myclass')]), 'h1_1':set(), 'newcol_h1_2':set([NEWCOL, PC('doublewidth', column_equiv=2)]), 'h1_2':set(), 'p_1': set(), 'p_2': set(), 'p_3': set(), 'h2_1':set(), 'h2_2':set(), 'h3_1':set(), 'h3_2':set(), 'h3_3':set(), 'newcol_h1_3':set([NEWCOL]), 'h1_3':set(), 'h2_3':set(), 'p_4': set(), 'p_5': set(), } pres2, html2 = extract_presentation(html) self.assertEqual(pres, pres2)
def _handled(): pres, html = extract_presentation(data) # Rewrite pres so that we can serialise it to JSON pres2 = {} for k, v in pres.items(): pres2[k] = [PI_to_dict(p) for p in v] return dict(presentation=pres2, html=html)
def test_extract_presentation(self): html = "<h1 class=\"foo\">Heading 1</h1><h2 class=\"bar baz\">Heading 2</h2><p class=\"whatsit\">Some paragraph</p>" pres, html2 = extract_presentation(html) self.assertEqual({'h1_1':set([PC('foo')]), 'h2_1':set([PC('bar'), PC('baz')]), 'p_1':set([PC('whatsit')]), }, pres) self.assertEqual("<h1 id=\"h1_1\">Heading 1</h1><h2 id=\"h2_1\">Heading 2</h2><p id=\"p_1\">Some paragraph</p>", html2)
def test_div_extract_hack(self): """ Check that a div with class "div" is recognised and turned back into a 'p' when extracting """ html = '<div class="div">Test</div>' pres, html2 = extract_presentation(html) self.assertEqual({'p_1':set([PC('div')])}, pres) self.assertEqual('<p id="p_1">Test</p>', html2);
def test_extract_nested_layout(self): """ Tests that we can properly extract a layout created using inner rows/columns. """ pres = {'newrow_h1_1':set([NEWROW]), 'newcol_h1_1':set([NEWCOL]), 'newcol_h1_2':set([NEWCOL]), 'innerrow_h1_3':set([NEWINNERROW]), 'innercol_h1_3':set([NEWINNERCOL]), 'innercol_h1_4':set([NEWINNERCOL]), 'newcol_h1_5':set([NEWCOL]), 'newrow_h1_6':set([NEWROW]), 'newcol_h1_6':set([NEWCOL]), 'newcol_h1_7':set([NEWCOL]), 'h1_1':set([]), 'h1_2':set([]), 'h1_3':set([]), 'h1_4':set([]), 'h1_5':set([]), 'h1_6':set([]), 'h1_7':set([]), } html = ('<div class="row columns3">' '<div class="column firstcolumn">' '<div><h1>1</h1></div>' '</div>' '<div class="column">' '<div>' '<h1>2</h1>' '<div class="row columns2">' '<div class="column firstcolumn">' '<div><h1>3</h1></div>' '</div>' '<div class="column lastcolumn">' '<div><h1>4</h1></div>' '</div>' '</div>' '</div>' '</div>' '<div class="column lastcolumn">' '<div><h1>5</h1></div>' '</div>' '</div>' '<div class="row columns2">' '<div class="column firstcolumn">' '<div><h1>6</h1></div>' '</div>' '<div class="column lastcolumn">' '<div><h1>7</h1></div>' '</div>' '</div>' ) pres2, html2 = extract_presentation(html) self.assertEqual(pres, pres2)
def test_extract_3(self): # Tests some other boundary conditions e.g. 1 column row, html = """ <div class="row"><div><div><h1>1</h1><h2>1.1</h2><h2>1.2</h2></div></div></div> """ pres = {'h1_1': set(), 'newrow_h1_1':set([NEWROW]), 'newcol_h1_1':set([NEWCOL]), 'h2_1': set(), 'h2_2': set(), } pres2, html2 = extract_presentation(html) self.assertEqual(pres, pres2)
def test_extract_nested_layout_2(self): """ Issue 37 """ pres = {'newrow_p_1':set([NEWROW]), 'newcol_p_1':set([NEWCOL]), 'innerrow_p_1':set([NEWINNERROW]), 'innercol_p_1':set([NEWINNERCOL]), 'innercol_p_2':set([NEWINNERCOL]), 'newcol_p_3':set([NEWCOL]), 'innerrow_p_3':set([NEWINNERROW]), 'innercol_p_3':set([NEWINNERCOL]), 'innercol_p_4':set([NEWINNERCOL]), 'p_1':set([]), 'p_2':set([]), 'p_3':set([]), 'p_4':set([]), } html = ('<div class="row columns2">' '<div class="column firstcolumn">' '<div>' '<div class="row columns2">' '<div class="column firstcolumn">' '<div><p>col1</p></div>' '</div>' '<div class="column lastcolumn">' '<div><p>col2</p></div>' '</div>' '</div>' '</div>' '</div>' '<div class="column firstcolumn">' '<div>' '<div class="row columns2">' '<div class="column firstcolumn">' '<div><p>col3</p></div>' '</div>' '<div class="column lastcolumn">' '<div><p>col4</p></div>' '</div>' '</div>' '</div>' '</div>' '</div>' ) pres2, html2 = extract_presentation(html) self.assertEqual(pres, pres2)
def test_extract_no_inner_col_div_2(self): # Tests that we can extract column structure if we don't have # an inner column div. # This is important for the case where LayoutDetails.use_inner_column_div = False # Double col structure html = """ <div class="row"><div class="column firstcolumn"><h1>1</h1></div><div class="column lastcolumn"><h2>1.1</h2></div></div> """ pres = {'h1_1': set(), 'newrow_h1_1':set([NEWROW]), 'newcol_h1_1':set([NEWCOL]), 'h2_1': set(), 'newcol_h2_1':set([NEWCOL]) } pres2, html2 = extract_presentation(html) self.assertEqual(pres, pres2)
def test_round_trip(self): # Test the round trip of html -> extract_presentation -> format_html stored_html = "<h1>Hello</h1>" pres, simple_html = extract_presentation(stored_html) formatted = format_html(simple_html, pres) self.assertEqual(formatted, stored_html)
def test_format_pre(self): html = "<pre>This\r\nis\r\na\r\ntest</pre>" # check that format_html doesn't do anything nasty inside the pre html2 = format_html(html, {}) pres, html3 = extract_presentation(html2) self.assertEqual(html, html3)
def test_extract_empty(self): pres, out_html = extract_presentation('') self.assertEqual('', out_html)