OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 /** Common utility functions used by code generated by the dwc compiler. */ | 5 /** Common utility functions used by code generated by the dwc compiler. */ |
6 library templating; | 6 library templating; |
7 | 7 |
8 import 'dart:async'; | 8 import 'dart:async'; |
9 import 'dart:collection'; | 9 import 'dart:collection'; |
10 import 'dart:html'; | 10 import 'dart:html'; |
| 11 import 'dart:svg' as svg; |
11 import 'package:web_ui/safe_html.dart'; | 12 import 'package:web_ui/safe_html.dart'; |
12 import 'package:web_ui/observe.dart'; | 13 import 'package:web_ui/observe.dart'; |
13 import 'package:web_ui/watcher.dart'; | 14 import 'package:web_ui/watcher.dart'; |
14 import 'package:web_ui/web_ui.dart' show WebComponent; | 15 import 'package:web_ui/web_ui.dart' show WebComponent; |
15 | 16 |
16 /** | 17 /** |
17 * Take the value of a bound expression and creates an HTML node with its value. | 18 * Empty sanitizer used to create HTML content that was already sanitized during |
18 * Normally bindings are associated with text nodes, unless [binding] has the | 19 * compilation. |
19 * [SafeHtml] type, in which case an html element is created for it. | |
20 */ | 20 */ |
21 Node nodeForBinding(binding) => binding is SafeHtml | 21 NodeTreeSanitizer nullTreeSanitizer = new _NullTreeSanitizer(); |
22 ? new Element.html(binding.toString()) : new Text(binding.toString()); | 22 class _NullTreeSanitizer implements NodeTreeSanitizer { |
| 23 void sanitizeTree(Node node) {} |
| 24 } |
23 | 25 |
24 /** | 26 /** |
25 * Updates a data-bound [node] to a new [value]. If the new value is not | 27 * Helper function to create a tag without it's context. Used to workaround the |
26 * [SafeHtml] and the node is a [Text] node, then we update the node in place. | 28 * change that removed support for `new Element.html('<td attr=foo></td>')`. |
27 * Otherwise, the node is replaced in the DOM tree and the new node is returned. | 29 */ |
| 30 Node createSafeNode(String tag, Map<String, String> attributes, String body) { |
| 31 var node = new Element.tag(tag); |
| 32 for (var key in attributes.keys) { |
| 33 node.attributes[key] = attributes[key]; |
| 34 } |
| 35 if (body != '') node.setInnerHtml(body, treeSanitizer: nullTreeSanitizer); |
| 36 return node; |
| 37 } |
| 38 |
| 39 /** |
| 40 * Take the value of a bound expression and creates an HTML node with its value. |
| 41 * Normally bindings are associated with text nodes, unless [binding] is a |
| 42 * [Node] (in which case the Node it self is used as the binding) or has the |
| 43 * [SafeHtml] type (in which case an html element is created for it). |
| 44 */ |
| 45 Node nodeForBinding(binding) => binding is Node ? binding |
| 46 : (binding is SafeHtml ? new Element.html(binding.toString()) |
| 47 : new Text(binding.toString())); |
| 48 |
| 49 /** |
| 50 * Updates a data-bound [node] to a new [value]. If the new value is a [Node], |
| 51 * then the new node will replace the old node. If value is not [SafeHtml] and |
| 52 * the node is a [Text] node, we try to update the node in place. Otherwise, |
| 53 * the node is replaced in the DOM tree and the new node is returned. |
28 * [stringValue] should be equivalent to `value.toString()` and can be passed | 54 * [stringValue] should be equivalent to `value.toString()` and can be passed |
29 * here if it has already been computed. | 55 * here if it has already been computed. |
30 */ | 56 */ |
31 Node updateBinding(value, Node node, [String stringValue]) { | 57 Node updateBinding(value, Node node, [String stringValue]) { |
| 58 if (value is Node) { |
| 59 node.replaceWith(value); |
| 60 return value; |
| 61 } |
| 62 |
32 var isSafeHtml = value is SafeHtml; | 63 var isSafeHtml = value is SafeHtml; |
33 if (stringValue == null) { | 64 if (stringValue == null) { |
34 stringValue = value.toString(); | 65 stringValue = value.toString(); |
35 } | 66 } |
36 | 67 |
37 if (!isSafeHtml && node is Text) { | 68 if (!isSafeHtml && node is Text) { |
38 node.text = stringValue; | 69 node.text = stringValue; |
39 } else { | 70 } else { |
40 var old = node; | 71 var old = node; |
41 node = isSafeHtml ? new Element.html(stringValue) : new Text(stringValue); | 72 node = !isSafeHtml ? new Text(stringValue) |
| 73 : new Element.html(stringValue, treeSanitizer: nullTreeSanitizer); |
42 old.replaceWith(node); | 74 old.replaceWith(node); |
43 } | 75 } |
44 return node; | 76 return node; |
45 } | 77 } |
46 | 78 |
47 /** | 79 /** |
48 * Adds CSS [classes] if [addClasses] is true, otherwise removes them. | 80 * Adds CSS [classes] if [addClasses] is true, otherwise removes them. |
49 * This is useful to keep one or more CSS classes in sync with a boolean | 81 * This is useful to keep one or more CSS classes in sync with a boolean |
50 * property. | 82 * property. |
51 * | 83 * |
(...skipping 593 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
645 node.nodes.clear(); | 677 node.nodes.clear(); |
646 nodes.clear(); | 678 nodes.clear(); |
647 } | 679 } |
648 | 680 |
649 void remove() { | 681 void remove() { |
650 _removeInternal(); | 682 _removeInternal(); |
651 stopper(); | 683 stopper(); |
652 stopper = null; | 684 stopper = null; |
653 } | 685 } |
654 } | 686 } |
OLD | NEW |