OLD | NEW |
---|---|
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of dart.dom.html; | 5 part of dart.dom.html; |
6 | 6 |
7 | 7 |
8 /** | 8 /** |
9 * Interface used to validate that only accepted elements and attributes are | 9 * Interface used to validate that only accepted elements and attributes are |
10 * allowed while parsing HTML strings into DOM nodes. | 10 * allowed while parsing HTML strings into DOM nodes. |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
168 node.remove(); | 168 node.remove(); |
169 } else { | 169 } else { |
170 parent._removeChild(node); | 170 parent._removeChild(node); |
171 } | 171 } |
172 } | 172 } |
173 | 173 |
174 void sanitizeNode(Node node, Node parent) { | 174 void sanitizeNode(Node node, Node parent) { |
175 switch (node.nodeType) { | 175 switch (node.nodeType) { |
176 case Node.ELEMENT_NODE: | 176 case Node.ELEMENT_NODE: |
177 Element element = node; | 177 Element element = node; |
178 if (element._hasCorruptedAttributes) { | 178 // If the _hasCorruptedAttributes does not successfully return false, |
179 window.console.warn('Removing element due to corrupted attributes on < ${element}>'); | 179 // then we consider it corrupted and remove. |
180 // TODO(alanknight): This is a workaround because on Firefox | |
181 // embed/object | |
182 // tags typeof is "function", not "object". We don't recognize them, and | |
183 // can't call methods. This does mean that you can't explicitly allow an | |
184 // embed tag. The only thing that will let it through is a null | |
185 // sanitizer that doesn't traverse the tree at all. But sanitizing while | |
186 // allowing embeds seems quite unlikely. | |
187 var corrupted = true; | |
188 var attrs; | |
189 var isAttr; | |
190 try { | |
sra1
2015/05/19 19:54:38
Try to move try-catches to helper functions.
They
Alan Knight
2015/05/19 23:12:12
Split it out into two additional functions, one wh
| |
191 // If getting/indexing attributes throws, count that as corrupt. | |
192 attrs = element.attributes; | |
193 isAttr = attrs['is']; | |
194 corrupted = element._hasCorruptedAttributes; | |
sra1
2015/05/19 19:54:38
It seems dangerous to store this information of el
Alan Knight
2015/05/19 21:54:55
We're not storing it, it's a temp, and it's only u
sra1
2015/05/19 23:45:02
Sorry, I thought that _hasCorruptedAttributes was
| |
195 } catch(e) {} | |
196 var elementText = 'element unprintable'; | |
197 try { | |
198 elementText = element.toString(); | |
199 } catch(e) {} | |
200 var elementTagName = 'element tag unavailable'; | |
201 try { | |
202 elementTagName = element.tagName; | |
203 } catch(e) {} | |
204 if (corrupted) { | |
sra1
2015/05/19 19:54:38
corrupted could be anything from the assignment at
Alan Knight
2015/05/19 23:12:12
Done. But did it as (true = corrupted). I assume t
sra1
2015/05/19 23:45:01
Sorry, this again came from the misunderstanding t
| |
205 window.console.warn( | |
206 'Removing element due to corrupted attributes on <$elementText>'); | |
180 _removeNode(node, parent); | 207 _removeNode(node, parent); |
181 break; | 208 break; |
182 } | 209 } |
183 var attrs = element.attributes; | |
184 if (!validator.allowsElement(element)) { | 210 if (!validator.allowsElement(element)) { |
185 window.console.warn( | 211 window.console.warn( |
186 'Removing disallowed element <${element.tagName}>'); | 212 'Removing disallowed element <$elementTagName>'); |
187 _removeNode(node, parent); | 213 _removeNode(node, parent); |
188 break; | 214 break; |
189 } | 215 } |
190 | 216 |
191 var isAttr = attrs['is']; | |
192 if (isAttr != null) { | 217 if (isAttr != null) { |
193 if (!validator.allowsAttribute(element, 'is', isAttr)) { | 218 if (!validator.allowsAttribute(element, 'is', isAttr)) { |
194 window.console.warn('Removing disallowed type extension ' | 219 window.console.warn('Removing disallowed type extension ' |
195 '<${element.tagName} is="$isAttr">'); | 220 '<$elementTagName is="$isAttr">'); |
196 _removeNode(node, parent); | 221 _removeNode(node, parent); |
197 break; | 222 break; |
198 } | 223 } |
199 } | 224 } |
200 | 225 |
201 // TODO(blois): Need to be able to get all attributes, irrespective of | 226 // TODO(blois): Need to be able to get all attributes, irrespective of |
202 // XMLNS. | 227 // XMLNS. |
203 var keys = attrs.keys.toList(); | 228 var keys = attrs.keys.toList(); |
204 for (var i = attrs.length - 1; i >= 0; --i) { | 229 for (var i = attrs.length - 1; i >= 0; --i) { |
205 var name = keys[i]; | 230 var name = keys[i]; |
(...skipping 13 matching lines...) Expand all Loading... | |
219 case Node.COMMENT_NODE: | 244 case Node.COMMENT_NODE: |
220 case Node.DOCUMENT_FRAGMENT_NODE: | 245 case Node.DOCUMENT_FRAGMENT_NODE: |
221 case Node.TEXT_NODE: | 246 case Node.TEXT_NODE: |
222 case Node.CDATA_SECTION_NODE: | 247 case Node.CDATA_SECTION_NODE: |
223 break; | 248 break; |
224 default: | 249 default: |
225 _removeNode(node, parent); | 250 _removeNode(node, parent); |
226 } | 251 } |
227 } | 252 } |
228 } | 253 } |
OLD | NEW |