Commits

Anonymous committed cff5a69

Add ``_nl`` argument to basic tag builders (``HTML.a()`` and ``HTML.tag()``).

Comments (0)

Files changed (3)

   - Fix spelling of Massachusetts.
 * webhelpers.html:
   - New ``HTML.cdata()`` method for producing "<!![CDATA[ ... ]]>" sections.
+  - The basic tag builders (``HTML.a()`` and ``HTML.tag("a")``) now have a
+    ``_nl`` arg which, if true, inserts a newline between content elements
+    and at the end of the tag for readability.  Example:  
+        HTML.a("A", "B", href="/")  =>   '<a href="/">AB</a>'
+        HTML.a("A", "B", href="/", _nl=True)   =>   '<a href="/">\nA\nB\n</a>\n'
+    This does not affect HTML attributes nor the higher-level tag helpers.
+    The exact spacing is subject to change.  The tag building code has been
+    refactored to accommodate this.
 * webhelpers.html.tags:
   - Set magic ID attribute in ``hidden`` helper to match behavior of the other
     tag helpers.
+from nose.tools import eq_
+
 from webhelpers.html import literal, lit_sub, escape, HTML
 
 def test_double_escape():
     
     result = HTML.form(_closed=False, action="hello")
     assert u'<form action="hello">' == result
+
+def test_newline_arg():
+    eq_(HTML.a(),         literal(u'<a></a>'))
+    eq_(HTML.a(_nl=True), literal(u'<a>\n</a>\n'))
+    eq_(HTML.a(_closed=False),           literal(u'<a>'))
+    eq_(HTML.a(_closed=False, _nl=True), literal(u'<a>\n'))
+    eq_(HTML.a("A", "B", href="/"),         literal(u'<a href="/">AB</a>'))
+    eq_(HTML.a("A", "B", href="/", _nl=True), literal(u'<a href="/">\nA\nB\n</a>\n'))

webhelpers/html/builder.py

         closed with a closing tag or an XHTML-style trailing slash as described
         below.
 
+    ``_nl``
+        If present and true, insert a newline before the first content
+        element, between each content element, and at the end of the tag.
+
     Example:
 
     >>> HTML.tag("a", href="http://www.yahoo.com", name=None, 
 "in conjunction with non-keyword arguments"
         args = kw.pop("c")
     closed = kw.pop("_closed", True)
+    nl = kw.pop("_nl", False)
     htmlArgs = [' %s="%s"' % (_attr_decode(attr), escape(value))
                 for attr, value in sorted(kw.iteritems())
                 if value is not None]
     if not args and tag in empty_tags and closed:
         substr = '<%s%s />'
-        return literal(substr % (tag, "".join(htmlArgs)))
+        html = literal(substr % (tag, "".join(htmlArgs)))
     else:
-        close_tag = ""
+        chunks = ["<%s%s>" % (tag, "".join(htmlArgs))]
+        chunks.extend(escape(x) for x in args)
         if closed:
-            close_tag = "</%s>" %(tag)
-        return literal("<%s%s>%s%s" % (
-            tag,
-            "".join(htmlArgs),
-            "".join([escape(x) for x in args]),
-            close_tag))
+            chunks.append("</%s>" % tag)
+        if nl:
+            html = "\n".join(chunks)
+        else:
+            html = "".join(chunks)
+    if nl:
+        html += "\n"
+    return literal(html)
 
 
 class literal(unicode):
 
 empty_tags = set("area base basefont br col frame hr img input isindex link meta param".split())
 
-# Constants depending on literal().
+HTML = HTMLBuilder()
+
+# Constants depending on ``literal()`` and/or ``HTML``.
+NL = literal("\n")
+EMPTY = literal("")
+BR = HTML.br(_nl=True)
 _CDATA_START = literal(u"<![CDATA[") 
 _CDATA_END = literal(u"]]>")
-    
-HTML = HTMLBuilder()