| 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 Node, | 9 Node, |
| 10 NodeFilter, | 10 NodeFilter, |
| 11 Selection, | 11 Selection, |
| 12 Text, | 12 Text, |
| 13 TreeWalker; | 13 TreeWalker; |
| 14 | 14 |
| 15 import 'decoration.dart'; | 15 import 'decoration.dart'; |
| 16 | 16 |
| 17 class TrySelection { | 17 class TrySelection { |
| 18 final Node root; | 18 final Node root; |
| 19 Node anchorNode; | 19 Node anchorNode; |
| 20 int anchorOffset; | 20 int anchorOffset; |
| 21 | 21 |
| 22 String text; | 22 String text; |
| 23 int globalOffset = -1; | 23 int globalOffset = -1; |
| 24 | 24 |
| 25 TrySelection(this.root, Selection selection) | 25 TrySelection(this.root, Selection selection) |
| 26 : this.anchorNode = selection.isCollapsed ? selection.anchorNode : null, | 26 : anchorNode = isCollapsed(selection) ? selection.anchorNode : null, |
| 27 this.anchorOffset = selection.isCollapsed ? selection.anchorOffset : -1; | 27 anchorOffset = isCollapsed(selection) ? selection.anchorOffset : -1; |
| 28 | 28 |
| 29 Text addNodeFromSubstring(int start, | 29 Text addNodeFromSubstring(int start, |
| 30 int end, | 30 int end, |
| 31 List<Node> nodes, | 31 List<Node> nodes, |
| 32 [Decoration decoration]) { | 32 [Decoration decoration]) { |
| 33 if (start == end) return null; | 33 if (start == end) return null; |
| 34 | 34 |
| 35 Text textNode = new Text(text.substring(start, end)); | 35 Text textNode = new Text(text.substring(start, end)); |
| 36 | 36 |
| 37 if (start <= globalOffset && globalOffset < end) { | 37 if (start <= globalOffset && globalOffset < end) { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 67 CharacterData text = node; | 67 CharacterData text = node; |
| 68 if (anchorNode == text) { | 68 if (anchorNode == text) { |
| 69 return anchorOffset + offset; | 69 return anchorOffset + offset; |
| 70 } | 70 } |
| 71 offset += text.data.length; | 71 offset += text.data.length; |
| 72 } | 72 } |
| 73 | 73 |
| 74 return -1; | 74 return -1; |
| 75 } | 75 } |
| 76 } | 76 } |
| 77 |
| 78 bool isCollapsed(Selection selection) { |
| 79 // Firefox and Chrome don't agree on if the selection is collapsed if there |
| 80 // is no node selected. |
| 81 return selection.isCollapsed && selection.anchorNode != null; |
| 82 } |
| OLD | NEW |