Commit cb362793 authored by Paul Winkler's avatar Paul Winkler

Simpler, faster implementation of DocumentTemplate.DT_Var.newline_to_br(), with tests.

parent 9d22d6e2
......@@ -520,6 +520,9 @@ Zope Changes
- 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
......
......@@ -358,8 +358,8 @@ def newline_to_br(v, name='(Unknown name)', md={}):
# 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={}):
......
##############################################################################
#
# 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')
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment