1 # -*- coding: utf-8 -*- |
|
2 # copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
|
3 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
|
4 # |
|
5 # This file is part of CubicWeb. |
|
6 # |
|
7 # CubicWeb is free software: you can redistribute it and/or modify it under the |
|
8 # terms of the GNU Lesser General Public License as published by the Free |
|
9 # Software Foundation, either version 2.1 of the License, or (at your option) |
|
10 # any later version. |
|
11 # |
|
12 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
|
13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
14 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
|
15 # details. |
|
16 # |
|
17 # You should have received a copy of the GNU Lesser General Public License along |
|
18 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
|
19 """unittests for cubicweb.uilib""" |
|
20 |
|
21 __docformat__ = "restructuredtext en" |
|
22 |
|
23 |
|
24 import pkg_resources |
|
25 |
|
26 try: |
|
27 from unittest import skipIf |
|
28 except ImportError: |
|
29 from unittest2 import skipIf |
|
30 |
|
31 from logilab.common.testlib import DocTest, TestCase, unittest_main |
|
32 |
|
33 from cubicweb import uilib, utils as cwutils |
|
34 |
|
35 lxml_version = pkg_resources.get_distribution('lxml').version.split('.') |
|
36 |
|
37 class UILIBTC(TestCase): |
|
38 |
|
39 def test_remove_tags(self): |
|
40 """make sure remove_tags remove all tags""" |
|
41 data = [ |
|
42 ('<h1>Hello</h1>', 'Hello'), |
|
43 ('<h1>Hello <a href="foo/bar"><b>s</b>pam</a></h1>', 'Hello spam'), |
|
44 ('<br>Hello<img src="doh.png"/>', 'Hello'), |
|
45 ('<p></p>', ''), |
|
46 ] |
|
47 for text, expected in data: |
|
48 got = uilib.remove_html_tags(text) |
|
49 self.assertEqual(got, expected) |
|
50 |
|
51 def test_fallback_safe_cut(self): |
|
52 self.assertEqual(uilib.fallback_safe_cut(u'ab <a href="hello">cd</a>', 4), u'ab c...') |
|
53 self.assertEqual(uilib.fallback_safe_cut(u'ab <a href="hello">cd</a>', 5), u'ab <a href="hello">cd</a>') |
|
54 self.assertEqual(uilib.fallback_safe_cut(u'ab <a href="hello">&d</a>', 4), u'ab &...') |
|
55 self.assertEqual(uilib.fallback_safe_cut(u'ab <a href="hello">&d</a> ef', 5), u'ab &d...') |
|
56 self.assertEqual(uilib.fallback_safe_cut(u'ab <a href="hello">ìd</a>', 4), u'ab ì...') |
|
57 self.assertEqual(uilib.fallback_safe_cut(u'& <a href="hello">&d</a> ef', 4), u'& &d...') |
|
58 |
|
59 def test_lxml_safe_cut(self): |
|
60 self.assertEqual(uilib.safe_cut(u'aaa<div>aaad</div> ef', 4), u'<p>aaa</p><div>a...</div>') |
|
61 self.assertEqual(uilib.safe_cut(u'aaa<div>aaad</div> ef', 7), u'<p>aaa</p><div>aaad</div>...') |
|
62 self.assertEqual(uilib.safe_cut(u'aaa<div>aaad</div>', 7), u'<p>aaa</p><div>aaad</div>') |
|
63 # Missing ellipsis due to space management but we don't care |
|
64 self.assertEqual(uilib.safe_cut(u'ab <a href="hello">&d</a>', 4), u'<p>ab <a href="hello">&...</a></p>') |
|
65 |
|
66 def test_cut(self): |
|
67 """tests uilib.cut() behaviour""" |
|
68 data = [ |
|
69 ('hello', 'hello'), |
|
70 ('hello world', 'hello wo...'), |
|
71 ("hell<b>O'</b> world", "hell<b>O..."), |
|
72 ] |
|
73 for text, expected in data: |
|
74 got = uilib.cut(text, 8) |
|
75 self.assertEqual(got, expected) |
|
76 |
|
77 def test_text_cut(self): |
|
78 """tests uilib.text_cut() behaviour with no text""" |
|
79 data = [('',''), |
|
80 ("""Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod |
|
81 tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, |
|
82 quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo |
|
83 consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse |
|
84 cillum dolore eu fugiat nulla pariatur.""", |
|
85 "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod \ |
|
86 tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, \ |
|
87 quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo \ |
|
88 consequat."), |
|
89 ("""Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod |
|
90 tempor incididunt ut labore et dolore magna aliqua Ut enim ad minim veniam, |
|
91 quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo |
|
92 consequat Duis aute irure dolor in reprehenderit in voluptate velit esse |
|
93 cillum dolore eu fugiat nulla pariatur Excepteur sint occaecat cupidatat non |
|
94 proident, sunt in culpa qui officia deserunt mollit anim id est laborum |
|
95 """, |
|
96 "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod \ |
|
97 tempor incididunt ut labore et dolore magna aliqua Ut enim ad minim veniam, \ |
|
98 quis nostrud exercitation ullamco laboris nisi"), |
|
99 ] |
|
100 for text, expected in data: |
|
101 got = uilib.text_cut(text, 30) |
|
102 self.assertEqual(got, expected) |
|
103 |
|
104 def test_soup2xhtml_0(self): |
|
105 self.assertEqual(uilib.soup2xhtml('hop\r\nhop', 'ascii'), |
|
106 'hop\nhop') |
|
107 |
|
108 def test_soup2xhtml_1_1(self): |
|
109 self.assertEqual(uilib.soup2xhtml('hop', 'ascii'), |
|
110 'hop') |
|
111 self.assertEqual(uilib.soup2xhtml('hop<div>', 'ascii'), |
|
112 'hop<div/>') |
|
113 self.assertEqual(uilib.soup2xhtml('hop <div>', 'ascii'), |
|
114 'hop <div/>') |
|
115 self.assertEqual(uilib.soup2xhtml('<div> hop', 'ascii'), |
|
116 '<div> hop</div>') |
|
117 self.assertEqual(uilib.soup2xhtml('hop <div> hop', 'ascii'), |
|
118 'hop <div> hop</div>') |
|
119 |
|
120 def test_soup2xhtml_1_2(self): |
|
121 self.assertEqual(uilib.soup2xhtml('hop </div>', 'ascii'), |
|
122 'hop ') |
|
123 self.assertEqual(uilib.soup2xhtml('</div> hop', 'ascii'), |
|
124 '<div/> hop') |
|
125 self.assertEqual(uilib.soup2xhtml('hop </div> hop', 'ascii'), |
|
126 '<div>hop </div> hop') |
|
127 |
|
128 def test_soup2xhtml_2_1(self): |
|
129 self.assertEqual(uilib.soup2xhtml('hop <body>', 'ascii'), |
|
130 'hop ') |
|
131 self.assertEqual(uilib.soup2xhtml('<body> hop', 'ascii'), |
|
132 ' hop') |
|
133 self.assertEqual(uilib.soup2xhtml('hop <body> hop', 'ascii'), |
|
134 'hop hop') |
|
135 |
|
136 def test_soup2xhtml_2_2a(self): |
|
137 self.assertEqual(uilib.soup2xhtml('hop </body>', 'ascii'), |
|
138 'hop ') |
|
139 self.assertEqual(uilib.soup2xhtml('</body> hop', 'ascii'), |
|
140 ' hop') |
|
141 |
|
142 @skipIf(lxml_version < ['2', '2'], 'expected behaviour on recent version of lxml only') |
|
143 def test_soup2xhtml_2_2b(self): |
|
144 self.assertEqual(uilib.soup2xhtml('hop </body> hop', 'ascii'), |
|
145 'hop hop') |
|
146 |
|
147 def test_soup2xhtml_3_1(self): |
|
148 self.assertEqual(uilib.soup2xhtml('hop <html>', 'ascii'), |
|
149 'hop ') |
|
150 self.assertEqual(uilib.soup2xhtml('<html> hop', 'ascii'), |
|
151 ' hop') |
|
152 self.assertEqual(uilib.soup2xhtml('hop <html> hop', 'ascii'), |
|
153 'hop hop') |
|
154 |
|
155 def test_soup2xhtml_3_2(self): |
|
156 self.assertEqual(uilib.soup2xhtml('hop </html>', 'ascii'), |
|
157 'hop ') |
|
158 self.assertEqual(uilib.soup2xhtml('</html> hop', 'ascii'), |
|
159 ' hop') |
|
160 self.assertEqual(uilib.soup2xhtml('hop </html> hop', 'ascii'), |
|
161 'hop hop') |
|
162 |
|
163 def test_soup2xhtml_3_3(self): |
|
164 self.assertEqual(uilib.soup2xhtml('<script>test</script> hop ', 'ascii'), |
|
165 ' hop ') |
|
166 |
|
167 def test_js(self): |
|
168 self.assertEqual(str(uilib.js.pouet(1, "2")), |
|
169 'pouet(1,"2")') |
|
170 self.assertEqual(str(uilib.js.cw.pouet(1, "2")), |
|
171 'cw.pouet(1,"2")') |
|
172 self.assertEqual(str(uilib.js.cw.pouet(1, "2").pouet(None)), |
|
173 'cw.pouet(1,"2").pouet(null)') |
|
174 self.assertEqual(str(uilib.js.cw.pouet(1, cwutils.JSString("$")).pouet(None)), |
|
175 'cw.pouet(1,$).pouet(null)') |
|
176 self.assertEqual(str(uilib.js.cw.pouet(1, {'callback': cwutils.JSString("cw.cb")}).pouet(None)), |
|
177 'cw.pouet(1,{callback: cw.cb}).pouet(null)') |
|
178 |
|
179 |
|
180 def test_embedded_css(self): |
|
181 incoming = u"""voir le ticket <style type="text/css">@font-face { font-family: "Cambria"; }p.MsoNormal, li.MsoNormal, div.MsoNormal { margin: 0cm 0cm 10pt; font-size: 12pt; font-family: "Times New Roman"; }a:link, span.MsoHyperlink { color: blue; text-decoration: underline; }a:visited, span.MsoHyperlinkFollowed { color: purple; text-decoration: underline; }div.Section1 { page: Section1; }</style></p><p class="MsoNormal">text</p>""" |
|
182 expected = 'voir le ticket <p class="MsoNormal">text</p>' |
|
183 self.assertMultiLineEqual(uilib.soup2xhtml(incoming, 'ascii'), expected) |
|
184 |
|
185 def test_unknown_namespace(self): |
|
186 incoming = '''<table cellspacing="0" cellpadding="0" width="81" border="0" x:str="" style="width: 61pt; border-collapse: collapse">\ |
|
187 <colgroup><col width="81" style="width: 61pt; mso-width-source: userset; mso-width-alt: 2962"/></colgroup>\ |
|
188 <tbody><tr height="17" style="height: 12.75pt"><td width="81" height="17" style="border-right: #e0dfe3; border-top: #e0dfe3; border-left: #e0dfe3; width: 61pt; border-bottom: #e0dfe3; height: 12.75pt; background-color: transparent"><font size="2">XXXXXXX</font></td></tr></tbody>\ |
|
189 </table>''' |
|
190 expected = '''<table cellspacing="0" cellpadding="0" width="81" border="0">\ |
|
191 <colgroup><col width="81"/></colgroup>\ |
|
192 <tbody><tr height="17"><td width="81" height="17">XXXXXXX</td></tr></tbody>\ |
|
193 </table>''' |
|
194 self.assertMultiLineEqual(uilib.soup2xhtml(incoming, 'ascii'), expected) |
|
195 |
|
196 |
|
197 class DocTest(DocTest): |
|
198 module = uilib |
|
199 |
|
200 |
|
201 if __name__ == '__main__': |
|
202 unittest_main() |
|