1 /* Functionality for finding, storing, and restoring selections
3 * This does not provide a generic API, just the minimal functionality
4 * required by the CodeMirror system.
11 select.ie_selection = document.selection && document.selection.createRangeCollection;
13 // Find the 'top-level' (defined as 'a direct child of the node
14 // passed as the top argument') node that the given node is
15 // contained in. Return null if the given node is not inside the top
17 function topLevelNodeAt(node, top) {
18 while (node && node.parentNode != top)
19 node = node.parentNode;
23 // Find the top-level node that contains the node before this one.
24 function topLevelNodeBefore(node, top) {
25 while (!node.previousSibling && node.parentNode != top)
26 node = node.parentNode;
27 return topLevelNodeAt(node.previousSibling, top);
30 var fourSpaces = "\u00a0\u00a0\u00a0\u00a0";
32 select.scrollToNode = function(element) {
34 var doc = element.ownerDocument, body = doc.body,
35 win = (doc.defaultView || doc.parentWindow),
36 html = doc.documentElement,
37 atEnd = !element.nextSibling || !element.nextSibling.nextSibling
38 || !element.nextSibling.nextSibling.nextSibling;
39 // In Opera (and recent Webkit versions), BR elements *always*
40 // have a scrollTop property of zero.
41 var compensateHack = 0;
42 while (element && !element.offsetTop) {
44 element = element.previousSibling;
46 // atEnd is another kludge for these browsers -- if the cursor is
47 // at the end of the document, and the node doesn't have an
48 // offset, just scroll to the end.
49 if (compensateHack == 0) atEnd = false;
51 var y = compensateHack * (element ? element.offsetHeight : 0), x = 0, pos = element;
52 while (pos && pos.offsetParent) {
54 // Don't count X offset for <br> nodes
55 if (pos.nodeName != "BR")
57 pos = pos.offsetParent;
60 var scroll_x = body.scrollLeft || html.scrollLeft || 0,
61 scroll_y = body.scrollTop || html.scrollTop || 0,
62 screen_x = x - scroll_x, screen_y = y - scroll_y, scroll = false;
64 if (screen_x < 0 || screen_x > (win.innerWidth || html.clientWidth || 0)) {
68 if (screen_y < 0 || atEnd || screen_y > (win.innerHeight || html.clientHeight || 0) - 50) {
69 scroll_y = atEnd ? 1e10 : y;
72 if (scroll) win.scrollTo(scroll_x, scroll_y);
75 select.scrollToCursor = function(container) {
76 select.scrollToNode(select.selectionTopNode(container, true) || container.firstChild);
79 // Used to prevent restoring a selection when we do not need to.
80 var currentSelection = null;
82 select.snapshotChanged = function() {
83 if (currentSelection) currentSelection.changed = true;
86 // This is called by the code in editor.js whenever it is replacing
87 // a text node. The function sees whether the given oldNode is part
88 // of the current selection, and updates this selection if it is.
89 // Because nodes are often only partially replaced, the length of
90 // the part that gets replaced has to be taken into account -- the
91 // selection might stay in the oldNode if the newNode is smaller
92 // than the selection's offset. The offset argument is needed in
93 // case the selection does move to the new object, and the given
94 // length is not the whole length of the new node (part of it might
95 // have been used to replace another node).
96 select.snapshotReplaceNode = function(from, to, length, offset) {
97 if (!currentSelection) return;
99 function replace(point) {
100 if (from == point.node) {
101 currentSelection.changed = true;
102 if (length && point.offset > length) {
103 point.offset -= length;
107 point.offset += (offset || 0);
111 replace(currentSelection.start);
112 replace(currentSelection.end);
115 select.snapshotMove = function(from, to, distance, relative, ifAtStart) {
116 if (!currentSelection) return;
118 function move(point) {
119 if (from == point.node && (!ifAtStart || point.offset == 0)) {
120 currentSelection.changed = true;
122 if (relative) point.offset = Math.max(0, point.offset + distance);
123 else point.offset = distance;
126 move(currentSelection.start);
127 move(currentSelection.end);
130 // Most functions are defined in two ways, one for the IE selection
131 // model, one for the W3C one.
132 if (select.ie_selection) {
133 function selectionNode(win, start) {
134 var range = win.document.selection.createRange();
135 range.collapse(start);
137 function nodeAfter(node) {
139 while (!found && node) {
140 found = node.nextSibling;
141 node = node.parentNode;
143 return nodeAtStartOf(found);
146 function nodeAtStartOf(node) {
147 while (node && node.firstChild) node = node.firstChild;
148 return {node: node, offset: 0};
151 var containing = range.parentElement();
152 if (!isAncestor(win.document.body, containing)) return null;
153 if (!containing.firstChild) return nodeAtStartOf(containing);
155 var working = range.duplicate();
156 working.moveToElementText(containing);
157 working.collapse(true);
158 for (var cur = containing.firstChild; cur; cur = cur.nextSibling) {
159 if (cur.nodeType == 3) {
160 var size = cur.nodeValue.length;
161 working.move("character", size);
164 working.moveToElementText(cur);
165 working.collapse(false);
168 var dir = range.compareEndPoints("StartToStart", working);
169 if (dir == 0) return nodeAfter(cur);
170 if (dir == 1) continue;
171 if (cur.nodeType != 3) return nodeAtStartOf(cur);
173 working.setEndPoint("StartToEnd", range);
174 return {node: cur, offset: size - working.text.length};
176 return nodeAfter(containing);
179 select.markSelection = function(win) {
180 currentSelection = null;
181 var sel = win.document.selection;
183 var start = selectionNode(win, true),
184 end = selectionNode(win, false);
185 if (!start || !end) return;
186 currentSelection = {start: start, end: end, window: win, changed: false};
189 select.selectMarked = function() {
190 if (!currentSelection || !currentSelection.changed) return;
191 var win = currentSelection.window, doc = win.document;
193 function makeRange(point) {
194 var range = doc.body.createTextRange(),
197 range.moveToElementText(currentSelection.window.document.body);
198 range.collapse(false);
200 else if (node.nodeType == 3) {
201 range.moveToElementText(node.parentNode);
202 var offset = point.offset;
203 while (node.previousSibling) {
204 node = node.previousSibling;
205 offset += (node.innerText || "").length;
207 range.move("character", offset);
210 range.moveToElementText(node);
211 range.collapse(true);
216 var start = makeRange(currentSelection.start), end = makeRange(currentSelection.end);
217 start.setEndPoint("StartToEnd", end);
221 // Get the top-level node that one end of the cursor is inside or
222 // after. Note that this returns false for 'no cursor', and null
223 // for 'start of document'.
224 select.selectionTopNode = function(container, start) {
225 var selection = container.ownerDocument.selection;
226 if (!selection) return false;
228 var range = selection.createRange(), range2 = range.duplicate();
229 range.collapse(start);
230 var around = range.parentElement();
231 if (around && isAncestor(container, around)) {
232 // Only use this node if the selection is not at its start.
233 range2.moveToElementText(around);
234 if (range.compareEndPoints("StartToStart", range2) == 1)
235 return topLevelNodeAt(around, container);
238 // Move the start of a range to the start of a node,
239 // compensating for the fact that you can't call
240 // moveToElementText with text nodes.
241 function moveToNodeStart(range, node) {
242 if (node.nodeType == 3) {
243 var count = 0, cur = node.previousSibling;
244 while (cur && cur.nodeType == 3) {
245 count += cur.nodeValue.length;
246 cur = cur.previousSibling;
249 try{range.moveToElementText(cur);}
250 catch(e){alert(cur + " " + cur.nodeType + " " + (cur && cur.outerHTML));}
251 range.collapse(false);
253 else range.moveToElementText(node.parentNode);
254 if (count) range.move("character", count);
256 else try{range.moveToElementText(node);} catch(e) {};
259 // Do a binary search through the container object, comparing
260 // the start of each node to the selection
261 var start = 0, end = container.childNodes.length;
262 while (start != end) {
263 var middle = Math.ceil((end + start) / 2), node = container.childNodes[middle];
264 if (!node) return false; // Don't ask. IE6 manages this sometimes.
265 moveToNodeStart(range2, node);
266 if (range.compareEndPoints("StartToStart", range2) == 1)
271 return container.childNodes[start] || null;
274 // Place the cursor after this.start. This is only useful when
275 // manually moving the cursor instead of restoring it to its old
277 select.focusAfterNode = function(node, container) {
278 var range = container.ownerDocument.body.createTextRange();
279 range.moveToElementText(node || container);
280 range.collapse(!node);
284 select.somethingSelected = function(win) {
285 var sel = win.document.selection;
286 return sel && (sel.createRange().text != "");
289 function insertAtCursor(window, html) {
290 var selection = window.document.selection;
292 var range = selection.createRange();
293 range.pasteHTML(html);
294 range.collapse(false);
299 // Used to normalize the effect of the enter key, since browsers
300 // do widely different things when pressing enter in designMode.
301 select.insertNewlineAtCursor = function(window) {
302 insertAtCursor(window, "<br>");
305 select.insertTabAtCursor = function(window) {
306 insertAtCursor(window, fourSpaces);
309 // Get the BR node at the start of the line on which the cursor
310 // currently is, and the offset into the line. Returns null as
311 // node if cursor is on first line.
312 select.cursorPos = function(container, start) {
313 var selection = container.ownerDocument.selection;
314 if (!selection) return null;
316 var topNode = select.selectionTopNode(container, start);
317 while (topNode && topNode.nodeName != "BR")
318 topNode = topNode.previousSibling;
320 var range = selection.createRange(), range2 = range.duplicate();
321 range.collapse(start);
323 range2.moveToElementText(topNode);
324 range2.collapse(false);
327 // When nothing is selected, we can get all kinds of funky errors here.
328 try { range2.moveToElementText(container); }
329 catch (e) { return null; }
330 range2.collapse(true);
332 range.setEndPoint("StartToStart", range2);
334 return {node: topNode, offset: range.text.length};
337 select.setCursorPos = function(container, from, to) {
338 function rangeAt(pos) {
339 var range = container.ownerDocument.body.createTextRange();
341 range.moveToElementText(container);
342 range.collapse(true);
345 range.moveToElementText(pos.node);
346 range.collapse(false);
348 range.move("character", pos.offset);
352 var range = rangeAt(from);
353 if (to && to != from)
354 range.setEndPoint("EndToEnd", rangeAt(to));
358 // Some hacks for storing and re-storing the selection when the editor loses and regains focus.
359 select.selectionCoords = function (win) {
360 var selection = win.document.selection;
361 if (!selection) return null;
362 var start = selection.createRange(), end = start.duplicate();
363 start.collapse(true);
366 var body = win.document.body;
367 return {start: {x: start.boundingLeft + body.scrollLeft - 1,
368 y: start.boundingTop + body.scrollTop},
369 end: {x: end.boundingLeft + body.scrollLeft - 1,
370 y: end.boundingTop + body.scrollTop}};
373 // Restore a stored selection.
374 select.selectCoords = function(win, coords) {
377 var range1 = win.document.body.createTextRange(), range2 = range1.duplicate();
378 // This can fail for various hard-to-handle reasons.
380 range1.moveToPoint(coords.start.x, coords.start.y);
381 range2.moveToPoint(coords.end.x, coords.end.y);
382 range1.setEndPoint("EndToStart", range2);
389 // Store start and end nodes, and offsets within these, and refer
390 // back to the selection object from those nodes, so that this
391 // object can be updated when the nodes are replaced before the
392 // selection is restored.
393 select.markSelection = function (win) {
394 var selection = win.getSelection();
395 if (!selection || selection.rangeCount == 0)
396 return (currentSelection = null);
397 var range = selection.getRangeAt(0);
400 start: {node: range.startContainer, offset: range.startOffset},
401 end: {node: range.endContainer, offset: range.endOffset},
406 // We want the nodes right at the cursor, not one of their
407 // ancestors with a suitable offset. This goes down the DOM tree
408 // until a 'leaf' is reached (or is it *up* the DOM tree?).
409 function normalize(point){
410 while (point.node.nodeType != 3 && point.node.nodeName != "BR") {
411 var newNode = point.node.childNodes[point.offset] || point.node.nextSibling;
413 while (!newNode && point.node.parentNode) {
414 point.node = point.node.parentNode;
415 newNode = point.node.nextSibling;
417 point.node = newNode;
423 normalize(currentSelection.start);
424 normalize(currentSelection.end);
427 select.selectMarked = function () {
428 if (!currentSelection || !currentSelection.changed) return;
429 var win = currentSelection.window, range = win.document.createRange();
431 function setPoint(point, which) {
433 // Some magic to generalize the setting of the start and end
435 if (point.offset == 0)
436 range["set" + which + "Before"](point.node);
438 range["set" + which](point.node, point.offset);
441 range.setStartAfter(win.document.body.lastChild || win.document.body);
445 setPoint(currentSelection.end, "End");
446 setPoint(currentSelection.start, "Start");
447 selectRange(range, win);
450 // Helper for selecting a range object.
451 function selectRange(range, window) {
452 var selection = window.getSelection();
453 selection.removeAllRanges();
454 selection.addRange(range);
456 function selectionRange(window) {
457 var selection = window.getSelection();
458 if (!selection || selection.rangeCount == 0)
461 return selection.getRangeAt(0);
464 // Finding the top-level node at the cursor in the W3C is, as you
465 // can see, quite an involved process.
466 select.selectionTopNode = function(container, start) {
467 var range = selectionRange(container.ownerDocument.defaultView);
468 if (!range) return false;
470 var node = start ? range.startContainer : range.endContainer;
471 var offset = start ? range.startOffset : range.endOffset;
472 // Work around (yet another) bug in Opera's selection model.
473 if (window.opera && !start && range.endContainer == container && range.endOffset == range.startOffset + 1 &&
474 container.childNodes[range.startOffset] && container.childNodes[range.startOffset].nodeName == "BR")
477 // For text nodes, we look at the node itself if the cursor is
478 // inside, or at the node before it if the cursor is at the
480 if (node.nodeType == 3){
482 return topLevelNodeAt(node, container);
484 return topLevelNodeBefore(node, container);
486 // Occasionally, browsers will return the HTML node as
487 // selection. If the offset is 0, we take the start of the frame
488 // ('after null'), otherwise, we take the last node.
489 else if (node.nodeName == "HTML") {
490 return (offset == 1 ? null : container.lastChild);
492 // If the given node is our 'container', we just look up the
493 // correct node by using the offset.
494 else if (node == container) {
495 return (offset == 0) ? null : node.childNodes[offset - 1];
497 // In any other case, we have a regular node. If the cursor is
498 // at the end of the node, we use the node itself, if it is at
499 // the start, we use the node before it, and in any other
500 // case, we look up the child before the cursor and use that.
502 if (offset == node.childNodes.length)
503 return topLevelNodeAt(node, container);
504 else if (offset == 0)
505 return topLevelNodeBefore(node, container);
507 return topLevelNodeAt(node.childNodes[offset - 1], container);
511 select.focusAfterNode = function(node, container) {
512 var win = container.ownerDocument.defaultView,
513 range = win.document.createRange();
514 range.setStartBefore(container.firstChild || container);
515 // In Opera, setting the end of a range at the end of a line
516 // (before a BR) will cause the cursor to appear on the next
517 // line, so we set the end inside of the start node when
519 if (node && !node.firstChild)
520 range.setEndAfter(node);
522 range.setEnd(node, node.childNodes.length);
524 range.setEndBefore(container.firstChild || container);
525 range.collapse(false);
526 selectRange(range, win);
529 select.somethingSelected = function(win) {
530 var range = selectionRange(win);
531 return range && !range.collapsed;
534 function insertNodeAtCursor(window, node) {
535 var range = selectionRange(window);
538 range.deleteContents();
539 range.insertNode(node);
540 webkitLastLineHack(window.document.body);
541 range = window.document.createRange();
542 range.selectNode(node);
543 range.collapse(false);
544 selectRange(range, window);
547 select.insertNewlineAtCursor = function(window) {
548 insertNodeAtCursor(window, window.document.createElement("BR"));
551 select.insertTabAtCursor = function(window) {
552 insertNodeAtCursor(window, window.document.createTextNode(fourSpaces));
555 select.cursorPos = function(container, start) {
556 var range = selectionRange(window);
559 var topNode = select.selectionTopNode(container, start);
560 while (topNode && topNode.nodeName != "BR")
561 topNode = topNode.previousSibling;
563 range = range.cloneRange();
564 range.collapse(start);
566 range.setStartAfter(topNode);
568 range.setStartBefore(container);
569 return {node: topNode, offset: range.toString().length};
572 select.setCursorPos = function(container, from, to) {
573 var win = container.ownerDocument.defaultView,
574 range = win.document.createRange();
576 function setPoint(node, offset, side) {
578 node = container.firstChild;
580 node = node.nextSibling;
586 range["set" + side + "Before"](node);
591 function decompose(node) {
592 if (node.nodeType == 3)
595 forEach(node.childNodes, decompose);
598 while (node && !backlog.length) {
600 node = node.nextSibling;
602 var cur = backlog.shift();
603 if (!cur) return false;
605 var length = cur.nodeValue.length;
606 if (length >= offset) {
607 range["set" + side](cur, offset);
615 if (setPoint(to.node, to.offset, "End") && setPoint(from.node, from.offset, "Start"))
616 selectRange(range, win);