1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.rendering.wikimodel.test.xhtml; |
21 |
|
|
22 |
|
import java.io.StringReader; |
23 |
|
|
24 |
|
import javax.xml.parsers.SAXParserFactory; |
25 |
|
|
26 |
|
import org.xml.sax.InputSource; |
27 |
|
import org.xml.sax.XMLReader; |
28 |
|
import org.xwiki.rendering.wikimodel.xhtml.filter.XHTMLWhitespaceXMLFilter; |
29 |
|
|
30 |
|
import junit.framework.TestCase; |
31 |
|
|
32 |
|
|
33 |
|
@version |
34 |
|
@since |
35 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (58) |
Complexity: 6 |
Complexity Density: 0.12 |
|
36 |
|
public class XHTMLWhitespaceXMLFilterTest extends TestCase |
37 |
|
{ |
38 |
|
private XMLWriter writerFilter; |
39 |
|
|
40 |
|
private XHTMLWhitespaceXMLFilter whitespaceFilter; |
41 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
42 |
2 |
@Override... |
43 |
|
protected void setUp() throws Exception |
44 |
|
{ |
45 |
2 |
XMLReader xmlReader = SAXParserFactory |
46 |
|
.newInstance() |
47 |
|
.newSAXParser() |
48 |
|
.getXMLReader(); |
49 |
2 |
whitespaceFilter = new XHTMLWhitespaceXMLFilter(xmlReader); |
50 |
2 |
writerFilter = new XMLWriter(); |
51 |
|
|
52 |
2 |
whitespaceFilter.setFeature( |
53 |
|
"http://xml.org/sax/features/namespaces", |
54 |
|
true); |
55 |
2 |
whitespaceFilter.setContentHandler(writerFilter); |
56 |
2 |
whitespaceFilter.setProperty( |
57 |
|
"http://xml.org/sax/properties/lexical-handler", |
58 |
|
writerFilter); |
59 |
|
} |
60 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (34) |
Complexity: 1 |
Complexity Density: 0.03 |
1PASS
|
|
61 |
1 |
public void testWhiteSpaceStripping() throws Exception... |
62 |
|
{ |
63 |
1 |
assertCleanedHTML("<p>one two</p>", "<p> one two </p>"); |
64 |
1 |
assertCleanedHTML( |
65 |
|
"<p>one two <b>three</b></p>", |
66 |
|
"<p> one two <b>three</b></p>"); |
67 |
1 |
assertCleanedHTML("<p>one two</p>", "<p>\n\r\tone\n\r\ttwo\n\r\t</p>"); |
68 |
1 |
assertCleanedHTML( |
69 |
|
"<p>one <b>two</b> <b>three</b></p>", |
70 |
|
"<p>one <b>two</b> <b>three</b></p>"); |
71 |
1 |
assertCleanedHTML( |
72 |
|
"<p>one <b>two</b> <em><b>three</b>a</em></p>", |
73 |
|
"<p>one <b>two</b> <em><b>three</b>a</em></p>"); |
74 |
1 |
assertCleanedHTML( |
75 |
|
"<p>one</p>two<p>three</p>", |
76 |
|
"<p>one</p> two <p>three</p>"); |
77 |
1 |
assertCleanedHTML("<![CDATA[\n one \n]]>", "<![CDATA[\n one \n]]>"); |
78 |
1 |
assertCleanedHTML("<pre>\n one \n</pre>", "<pre>\n one \n</pre>"); |
79 |
1 |
assertCleanedHTML( |
80 |
|
"<p>toto <tt>\n one \n</tt></p>", |
81 |
|
"<p>toto <tt class=\"wikimodel-verbatim\">\n one \n</tt></p>"); |
82 |
1 |
assertCleanedHTML( |
83 |
|
"<p>one <tt>two</tt> three</p>", |
84 |
|
"<p>one <tt class=\"wikimodel-verbatim\">two</tt> three</p>"); |
85 |
1 |
assertCleanedHTML( |
86 |
|
"<p>one <!--comment-->two</p>", |
87 |
|
"<p> one <!--comment--> two </p>"); |
88 |
1 |
assertCleanedHTML( |
89 |
|
"<p><!--comment-->one <b>two</b></p>", |
90 |
|
"<p><!--comment--> one <b>two</b></p>"); |
91 |
1 |
assertCleanedHTML( |
92 |
|
"<p>one <b>two</b> three</p>", |
93 |
|
"<p>one <b>two</b> three </p>"); |
94 |
1 |
assertCleanedHTML( |
95 |
|
"<!--comment-->one<![CDATA[two]]>", |
96 |
|
"<!--comment--> one <![CDATA[two]]>"); |
97 |
1 |
assertCleanedHTML("<p></p>", "<p> </p>"); |
98 |
1 |
assertCleanedHTML("<p><b>text</b></p>", "<p> <b> text </b> </p>"); |
99 |
1 |
assertCleanedHTML( |
100 |
|
"<!--startmacro:something--> <!--nonsemantic--><!--stopmacro-->", |
101 |
|
" <!--startmacro:something--> <!--nonsemantic--> <!--stopmacro--> "); |
102 |
1 |
assertCleanedHTML( |
103 |
|
"<p>one <!--startmacro:something--><!--stopmacro--></p>", |
104 |
|
"<p>one <!--startmacro:something--><!--stopmacro--></p>"); |
105 |
1 |
assertCleanedHTML( |
106 |
|
"<p>one <!--startmacro:something--><!--stopmacro--> two</p>", |
107 |
|
"<p>one <!--startmacro:something--><!--stopmacro--> two</p>"); |
108 |
1 |
assertCleanedHTML( |
109 |
|
"<!--comment-->one<![CDATA[two]]>", |
110 |
|
"<!--comment--> one<![CDATA[two]]>"); |
111 |
1 |
assertCleanedHTML( |
112 |
|
"<p>one <!--comment-->two<![CDATA[three]]></p>", |
113 |
|
"<p>one <!--comment--> two<![CDATA[three]]></p>"); |
114 |
1 |
assertCleanedHTML( |
115 |
|
"<p>one <span>two </span><!--comment-->three<![CDATA[four]]></p>", |
116 |
|
"<p> one <span> two </span><!--comment--> three <![CDATA[four]]></p>"); |
117 |
1 |
assertCleanedHTML( |
118 |
|
"<p>This<strong> Spore Cheat Sheet</strong></p>", |
119 |
|
"<p>This<strong>\nSpore Cheat Sheet</strong></p>"); |
120 |
1 |
assertCleanedHTML( |
121 |
|
"<table><tbody><tr><td>First doc:<div><p>inside</p></div></td></tr></tbody></table>", |
122 |
|
"<table><tbody>\n<tr><td>First doc:<div>\n<p>inside</p></div></td></tr></tbody></table>"); |
123 |
1 |
assertCleanedHTML( |
124 |
|
"<p>one two three<br></br><br></br>hello</p>", |
125 |
|
"<p>one two three<br/><br/>hello</p>"); |
126 |
1 |
assertCleanedHTML( |
127 |
|
"<p><strong><span>hello</span></strong><span>world</span></p>", |
128 |
|
"<p><strong><span>hello</span></strong><span>world</span></p>"); |
129 |
1 |
assertCleanedHTML( |
130 |
|
"<div><p></p><p></p></div>", |
131 |
|
"<div><p></p> <p></p></div>"); |
132 |
1 |
assertCleanedHTML( |
133 |
|
"<unknow>hello word</unknow>", |
134 |
|
" <unknow> hello word </unknow> "); |
135 |
1 |
assertCleanedHTML( |
136 |
|
"<p><unknow>hello word </unknow>text</p>", |
137 |
|
"<p> <unknow> hello word </unknow> text</p>"); |
138 |
1 |
assertCleanedHTML( |
139 |
|
"one<script> one two </script> two", |
140 |
|
"one <script> one two </script> two"); |
141 |
1 |
assertCleanedHTML( |
142 |
|
"<span>one two</span>", |
143 |
|
"<span> one two </span>", |
144 |
|
false); |
145 |
1 |
assertCleanedHTML( |
146 |
|
"one<script> one two </script>", |
147 |
|
"one <script> one two </script>"); |
148 |
1 |
assertCleanedHTML( |
149 |
|
"one<script>//<![CDATA[\nsome script\n//]]></script>", |
150 |
|
"one<script>//<![CDATA[\nsome script\n//]]></script>"); |
151 |
1 |
assertCleanedHTML("one <img></img> two", "one <img/> two"); |
152 |
|
} |
153 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
154 |
1 |
public void testWhiteSpaceStrippingForBlockElements() throws Exception... |
155 |
|
{ |
156 |
1 |
assertCleanedHTML("<p></p><p></p>", "<p></p> \n\r\t<p></p>"); |
157 |
1 |
assertCleanedHTML( |
158 |
|
"<ul><li></li><li></li></ul>", |
159 |
|
"<ul> <li></li> <li></li> </ul>"); |
160 |
1 |
assertCleanedHTML("<table></table><p></p>", "<table></table> <p></p>"); |
161 |
|
} |
162 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
163 |
36 |
private void assertCleanedHTML(String expected, String originalContent)... |
164 |
|
throws Exception |
165 |
|
{ |
166 |
36 |
assertCleanedHTML(expected, originalContent, true); |
167 |
|
} |
168 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 2 |
Complexity Density: 0.29 |
|
169 |
37 |
private void assertCleanedHTML(... |
170 |
|
String expected, |
171 |
|
String originalContent, |
172 |
|
boolean protect) throws Exception |
173 |
|
{ |
174 |
37 |
if (protect) { |
175 |
36 |
expected = "<html>" + expected + "</html>"; |
176 |
36 |
originalContent = "<html>" + originalContent + "</html>"; |
177 |
|
} |
178 |
|
|
179 |
37 |
InputSource source = new InputSource(new StringReader(originalContent)); |
180 |
37 |
whitespaceFilter.parse(source); |
181 |
37 |
assertEquals(expected, writerFilter.getBuffer()); |
182 |
37 |
writerFilter.reset(); |
183 |
|
} |
184 |
|
} |