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 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 : this.anchorNode = isCollapsed(selection) ? selection.anchorNode : null, |
|
kasperl
2014/05/06 04:42:22
You could get rid of the 'this.' prefixes here (th
ahe
2014/05/06 08:10:16
Done.
| |
| 27 this.anchorOffset = selection.isCollapsed ? selection.anchorOffset : -1; | 27 this.anchorOffset = |
| 28 isCollapsed(selection) ? selection.anchorOffset : -1; | |
| 28 | 29 |
| 29 Text addNodeFromSubstring(int start, | 30 Text addNodeFromSubstring(int start, |
| 30 int end, | 31 int end, |
| 31 List<Node> nodes, | 32 List<Node> nodes, |
| 32 [Decoration decoration]) { | 33 [Decoration decoration]) { |
| 33 if (start == end) return null; | 34 if (start == end) return null; |
| 34 | 35 |
| 35 Text textNode = new Text(text.substring(start, end)); | 36 Text textNode = new Text(text.substring(start, end)); |
| 36 | 37 |
| 37 if (start <= globalOffset && globalOffset < end) { | 38 if (start <= globalOffset && globalOffset < end) { |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 67 CharacterData text = node; | 68 CharacterData text = node; |
| 68 if (anchorNode == text) { | 69 if (anchorNode == text) { |
| 69 return anchorOffset + offset; | 70 return anchorOffset + offset; |
| 70 } | 71 } |
| 71 offset += text.data.length; | 72 offset += text.data.length; |
| 72 } | 73 } |
| 73 | 74 |
| 74 return -1; | 75 return -1; |
| 75 } | 76 } |
| 76 } | 77 } |
| 78 | |
| 79 bool isCollapsed(Selection selection) { | |
| 80 // Firefox and Chrome don't agree on if the selection is collapsed if there | |
| 81 // is no node selected. | |
| 82 return selection.isCollapsed && selection.anchorNode != null; | |
| 83 } | |
| OLD | NEW |