def test_plugin_p_hack_empty_only(self): """ Check that if 'p' has any text in it, it is not converted """ html = '<p>X <img src="blah" id="plugin_obj_123" /></p>' outh = '<p>X <img src="blah" id="plugin_obj_123"/></p>' self.assertEqual(outh, format_html(html, {})) html2 = '<p> <img src="blah" id="plugin_obj_123" />X</p>' outh2 = '<p> <img src="blah" id="plugin_obj_123"/>X</p>' self.assertEqual(outh2, format_html(html2, {}))
def test_nested_layout_2(self): html = ("<h1>1</h1>" "<h1>2</h1>" "<h1>3</h1>" "<h1>4</h1>") # - row 1: # - 2 columns # - 2nd middle column has inner row structure with # - 1 row # - 2 columns pres = {'newrow_h1_1':[NEWROW], 'newcol_h1_2':[NEWCOL], 'innerrow_h1_3':[NEWINNERROW], 'innercol_h1_4':[NEWINNERCOL], } outh = ('<div class="row columns2">' '<div class="column firstcolumn">' '<div><h1>1</h1></div>' '</div>' '<div class="column lastcolumn">' '<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>' ) self.assertEqual(outh, format_html(html, pres))
def test_columns_with_double_width_2(self): html = "<h1>1</h1><p>para 1</p><h1>2</h1>" outh = "<div class=\"row columns3\"><div class=\"column firstcolumn\"><div><h1>1</h1><p>para 1</p></div></div><div class=\"column lastcolumn doublewidth\"><div><h1>2</h1></div></div></div>" pres = {'newrow_h1_1':[NEWROW], 'newcol_h1_1':[NEWCOL], 'newcol_h1_2':[NEWCOL, PC('doublewidth', column_equiv=2)]} self.assertEqual(outh, format_html(html, pres))
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_unknown_block_elements(self): """ Ensure we don't remove block elements that we don't know about """ html = '<foo>Test</foo>' outh = '<foo>Test</foo>' self.assertEqual(outh, format_html(html, {}))
def test_div_format_hack(self): """ Check that we can convert 'p' tags into 'div' using the 'div' class hack """ html = '<p>Test</p>' pres = {'p_1':[PC('div')]} outh = '<div class="div">Test</div>' self.assertEqual(outh, format_html(html, pres))
def test_columns_2(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>") pres = {'newrow_h2_1':[NEWROW], 'newcol_h2_2':[NEWCOL], 'newrow_h2_3':[NEWROW], 'newcol_h2_4':[NEWCOL], 'newrow_h1_3':[NEWROW], 'newcol_h1_4':[NEWCOL], } outh = ("<h1>1</h1>" "<h1>2</h1>" "<div class=\"row columns2\">" "<div class=\"column firstcolumn\">" "<div>" "<h2>2.1</h2>" "</div>" "</div>" "<div class=\"column lastcolumn\">" "<div>" "<h2>2.2</h2>" "</div>" "</div>" "</div>" "<div class=\"row columns2\">" "<div class=\"column firstcolumn\">" "<div>" "<h2>2.3</h2>" "</div>" "</div>" "<div class=\"column lastcolumn\">" "<div>" "<h2>2.4</h2>" "</div>" "</div>" "</div>" "<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>") self.assertEqual(outh, format_html(html, pres))
def test_plugin_p_hack(self): """ Check that a 'p' with only a plugin object is converted to a 'div' """ # NB: current implementation of plugin objects is that they are # represented by an image in the editor. Our code has to run before # these are converted, so we have to work with this implementation detail. html = '<p> <img src="blah" id="plugin_obj_123"/></p>' outh = '<div class="div"> <img src="blah" id="plugin_obj_123"/></div>' self.assertEqual(outh, format_html(html, {}))
def combine_presentation(request): """ Combines submitted 'html' and 'presentation' data, returning a dictionary containing { html: <combined html> } """ html = request.POST.get('html', '') presentation = request.POST.get('presentation', '{}') presentation = simplejson.loads(presentation) presentation = _convert_pres(presentation) return graceful_errors(AllUserErrors, lambda: dict(html=format_html(html, presentation, pretty_print=True)))
def combine_presentation(request): """ Combines submitted 'html' and 'presentation' data, returning a dictionary containing { html: <combined html> } """ html = request.POST.get('html', '') presentation = request.POST.get('presentation', '{}') presentation = simplejson.loads(presentation) presentation = _convert_pres(presentation) return graceful_errors( AllUserErrors, lambda: dict(html=format_html(html, presentation, pretty_print=True)))
def test_pretty_print(self): # Pretty print is used by the view function, so that the output HTML is # easier to read by web designers who may need to debug it html = "<ul><li>1</li></ul>" # The exact HTML currently looks like this: pretty_html = """ <ul> <li>1</li> </ul> """ out_html = format_html(html, {}, pretty_print=True) self.assertEqual(pretty_html, out_html)
def test_remove_command_divs(self): self.assertEqual('<p>Test</p>', format_html('<div class="newrow">* </div><p>Test</p>', {})) self.assertEqual('<p>Test</p>', format_html('<p class="newrow">* </p><p>Test</p>', {}))
def test_empty(self): self.assertEqual('', format_html('', {}));
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_no_headings(self): html = '<p>Test</p>' outh = '<p>Test</p>' self.assertEqual(outh, format_html(html, {}))
def test_columns_single_col(self): html = "<h1>1</h1><p>para 1</p><h2>2</h2>" outh = "<div class=\"row\"><div><div><h1>1</h1><p>para 1</p><h2>2</h2></div></div></div>" pres = {'newrow_h1_1':[NEWROW]} self.assertEqual(outh, format_html(html, pres))
def test_layout_with_styling(self): html = "<h1>1</h1><p>para 1</p><h1>2</h1><h1>3</h1>" outh = "<div class=\"row columns2 fancyrow\"><div class=\"column firstcolumn\"><div><h1>1</h1><p>para 1</p></div></div><div class=\"column lastcolumn\"><div class=\"fancycol\"><h1>2</h1><h1>3</h1></div></div></div>" pres = {'newrow_h1_1':[NEWROW, PC('fancyrow')], 'newcol_h1_2':[NEWCOL, PC('fancycol')]} self.assertEqual(outh, format_html(html, pres))
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 = '<p><foo><bar><fribble><p>Some text para some more more of this stuff </p></fribble></bar></foo></p>' self.assertEqual(outh, format_html(html, {}))
def test_columns_1(self): html = "<h1>1</h1><p>para 1</p><h1>2</h1><h1>3</h1>" outh = "<div class=\"row columns2\"><div class=\"column firstcolumn\"><div><h1>1</h1><p>para 1</p></div></div><div class=\"column lastcolumn\"><div><h1>2</h1><h1>3</h1></div></div></div>" pres = {'newrow_h1_1':[NEWROW], 'newcol_h1_2':[NEWCOL]} self.assertEqual(outh, format_html(html, pres))
def test_add_css_classes(self): html = "<h1>Hello <em>you</em></h1><h2>Hi</h2>" outh = '<h1 class=\"myclass\">Hello <em>you</em></h1><h2 class=\"c1 c2\">Hi</h2>' pres = {'h1_1':[PC('myclass')], 'h2_1':[PC('c1'), PC('c2')]} self.assertEqual(outh, format_html(html, pres))