|
1 /* |
|
2 * Copyright (c) 2007 Henri Sivonen |
|
3 * Copyright (c) 2007-2011 Mozilla Foundation |
|
4 * |
|
5 * Permission is hereby granted, free of charge, to any person obtaining a |
|
6 * copy of this software and associated documentation files (the "Software"), |
|
7 * to deal in the Software without restriction, including without limitation |
|
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
|
9 * and/or sell copies of the Software, and to permit persons to whom the |
|
10 * Software is furnished to do so, subject to the following conditions: |
|
11 * |
|
12 * The above copyright notice and this permission notice shall be included in |
|
13 * all copies or substantial portions of the Software. |
|
14 * |
|
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
|
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
|
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
|
21 * DEALINGS IN THE SOFTWARE. |
|
22 */ |
|
23 |
|
24 package nu.validator.htmlparser.impl; |
|
25 |
|
26 import nu.validator.htmlparser.annotation.Inline; |
|
27 import nu.validator.htmlparser.annotation.Local; |
|
28 import nu.validator.htmlparser.annotation.NsUri; |
|
29 |
|
30 final class StackNode<T> { |
|
31 final int flags; |
|
32 |
|
33 final @Local String name; |
|
34 |
|
35 final @Local String popName; |
|
36 |
|
37 final @NsUri String ns; |
|
38 |
|
39 final T node; |
|
40 |
|
41 // Only used on the list of formatting elements |
|
42 HtmlAttributes attributes; |
|
43 |
|
44 private int refcount = 1; |
|
45 |
|
46 // [NOCPP[ |
|
47 |
|
48 private final TaintableLocatorImpl locator; |
|
49 |
|
50 public TaintableLocatorImpl getLocator() { |
|
51 return locator; |
|
52 } |
|
53 |
|
54 // ]NOCPP] |
|
55 |
|
56 @Inline public int getFlags() { |
|
57 return flags; |
|
58 } |
|
59 |
|
60 public int getGroup() { |
|
61 return flags & ElementName.GROUP_MASK; |
|
62 } |
|
63 |
|
64 public boolean isScoping() { |
|
65 return (flags & ElementName.SCOPING) != 0; |
|
66 } |
|
67 |
|
68 public boolean isSpecial() { |
|
69 return (flags & ElementName.SPECIAL) != 0; |
|
70 } |
|
71 |
|
72 public boolean isFosterParenting() { |
|
73 return (flags & ElementName.FOSTER_PARENTING) != 0; |
|
74 } |
|
75 |
|
76 public boolean isHtmlIntegrationPoint() { |
|
77 return (flags & ElementName.HTML_INTEGRATION_POINT) != 0; |
|
78 } |
|
79 |
|
80 // [NOCPP[ |
|
81 |
|
82 public boolean isOptionalEndTag() { |
|
83 return (flags & ElementName.OPTIONAL_END_TAG) != 0; |
|
84 } |
|
85 |
|
86 // ]NOCPP] |
|
87 |
|
88 /** |
|
89 * Constructor for copying. This doesn't take another <code>StackNode</code> |
|
90 * because in C++ the caller is reponsible for reobtaining the local names |
|
91 * from another interner. |
|
92 * |
|
93 * @param flags |
|
94 * @param ns |
|
95 * @param name |
|
96 * @param node |
|
97 * @param popName |
|
98 * @param attributes |
|
99 */ |
|
100 StackNode(int flags, @NsUri String ns, @Local String name, T node, |
|
101 @Local String popName, HtmlAttributes attributes |
|
102 // [NOCPP[ |
|
103 , TaintableLocatorImpl locator |
|
104 // ]NOCPP] |
|
105 ) { |
|
106 this.flags = flags; |
|
107 this.name = name; |
|
108 this.popName = popName; |
|
109 this.ns = ns; |
|
110 this.node = node; |
|
111 this.attributes = attributes; |
|
112 this.refcount = 1; |
|
113 // [NOCPP[ |
|
114 this.locator = locator; |
|
115 // ]NOCPP] |
|
116 } |
|
117 |
|
118 /** |
|
119 * Short hand for well-known HTML elements. |
|
120 * |
|
121 * @param elementName |
|
122 * @param node |
|
123 */ |
|
124 StackNode(ElementName elementName, T node |
|
125 // [NOCPP[ |
|
126 , TaintableLocatorImpl locator |
|
127 // ]NOCPP] |
|
128 ) { |
|
129 this.flags = elementName.getFlags(); |
|
130 this.name = elementName.name; |
|
131 this.popName = elementName.name; |
|
132 this.ns = "http://www.w3.org/1999/xhtml"; |
|
133 this.node = node; |
|
134 this.attributes = null; |
|
135 this.refcount = 1; |
|
136 assert !elementName.isCustom() : "Don't use this constructor for custom elements."; |
|
137 // [NOCPP[ |
|
138 this.locator = locator; |
|
139 // ]NOCPP] |
|
140 } |
|
141 |
|
142 /** |
|
143 * Constructor for HTML formatting elements. |
|
144 * |
|
145 * @param elementName |
|
146 * @param node |
|
147 * @param attributes |
|
148 */ |
|
149 StackNode(ElementName elementName, T node, HtmlAttributes attributes |
|
150 // [NOCPP[ |
|
151 , TaintableLocatorImpl locator |
|
152 // ]NOCPP] |
|
153 ) { |
|
154 this.flags = elementName.getFlags(); |
|
155 this.name = elementName.name; |
|
156 this.popName = elementName.name; |
|
157 this.ns = "http://www.w3.org/1999/xhtml"; |
|
158 this.node = node; |
|
159 this.attributes = attributes; |
|
160 this.refcount = 1; |
|
161 assert !elementName.isCustom() : "Don't use this constructor for custom elements."; |
|
162 // [NOCPP[ |
|
163 this.locator = locator; |
|
164 // ]NOCPP] |
|
165 } |
|
166 |
|
167 /** |
|
168 * The common-case HTML constructor. |
|
169 * |
|
170 * @param elementName |
|
171 * @param node |
|
172 * @param popName |
|
173 */ |
|
174 StackNode(ElementName elementName, T node, @Local String popName |
|
175 // [NOCPP[ |
|
176 , TaintableLocatorImpl locator |
|
177 // ]NOCPP] |
|
178 ) { |
|
179 this.flags = elementName.getFlags(); |
|
180 this.name = elementName.name; |
|
181 this.popName = popName; |
|
182 this.ns = "http://www.w3.org/1999/xhtml"; |
|
183 this.node = node; |
|
184 this.attributes = null; |
|
185 this.refcount = 1; |
|
186 // [NOCPP[ |
|
187 this.locator = locator; |
|
188 // ]NOCPP] |
|
189 } |
|
190 |
|
191 /** |
|
192 * Constructor for SVG elements. Note that the order of the arguments is |
|
193 * what distinguishes this from the HTML constructor. This is ugly, but |
|
194 * AFAICT the least disruptive way to make this work with Java's generics |
|
195 * and without unnecessary branches. :-( |
|
196 * |
|
197 * @param elementName |
|
198 * @param popName |
|
199 * @param node |
|
200 */ |
|
201 StackNode(ElementName elementName, @Local String popName, T node |
|
202 // [NOCPP[ |
|
203 , TaintableLocatorImpl locator |
|
204 // ]NOCPP] |
|
205 ) { |
|
206 this.flags = prepareSvgFlags(elementName.getFlags()); |
|
207 this.name = elementName.name; |
|
208 this.popName = popName; |
|
209 this.ns = "http://www.w3.org/2000/svg"; |
|
210 this.node = node; |
|
211 this.attributes = null; |
|
212 this.refcount = 1; |
|
213 // [NOCPP[ |
|
214 this.locator = locator; |
|
215 // ]NOCPP] |
|
216 } |
|
217 |
|
218 /** |
|
219 * Constructor for MathML. |
|
220 * |
|
221 * @param elementName |
|
222 * @param node |
|
223 * @param popName |
|
224 * @param markAsIntegrationPoint |
|
225 */ |
|
226 StackNode(ElementName elementName, T node, @Local String popName, |
|
227 boolean markAsIntegrationPoint |
|
228 // [NOCPP[ |
|
229 , TaintableLocatorImpl locator |
|
230 // ]NOCPP] |
|
231 ) { |
|
232 this.flags = prepareMathFlags(elementName.getFlags(), |
|
233 markAsIntegrationPoint); |
|
234 this.name = elementName.name; |
|
235 this.popName = popName; |
|
236 this.ns = "http://www.w3.org/1998/Math/MathML"; |
|
237 this.node = node; |
|
238 this.attributes = null; |
|
239 this.refcount = 1; |
|
240 // [NOCPP[ |
|
241 this.locator = locator; |
|
242 // ]NOCPP] |
|
243 } |
|
244 |
|
245 private static int prepareSvgFlags(int flags) { |
|
246 flags &= ~(ElementName.FOSTER_PARENTING | ElementName.SCOPING |
|
247 | ElementName.SPECIAL | ElementName.OPTIONAL_END_TAG); |
|
248 if ((flags & ElementName.SCOPING_AS_SVG) != 0) { |
|
249 flags |= (ElementName.SCOPING | ElementName.SPECIAL | ElementName.HTML_INTEGRATION_POINT); |
|
250 } |
|
251 return flags; |
|
252 } |
|
253 |
|
254 private static int prepareMathFlags(int flags, |
|
255 boolean markAsIntegrationPoint) { |
|
256 flags &= ~(ElementName.FOSTER_PARENTING | ElementName.SCOPING |
|
257 | ElementName.SPECIAL | ElementName.OPTIONAL_END_TAG); |
|
258 if ((flags & ElementName.SCOPING_AS_MATHML) != 0) { |
|
259 flags |= (ElementName.SCOPING | ElementName.SPECIAL); |
|
260 } |
|
261 if (markAsIntegrationPoint) { |
|
262 flags |= ElementName.HTML_INTEGRATION_POINT; |
|
263 } |
|
264 return flags; |
|
265 } |
|
266 |
|
267 @SuppressWarnings("unused") private void destructor() { |
|
268 Portability.delete(attributes); |
|
269 } |
|
270 |
|
271 public void dropAttributes() { |
|
272 attributes = null; |
|
273 } |
|
274 |
|
275 // [NOCPP[ |
|
276 /** |
|
277 * @see java.lang.Object#toString() |
|
278 */ |
|
279 @Override public @Local String toString() { |
|
280 return name; |
|
281 } |
|
282 |
|
283 // ]NOCPP] |
|
284 |
|
285 public void retain() { |
|
286 refcount++; |
|
287 } |
|
288 |
|
289 public void release() { |
|
290 refcount--; |
|
291 if (refcount == 0) { |
|
292 Portability.delete(this); |
|
293 } |
|
294 } |
|
295 } |