Chromium Code Reviews| Index: dart/site/try/src/selection.dart |
| diff --git a/dart/site/try/src/selection.dart b/dart/site/try/src/selection.dart |
| index 641eb7434d4528d1ecd695f9696c5807d42656d1..e989d59b3caeaea340eff6aaa91e3bd02231846c 100644 |
| --- a/dart/site/try/src/selection.dart |
| +++ b/dart/site/try/src/selection.dart |
| @@ -6,12 +6,17 @@ library trydart.selection; |
| import 'dart:html' show |
| CharacterData, |
| + Element, |
| Node, |
| NodeFilter, |
| Selection, |
| Text, |
| TreeWalker; |
| +import 'shadow_root.dart' show |
| + WALKER_RETURN, |
| + walkNodes; |
| + |
| import 'decoration.dart'; |
| class TrySelection { |
| @@ -34,8 +39,6 @@ class TrySelection { |
| int end, |
| List<Node> nodes, |
| [Decoration decoration]) { |
| - if (start == end) return null; |
| - |
| Text textNode = new Text(text.substring(start, end)); |
| if (start <= globalOffset && globalOffset <= end) { |
| @@ -70,18 +73,22 @@ class TrySelection { |
| if (anchorOffset == -1) return -1; |
| int offset = 0; |
| - TreeWalker walker = new TreeWalker(root, NodeFilter.SHOW_TEXT); |
| - for (Node node = walker.nextNode(); |
| - node != null; |
| - node = walker.nextNode()) { |
| - CharacterData text = node; |
| - if (anchorNode == text) { |
|
ahe
2014/06/27 11:23:28
This assumed that the anchorNode would always be a
|
| - return anchorOffset + offset; |
| + bool found = false; |
| + walkNodes(root, (Node node) { |
| + if (anchorNode == node) { |
| + offset += anchorOffset; |
| + found = true; |
| + return WALKER_RETURN; |
| } |
| - offset += text.data.length; |
| - } |
| - |
| - return -1; |
| + switch (node.nodeType) { |
| + case Node.CDATA_SECTION_NODE: |
| + case Node.TEXT_NODE: |
| + CharacterData text = node; |
| + offset += text.data.length; |
| + break; |
| + } |
| + }); |
| + return found ? offset : -1; |
| } |
| } |