def test_comment_whitespace(): """Comments should not add an additional newline (ticket #107).""" expected = '<?xml version="1.0" encoding="utf-8"?>\n<html>\n' \ '<!-- a comment -->\n<element />\n</html>' assert kid.Template(expected).serialize(output='xml') == expected expected = serialize_doctype(doctypes['html']) + '\n<HTML>\n' \ '<!-- a comment -->\n<ELEMENT>\n</ELEMENT>\n</HTML>' assert kid.Template(expected).serialize(output='HTML') == expected expected = serialize_doctype(doctypes['xhtml']) + '\n<html>\n' \ '<!-- a comment -->\n<element>\n</element>\n</html>' assert kid.Template(expected).serialize(output='xhtml') == expected
def test_HTML_output_method(): t = kid.Template('<html><p>Test</p><br /></html>') for output in 'HTML', 'HTML-strict': rslt = t.serialize(output=output) expected = serialize_doctype(doctypes[output.lower()]) + \ '\n<HTML><P>Test</P><BR></HTML>' assert rslt == expected
def test_comment_interpolation(): """Comments starting with '[' or '<![' should be interpolated.""" for b in ('', ' '): for c in '! [ < <[ <! ![ <![ ${'.split(): for d in '/ // ! ]'.split(): before_comment = '<!--%s%s $before %s-->' % (b, c, d) if c.startswith('!'): after_comment = '' elif c == '[' or c == '<![' or d == '//': after_comment = '<!--%s%s after %s-->' % (b, c, d) else: after_comment = before_comment before = '<html>%s</html>' % before_comment t = kid.Template(before, before='after') for output in ('HTML', 'xhtml', 'xml'): if output == 'HTML': after = '<HTML>%s</HTML>' % after_comment elif output == 'xhtml' or after_comment: after = '<html>%s</html>' % after_comment else: after = '<html />' if output == 'xml': after = '<?xml version="1.0" encoding="utf-8"?>\n' + after else: doctype = serialize_doctype(doctypes[output.lower()]) after = doctype + '\n' + after assert t.serialize(output=output) == after
def test_nested_comments(): """Nested comments.""" t = kid.Template('<!--1--><html><!--2--><div><!--3-->' '<p><!--4--></p><!--5--></div><!--7--></html><!--8-->') assert (t.serialize(output='HTML') == serialize_doctype(doctypes['html']) + '\n<!--1--><HTML><!--2--><DIV><!--3-->' '<P><!--4--></P><!--5--></DIV><!--7--></HTML><!--8-->')
def test_html_output_method(): t = kid.Template('<html><p>Test</p><br /></html>') for output in 'html', 'html-strict': rslt = t.serialize(output=output) expected = serialize_doctype(doctypes[output]) + \ '\n<html><p>Test</p><br></html>' assert rslt == expected
def test_comment_removal(): """Comments that start with '!' should not be output.""" t = kid.Template('<html><!-- !comment --></html>') assert t.serialize(output='HTML') == \ serialize_doctype(doctypes['html']) + '\n<HTML></HTML>' assert t.serialize(output='xhtml') == \ serialize_doctype(doctypes['xhtml']) + '\n<html></html>' assert t.serialize(output='xml') == \ '<?xml version="1.0" encoding="utf-8"?>\n<html />' t = kid.Template('<html><!--!comment--></html>') assert t.serialize(output='HTML') == \ serialize_doctype(doctypes['html']) + '\n<HTML></HTML>' assert t.serialize(output='xhtml') == \ serialize_doctype(doctypes['xhtml']) + '\n<html></html>' assert t.serialize(output='xml') == \ '<?xml version="1.0" encoding="utf-8"?>\n<html />'
def test_xhtml_output_method(): t = kid.Template('<html xmlns="http://www.w3.org/1999/xhtml">' '<p>test</p><img src="some.gif" /><br /></html>') for output in 'xhtml', 'xhtml-strict': rslt = t.serialize(output=output) expected = serialize_doctype(doctypes[output]) \ + '\n<html xmlns="http://www.w3.org/1999/xhtml">' \ + '<p>test</p>' \ + '<img src="some.gif" /><br /></html>' assert rslt == expected
def test_empty_lines(): """Handling of empty lines in templates. Empty lines between elements should be removed. We assume that balanced_blocks is enabled for both HTML and XHTML templates. Inline elements in template should remain so. Other elements should be indented. """ t = kid.Template(""" <html> <script>some lines and more lines</script><body> <a href="/"><img src="pic.jpg"/></a> </body> </html>""") expected = serialize_doctype( doctypes['html']) + """\n<HTML> <SCRIPT>some lines and more lines</SCRIPT><BODY> <A HREF="/"><IMG SRC="pic.jpg"></A> </BODY> </HTML>""" assert t.serialize(output='HTML') == expected expected = serialize_doctype( doctypes['xhtml']) + """\n<html> <script>some lines and more lines</script><body> <a href="/"><img src="pic.jpg" /></a> </body> </html>""" assert t.serialize(output='xhtml') == expected
def test_empty_lines(): """Handling of empty lines in templates. Empty lines between elements should be removed. We assume that balanced_blocks is enabled for both HTML and XHTML templates. Inline elements in template should remain so. Other elements should be indented. """ t = kid.Template(""" <html> <script>some lines and more lines</script><body> <a href="/"><img src="pic.jpg"/></a> </body> </html>""") expected = serialize_doctype(doctypes['html']) + """\n<HTML> <SCRIPT>some lines and more lines</SCRIPT><BODY> <A HREF="/"><IMG SRC="pic.jpg"></A> </BODY> </HTML>""" assert t.serialize(output='HTML') == expected expected = serialize_doctype(doctypes['xhtml']) + """\n<html> <script>some lines and more lines</script><body> <a href="/"><img src="pic.jpg" /></a> </body> </html>""" assert t.serialize(output='xhtml') == expected
def test_xhtml_empty_elements(): noempty_tags = 'div p pre textarea'.split() empty_tags = 'br hr img input'.split() for namespace in ('', ' xmlns="%s"' % xhtml_namespace): for tag in noempty_tags + empty_tags: xml = '<html%s><%s/></html>' % (namespace, tag) t = kid.Template(xml) rslt = t.serialize(output='xhtml') if tag in empty_tags: xhtml = '<%s />' % tag else: xhtml = '<%s></%s>' % (tag, tag) xhtml = xml.replace('<%s/>' % tag, xhtml) xhtml = serialize_doctype(doctypes['xhtml']) + '\n' + xhtml assert rslt == xhtml
def test_comments_and_python(): """Comments and PI outside the root element evaluated inside.""" t = kid.Template('<!-- comment 1 --><?python x=42 ?>' '<!-- comment 2 --><html>$x</html>') assert (t.serialize(output='HTML') == serialize_doctype(doctypes['html']) + '\n<!-- comment 1 --><!-- comment 2 --><HTML>42</HTML>')
def test_comments_outside(): """Comments outside the root element (ticket #134).""" t = kid.Template('<!-- comment 1 --><html></html><!-- comment 2 -->') assert (t.serialize(output='HTML') == serialize_doctype(doctypes['html']) + '\n<!-- comment 1 --><HTML></HTML><!-- comment 2 -->')
def test_html_quirks_output_method(): t = kid.Template('<html><p>test</p><br /></html>') rslt = t.serialize(output='HTML-quirks') expected = serialize_doctype(doctypes['html-quirks']) + \ '\n<HTML><P>test</P><BR></HTML>' assert rslt == expected