comparison genshi/tests/filters.py @ 261:da3137c427a2 stable-0.3.x

Ported [321:323] to 0.3.x stable branch.
author cmlenz
date Fri, 22 Sep 2006 12:07:23 +0000
parents 84168828b074
children d91cbdeb75e9
comparison
equal deleted inserted replaced
257:5c8125e03b72 261:da3137c427a2
10 # This software consists of voluntary contributions made by many 10 # This software consists of voluntary contributions made by many
11 # individuals. For the exact contribution history, see the revision 11 # individuals. For the exact contribution history, see the revision
12 # history and logs, available at http://genshi.edgewall.org/log/. 12 # history and logs, available at http://genshi.edgewall.org/log/.
13 13
14 import doctest 14 import doctest
15 import os
16 import shutil
17 import tempfile
15 import unittest 18 import unittest
16 19
17 from genshi.core import Stream 20 from genshi.core import Stream
18 from genshi.input import HTML, ParseError 21 from genshi.input import HTML, ParseError
19 from genshi.filters import HTMLSanitizer 22 from genshi.filters import HTMLSanitizer
23 from genshi.template import TemplateLoader
20 24
21 25
22 class HTMLSanitizerTestCase(unittest.TestCase): 26 class HTMLSanitizerTestCase(unittest.TestCase):
23 27
24 def test_sanitize_unchanged(self): 28 def test_sanitize_unchanged(self):
114 # Embedded tab character in protocol, but encoded this time 118 # Embedded tab character in protocol, but encoded this time
115 html = HTML('<IMG SRC=\'jav&#x09;ascript:alert("foo");\'>') 119 html = HTML('<IMG SRC=\'jav&#x09;ascript:alert("foo");\'>')
116 self.assertEquals(u'<img/>', unicode(html | HTMLSanitizer())) 120 self.assertEquals(u'<img/>', unicode(html | HTMLSanitizer()))
117 121
118 122
123 class IncludeFilterTestCase(unittest.TestCase):
124
125 def setUp(self):
126 self.dirname = tempfile.mkdtemp(suffix='markup_test')
127
128 def tearDown(self):
129 shutil.rmtree(self.dirname)
130
131 def test_select_inluded_elements(self):
132 file1 = open(os.path.join(self.dirname, 'tmpl1.html'), 'w')
133 try:
134 file1.write("""<li>$item</li>""")
135 finally:
136 file1.close()
137
138 file2 = open(os.path.join(self.dirname, 'tmpl2.html'), 'w')
139 try:
140 file2.write("""<html xmlns:xi="http://www.w3.org/2001/XInclude"
141 xmlns:py="http://genshi.edgewall.org/">
142 <ul py:match="ul">${select('li')}</ul>
143 <ul py:with="items=(1, 2, 3)">
144 <xi:include href="tmpl1.html" py:for="item in items" />
145 </ul>
146 </html>""")
147 finally:
148 file2.close()
149
150 loader = TemplateLoader([self.dirname])
151 tmpl = loader.load('tmpl2.html')
152 self.assertEqual("""<html>
153 <ul><li>1</li><li>2</li><li>3</li></ul>
154 </html>""", tmpl.generate().render())
155
156
119 def suite(): 157 def suite():
120 suite = unittest.TestSuite() 158 suite = unittest.TestSuite()
121 suite.addTest(unittest.makeSuite(HTMLSanitizerTestCase, 'test')) 159 suite.addTest(unittest.makeSuite(HTMLSanitizerTestCase, 'test'))
160 suite.addTest(unittest.makeSuite(IncludeFilterTestCase, 'test'))
122 return suite 161 return suite
123 162
124 if __name__ == '__main__': 163 if __name__ == '__main__':
125 unittest.main(defaultTest='suite') 164 unittest.main(defaultTest='suite')
Copyright (C) 2012-2017 Edgewall Software