[Zope-Checkins] SVN: Zope/trunk/ Simpler, faster implementation of DocumentTemplate.DT_Var.newline_to_br(), with tests.

Paul Winkler slinkp at gmail.com
Tue Nov 18 15:27:53 EST 2008


Log message for revision 93099:
  Simpler, faster implementation of DocumentTemplate.DT_Var.newline_to_br(), with tests.

Changed:
  U   Zope/trunk/doc/CHANGES.txt
  U   Zope/trunk/lib/python/DocumentTemplate/DT_Var.py
  A   Zope/trunk/lib/python/DocumentTemplate/tests/test_DT_Var.py

-=-
Modified: Zope/trunk/doc/CHANGES.txt
===================================================================
--- Zope/trunk/doc/CHANGES.txt	2008-11-18 20:15:21 UTC (rev 93098)
+++ Zope/trunk/doc/CHANGES.txt	2008-11-18 20:27:52 UTC (rev 93099)
@@ -520,6 +520,9 @@
       - Added lib/python/webdav/litmus-results.txt explaining current
         test results from the litmus WebDAV torture test.
 
+      - DocumentTemplate.DT_Var.newline_to_br(): Simpler, faster implementation.
+
+
   Zope 2.10.0 beta 1 (2006/05/30)
 
     Restructuring

Modified: Zope/trunk/lib/python/DocumentTemplate/DT_Var.py
===================================================================
--- Zope/trunk/lib/python/DocumentTemplate/DT_Var.py	2008-11-18 20:15:21 UTC (rev 93098)
+++ Zope/trunk/lib/python/DocumentTemplate/DT_Var.py	2008-11-18 20:27:52 UTC (rev 93099)
@@ -358,8 +358,8 @@
     # quoted later on anyway.
     if isinstance(v, TaintedString): v = v.quoted()
     v=ustr(v)
-    if v.find('\r') >= 0: v=''.join(v.split('\r'))
-    if v.find('\n') >= 0: v='<br />\n'.join(v.split('\n'))
+    v = v.replace('\r', '')
+    v = v.replace('\n', '<br />\n')
     return v
 
 def whole_dollars(v, name='(Unknown name)', md={}):

Added: Zope/trunk/lib/python/DocumentTemplate/tests/test_DT_Var.py
===================================================================
--- Zope/trunk/lib/python/DocumentTemplate/tests/test_DT_Var.py	                        (rev 0)
+++ Zope/trunk/lib/python/DocumentTemplate/tests/test_DT_Var.py	2008-11-18 20:27:52 UTC (rev 93099)
@@ -0,0 +1,70 @@
+##############################################################################
+#
+# Copyright (c) 2008 Zope Corporation and Contributors. All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Tests for functions and classes in DT_Var.
+
+$Id$
+"""
+
+import unittest, doctest
+from DocumentTemplate import DT_Var
+
+class TestNewlineToBr(doctest.DocTestCase):
+
+    def test_newline_to_br(self):
+        r"""
+        newline_to_br should work identically with either DOS-style or
+        Unix-style newlines.
+
+        >>> text = '''
+        ... line one
+        ... line two
+        ...
+        ... line three
+        ... '''
+        >>> print DT_Var.newline_to_br(text)
+        <br />
+        line one<br />
+        line two<br />
+        <br />
+        line three<br />
+        <BLANKLINE>
+        
+        >>> dos = text.replace('\n', '\r\n')
+        >>> DT_Var.newline_to_br(text) == DT_Var.newline_to_br(dos)
+        True
+        """
+
+
+    def test_newline_to_br_tainted(self):
+        """
+        >>> text = '''
+        ... <li>line one</li>
+        ... <li>line two</li>
+        ... '''
+        >>> from ZPublisher.TaintedString import TaintedString
+        >>> tainted = TaintedString(text)
+        >>> print DT_Var.newline_to_br(tainted)
+        <br />
+        &lt;li&gt;line one&lt;/li&gt;<br />
+        &lt;li&gt;line two&lt;/li&gt;<br />
+        <BLANKLINE>
+
+        """
+
+def test_suite():
+    suite = unittest.TestSuite()
+    suite.addTest(doctest.DocTestSuite())
+    return suite
+
+if __name__ == '__main__':
+    unittest.main(defaultTest='test_suite')


Property changes on: Zope/trunk/lib/python/DocumentTemplate/tests/test_DT_Var.py
___________________________________________________________________
Added: svn:keywords
   + "Author Date Revision"



More information about the Zope-Checkins mailing list