Mike Orr avatar Mike Orr committed 33fe693

New ``HTML.cdata()`` method for producing "<!![CDATA[ ... ]]>" sections.

Comments (0)

Files changed (2)

 -------------------------
 * webhelpers.constants:
   - Fix spelling of Massachusetts.
+* webhelpers.html:
+  - New ``HTML.cdata()`` method for producing "<!![CDATA[ ... ]]>" sections.
 * webhelpers.html.tools:
   - Fix HTML overescaping and underescaping in auto_link().  Patch by Marius
     Gedminas.

webhelpers/html/builder.py

     >>> HTML.a("Foo", href="http://example.com/", class_="important")
     literal(u'<a class="important" href="http://example.com/">Foo</a>')
 
+``HTML.cdata``
+    Wrap the text in a "<![CDATA[ ... ]]>" section. Plain strings will not be
+    escaped because CDATA itself is an escaping syntax.
+
+    >>> HTML.cdata(u"Foo")
+    literal(u'<![CDATA[Foo]]>')
+
+    >>> HTML.cdata(u"<p>")
+    literal(u'<![CDATA[<p>]]>')
+
 The protocol is simple: if an object has an ``.__html__`` method, ``escape()``
 calls it rather than ``.__str__()`` to obtain a string representation.
 
     def tag(self, tag, *args, **kw):
         return make_tag(tag, *args, **kw)
 
+    def cdata(self, *content): 
+        """Wrap the content in a "<![CDATA[ ... ]]>" section.
+
+        The content will not be escaped because CDATA itself is an 
+        escaping syntax.
+        """
+        # _CDATA_START and _CDATA_END are defined at end of module.
+        parts = []
+        parts.append(_CDATA_START)
+        parts.extend(content)
+        parts.append(_CDATA_END)
+        s = "".join(parts)
+        return literal(s)
 
 def _attr_decode(v):
     """Parse out attributes that begin with '_'."""
 
 empty_tags = set("area base basefont br col frame hr img input isindex link meta param".split())
 
+# Constants depending on literal().
+_CDATA_START = literal(u"<![CDATA[") 
+_CDATA_END = literal(u"]]>")
+    
 HTML = HTMLBuilder()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.