LEFT | RIGHT |
1 // Copyright (C) 2008 Google Inc. | 1 // Copyright (C) 2008 Google Inc. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with 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 | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 /** | 46 /** |
47 * An HTML schema which defines attributes of elements and which elements are | 47 * An HTML schema which defines attributes of elements and which elements are |
48 * allowed. | 48 * allowed. |
49 * | 49 * |
50 * @author mikesamuel@gmail.com | 50 * @author mikesamuel@gmail.com |
51 */ | 51 */ |
52 public final class HtmlSchema { | 52 public final class HtmlSchema { |
53 private static final String VIRTUALIZATION_PREFIX = "caja-v-"; | 53 private static final String VIRTUALIZATION_PREFIX = "caja-v-"; |
54 private static final ElKey SCRIPT = ElKey.forHtmlElement("script"); | 54 private static final ElKey SCRIPT = ElKey.forHtmlElement("script"); |
55 private static final ElKey STYLE = ElKey.forHtmlElement("style"); | 55 private static final ElKey STYLE = ElKey.forHtmlElement("style"); |
56 private static final String DEFAULT_INTERFACE = "HTMLElement"; | 56 private static final String DEFAULT_SCHEMA_INTERFACE = "HTMLElement"; |
| 57 private static final String UNKNOWN_INTERFACE = "HTMLUnknownElement"; |
57 ·· | 58 ·· |
58 private final Set<ElKey> allowedElements; | 59 private final Set<ElKey> allowedElements; |
59 private final Map<ElKey, HTML.Element> elementDetails; | 60 private final Map<ElKey, HTML.Element> elementDetails; |
60 private final Set<AttribKey> allowedAttributes; | 61 private final Set<AttribKey> allowedAttributes; |
61 private final Map<AttribKey, HTML.Attribute> attributeDetails; | 62 private final Map<AttribKey, HTML.Attribute> attributeDetails; |
62 private final List<HTML.Attribute> attributesForUnknownHTMLElement; | 63 private final List<HTML.Attribute> attributesForUnknownHTMLElement; |
63 | 64 |
64 private static Pair<HtmlSchema, List<Message>> defaultSchema; | 65 private static Pair<HtmlSchema, List<Message>> defaultSchema; |
65 /** | 66 /** |
66 * The default HTML4 whitelist. See the JSON files in this directory for | 67 * The default HTML4 whitelist. See the JSON files in this directory for |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 present.add(a.getKey().onElement(wc)); | 211 present.add(a.getKey().onElement(wc)); |
211 } | 212 } |
212 for (HTML.Attribute a : general) { | 213 for (HTML.Attribute a : general) { |
213 if (!present.contains(a.getKey())) { attrs.add(a); } | 214 if (!present.contains(a.getKey())) { attrs.add(a); } |
214 } | 215 } |
215 } | 216 } |
216 boolean empty = (Boolean) def.get("empty", Boolean.FALSE); | 217 boolean empty = (Boolean) def.get("empty", Boolean.FALSE); |
217 boolean optionalEnd = (Boolean) def.get("optionalEnd", Boolean.FALSE); | 218 boolean optionalEnd = (Boolean) def.get("optionalEnd", Boolean.FALSE); |
218 boolean containsText = (Boolean) def.get("textContent", Boolean.TRUE) | 219 boolean containsText = (Boolean) def.get("textContent", Boolean.TRUE) |
219 && !empty; | 220 && !empty; |
220 String domInterface = (String) def.get("interface", DEFAULT_INTERFACE); | 221 String domInterface = (String) def.get("interface", |
| 222 DEFAULT_SCHEMA_INTERFACE); |
221 elementDetails.put( | 223 elementDetails.put( |
222 key, new HTML.Element(key, attrs, empty, optionalEnd, containsText, | 224 key, new HTML.Element(key, attrs, empty, optionalEnd, containsText, |
223 domInterface)); | 225 domInterface)); |
224 } | 226 } |
225 } | 227 } |
226 | 228 |
227 public Set<AttribKey> getAttributeNames() { | 229 public Set<AttribKey> getAttributeNames() { |
228 return attributeDetails.keySet(); | 230 return attributeDetails.keySet(); |
229 } | 231 } |
230 | 232 |
231 // TODO(kpreid): Virtualization makes this set arguably unbounded; review | 233 // TODO(kpreid): Virtualization makes this set arguably unbounded; review |
232 // whether any code is affected. | 234 // whether any code is affected. |
233 public Set<ElKey> getElementNames() { | 235 public Set<ElKey> getElementNames() { |
234 return elementDetails.keySet(); | 236 return elementDetails.keySet(); |
235 } | 237 } |
236 | 238 |
237 public boolean isElementAllowed(ElKey elementName) { | 239 public boolean isElementAllowed(ElKey elementName) { |
238 return allowedElements.contains(elementName) || | 240 return allowedElements.contains(elementName) || |
239 elementName.localName.startsWith(VIRTUALIZATION_PREFIX); | 241 elementName.localName.startsWith(VIRTUALIZATION_PREFIX); |
240 } | 242 } |
241 | 243 |
242 public HTML.Element lookupElement(ElKey elementName) { | 244 public HTML.Element lookupElement(ElKey elementName) { |
243 HTML.Element details = elementDetails.get(elementName); | 245 HTML.Element details = elementDetails.get(elementName); |
244 if (details != null) { | 246 if (details != null) { |
245 return details; | 247 return details; |
246 } else { | 248 } else { |
247 // May be a virtualized form of an unknown element, but we don't care -- | 249 // May be a virtualized form of an unknown element, but we don't care -- |
248 // just virtualize all non-global attributes on it. | 250 // just virtualize all non-global attributes on it. |
249 return new HTML.Element(elementName, attributesForUnknownHTMLElement, | 251 return new HTML.Element(elementName, attributesForUnknownHTMLElement, |
250 false, false, false, DEFAULT_INTERFACE); | 252 false, false, false, UNKNOWN_INTERFACE); |
251 } | 253 } |
252 } | 254 } |
253 | 255 |
254 /** | 256 /** |
255 * Elements that are to be rewritten with different tag names to avoid the | 257 * Elements that are to be rewritten with different tag names to avoid the |
256 * known or unknown browser semantics of them. | 258 * known or unknown browser semantics of them. |
257 */ | 259 */ |
258 public boolean isElementVirtualized(ElKey el) { | 260 public boolean isElementVirtualized(ElKey el) { |
259 // <script> and <style> are excluded because we want to rewrite them, | 261 // <script> and <style> are excluded because we want to rewrite them, |
260 // not virtualize them, but they are considered unsafe because their | 262 // not virtualize them, but they are considered unsafe because their |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
307 return a; | 309 return a; |
308 } | 310 } |
309 | 311 |
310 private static RegularCriterion conjunction( | 312 private static RegularCriterion conjunction( |
311 RegularCriterion a, RegularCriterion b) { | 313 RegularCriterion a, RegularCriterion b) { |
312 RegularCriterion c = a; | 314 RegularCriterion c = a; |
313 if (b != null) { c = c == null ? b : RegularCriterion.Factory.and(c, b); } | 315 if (b != null) { c = c == null ? b : RegularCriterion.Factory.and(c, b); } |
314 return c != null ? c : RegularCriterion.Factory.optimist(); | 316 return c != null ? c : RegularCriterion.Factory.optimist(); |
315 } | 317 } |
316 } | 318 } |
LEFT | RIGHT |