def test_comment(self): x = xml.Renderer() r = serialize(x.comment('hello'), 'text/xml', '<!DOCTYPE html>', False) self.assertEqual(r, ('text/xml', '<!--hello-->')) r = serialize(x.comment(u'été'), 'text/xml', '<!DOCTYPE html>', False) self.assertEqual(r, ('text/xml', '<!--\xc3\xa9t\xc3\xa9-->')) r = serialize(x.comment('hello'), 'text/xml', '<!DOCTYPE html>', True) self.assertEqual(r, ('text/xml', "<?xml version='1.0' encoding='utf-8'?>\n<!DOCTYPE html>\n<!--hello-->"))
def test_pi(self): x = xml.Renderer() r = serialize(x.processing_instruction('hello'), 'text/xml', '<!DOCTYPE html>', False) self.assertEqual(r, ('text/xml', '<?hello ?>')) r = serialize(x.processing_instruction(u'été'), 'text/xml', '<!DOCTYPE html>', False) self.assertEqual(r, ('text/xml', '<?\xc3\xa9t\xc3\xa9 ?>')) r = serialize(x.processing_instruction('hello'), 'text/xml', '<!DOCTYPE html>', True) self.assertEqual(r, ('text/xml', "<?xml version='1.0' encoding='utf-8'?>\n<!DOCTYPE html>\n<?hello ?>"))
def test_xhtml(self): h = xhtml.Renderer() r = serialize(h.p('hello'), 'application/xhtml+xml', '<!DOCTYPE html>', False) self.assertEqual(r, ('application/xhtml+xml', '<p>hello</p>')) r = serialize(h.p(u'été'), 'application/xhtml+xml', '<!DOCTYPE html>', False) self.assertEqual(r, ('application/xhtml+xml', '<p>\xc3\xa9t\xc3\xa9</p>')) r = serialize(h.p('hello'), 'application/xhtml+xml', '<!DOCTYPE html>', True) self.assertEqual(r, ('application/xhtml+xml', "<?xml version='1.0' encoding='utf-8'?>\n<!DOCTYPE html>\n<p>hello</p>"))
def test_xml(self): x = xml.Renderer() r = serialize(x.person('hello'), 'text/xml', '<!DOCTYPE html>', False) self.assertEqual(r, ('text/xml', '<person>hello</person>')) r = serialize(x.person(u'été'), 'text/xml', '<!DOCTYPE html>', False) self.assertEqual(r, ('text/xml', '<person>\xc3\xa9t\xc3\xa9</person>')) r = serialize(x.person('hello'), 'text/xml', '<!DOCTYPE html>', True) self.assertEqual(r, ('text/xml', "<?xml version='1.0' encoding='utf-8'?>\n<!DOCTYPE html>\n<person>hello</person>"))
def test_html(self): h = xhtml.Renderer() r = serialize(h.p('hello'), 'text/html', '<!DOCTYPE html>', False) self.assertEqual(r, ('text/html', '<p>hello</p>\n')) r = serialize(h.p(u'été'), 'text/html', '<!DOCTYPE html>', False) self.assertEqual(r, ('text/html', '<p>\xc3\xa9t\xc3\xa9</p>\n')) r = serialize(h.p('hello'), 'text/html', '<!DOCTYPE html>', True) self.assertEqual(r, ('text/html', '<!DOCTYPE html>\n<p>hello</p>\n'))
def test_etree(self): e = etree.Element('person') e.text = 'hello' r = serialize(e, 'text/xml', '<!DOCTYPE html>', False) self.assertEqual(r, ('text/xml', '<person>hello</person>')) e = etree.Element('person') e.text = u'été' r = serialize(e, 'text/xml', '<!DOCTYPE html>', False) self.assertEqual(r, ('text/xml', '<person>\xc3\xa9t\xc3\xa9</person>')) e = etree.Element('person') e.text = 'hello' r = serialize(e, 'text/xml', '<!DOCTYPE html>', True) self.assertEqual(r, ('text/xml', "<?xml version='1.0' encoding='utf-8'?>\n<!DOCTYPE html>\n<person>hello</person>"))
def render(self, h, comp, *args): """The view where an application is redirected when an error occurs""" (request, (exc_type, exc_value, tb)) = self.get_exception(self.app_in_error) if request is None: raise webob.exc.HTTPTemporaryRedirect(location='/' + self.app_in_error) # Get the javascript 'reload' view view = comp.render(h.AsyncRenderer(request=h.request, response=h.response, async_header=True), model='reload') js = serializer.serialize(view, '', '', False)[1] # .encode('utf-8') # Push it to the IDE comet.channels.send(CHANNEL_ID, js + ';') h.response.content_type = 'text/html' h.head.css('exception', ''' .exception { margin: 40px 40px 40px 0; background-color: #f3f2f1 } .source { font-size: 70%; padding-left: 20px } #content { margin-left: 0 } body { font-size: 17px } ''') h.head.css_url('/static/nagare/application.css') h.head.css_url('ide.css') h.head << h.head.title('Exception in application ' + self.app_in_error) with h.div(id='body'): h << h.a(h.img(src='/static/nagare/img/logo_small.png'), id='logo', href='http://www.nagare.org/', title='Nagare home') with h.div(id='content'): h << h.div('Exception', id='title') with h.div(id='main'): with h.div(class_='warning'): h << h.span('An exception occured in the application ', h.i('/' + self.app_in_error)) with h.div(class_='exception'): h << h.b(exc_type.__name__, ': ', str(exc_value)) while tb.tb_next: tb = tb.tb_next h << self.error with h.div: h << 'You can:' with h.ul: h << h.li(h.a('Switch to the Nagare IDE window', href='#', target='nagare_ide_window')) h << h.li(h.a('Open a new IDE window', href=self.url, target='nagare_ide_window')) if request: h << h.li(h.a('Retry the last action', href=request.url)) h << h.li(h.a('Open a new session in application /' + self.app_in_error, href='/' + self.app_in_error)) return h.root
def serialize(lazy, content_type, doctype, declaration): """Generic method to generate a text from a lazy string In: - ``lazy`` -- the lazy string - ``content_type`` -- the rendered content type - ``doctype`` -- the (optional) doctype - ``declaration`` -- is the XML declaration to be outputed ? Return: - a tuple (content_type, content) """ return serializer.serialize(lazy.value, content_type, doctype, declaration)
def serialize_body(view_to_js, content_type, doctype): """Wrap a view body into a javascript code In: - ``view_to_js`` -- the view - ``content_type`` -- the rendered content type - ``doctype`` -- the (optional) doctype - ``declaration`` -- is the XML declaration to be outputed? Return: - Javascript to evaluate on the client """ # Get the HTML or XHTML of the view body = serializer.serialize(view_to_js.output, content_type, doctype, False)[1] # Wrap it into a javascript code return "%s('%s', %s)" % (view_to_js.js, view_to_js.id.encode('utf-8'), py2js(body, view_to_js.renderer))
def test_unicode(self): r = serialize(u'hello world', 'text/msg', '<!DOCTYPE html>', False) self.assertEqual(r, ('text/msg', 'hello world')) r = serialize(u'été', 'text/msg', '<!DOCTYPE html>', False) self.assertEqual(r, ('text/msg', '\xc3\xa9t\xc3\xa9')) r = serialize(u'hello world', 'text/msg', '<!DOCTYPE html>', True) self.assertEqual(r, ('text/msg', 'hello world')) r = serialize(u'hello world', '', '<!DOCTYPE html>', False) self.assertEqual(r, ('text/plain', 'hello world')) r = serialize(u'été', '', '<!DOCTYPE html>', False) self.assertEqual(r, ('text/plain', '\xc3\xa9t\xc3\xa9')) r = serialize(u'hello world', '', '<!DOCTYPE html>', True) self.assertEqual(r, ('text/plain', 'hello world'))
def _phase2(self, output, content_type, doctype, is_xhr, response): """Final step of the phase 2 Phase 2 of the request processing: - The object graph is rendered - No modification of the objects is allowed In: - ``output`` -- the rendered content - ``content_type`` -- the rendered content type - ``doctype`` -- the (optional) doctype - ``is_xhr`` -- is the request a XHR request ? Out: - ``response`` -- the response object """ (response.content_type, response.body) = serializer.serialize(output, content_type, doctype, not is_xhr) response.charset = 'utf-8'
def test_list(self): h = xhtml.Renderer() x = xml.Renderer() e = etree.Element('person') e.text = 'hello' l = [h.p('hello'), x.person('hello'), x.comment('hello'), x.processing_instruction('hello'), e, 'hello', u'hello'] r = serialize(l, 'text/html', '<!DOCTYPE html>', False) self.assertEqual(r, ('text/html', '<p>hello</p>\n<person>hello</person>\n<!--hello-->\n<?hello >\n<person>hello</person>\nhellohello')) r = serialize(l, 'text/html', '<!DOCTYPE html>', True) self.assertEqual(r, ('text/html', '<!DOCTYPE html>\n<p>hello</p>\n<person>hello</person>\n<!--hello-->\n<?hello >\n<person>hello</person>\nhellohello')) r = serialize(l, 'application/xhtml+xml', '<!DOCTYPE html>', False) self.assertEqual(r, ('application/xhtml+xml', '<p>hello</p><person>hello</person><!--hello--><?hello ?><person>hello</person>hellohello')) r = serialize(l, 'application/xhtml+xml', '<!DOCTYPE html>', True) self.assertEqual(r, ('application/xhtml+xml', "<?xml version='1.0' encoding='utf-8'?>\n<!DOCTYPE html>\n<p>hello</p><person>hello</person><!--hello--><?hello ?><person>hello</person>hellohello")) r = serialize(l, 'text/msg', '<!DOCTYPE html>', False) self.assertEqual(r, ('text/msg', '<p>hello</p>\n<person>hello</person><!--hello--><?hello ?><person>hello</person>hellohello')) r = serialize(l, 'text/msg', '<!DOCTYPE html>', True) self.assertEqual(r, ('text/msg', '<!DOCTYPE html>\n<p>hello</p>\n<person>hello</person><!--hello--><?hello ?><person>hello</person>hellohello')) r = serialize(l, '', '<!DOCTYPE html>', False) self.assertEqual(r, ('', '<p>hello</p>\n<person>hello</person><!--hello--><?hello ?><person>hello</person>hellohello')) r = serialize(l, '', '<!DOCTYPE html>', True) self.assertEqual(r, ('', '<!DOCTYPE html>\n<p>hello</p>\n<person>hello</person><!--hello--><?hello ?><person>hello</person>hellohello')) l = l[1:] + [l[0]] r = serialize(l, 'text/html', '<!DOCTYPE html>', False) self.assertEqual(r, ('text/html', '<person>hello</person>\n<!--hello-->\n<?hello >\n<person>hello</person>\nhellohello<p>hello</p>\n')) r = serialize(l, 'text/html', '<!DOCTYPE html>', True) self.assertEqual(r, ('text/html', '<!DOCTYPE html>\n<person>hello</person>\n<!--hello-->\n<?hello >\n<person>hello</person>\nhellohello<p>hello</p>\n')) r = serialize(l, 'application/xhtml+xml', '<!DOCTYPE html>', False) self.assertEqual(r, ('application/xhtml+xml', '<person>hello</person><!--hello--><?hello ?><person>hello</person>hellohello<p>hello</p>')) r = serialize(l, 'application/xhtml+xml', '<!DOCTYPE html>', True) self.assertEqual(r, ('application/xhtml+xml', "<?xml version='1.0' encoding='utf-8'?>\n<!DOCTYPE html>\n<person>hello</person><!--hello--><?hello ?><person>hello</person>hellohello<p>hello</p>")) r = serialize(l, 'text/msg', '<!DOCTYPE html>', False) self.assertEqual(r, ('text/msg', "<person>hello</person><!--hello--><?hello ?><person>hello</person>hellohello<p>hello</p>\n")) r = serialize(l, 'text/msg', '<!DOCTYPE html>', True) self.assertEqual(r, ('text/msg', "<?xml version='1.0' encoding='utf-8'?>\n<!DOCTYPE html>\n<person>hello</person><!--hello--><?hello ?><person>hello</person>hellohello<p>hello</p>\n")) r = serialize(l, '', '<!DOCTYPE html>', False) self.assertEqual(r, ('', '<person>hello</person><!--hello--><?hello ?><person>hello</person>hellohello<p>hello</p>\n')) r = serialize(l, '', '<!DOCTYPE html>', True) self.assertEqual(r, ('', "<?xml version='1.0' encoding='utf-8'?>\n<!DOCTYPE html>\n<person>hello</person><!--hello--><?hello ?><person>hello</person>hellohello<p>hello</p>\n")) l = l[1:] + [l[0]] r = serialize(l, 'text/html', '<!DOCTYPE html>', False) self.assertEqual(r, ('text/html', '<!--hello-->\n<?hello >\n<person>hello</person>\nhellohello<p>hello</p>\n<person>hello</person>\n')) r = serialize(l, 'text/html', '<!DOCTYPE html>', True) self.assertEqual(r, ('text/html', '<!DOCTYPE html>\n<!--hello-->\n<?hello >\n<person>hello</person>\nhellohello<p>hello</p>\n<person>hello</person>\n')) r = serialize(l, 'application/xhtml+xml', '<!DOCTYPE html>', False) self.assertEqual(r, ('application/xhtml+xml', '<!--hello--><?hello ?><person>hello</person>hellohello<p>hello</p><person>hello</person>')) r = serialize(l, 'application/xhtml+xml', '<!DOCTYPE html>', True) self.assertEqual(r, ('application/xhtml+xml', "<?xml version='1.0' encoding='utf-8'?>\n<!DOCTYPE html>\n<!--hello--><?hello ?><person>hello</person>hellohello<p>hello</p><person>hello</person>")) r = serialize(l, 'text/msg', '<!DOCTYPE html>', False) self.assertEqual(r, ('text/msg', '<!--hello--><?hello ?><person>hello</person>hellohello<p>hello</p>\n<person>hello</person>')) r = serialize(l, 'text/msg', '<!DOCTYPE html>', True) self.assertEqual(r, ('text/msg', "<?xml version='1.0' encoding='utf-8'?>\n<!DOCTYPE html>\n<!--hello--><?hello ?><person>hello</person>hellohello<p>hello</p>\n<person>hello</person>")) r = serialize(l, '', '<!DOCTYPE html>', False) self.assertEqual(r, ('', '<!--hello--><?hello ?><person>hello</person>hellohello<p>hello</p>\n<person>hello</person>')) r = serialize(l, '', '<!DOCTYPE html>', True) self.assertEqual(r, ('', "<?xml version='1.0' encoding='utf-8'?>\n<!DOCTYPE html>\n<!--hello--><?hello ?><person>hello</person>hellohello<p>hello</p>\n<person>hello</person>")) l = l[1:] + [l[0]] r = serialize(l, 'text/html', '<!DOCTYPE html>', False) self.assertEqual(r, ('text/html', '<?hello >\n<person>hello</person>\nhellohello<p>hello</p>\n<person>hello</person>\n<!--hello-->\n')) r = serialize(l, 'text/html', '<!DOCTYPE html>', True) self.assertEqual(r, ('text/html', '<!DOCTYPE html>\n<?hello >\n<person>hello</person>\nhellohello<p>hello</p>\n<person>hello</person>\n<!--hello-->\n')) r = serialize(l, 'application/xhtml+xml', '<!DOCTYPE html>', False) self.assertEqual(r, ('application/xhtml+xml', '<?hello ?><person>hello</person>hellohello<p>hello</p><person>hello</person><!--hello-->')) r = serialize(l, 'application/xhtml+xml', '<!DOCTYPE html>', True) self.assertEqual(r, ('application/xhtml+xml', "<?xml version='1.0' encoding='utf-8'?>\n<!DOCTYPE html>\n<?hello ?><person>hello</person>hellohello<p>hello</p><person>hello</person><!--hello-->")) r = serialize(l, 'text/msg', '<!DOCTYPE html>', False) self.assertEqual(r, ('text/msg', '<?hello ?><person>hello</person>hellohello<p>hello</p>\n<person>hello</person><!--hello-->')) r = serialize(l, 'text/msg', '<!DOCTYPE html>', True) self.assertEqual(r, ('text/msg', "<?xml version='1.0' encoding='utf-8'?>\n<!DOCTYPE html>\n<?hello ?><person>hello</person>hellohello<p>hello</p>\n<person>hello</person><!--hello-->")) r = serialize(l, '', '<!DOCTYPE html>', False) self.assertEqual(r, ('', '<?hello ?><person>hello</person>hellohello<p>hello</p>\n<person>hello</person><!--hello-->')) r = serialize(l, '', '<!DOCTYPE html>', True) self.assertEqual(r, ('', "<?xml version='1.0' encoding='utf-8'?>\n<!DOCTYPE html>\n<?hello ?><person>hello</person>hellohello<p>hello</p>\n<person>hello</person><!--hello-->")) l = l[1:] + [l[0]] r = serialize(l, 'text/html', '<!DOCTYPE html>', False) self.assertEqual(r, ('text/html', '<person>hello</person>\nhellohello<p>hello</p>\n<person>hello</person>\n<!--hello-->\n<?hello >\n')) r = serialize(l, 'text/html', '<!DOCTYPE html>', True) self.assertEqual(r, ('text/html', '<!DOCTYPE html>\n<person>hello</person>\nhellohello<p>hello</p>\n<person>hello</person>\n<!--hello-->\n<?hello >\n')) r = serialize(l, 'application/xhtml+xml', '<!DOCTYPE html>', False) self.assertEqual(r, ('application/xhtml+xml', '<person>hello</person>hellohello<p>hello</p><person>hello</person><!--hello--><?hello ?>')) r = serialize(l, 'application/xhtml+xml', '<!DOCTYPE html>', True) self.assertEqual(r, ('application/xhtml+xml', "<?xml version='1.0' encoding='utf-8'?>\n<!DOCTYPE html>\n<person>hello</person>hellohello<p>hello</p><person>hello</person><!--hello--><?hello ?>")) r = serialize(l, 'text/msg', '<!DOCTYPE html>', False) self.assertEqual(r, ('text/msg', '<person>hello</person>hellohello<p>hello</p>\n<person>hello</person><!--hello--><?hello ?>')) r = serialize(l, 'text/msg', '<!DOCTYPE html>', True) self.assertEqual(r, ('text/msg', "<?xml version='1.0' encoding='utf-8'?>\n<!DOCTYPE html>\n<person>hello</person>hellohello<p>hello</p>\n<person>hello</person><!--hello--><?hello ?>")) r = serialize(l, '', '<!DOCTYPE html>', False) self.assertEqual(r, ('', '<person>hello</person>hellohello<p>hello</p>\n<person>hello</person><!--hello--><?hello ?>')) r = serialize(l, '', '<!DOCTYPE html>', True) self.assertEqual(r, ('', "<?xml version='1.0' encoding='utf-8'?>\n<!DOCTYPE html>\n<person>hello</person>hellohello<p>hello</p>\n<person>hello</person><!--hello--><?hello ?>")) l = l[1:] + [l[0]] r = serialize(l, 'text/html', '<!DOCTYPE html>', False) self.assertEqual(r, ('text/html', 'hellohello<p>hello</p>\n<person>hello</person>\n<!--hello-->\n<?hello >\n<person>hello</person>\n')) r = serialize(l, 'text/html', '<!DOCTYPE html>', True) self.assertEqual(r, ('text/html', 'hellohello<p>hello</p>\n<person>hello</person>\n<!--hello-->\n<?hello >\n<person>hello</person>\n')) r = serialize(l, 'application/xhtml+xml', '<!DOCTYPE html>', False) self.assertEqual(r, ('application/xhtml+xml', 'hellohello<p>hello</p><person>hello</person><!--hello--><?hello ?><person>hello</person>')) r = serialize(l, 'application/xhtml+xml', '<!DOCTYPE html>', True) self.assertEqual(r, ('application/xhtml+xml', 'hellohello<p>hello</p><person>hello</person><!--hello--><?hello ?><person>hello</person>')) r = serialize(l, 'text/msg', '<!DOCTYPE html>', False) self.assertEqual(r, ('text/msg', 'hellohello<p>hello</p>\n<person>hello</person><!--hello--><?hello ?><person>hello</person>')) r = serialize(l, 'text/msg', '<!DOCTYPE html>', True) self.assertEqual(r, ('text/msg', 'hellohello<p>hello</p>\n<person>hello</person><!--hello--><?hello ?><person>hello</person>')) r = serialize(l, '', '<!DOCTYPE html>', False) self.assertEqual(r, ('', 'hellohello<p>hello</p>\n<person>hello</person><!--hello--><?hello ?><person>hello</person>')) r = serialize(l, '', '<!DOCTYPE html>', True) self.assertEqual(r, ('', 'hellohello<p>hello</p>\n<person>hello</person><!--hello--><?hello ?><person>hello</person>')) l = l[1:] + [l[0]] r = serialize(l, 'text/html', '<!DOCTYPE html>', False) self.assertEqual(r, ('text/html', 'hello<p>hello</p>\n<person>hello</person>\n<!--hello-->\n<?hello >\n<person>hello</person>\nhello')) r = serialize(l, 'text/html', '<!DOCTYPE html>', True) self.assertEqual(r, ('text/html', 'hello<p>hello</p>\n<person>hello</person>\n<!--hello-->\n<?hello >\n<person>hello</person>\nhello')) r = serialize(l, 'application/xhtml+xml', '<!DOCTYPE html>', False) self.assertEqual(r, ('application/xhtml+xml', 'hello<p>hello</p><person>hello</person><!--hello--><?hello ?><person>hello</person>hello')) r = serialize(l, 'application/xhtml+xml', '<!DOCTYPE html>', True) self.assertEqual(r, ('application/xhtml+xml', 'hello<p>hello</p><person>hello</person><!--hello--><?hello ?><person>hello</person>hello')) r = serialize(l, 'text/msg', '<!DOCTYPE html>', False) self.assertEqual(r, ('text/msg', 'hello<p>hello</p>\n<person>hello</person><!--hello--><?hello ?><person>hello</person>hello')) r = serialize(l, 'text/msg', '<!DOCTYPE html>', True) self.assertEqual(r, ('text/msg', 'hello<p>hello</p>\n<person>hello</person><!--hello--><?hello ?><person>hello</person>hello')) r = serialize(l, '', '<!DOCTYPE html>', False) self.assertEqual(r, ('', 'hello<p>hello</p>\n<person>hello</person><!--hello--><?hello ?><person>hello</person>hello')) r = serialize(l, '', '<!DOCTYPE html>', True) self.assertEqual(r, ('', 'hello<p>hello</p>\n<person>hello</person><!--hello--><?hello ?><person>hello</person>hello'))