Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(121)

Side by Side Diff: dart/site/try/src/selection.dart

Issue 345553008: Fix issues that broke editing on browsers without Shadow DOM support. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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_RETURN,
18 walkNodes;
19
15 import 'decoration.dart'; 20 import 'decoration.dart';
16 21
17 class TrySelection { 22 class TrySelection {
18 final Node root; 23 final Node root;
19 Node anchorNode; 24 Node anchorNode;
20 int anchorOffset; 25 int anchorOffset;
21 26
22 String text; 27 String text;
23 int globalOffset = -1; 28 int globalOffset = -1;
24 29
25 TrySelection(this.root, Selection selection) 30 TrySelection(this.root, Selection selection)
26 : anchorNode = isCollapsed(selection) ? selection.anchorNode : null, 31 : anchorNode = isCollapsed(selection) ? selection.anchorNode : null,
27 anchorOffset = isCollapsed(selection) ? selection.anchorOffset : -1; 32 anchorOffset = isCollapsed(selection) ? selection.anchorOffset : -1;
28 33
29 TrySelection.empty(this.root) 34 TrySelection.empty(this.root)
30 : anchorNode = null, 35 : anchorNode = null,
31 anchorOffset = -1; 36 anchorOffset = -1;
32 37
33 Text addNodeFromSubstring(int start, 38 Text addNodeFromSubstring(int start,
34 int end, 39 int end,
35 List<Node> nodes, 40 List<Node> nodes,
36 [Decoration decoration]) { 41 [Decoration decoration]) {
37 if (start == end) return null;
38
39 Text textNode = new Text(text.substring(start, end)); 42 Text textNode = new Text(text.substring(start, end));
40 43
41 if (start <= globalOffset && globalOffset <= end) { 44 if (start <= globalOffset && globalOffset <= end) {
42 anchorNode = textNode; 45 anchorNode = textNode;
43 anchorOffset = globalOffset - start; 46 anchorOffset = globalOffset - start;
44 } 47 }
45 48
46 nodes.add(decoration == null ? textNode : decoration.applyTo(textNode)); 49 nodes.add(decoration == null ? textNode : decoration.applyTo(textNode));
47 50
48 return textNode; 51 return textNode;
(...skipping 14 matching lines...) Expand all
63 return new TrySelection.empty(root) 66 return new TrySelection.empty(root)
64 ..anchorNode = anchorNode 67 ..anchorNode = anchorNode
65 ..anchorOffset = anchorOffset; 68 ..anchorOffset = anchorOffset;
66 } 69 }
67 70
68 /// Computes the global offset, that is, the offset from [root]. 71 /// Computes the global offset, that is, the offset from [root].
69 static int computeGlobalOffset(Node root, Node anchorNode, int anchorOffset) { 72 static int computeGlobalOffset(Node root, Node anchorNode, int anchorOffset) {
70 if (anchorOffset == -1) return -1; 73 if (anchorOffset == -1) return -1;
71 74
72 int offset = 0; 75 int offset = 0;
73 TreeWalker walker = new TreeWalker(root, NodeFilter.SHOW_TEXT); 76 bool found = false;
74 for (Node node = walker.nextNode(); 77 walkNodes(root, (Node node) {
75 node != null; 78 if (anchorNode == node) {
76 node = walker.nextNode()) { 79 offset += anchorOffset;
77 CharacterData text = node; 80 found = true;
78 if (anchorNode == text) { 81 return WALKER_RETURN;
ahe 2014/06/27 11:23:28 This assumed that the anchorNode would always be a
79 return anchorOffset + offset;
80 } 82 }
81 offset += text.data.length; 83 switch (node.nodeType) {
82 } 84 case Node.CDATA_SECTION_NODE:
83 85 case Node.TEXT_NODE:
84 return -1; 86 CharacterData text = node;
87 offset += text.data.length;
88 break;
89 }
90 });
91 return found ? offset : -1;
85 } 92 }
86 } 93 }
87 94
88 bool isCollapsed(Selection selection) { 95 bool isCollapsed(Selection selection) {
89 // Firefox and Chrome don't agree on if the selection is collapsed if there 96 // Firefox and Chrome don't agree on if the selection is collapsed if there
90 // is no node selected. 97 // is no node selected.
91 return selection.isCollapsed && selection.anchorNode != null; 98 return selection.isCollapsed && selection.anchorNode != null;
92 } 99 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698