OLD | NEW |
| (Empty) |
1 // Copyright (C) 2008 Google Inc. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 | |
15 package com.google.caja.plugin; | |
16 | |
17 import com.google.caja.lexer.FilePosition; | |
18 import com.google.caja.parser.js.Block; | |
19 import com.google.caja.parser.js.Expression; | |
20 import com.google.caja.util.CajaTestCase; | |
21 import com.google.caja.util.Name; | |
22 import com.google.caja.util.TestUtil; | |
23 | |
24 public class DomProcessingEventsTest extends CajaTestCase { | |
25 | |
26 public void testAttribsMustBeClosed() throws Exception { | |
27 FilePosition unk = FilePosition.UNKNOWN; | |
28 try { | |
29 DomProcessingEvents dpe = new DomProcessingEvents(); | |
30 dpe.begin(unk, Name.html("p")); | |
31 dpe.end(unk, Name.html("p")); | |
32 fail(); | |
33 } catch (IllegalStateException ex) { | |
34 // pass | |
35 } | |
36 try { | |
37 DomProcessingEvents dpe = new DomProcessingEvents(); | |
38 dpe.begin(unk, Name.html("p")); | |
39 dpe.pcdata(unk, "Hello"); | |
40 fail(); | |
41 } catch (IllegalStateException ex) { | |
42 // pass | |
43 } | |
44 try { | |
45 DomProcessingEvents dpe = new DomProcessingEvents(); | |
46 dpe.begin(unk, Name.html("p")); | |
47 dpe.toJavascript(new Block()); | |
48 fail(); | |
49 } catch (IllegalStateException ex) { | |
50 // pass | |
51 } | |
52 try { | |
53 DomProcessingEvents dpe = new DomProcessingEvents(); | |
54 dpe.begin(unk, Name.html("p")); | |
55 dpe.toJavascript(new Block()); | |
56 fail(); | |
57 } catch (IllegalStateException ex) { | |
58 // pass | |
59 } | |
60 try { | |
61 DomProcessingEvents dpe = new DomProcessingEvents(); | |
62 dpe.begin(unk, Name.html("p")); | |
63 dpe.attr(unk, Name.html("foo"), "bar"); | |
64 dpe.toJavascript(new Block()); | |
65 fail(); | |
66 } catch (IllegalStateException ex) { | |
67 // pass | |
68 } | |
69 try { | |
70 DomProcessingEvents dpe = new DomProcessingEvents(); | |
71 dpe.begin(unk, Name.html("p")); | |
72 dpe.begin(unk, Name.html("p")); | |
73 fail(); | |
74 } catch (IllegalStateException ex) { | |
75 // pass | |
76 } | |
77 try { | |
78 DomProcessingEvents dpe = new DomProcessingEvents(); | |
79 dpe.begin(unk, Name.html("p")); | |
80 dpe.finishAttrs(false); | |
81 dpe.attr(unk, Name.html("foo"), "bar"); | |
82 fail(); | |
83 } catch (IllegalStateException ex) { | |
84 // pass | |
85 } | |
86 try { | |
87 DomProcessingEvents dpe = new DomProcessingEvents(); | |
88 dpe.attr(unk, Name.html("foo"), "bar"); | |
89 fail(); | |
90 } catch (IllegalStateException ex) { | |
91 // pass | |
92 } | |
93 try { | |
94 DomProcessingEvents dpe = new DomProcessingEvents(); | |
95 dpe.finishAttrs(false); | |
96 fail(); | |
97 } catch (IllegalStateException ex) { | |
98 // pass | |
99 } | |
100 try { | |
101 DomProcessingEvents dpe = new DomProcessingEvents(); | |
102 dpe.begin(unk, Name.html("p")); | |
103 dpe.script(js(fromString("foo();"))); | |
104 fail(); | |
105 } catch (IllegalStateException ex) { | |
106 // pass | |
107 } | |
108 } | |
109 | |
110 public void testUnbalancedTags() { | |
111 FilePosition unk = FilePosition.UNKNOWN; | |
112 try { | |
113 DomProcessingEvents dpe = new DomProcessingEvents(); | |
114 dpe.begin(unk, Name.html("p")); | |
115 dpe.finishAttrs(false); | |
116 dpe.end(unk, Name.html("q")); | |
117 dpe.toJavascript(new Block()); | |
118 fail(); | |
119 } catch (IllegalStateException ex) { | |
120 // pass | |
121 } | |
122 try { | |
123 DomProcessingEvents dpe = new DomProcessingEvents(); | |
124 dpe.begin(unk, Name.html("p")); | |
125 dpe.finishAttrs(false); | |
126 dpe.toJavascript(new Block()); | |
127 fail(); | |
128 } catch (IllegalStateException ex) { | |
129 // pass | |
130 } | |
131 try { | |
132 DomProcessingEvents dpe = new DomProcessingEvents(); | |
133 dpe.begin(unk, Name.html("p")); | |
134 dpe.finishAttrs(false); | |
135 dpe.begin(unk, Name.html("p")); | |
136 dpe.finishAttrs(false); | |
137 dpe.end(unk, Name.html("p")); | |
138 dpe.end(unk, Name.html("p")); | |
139 dpe.end(unk, Name.html("p")); | |
140 dpe.toJavascript(new Block()); | |
141 fail(); | |
142 } catch (IllegalStateException ex) { | |
143 // pass | |
144 } | |
145 } | |
146 | |
147 public void testTooMuchRecursionFix() throws Exception { | |
148 FilePosition unk = FilePosition.UNKNOWN; | |
149 Expression x = jsExpr(fromString("x")); | |
150 DomProcessingEvents dpe = new DomProcessingEvents(); | |
151 for (int i = 0; i < 30; ++i) { | |
152 dpe.begin(unk, Name.html("p")); | |
153 dpe.attr(Name.html("id"), x); // defeat optimization | |
154 dpe.finishAttrs(false); | |
155 } | |
156 for (int i = 0; i < 30; ++i) { dpe.end(unk, Name.html("p")); } | |
157 | |
158 Block block = new Block(); | |
159 dpe.toJavascript(block); | |
160 TestUtil.removePseudoNodes(block); | |
161 | |
162 String prefix = "IMPORTS___.htmlEmitter___"; | |
163 String startOne = ".b('p').a('id',x).f(false)"; | |
164 String startTen = (startOne + startOne + startOne + startOne + startOne | |
165 + startOne + startOne + startOne + startOne + startOne); | |
166 String endOne = ".e('p')"; | |
167 String endTen = (endOne + endOne + endOne + endOne + endOne | |
168 + endOne + endOne + endOne + endOne + endOne); | |
169 | |
170 assertEquals( | |
171 "{" | |
172 + prefix + startTen + startTen + startTen + endTen + ";" | |
173 // Split across two lines | |
174 + prefix + endTen + endTen + ";" | |
175 + "}", | |
176 minify(block)); | |
177 } | |
178 } | |
OLD | NEW |