Index: dart/site/try/src/shadow_root.dart |
diff --git a/dart/site/try/src/shadow_root.dart b/dart/site/try/src/shadow_root.dart |
index b14afc6594e29495a7d56e8bf23ab8afc0d94368..b84b77eba2a70809b11c743b76cb5bc73c195059 100644 |
--- a/dart/site/try/src/shadow_root.dart |
+++ b/dart/site/try/src/shadow_root.dart |
@@ -12,6 +12,10 @@ import 'selection.dart' show |
import 'html_to_text.dart' show |
htmlToText; |
+const int WALKER_NEXT = 0; |
+const int WALKER_RETURN = 1; |
+const int WALKER_SKIP_NODE = 2; |
+ |
void setShadowRoot(Element node, text) { |
if (text is String) { |
text = new Text(text); |
@@ -53,3 +57,39 @@ String getText(Element node) { |
node, buffer, new TrySelection.empty(node), treatRootAsInline: true); |
return '$buffer'; |
} |
+ |
+/// Position [walker] at the last predecessor (that is, child of child of |
+/// child...) of [node]. The next call to walker.nextNode will return the first |
+/// node after [node]. |
+void skip(Node node, TreeWalker walker) { |
+ if (walker.nextSibling() != null) { |
+ walker.previousNode(); |
+ return; |
+ } |
+ for (Node current = walker.nextNode(); |
+ current != null; |
+ current = walker.nextNode()) { |
+ if (!node.contains(current)) { |
+ walker.previousNode(); |
+ return; |
+ } |
+ } |
+} |
+ |
+/// Call [f] on each node in [root] in same order as [TreeWalker]. Skip any |
+/// nodes used to implement shadow root polyfill. |
+void walkNodes(Node root, int f(Node node)) { |
+ TreeWalker walker = new TreeWalker(root, NodeFilter.SHOW_ALL); |
+ |
+ for (Node node = root; node != null; node = walker.nextNode()) { |
+ if (!ShadowRoot.supported && |
+ node is Element && |
+ node.getAttribute('try-dart-shadow-root') != null) { |
+ skip(node, walker); |
+ } |
+ int action = f(node); |
+ if (action == WALKER_RETURN) return; |
+ if (action == WALKER_SKIP_NODE) skip(node, walker); |
Johnni Winther
2014/07/02 08:40:54
'continue' after skip to avoid the assertion of `a
ahe
2014/07/04 13:52:00
I turned it into a switch.
|
+ assert(action == WALKER_NEXT); |
+ } |
+} |