def test_canonical(self): h = xhtml.Renderer(request=Request({'PATH_INFO': ''}), response=self.h.response) self.assertEqual(top.wrap('text/html', h, h.root).write_xmlstring(), '<html><head/><body></body></html>') h = xhtml.Renderer(request=Request({'PATH_INFO': ''}), response=self.h.response) h.head << h.head.link(rel='canonical', href='/bar') self.assertEqual(top.wrap('text/html', h, h.root).write_xmlstring(), '<html><head><link href="/bar" rel="canonical"/></head><body></body></html>') h = xhtml.Renderer(request=Request({'PATH_INFO': '/foo'}), response=self.h.response) self.assertEqual(top.wrap('text/html', h, h.root).write_xmlstring(), '<html><head><link href="/foo" rel="canonical"/></head><body></body></html>') h = xhtml.Renderer(request=Request({'PATH_INFO': '/foo'}), response=self.h.response) h.head << h.head.link(rel='canonical', href='/bar') self.assertEqual(top.wrap('text/html', h, h.root).write_xmlstring(), '<html><head><link href="/bar" rel="canonical"/></head><body></body></html>')
def test_html_render_add_tag1(): """ XHTML namespace unit test - HTMLRender - add tag - create simple html """ h = xhtml.Renderer() h << h.html(h.body(h.table(h.tr(h.td()), h.tr(h.td())))) assert c14n(h.root) == c14n( '<html><body><table><tr><td></td></tr><tr><td></td></tr></table></body></html>' )
def test_global1(): """ XHTML namespace unit test - create xhtml by procedural way """ t = ((1, 'a'), (2, 'b'), (3, 'c')) h = xhtml.Renderer() with h.html: with h.body(onload='javascript:alert()'): with h.ul: with h.li('Hello'): pass with h.li: h << 'world' h << h.li('yeah') with h.div(class_='toto'): with h.h1('moi'): h << h.i('toto') with h.div: h << 'yeah' for i in range(3): h << i with h.table(toto='toto'): for row in t: with h.tr: for column in row: with h.td: h << column assert c14n(h.root) == xml_test1_out
def test_htmltag_write_xmlstring3(): """ XHTML namespace unit test - HTMLRender - write_htmlstring - with pipeline == False """ h = xhtml.Renderer() h << h.table(h.tr(h.td().meld_id('false'), h.tr(h.td().meld_id('false')))) assert c14n(h.root.write_htmlstring(pipeline=False)) == c14n( '<table><tr><td xmlns:ns0="http://www.plope.com/software/meld3"></td><tr><td xmlns:ns0="http://www.plope.com/software/meld3"></td></tr></tr></table>' )
def test_html_render_form3(): """ XHTML namespace unit test - Form - create 2 forms not imbrecated""" h = xhtml.Renderer() h << h.html( h.body(h.form(h.input(type="string", name="input1", value="value")), h.form(h.input(type="string", name="input2", value="value")))) assert len(h.root.xpath('.//form')) == 2
def autocompletion(self, value, static_url): h = xhtml.Renderer(static_url=static_url) return [ (u.email, component.Component(UserManager.get_app_user( u.username, data=u)).render(h, "search").write_htmlstring()) for u in self.autocomplete_method(value) ]
def test_html_render_parse_html11(): """ XHTML namespace unit test - HTMLRender - parse_xml - htmlstring - Test child type """ try: x = xhtml.Renderer() x.parse_xmlstring(html_tree_1) except: # noqa: E722 assert True else: assert False
def test_html_render_init2(): """ XHTML namespace unit test - HTMLRender - init - add bad element """ h = xhtml.Renderer() try: h << h.bad_element except AttributeError: assert True else: assert False
def test_html_render_parse_html5(): """ XHTML namespace unit test - HTMLRender - parse_html - get html from an URL """ try: h = xhtml.Renderer() h.parse_html('http://www.google.fr/') except: # noqa: E261 assert False else: assert True
def test_html_render_submit1(): """ XHTML namespace unit test - Input submit - init """ h = xhtml.Renderer() with h.html: with h.body: with h.form: input = h.input(name="submit1", type="submit") h << input assert isinstance(input, xhtml.SubmitInput)
def test_html_render_password1(): """ XHTML namespace unit test - Input password - init """ h = xhtml.Renderer() with h.html: with h.body: with h.form: input = h.input(name="password1", type="password") h << input assert isinstance(input, xhtml.PasswordInput)
def test_html_render_img3(): """ XHTML namespace unit test - Tag img - init - absolute source """ h = xhtml.Renderer(static_url='/tmp/static/') with h.html: with h.body: h << h.img(src="/logo.gif") assert c14n( h.root) == c14n('<html><body><img src="/logo.gif"/></body></html>')
def test_html_render_parse_html1(): """ XHTML namespace unit test - HTMLRender - parse_html - good encoding """ try: h = xhtml.Renderer() h.parse_html(os.path.join(os.path.dirname(__file__), 'helloworld.html')) except UnicodeDecodeError: assert False else: assert True
def test_html_render_hidden1(): """ XHTML namespace unit test - Input hidden - init """ h = xhtml.Renderer() with h.html: with h.body: with h.form: input = h.input(name="hidden1", type="hidden") h << input assert isinstance(input, xhtml.HiddenInput)
def test_html_render_textarea1(): """ XHTML namespace unit test - TextArea - init """ h = xhtml.Renderer() with h.html: with h.body: with h.form: h << h.textarea("test", name="textearea1", type="textarea") textarea = h.root.xpath('.//textarea')[0] assert textarea.text == 'test'
def test_html_render_file1(): """ XHTML namespace unit test - Input file - init """ h = xhtml.Renderer() with h.html: with h.body: with h.form: input = h.input(name="file1", type="file") h << input assert isinstance(input, xhtml.FileInput)
def test_html_render_action4(): """ XHTML namespace unit test - action - put action method on tag a (replace existing href) """ h = xhtml.Renderer(static_url='/tmp/static/') h.start_rendering(component.Component(None), None) with h.html: with h.body: a = h.a(src="logo.gif").action(lambda x: None) h << a assert a.attrib['src'] != 'http://www.google.com'
def test_html_render_img1(): """ XHTML namespace unit test - Tag img - init - external source """ h = xhtml.Renderer() with h.html: with h.body: h << h.img(src="http://www.google.com/intl/en_ALL/images/logo.gif") assert c14n(h.root) == c14n( '<html><body><img src="http://www.google.com/intl/en_ALL/images/logo.gif"/></body></html>' )
def test_html_render_parse_html2(): """ XHTML namespace unit test - HTMLRender - parse_html - bad encoding """ try: h = xhtml.Renderer() h.parse_html(os.path.join(os.path.dirname(__file__), 'iso-8859.html'), encoding='utf-8') except UnicodeDecodeError: assert True else: assert False
def test_html_render_select1(): """ XHTML namespace unit test - Select - form with simple select input """ h = xhtml.Renderer() with h.html: with h.body: with h.form: with h.select: h << h.option(value="option1") h << h.option(value="option2", selected='selected') options = h.root.xpath('.//option') assert options[1].attrib['selected'] == 'selected'
def test_html_render_action3(): """ XHTML namespace unit test - action - put action method on tag imagge """ h = xhtml.Renderer(static_url='/tmp/static/') h.start_rendering(component.Component(None), None) with h.html: with h.body: a = h.img().action(lambda x: None) h << a assert a.attrib['src'] is not None
def test_html_render_checkbox1(): """ XHTML namespace unit test - Checkboxes - init """ h = xhtml.Renderer() with h.html: with h.body: with h.form: h << h.input(type="checkbox", value="option1") h << h.input( type="checkbox", value="option2", checked='checked') checkboxes = h.root.xpath('.//input[@type="checkbox"]') assert 'checked' not in checkboxes[0].attrib.keys() assert 'checked' in checkboxes[1].attrib.keys()
def test_html_render_a1(): """ XHTML namespace unit test - Tag a - init """ h = xhtml.Renderer() with h.html: with h.body: h << h.a("google", href="http://www.google.com") a = h.root.xpath('.//a')[0] assert isinstance(a, xhtml.A) assert c14n(h.root) == c14n( '<html><body><a href="http://www.google.com">google</a></body></html>')
def test_html_render_form4(): """ XHTML namespace unit test - Form - create simple form with iso-8859-15 enconding""" h = xhtml.Renderer() h << h.html( h.body( h.form(h.input(type="string", name="input1", value="value"), h.input(type="submit", name="submit"), {"accept-charset": "iso-8859-15"}))) form = h.root.xpath('.//form') attributes = form[0].attrib assert attributes['method'] == "post" assert attributes['accept-charset'] == "iso-8859-15" assert attributes['enctype'] == "multipart/form-data" assert attributes['action'] == "?"
def autocompletion(self, value, static_url): """Return users with email which match with value. Method called by autocomplete. This method returns a list of tuples. First element of tuple is the email's user. The second is a HTML fragment to display in the list of matches. In: - ``value`` -- first letters of user email Return: - list of tuple (email, HTML string) """ h = xhtml.Renderer(static_url=static_url) return [(u.email, component.Component(UserManager.get_app_user(u.username, data=u)).render(h, "search").write_htmlstring()) for u in self.autocomplete_method(value)]
def test_html_render_select2(): """ XHTML namespace unit test - Select - test selected method """ h = xhtml.Renderer() with h.html: with h.body: with h.form: with h.select: h << h.option(value="option1") h << h.option(value="option2") options = h.root.xpath('.//option') for option in options: option.selected('option2') assert options[1].attrib['selected'] == 'selected'
def test_html_render_checkbox3(): """ XHTML namespace unit test - Checkboxes - test selected method to unselected checkbox """ h = xhtml.Renderer() with h.html: with h.body: with h.form: checkbox = h.input(type="checkbox", value="option1", checked='checked') h << checkbox h << h.input(type="checkbox", value="option2") checkboxes = h.root.xpath('.//input[@type="checkbox"]') checkbox.selected(False) assert 'checked' not in checkboxes[0].attrib.keys()
def test_html_render_radiobutton1(): """ XHTML namespace unit test - Radio - init """ h = xhtml.Renderer() with h.html: with h.body: with h.form: h << h.input(name="radio", type="radio", value="option1") h << h.input(name="radio", type="radio", value="option2", checked='checked') h << h.input(name="radio", type="radio", value="option3") radios = h.root.xpath('.//input[@type="radio"]') assert 'checked' not in radios[0].attrib.keys() assert 'checked' in radios[1].attrib.keys() assert 'checked' not in radios[2].attrib.keys()
def test_html_render_select6(): """ XHTML namespace unit test - Select - test selected method with multiple select """ h = xhtml.Renderer() with h.html: with h.body: with h.form: h << h.select([ h.option(value='option1').selected(['option1', 'option3']), h.option(value='option2').selected(['option1', 'option3']), h.option(value='option3').selected(['option1', 'option3']) ], multiple=True) options = h.root.xpath('.//option') assert options[0].attrib['selected'] == 'selected' assert options[2].attrib['selected'] == 'selected'
def test_html_render_select4(): """ XHTML namespace unit test - Select - test selected method with multiple select and prefixed select """ h = xhtml.Renderer() with h.html: with h.body: with h.form: with h.select(multiple=True): h << h.option(value="option1") h << h.option(value="option2", selected="selected") h << h.option(value="option3") options = h.root.xpath('.//option') for option in options: option.selected(['option1', 'option3']) assert options[0].attrib['selected'] == 'selected' assert options[2].attrib['selected'] == 'selected'