Translation
===========

This document contains functional template tests.

  >>> from chameleon.core.testing import render_xhtml

XHTML
-----

:: Plain HTML document

  >>> print render_xhtml("""\
  ... <div xmlns="http://www.w3.org/1999/xhtml">
  ...   Hello World!
  ... </div>""")
    <div>
      Hello World!
    </div>

:: Setting DOCTYPE

  >>> print render_xhtml("""\
  ... <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  ...    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  ... <html xmlns="http://www.w3.org/1999/xhtml">
  ...   Hello World!
  ... </html>""")
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
      Hello World!
    </html>

:: Unicode 

  >>> print render_xhtml("""\
  ... <div xmlns="http://www.w3.org/1999/xhtml">
  ...   La Peña
  ...   <img alt="La Peña" />
  ... </div>""")
    <div>
      La Peña
      <img alt="La Pe&ntilde;a" />
    </div>

:: CDATA blocks
  >>> print render_xhtml("""\
  ... <div xmlns="http://www.w3.org/1999/xhtml">
  ...   /* <![CDATA[ */
  ...   This is protected
  ...   /* ]]> */
  ...   <span>Not protected</span> <![CDATA[ This is protected ]]>
  ... </div>""")
    <div>
      /* <![CDATA[ */
      This is protected
      /* ]]> */
      <span>Not protected</span> <![CDATA[ This is protected ]]>
    </div>

Literals
--------

:: Named entities output literally

  >>> print render_xhtml("""\
  ... <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  ...    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  ... <html xmlns="http://www.w3.org/1999/xhtml">
  ...   Hello &nbsp; World!
  ... </html>""")
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
      Hello &nbsp; World!
    </html>

:: Although not XML-compliant, named entities are accepted even
   without a document type declaration.
    
  >>> print render_xhtml("""\
  ... <html xmlns="http://www.w3.org/1999/xhtml">
  ...   Hello &nbsp; World!
  ...   <a href="localhost" title="Singing &amp; Dancing"
  ...   >&rarr;</a>
  ...   <span class="&rarr;" />
  ... </html>""")
    <html>
      Hello &nbsp; World!
      <a href="localhost" title="Singing &amp; Dancing">&rarr;</a>
      <span class="&rarr;" />
    </html>

:: Processing instructions output literally

  >>> print render_xhtml("""\
  ... <html xmlns="http://www.w3.org/1999/xhtml">
  ...   <?xml-stylesheet href="classic.xsl" type="text/xml"?>
  ...   Hello World!
  ... </html>""")
    <html>
      <?xml-stylesheet href="classic.xsl" type="text/xml"?>
      Hello World!
    </html>

:: HTML comments

  >>> print render_xhtml("""\
  ... <div xmlns="http://www.w3.org/1999/xhtml">
  ...   <!-- a comment -->
  ...   <!-- a multi-
  ...        line comment -->
  ... </div>""")
  <div>
    <!-- a comment -->
    <!-- a multi-
         line comment -->
  </div>

:: Literal comments (without embedded expressions) output literally

  >>> print render_xhtml("""\
  ... <html xmlns="http://www.w3.org/1999/xhtml">
  ...   <!-- hello world -->
  ... </html>""")
  <html>
    <!-- hello world -->
  </html>
  
Text templates
--------------

  >>> from chameleon.core.testing import render_text

An example with a CSS stylesheet document:
  
  >>> css = """\
  ... #some-region {
  ...    background: url(http://nohost/plone/logo.gif) no-repeat;
  ... }"""

  >>> print render_text(css)
  #some-region {
     background: url(http://nohost/plone/logo.gif) no-repeat;
  }

A javascript document that prints out HTML:

  >>> js = """\
  ... print '<div class="description">Hello, world!</div>';"""

  >>> print render_text(js)
  print '<div class="description">Hello, world!</div>';

Ghosted templates
-----------------

To facilitate disk-caching, template instances can be pickled.

  >>> from chameleon.core.testing import compile_xhtml
  >>> template = compile_xhtml("""\
  ... <div xmlns="http://www.w3.org/1999/xhtml">
  ...   Hello World!
  ... </div>""")

  >>> from pickle import dumps, loads
  >>> pickle = dumps(template)
  >>> template = loads(pickle)

Check that we're getting a template object back.
  
  >>> template
  <ByteCodeTemplate parser="chameleon.core.testing.MockParser">

Let's try and render the template.
 
  >>> print template.render()
  <div>
    Hello World!
  </div>

  
Error handling
--------------

This section demonstrates how the package handles templates that
contain errors.

:: Malformed syntax

We expect the xml-parser to raise an exception.
  
  >>> body = '<div xmlns="http://www.w3.org/1999/xhtml"'
  >>> render_xhtml(body)
  Traceback (most recent call last):
    ...
  XMLSyntaxError: ...

:: Missing namespace definition

If a document type is provided, namespaces must be declared.

  >>> body = """\
  ... <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  ...                       "http://www.w3.org/TR/html4/loose.dtd">
  ... <div xmlns="http://www.w3.org/1999/xhtml" tal:content="'Hello World'" />
  ... """

  >>> print render_xhtml(body)
  Traceback (most recent call last):
    ...
  XMLSyntaxError: Namespace prefix tal for content on div is not defined...
