OLD | NEW |
---|---|
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 library trydart.shadow_root; | 5 library trydart.shadow_root; |
6 | 6 |
7 import 'dart:html'; | 7 import 'dart:html'; |
8 | 8 |
9 import 'selection.dart' show | 9 import 'selection.dart' show |
10 TrySelection; | 10 TrySelection; |
11 | 11 |
12 import 'html_to_text.dart' show | 12 import 'html_to_text.dart' show |
13 htmlToText; | 13 htmlToText; |
14 | 14 |
15 const int WALKER_NEXT = 0; | |
16 const int WALKER_RETURN = 1; | |
17 const int WALKER_SKIP_NODE = 2; | |
18 | |
15 void setShadowRoot(Element node, text) { | 19 void setShadowRoot(Element node, text) { |
16 if (text is String) { | 20 if (text is String) { |
17 text = new Text(text); | 21 text = new Text(text); |
18 } | 22 } |
19 getShadowRoot(node) | 23 getShadowRoot(node) |
20 ..nodes.clear() | 24 ..nodes.clear() |
21 ..append(text); | 25 ..append(text); |
22 } | 26 } |
23 | 27 |
24 | 28 |
(...skipping 21 matching lines...) Expand all Loading... | |
46 } | 50 } |
47 } | 51 } |
48 | 52 |
49 String getText(Element node) { | 53 String getText(Element node) { |
50 if (ShadowRoot.supported) return node.text; | 54 if (ShadowRoot.supported) return node.text; |
51 StringBuffer buffer = new StringBuffer(); | 55 StringBuffer buffer = new StringBuffer(); |
52 htmlToText( | 56 htmlToText( |
53 node, buffer, new TrySelection.empty(node), treatRootAsInline: true); | 57 node, buffer, new TrySelection.empty(node), treatRootAsInline: true); |
54 return '$buffer'; | 58 return '$buffer'; |
55 } | 59 } |
60 | |
61 /// Position [walker] at the last predecessor (that is, child of child of | |
62 /// child...) of [node]. The next call to walker.nextNode will return the first | |
63 /// node after [node]. | |
64 void skip(Node node, TreeWalker walker) { | |
65 if (walker.nextSibling() != null) { | |
66 walker.previousNode(); | |
67 return; | |
68 } | |
69 for (Node current = walker.nextNode(); | |
70 current != null; | |
71 current = walker.nextNode()) { | |
72 if (!node.contains(current)) { | |
73 walker.previousNode(); | |
74 return; | |
75 } | |
76 } | |
77 } | |
78 | |
79 /// Call [f] on each node in [root] in same order as [TreeWalker]. Skip any | |
80 /// nodes used to implement shadow root polyfill. | |
81 void walkNodes(Node root, int f(Node node)) { | |
82 TreeWalker walker = new TreeWalker(root, NodeFilter.SHOW_ALL); | |
83 | |
84 for (Node node = root; node != null; node = walker.nextNode()) { | |
85 if (!ShadowRoot.supported && | |
86 node is Element && | |
87 node.getAttribute('try-dart-shadow-root') != null) { | |
88 skip(node, walker); | |
89 } | |
90 int action = f(node); | |
91 switch (action) { | |
92 case WALKER_RETURN: | |
93 return; | |
94 case WALKER_SKIP_NODE: | |
95 skip(node, walker); | |
96 break; | |
97 case WALKER_NEXT: | |
98 break; | |
99 default: | |
100 throw 'Unexpected action returned from [f]: $action'; | |
ahe
2014/07/04 13:52:00
This revealed that I had forgotten a return statem
| |
101 } | |
102 } | |
103 } | |
OLD | NEW |