def test_head_render_render4(): """ XHTML namespace unit test - HeadRender - Render - call render two times with css_url method""" h = xhtml.HeadRenderer('/tmp/static_directory/') h << h.css_url('css') presentation.render(h, None, None, None) renderResult = presentation.render(h, None, None, None) assert not isinstance(renderResult, ListType) assert c14n(presentation.render(h, None, None, None)) == c14n('<head><link href="/tmp/static_directory/css" type="text/css" rel="stylesheet"/></head>')
def head_render_render_test4(): """ XHTML namespace unit test - HeadRender - Render - call render two times with css_url method""" h = xhtml.HeadRenderer('/tmp/static_directory/') h << h.css_url('css') presentation.render(h, None, None, None) renderResult = presentation.render(h, None, None, None) assert not isinstance(renderResult, ListType) assert presentation.render(h, None, None, None).write_htmlstring().replace('\n', '') == '<head><link href="/tmp/static_directory/css" type="text/css" rel="stylesheet"></head>'
def test_head_render_render4(): """ XHTML namespace unit test - HeadRender - Render - call render two times with css_url method""" h = xhtml.HeadRenderer('/tmp/static_directory/') h << h.css_url('css') presentation.render(h, None, None, None) renderResult = presentation.render(h, None, None, None) assert not isinstance(renderResult, ListType) assert c14n(presentation.render(h, None, None, None)) == c14n( '<head><link href="/tmp/static_directory/css" type="text/css" rel="stylesheet"/></head>' )
def test_head_render_render2(): """ XHTML namespace unit test - HeadRender - Render - render only css_url method """ h = xhtml.HeadRenderer('/tmp/static_directory/') h << h.css_url('css') assert c14n(presentation.render(h, None, None, None)) == c14n( '<head><link href="/tmp/static_directory/css" type="text/css" rel="stylesheet"/></head>' )
def test_head_render_render5(): """ XHTML namespace unit test - HeadRender - Render - render only css method """ h = xhtml.HeadRenderer('/tmp/static_directory/') h << h.css('css_test', 'test') assert c14n(presentation.render( h, None, None, None)) == c14n('<head><style type="text/css">test</style></head>')
def test_head_render_render6(): """ XHTML namespace unit test - HeadRender - Render - render only javascript_url method """ h = xhtml.HeadRenderer('/tmp/static_directory/') h << h.javascript_url('test.js') assert c14n(presentation.render(h, None, None, None)) == c14n( '<head><script src="/tmp/static_directory/test.js" type="text/javascript"></script></head>' )
def test_head_render_render7(): """ XHTML namespace unit test - HeadRender - Render - render only string js method """ h = xhtml.HeadRenderer('/tmp/static_directory/') h << h.javascript('test.js', 'function test() { return True }') assert c14n(presentation.render(h, None, None, None)) == c14n( '<head><script type="text/javascript">function test() { return True }</script></head>' )
def test_head_render_render10(): """ XHTML namespace unit test - HeadRender - Render - render with head & style """ h = xhtml.HeadRenderer('/tmp/static_directory/') with h.head({'id': 'id'}): h << h.style('test', {'id': 'id'}) assert c14n(presentation.render( h, None, None, None)) == c14n('<head id="id"><style id="id">test</style></head>')
def test_head_render_render8(): """ XHTML namespace unit test - HeadRender - Render - render only python2js method """ def js_method(arg1): return True h = xhtml.HeadRenderer('/tmp/static_directory/') with h.head({'lang': 'lang', 'dir': 'dir', 'id': 'id', 'profile': 'profile'}): h << h.javascript('test', js_method) assert c14n(presentation.render(h, None, None, None)) == c14n('<head lang="lang" profile="profile" id="id" dir="dir"><script src="/static/nagare/pyjslib.js" type="text/javascript"></script><script type="text/javascript">function nagare_namespaces_test_test_xhtmlns_js_method(arg1) { return true;}</script></head>')
def wrap(content_type, h, content): """Add the tags ``<html>``, ``<head>`` and ``<body>`` is they don't exist In: - ``content_type`` -- the content type to send to the browser - ``h`` -- the current renderer - ``content`` -- the rendered tree Return: - new tree with ``<html>``, ``<head>`` and ``<body>`` """ if 'html' not in content_type: return content if h.response.xml_output: h.namespaces = {None: 'http://www.w3.org/1999/xhtml'} if not isinstance(content, (list, tuple, types.GeneratorType)): content = [content] i, html = search_element('html', content) if html is None: html = content j, head = search_element('head', html) _, body = search_element('body', html[j:]) if body is None: # No ``<body>`` found, add it html[j:] = [h.body(html[j:])] if head is None: # No ``<head>`` found, add it head = h.head.head html.insert(0, head) if i == 0: # No ``<html>`` found, add it content = h.html(content) head1 = presentation.render(h.head, None, None, None) # The automatically generated ``<head>`` url = h.request.upath_info.strip('/') if url and not head1.xpath('./link[@rel="canonical"]'): head1.append( h.head.link(rel='canonical', href=h.request.uscript_name + '/' + url)) # Merge the attributes and child of the automatically generated ``<head>`` head.attrib.update(head1.attrib.items()) head.add_child(head1[:]) return content
def wrap(content_type, h, content): """Add the tags ``<html>``, ``<head>`` and ``<body>`` is they don't exist In: - ``content_type`` -- the content type to send to the browser - ``h`` -- the current renderer - ``content`` -- the rendered tree Return: - new tree with ``<html>``, ``<head>`` and ``<body>`` """ if 'html' not in content_type: return content if h.response.xml_output: h.namespaces = {None: 'http://www.w3.org/1999/xhtml'} if not isinstance(content, (list, tuple, types.GeneratorType)): content = [content] i, html = search_element('html', content) if html is None: html = content j, head = search_element('head', html) _, body = search_element('body', html[j:]) if body is None: # No ``<body>`` found, add it html[j:] = [h.body(html[j:])] if head is None: # No ``<head>`` found, add it head = h.head.head html.insert(0, head) if i == 0: # No ``<html>`` found, add it content = h.html(content) head1 = presentation.render(h.head, None, None, None) # The automatically generated ``<head>`` url = h.request.upath_info.strip('/') if url and not head1.xpath('./link[@rel="canonical"]'): head1.append(h.head.link(rel='canonical', href=h.request.uscript_name + '/' + url)) # Merge the attributes and child of the automatically generated ``<head>`` head.attrib.update(head1.attrib.items()) head.add_child(head1[:]) return content
def test_head_render_render8(): """ XHTML namespace unit test - HeadRender - Render - render only python2js method """ def js_method(arg1): return True h = xhtml.HeadRenderer('/tmp/static_directory/') with h.head({ 'lang': 'lang', 'dir': 'dir', 'id': 'id', 'profile': 'profile' }): h << h.javascript('test', js_method) assert c14n(presentation.render(h, None, None, None)) == c14n( '<head lang="lang" profile="profile" id="id" dir="dir"><script src="/static/nagare/pyjslib.js" type="text/javascript"></script><script type="text/javascript">function nagare_namespaces_test_test_xhtmlns_js_method(arg1) { return true;}</script></head>' )
def serialize(self, content_type, doctype, declaration): """Wrap a view into a javascript code In: - ``content_type`` -- the rendered content type - ``doctype`` -- the (optional) doctype - ``declaration`` -- is the XML declaration to be outputed? Return: - a tuple ('text/plain', Javascript to evaluate on the client) """ if self.output is None: return 'text/plain', '' # Get the javascript for the header head = presentation.render(self.renderer.head, self.renderer, None, None) # Wrap the body and the header into a javascript code return 'text/plain', serialize_body(self, content_type, doctype) + '; ' + head
def render(self, renderer, comp, model): """Rendering of a ``Component`` In: - ``renderer`` -- the renderer - ``comp`` -- the component - ``model`` -- the name of the view Return: - the view of the component object """ renderer = renderer.new() # Create a new renderer of the same class than the current renderer renderer.start_rendering(self, model) if model == 0: model = self.model output = presentation.render(self(), renderer, self, model) return renderer.end_rendering(output)
def serialize(self, content_type, doctype, declaration): """Wrap a view into a javascript code In: - ``content_type`` -- the rendered content type - ``doctype`` -- the (optional) doctype - ``declaration`` -- is the XML declaration to be outputed? Return: - a tuple ('text/plain', Javascript to evaluate on the client) """ if self.output is None: return ('text/plain', '') # Get the javascript for the header head = presentation.render(self.renderer.head, self.renderer, None, None) # Wrap the body and the header into a javascript code return ('text/plain', serialize_body(self, content_type, doctype) + '; ' + head)
def wrap(content_type, h, body): """Add the tags is they don't exist or merge them into the existing ones In: - ``content_type`` -- the content type to send to the browser - ``h`` -- the current renderer - ``body`` -- the rendered tree Return: - new tree with ``<html>``, ``<head>`` and ``<body>`` """ if 'html' in content_type: # Add the tags only for a (x)html content if not isinstance(body, xhtml_base._HTMLTag) or not body.tag.endswith('html'): # No ``<html>`` found, add it if h.response.xml_output: h.namespaces = { None : 'http://www.w3.org/1999/xhtml' } if not isinstance(body, xhtml_base._HTMLTag) or not body.tag.endswith('body'): # No ``<body>`` found, add it body = h.body(body) body = h.html(body) head1 = presentation.render(h.head, None, None, None) # The automatically generated ``<head>`` url = h.request.path_info.strip('/') if url: head1.append(h.head.link(rel='canonical', href=h.request.script_name+'/'+url)) head2 = body[0] if not head2.tag.endswith('head'): # No ``<head>`` found, add the automatically generated ``<head>`` body.insert(0, head1) else: # ``<head>`` found, merge the attributes and child of the automatically # generated ``<head>`` to it head2.attrib.update(head1.attrib.items()) head2.add_child(head1[:]) return body
def serialize_views(self, content_type, doctype, declaration): """Wrap a view into a javascript code In: - ``content_type`` -- the rendered content type - ``doctype`` -- the (optional) doctype - ``declaration`` -- is the XML declaration to be outputed? Return: - a tuple ('text/plain', Javascript to evaluate on the client) """ bodies = [serialize_body(view_to_js, content_type, doctype) for view_to_js in self if view_to_js.output is not None] if not bodies: return 'text/plain', '' # Get the javascript for the header head = presentation.render(self[0].renderer.head, self[0].renderer, None, None) return 'text/plain', '; '.join(bodies) + '; ' + head
def render(self, renderer, comp, model): """Rendering of a ``Component`` In: - ``renderer`` -- the renderer - ``comp`` -- the component - ``model`` -- the name of the view Return: - the view of the component object """ renderer = renderer.new( ) # Create a new renderer of the same class than the current renderer renderer.start_rendering(self, model) if model == 0: model = self.model output = presentation.render(self(), renderer, self, model) return renderer.end_rendering(output)
def serialize(self, content_type, doctype, declaration): """Wrap a view into a javascript code In: - ``content_type`` -- the rendered content type - ``doctype`` -- the (optional) doctype - ``declaration`` -- is the XML declaration to be outputed? Return: - a tuple ('text/plain', Javascript to evaluate on the client) """ bodies = [serialize_body(view_to_js, content_type, doctype) for view_to_js in self if view_to_js.output is not None] if not bodies: return ('text/plain', '') # Get the javascript for the header head = presentation.render(self[0].renderer.head, self[0].renderer, None, None) return ('text/plain', '; '.join(bodies) + '; ' + head)
def head_render_render_test7(): """ XHTML namespace unit test - HeadRender - Render - render only string js method """ h = xhtml.HeadRenderer('/tmp/static_directory/') h << h.javascript('test.js', 'function test() { return True }') assert presentation.render(h, None, None, None).write_htmlstring().replace('\n', '') == '<head><script type="text/javascript">function test() { return True }</script></head>'
h = Renderer() h.head << h.head.title('A test') h.head << h.head.javascript('__foo__', 'function() {}') h.head << h.head.meta(name='meta1', content='content1') 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_='foo'): with h.h1('moi'): h << h.i('foo') with h.div: h << 'yeah' for i in range(3): h << i with h.table(foo='foo'): for row in t: with h.tr: for column in row: with h.td: h << column print h.html(presentation.render(h.head, None, None, None), h.root).write_htmlstring(pretty_print=True)
def render_task(self, renderer, comp, *args): return presentation.render(self._go, renderer, comp, *args)
def head_render_render_test5(): """ XHTML namespace unit test - HeadRender - Render - render only css method """ h = xhtml.HeadRenderer('/tmp/static_directory/') h << h.css('css_test', 'test') assert presentation.render(h, None, None, None).write_htmlstring().replace('\n', '') == '<head><style type="text/css">test</style></head>'
def render(self, renderer, comp, *args): return presentation.render(self._go, renderer, comp, *args)
h << h.keygen h << h.output with h.ul: with h.li('Hello'): pass with h.li: h << 'world' h << h.li('yeah') with h.div(class_='foo'): with h.h1('moi'): h << h.i('foo') with h.div: h << 'yeah' for i in range(3): h << i with h.table(foo='foo'): for row in t: with h.tr: for column in row: with h.td: h << column with h.form: h << h.input(type='url') print h.html(presentation.render(h.head, None, None, None), h.root).write_htmlstring(pretty_print=True)
def test_head_render_render1(): """ XHTML namespace unit test - HeadRender - Render - render only style tag """ h = xhtml.HeadRenderer('/tmp/static_directory/') h << h.style() assert c14n(presentation.render(h, None, None, None)) == c14n('<head><style></style></head>')
def test_head_render_render7(): """ XHTML namespace unit test - HeadRender - Render - render only string js method """ h = xhtml.HeadRenderer('/tmp/static_directory/') h << h.javascript('test.js', 'function test() { return True }') assert c14n(presentation.render(h, None, None, None)) == c14n('<head><script type="text/javascript">function test() { return True }</script></head>')
def head_render_render_test2(): """ XHTML namespace unit test - HeadRender - Render - render only css_url method """ h = xhtml.HeadRenderer('/tmp/static_directory/') h << h.css_url('css') assert presentation.render(h, None, None, None).write_htmlstring().replace('\n', '') == '<head><link href="/tmp/static_directory/css" type="text/css" rel="stylesheet"></head>'
def test_head_render_render9(): """ XHTML namespace unit test - HeadRender - Render - render with head """ h = xhtml.HeadRenderer('/tmp/static_directory/') h << h.head({'id': 'id'}) assert c14n(presentation.render(h, None, None, None)) == c14n('<head id="id"></head>')
h.head << h.head.title('A test') h.head << h.head.javascript('__foo__', 'function() {}') h.head << h.head.meta(name='meta1', content='content1') 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_='foo'): with h.h1('moi'): h << h.i('foo') with h.div: h << 'yeah' for i in range(3): h << i with h.table(foo='foo'): for row in t: with h.tr: for column in row: with h.td: h << column print h.html(presentation.render(h.head, None, None, None), h.root).write_htmlstring(pretty_print=True)
def test_head_render_render2(): """ XHTML namespace unit test - HeadRender - Render - render only css_url method """ h = xhtml.HeadRenderer('/tmp/static_directory/') h << h.css_url('css') assert c14n(presentation.render(h, None, None, None)) == c14n('<head><link href="/tmp/static_directory/css" type="text/css" rel="stylesheet"/></head>')
def test_head_render_render1(): """ XHTML namespace unit test - HeadRender - Render - render only style tag """ h = xhtml.HeadRenderer('/tmp/static_directory/') h << h.style() assert c14n(presentation.render( h, None, None, None)) == c14n('<head><style></style></head>')
def render(self, renderer, model=0): """Rendering method of a component Forward the call to the generic method of the ``presentation`` service """ return presentation.render(self, renderer, self, model)
def head_render_render_test10(): """ XHTML namespace unit test - HeadRender - Render - render with head & style """ h = xhtml.HeadRenderer('/tmp/static_directory/') with h.head({'id':'id'}): h << h.style('test', {'id':'id'}) assert presentation.render(h, None, None, None).write_htmlstring().replace('\n', '') == '<head id="id"><style id="id">test</style></head>'
def head_render_render_test6(): """ XHTML namespace unit test - HeadRender - Render - render only javascript_url method """ h = xhtml.HeadRenderer('/tmp/static_directory/') h << h.javascript_url('test.js') assert presentation.render(h, None, None, None).write_htmlstring().replace('\n', '') == '<head><script src="/tmp/static_directory/test.js" type="text/javascript"></script></head>'
def test_head_render_render10(): """ XHTML namespace unit test - HeadRender - Render - render with head & style """ h = xhtml.HeadRenderer('/tmp/static_directory/') with h.head({'id': 'id'}): h << h.style('test', {'id': 'id'}) assert c14n(presentation.render(h, None, None, None)) == c14n('<head id="id"><style id="id">test</style></head>')
def test_head_render_render5(): """ XHTML namespace unit test - HeadRender - Render - render only css method """ h = xhtml.HeadRenderer('/tmp/static_directory/') h << h.css('css_test', 'test') assert c14n(presentation.render(h, None, None, None)) == c14n('<head><style type="text/css">test</style></head>')
def test_head_render_render6(): """ XHTML namespace unit test - HeadRender - Render - render only javascript_url method """ h = xhtml.HeadRenderer('/tmp/static_directory/') h << h.javascript_url('test.js') assert c14n(presentation.render(h, None, None, None)) == c14n('<head><script src="/tmp/static_directory/test.js" type="text/javascript"></script></head>')
def head_render_render_test1(): """ XHTML namespace unit test - HeadRender - Render - render only style tag """ h = xhtml.HeadRenderer('/tmp/static_directory/') h << h.style() assert presentation.render(h, None, None, None).write_htmlstring().replace('\n', '') == '<head><style></style></head>'