Chromium Code Reviews| 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.selection; | 5 library trydart.selection; |
| 6 | 6 |
| 7 import 'dart:html' show | 7 import 'dart:html' show |
| 8 CharacterData, | 8 CharacterData, |
| 9 Element, | |
| 9 Node, | 10 Node, |
| 10 NodeFilter, | 11 NodeFilter, |
| 11 Selection, | 12 Selection, |
| 12 Text, | 13 Text, |
| 13 TreeWalker; | 14 TreeWalker; |
| 14 | 15 |
| 16 import 'shadow_root.dart' show | |
| 17 WALKER_NEXT, | |
| 18 WALKER_RETURN, | |
| 19 walkNodes; | |
| 20 | |
| 15 import 'decoration.dart'; | 21 import 'decoration.dart'; |
| 16 | 22 |
| 17 class TrySelection { | 23 class TrySelection { |
| 18 final Node root; | 24 final Node root; |
| 19 Node anchorNode; | 25 Node anchorNode; |
| 20 int anchorOffset; | 26 int anchorOffset; |
| 21 | 27 |
| 22 String text; | 28 String text; |
| 23 int globalOffset = -1; | 29 int globalOffset = -1; |
| 24 | 30 |
| 25 TrySelection(this.root, Selection selection) | 31 TrySelection(this.root, Selection selection) |
| 26 : anchorNode = isCollapsed(selection) ? selection.anchorNode : null, | 32 : anchorNode = isCollapsed(selection) ? selection.anchorNode : null, |
| 27 anchorOffset = isCollapsed(selection) ? selection.anchorOffset : -1; | 33 anchorOffset = isCollapsed(selection) ? selection.anchorOffset : -1; |
| 28 | 34 |
| 29 TrySelection.empty(this.root) | 35 TrySelection.empty(this.root) |
| 30 : anchorNode = null, | 36 : anchorNode = null, |
| 31 anchorOffset = -1; | 37 anchorOffset = -1; |
| 32 | 38 |
| 33 Text addNodeFromSubstring(int start, | 39 Text addNodeFromSubstring(int start, |
| 34 int end, | 40 int end, |
| 35 List<Node> nodes, | 41 List<Node> nodes, |
| 36 [Decoration decoration]) { | 42 [Decoration decoration]) { |
| 37 if (start == end) return null; | 43 if (start == end) return null; |
|
ahe
2014/07/04 13:52:00
This check prevents adding empty Text nodes. I had
| |
| 38 | 44 |
| 39 Text textNode = new Text(text.substring(start, end)); | 45 Text textNode = new Text(text.substring(start, end)); |
| 40 | 46 |
| 41 if (start <= globalOffset && globalOffset <= end) { | 47 if (start <= globalOffset && globalOffset <= end) { |
| 42 anchorNode = textNode; | 48 anchorNode = textNode; |
| 43 anchorOffset = globalOffset - start; | 49 anchorOffset = globalOffset - start; |
| 44 } | 50 } |
| 45 | 51 |
| 46 nodes.add(decoration == null ? textNode : decoration.applyTo(textNode)); | 52 nodes.add(decoration == null ? textNode : decoration.applyTo(textNode)); |
| 47 | 53 |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 63 return new TrySelection.empty(root) | 69 return new TrySelection.empty(root) |
| 64 ..anchorNode = anchorNode | 70 ..anchorNode = anchorNode |
| 65 ..anchorOffset = anchorOffset; | 71 ..anchorOffset = anchorOffset; |
| 66 } | 72 } |
| 67 | 73 |
| 68 /// Computes the global offset, that is, the offset from [root]. | 74 /// Computes the global offset, that is, the offset from [root]. |
| 69 static int computeGlobalOffset(Node root, Node anchorNode, int anchorOffset) { | 75 static int computeGlobalOffset(Node root, Node anchorNode, int anchorOffset) { |
| 70 if (anchorOffset == -1) return -1; | 76 if (anchorOffset == -1) return -1; |
| 71 | 77 |
| 72 int offset = 0; | 78 int offset = 0; |
| 73 TreeWalker walker = new TreeWalker(root, NodeFilter.SHOW_TEXT); | 79 bool found = false; |
| 74 for (Node node = walker.nextNode(); | 80 walkNodes(root, (Node node) { |
| 75 node != null; | 81 if (anchorNode == node) { |
| 76 node = walker.nextNode()) { | 82 offset += anchorOffset; |
| 77 CharacterData text = node; | 83 found = true; |
| 78 if (anchorNode == text) { | 84 return WALKER_RETURN; |
| 79 return anchorOffset + offset; | |
| 80 } | 85 } |
| 81 offset += text.data.length; | 86 switch (node.nodeType) { |
| 82 } | 87 case Node.CDATA_SECTION_NODE: |
| 83 | 88 case Node.TEXT_NODE: |
| 84 return -1; | 89 CharacterData text = node; |
| 90 offset += text.data.length; | |
| 91 break; | |
| 92 } | |
| 93 return WALKER_NEXT; | |
| 94 }); | |
| 95 return found ? offset : -1; | |
| 85 } | 96 } |
| 86 } | 97 } |
| 87 | 98 |
| 88 bool isCollapsed(Selection selection) { | 99 bool isCollapsed(Selection selection) { |
| 89 // Firefox and Chrome don't agree on if the selection is collapsed if there | 100 // Firefox and Chrome don't agree on if the selection is collapsed if there |
| 90 // is no node selected. | 101 // is no node selected. |
| 91 return selection.isCollapsed && selection.anchorNode != null; | 102 return selection.isCollapsed && selection.anchorNode != null; |
| 92 } | 103 } |
| OLD | NEW |