1 /* The Editor object manages the content of the editable frame. It
2 * catches events, colours nodes, and indents lines. This file also
3 * holds some functions for transforming arbitrary DOM structures into
4 * plain sequences of <span> and <br> elements
7 var internetExplorer = document.selection && window.ActiveXObject && /MSIE/.test(navigator.userAgent);
8 var webkit = /AppleWebKit/.test(navigator.userAgent);
9 var safari = /Apple Computers, Inc/.test(navigator.vendor);
10 var gecko = /gecko\/(\d{8})/i.test(navigator.userAgent);
11 // TODO this is related to the backspace-at-end-of-line bug. Remove
12 // this if Opera gets their act together, make the version check more
13 // broad if they don't.
14 var brokenOpera = window.opera && /Version\/10.[56]/.test(navigator.userAgent);
16 // Make sure a string does not contain two consecutive 'collapseable'
17 // whitespace characters.
18 function makeWhiteSpace(n) {
19 var buffer = [], nb = true;
21 buffer.push((nb || n == 1) ? nbsp : " ");
24 return buffer.join("");
27 // Create a set of white-space characters that will not be collapsed
28 // by the browser, but will not break text-wrapping either.
29 function fixSpaces(string) {
30 if (string.charAt(0) == " ") string = nbsp + string.slice(1);
31 return string.replace(/\t/g, function() {return makeWhiteSpace(indentUnit);})
32 .replace(/[ \u00a0]{2,}/g, function(s) {return makeWhiteSpace(s.length);});
35 function cleanText(text) {
36 return text.replace(/\u00a0/g, " ");
39 // Create a SPAN node with the expected properties for document part
41 function makePartSpan(value, doc) {
43 if (value.nodeType == 3) text = value.nodeValue;
44 else value = doc.createTextNode(text);
46 var span = doc.createElement("SPAN");
48 span.appendChild(value);
49 span.currentText = text;
53 var Editor = (function(){
54 // The HTML elements whose content should be suffixed by a newline
55 // when converting them to flat text.
56 var newlineElements = {"P": true, "DIV": true, "LI": true};
58 function asEditorLines(string) {
59 var tab = makeWhiteSpace(indentUnit);
60 return map(string.replace(/\t/g, tab).replace(/\u00a0/g, " ").replace(/\r\n?/g, "\n").split("\n"), fixSpaces);
63 // Helper function for traverseDOM. Flattens an arbitrary DOM node
64 // into an array of textnodes and <br> tags.
65 function simplifyDOM(root, atEnd) {
66 var doc = root.ownerDocument;
70 function simplifyNode(node, top) {
71 if (node.nodeType == 3) {
72 var text = node.nodeValue = fixSpaces(node.nodeValue.replace(/\r/g, "").replace(/\n/g, " "));
73 if (text.length) leaving = false;
76 else if (isBR(node) && node.childNodes.length == 0) {
81 for (var n = node.firstChild; n; n = n.nextSibling) simplifyNode(n);
82 if (!leaving && newlineElements.hasOwnProperty(node.nodeName.toUpperCase())) {
85 result.push(doc.createElement("BR"));
90 simplifyNode(root, true);
94 // Creates a MochiKit-style iterator that goes over a series of DOM
95 // nodes. The values it yields are strings, the textual content of
96 // the nodes. It makes sure that all nodes up to and including the
97 // one whose text is being yielded have been 'normalized' to be just
98 // <span> and <br> elements.
99 function traverseDOM(start){
100 var owner = start.ownerDocument;
103 // Create a function that can be used to insert nodes after the
104 // one given as argument.
105 function pointAt(node){
106 var parent = node.parentNode;
107 var next = node.nextSibling;
108 return function(newnode) {
109 parent.insertBefore(newnode, next);
114 // This an Opera-specific hack -- always insert an empty span
115 // between two BRs, because Opera's cursor code gets terribly
116 // confused when the cursor is between two BRs.
119 // Insert a normalized node at the current point. If it is a text
120 // node, wrap it in a <span>, and give that span a currentText
121 // property -- this is used to cache the nodeValue, because
122 // directly accessing nodeValue is horribly slow on some browsers.
123 // The dirty property is used by the highlighter to determine
124 // which parts of the document have to be re-highlighted.
125 function insertPart(part){
127 if (part.nodeType == 3) {
128 select.snapshotChanged();
129 part = makePartSpan(part, owner);
130 text = part.currentText;
134 if (afterBR && window.opera)
135 point(makePartSpan("", owner));
139 nodeQueue.push(part);
144 // Extract the text and newlines from a DOM node, insert them into
145 // the document, and return the textual content. Used to replace
146 // non-normalized nodes.
147 function writeNode(node, end) {
148 var simplified = simplifyDOM(node, end);
149 for (var i = 0; i < simplified.length; i++)
150 simplified[i] = insertPart(simplified[i]);
151 return simplified.join("");
154 // Check whether a node is a normalized <span> element.
155 function partNode(node){
156 if (node.isPart && node.childNodes.length == 1 && node.firstChild.nodeType == 3) {
157 node.currentText = node.firstChild.nodeValue;
158 return !/[\n\t\r]/.test(node.currentText);
163 // Advance to next node, return string for current node.
165 if (!start) throw StopIteration;
167 start = node.nextSibling;
170 nodeQueue.push(node);
172 return node.currentText;
174 else if (isBR(node)) {
175 if (afterBR && window.opera)
176 node.parentNode.insertBefore(makePartSpan("", owner), node);
177 nodeQueue.push(node);
182 var end = !node.nextSibling;
183 point = pointAt(node);
185 return writeNode(node, end);
189 // MochiKit iterators are objects with a next function that
190 // returns the next value or throws StopIteration when there are
192 return {next: next, nodes: nodeQueue};
195 // Determine the text size of a processed node.
196 function nodeSize(node) {
197 return isBR(node) ? 1 : node.currentText.length;
200 // Search backwards through the top-level nodes until the next BR or
201 // the start of the frame.
202 function startOfLine(node) {
203 while (node && !isBR(node)) node = node.previousSibling;
206 function endOfLine(node, container) {
207 if (!node) node = container.firstChild;
208 else if (isBR(node)) node = node.nextSibling;
210 while (node && !isBR(node)) node = node.nextSibling;
214 function time() {return new Date().getTime();}
216 // Client interface for searching the content of the editor. Create
217 // these by calling CodeMirror.getSearchCursor. To use, call
218 // findNext on the resulting object -- this returns a boolean
219 // indicating whether anything was found, and can be called again to
220 // skip to the next find. Use the select and replace methods to
221 // actually do something with the found locations.
222 function SearchCursor(editor, string, fromCursor, caseFold) {
223 this.editor = editor;
224 if (caseFold == undefined) {
225 caseFold = (string == string.toLowerCase());
227 this.caseFold = caseFold;
228 if (caseFold) string = string.toLowerCase();
229 this.history = editor.history;
230 this.history.commit();
232 // Are we currently at an occurrence of the search string?
233 this.atOccurrence = false;
234 // The object stores a set of nodes coming after its current
235 // position, so that when the current point is taken out of the
236 // DOM tree, we can still try to continue.
237 this.fallbackSize = 15;
239 // Start from the cursor when specified and a cursor can be found.
240 if (fromCursor && (cursor = select.cursorPos(this.editor.container))) {
241 this.line = cursor.node;
242 this.offset = cursor.offset;
248 this.valid = !!string;
250 // Create a matcher function based on the kind of string we have.
251 var target = string.split("\n"), self = this;
252 this.matches = (target.length == 1) ?
253 // For one-line strings, searching can be done simply by calling
254 // indexOf on the current line.
256 var line = cleanText(self.history.textAfter(self.line).slice(self.offset));
257 var match = (self.caseFold ? line.toLowerCase() : line).indexOf(string);
259 return {from: {node: self.line, offset: self.offset + match},
260 to: {node: self.line, offset: self.offset + match + string.length}};
262 // Multi-line strings require internal iteration over lines, and
263 // some clunky checks to make sure the first match ends at the
264 // end of the line and the last match starts at the start.
266 var firstLine = cleanText(self.history.textAfter(self.line).slice(self.offset));
267 var match = (self.caseFold ? firstLine.toLowerCase() : firstLine).lastIndexOf(target[0]);
268 if (match == -1 || match != firstLine.length - target[0].length)
270 var startOffset = self.offset + match;
272 var line = self.history.nodeAfter(self.line);
273 for (var i = 1; i < target.length - 1; i++) {
274 var lineText = cleanText(self.history.textAfter(line));
275 if ((self.caseFold ? lineText.toLowerCase() : lineText) != target[i])
277 line = self.history.nodeAfter(line);
280 var lastLine = cleanText(self.history.textAfter(line));
281 if ((self.caseFold ? lastLine.toLowerCase() : lastLine).indexOf(target[target.length - 1]) != 0)
284 return {from: {node: self.line, offset: startOffset},
285 to: {node: line, offset: target[target.length - 1].length}};
289 SearchCursor.prototype = {
290 findNext: function() {
291 if (!this.valid) return false;
292 this.atOccurrence = false;
295 // Go back to the start of the document if the current line is
296 // no longer in the DOM tree.
297 if (this.line && !this.line.parentNode) {
302 // Set the cursor's position one character after the given
304 function saveAfter(pos) {
305 if (self.history.textAfter(pos.node).length > pos.offset) {
306 self.line = pos.node;
307 self.offset = pos.offset + 1;
310 self.line = self.history.nodeAfter(pos.node);
316 var match = this.matches();
317 // Found the search string.
319 this.atOccurrence = match;
320 saveAfter(match.from);
323 this.line = this.history.nodeAfter(this.line);
334 if (this.atOccurrence) {
335 select.setCursorPos(this.editor.container, this.atOccurrence.from, this.atOccurrence.to);
336 select.scrollToCursor(this.editor.container);
340 replace: function(string) {
341 if (this.atOccurrence) {
342 var end = this.editor.replaceRange(this.atOccurrence.from, this.atOccurrence.to, string);
343 this.line = end.node;
344 this.offset = end.offset;
345 this.atOccurrence = false;
350 // The Editor object is the main inside-the-iframe interface.
351 function Editor(options) {
352 this.options = options;
353 window.indentUnit = options.indentUnit;
354 this.parent = parent;
356 var container = this.container = this.doc.body;
358 this.history = new UndoHistory(container, options.undoDepth, options.undoDelay, this);
362 throw "No parser loaded.";
363 if (options.parserConfig && Editor.Parser.configure)
364 Editor.Parser.configure(options.parserConfig);
366 if (!options.readOnly)
367 select.setCursorPos(container, {node: null, offset: 0});
370 this.importCode(options.content || "");
371 this.history.onChange = options.onChange;
373 if (!options.readOnly) {
374 if (options.continuousScanning !== false) {
375 this.scanner = this.documentScanner(options.passTime);
376 this.delayScanning();
379 function setEditable() {
380 // Use contentEditable instead of designMode on IE, since designMode frames
381 // can not run any scripts. It would be nice if we could use contentEditable
382 // everywhere, but it is significantly flakier than designMode on every
383 // single non-IE browser.
384 if (document.body.contentEditable != undefined && internetExplorer)
385 document.body.contentEditable = "true";
387 document.designMode = "on";
389 document.documentElement.style.borderWidth = "0";
390 if (!options.textWrapping)
391 container.style.whiteSpace = "nowrap";
394 // If setting the frame editable fails, try again when the user
395 // focus it (happens when the frame is not visible on
396 // initialisation, in Firefox).
401 var focusEvent = addEventHandler(document, "focus", function() {
407 addEventHandler(document, "keydown", method(this, "keyDown"));
408 addEventHandler(document, "keypress", method(this, "keyPress"));
409 addEventHandler(document, "keyup", method(this, "keyUp"));
411 function cursorActivity() {self.cursorActivity(false);}
412 addEventHandler(document.body, "mouseup", cursorActivity);
413 addEventHandler(document.body, "cut", cursorActivity);
415 // workaround for a gecko bug [?] where going forward and then
416 // back again breaks designmode (no more cursor)
418 addEventHandler(this.win, "pagehide", function(){self.unloaded = true;});
420 addEventHandler(document.body, "paste", function(event) {
424 var clipboardData = event.clipboardData || window.clipboardData;
425 if (clipboardData) text = clipboardData.getData('Text');
430 self.replaceSelection(text);
431 select.scrollToCursor(self.container);
435 if (this.options.autoMatchParens)
436 addEventHandler(document.body, "click", method(this, "scheduleParenHighlight"));
438 else if (!options.textWrapping) {
439 container.style.whiteSpace = "nowrap";
443 function isSafeKey(code) {
444 return (code >= 16 && code <= 18) || // shift, control, alt
445 (code >= 33 && code <= 40); // arrows, home, end
449 // Import a piece of code into the editor.
450 importCode: function(code) {
451 this.history.push(null, null, asEditorLines(code));
452 this.history.reset();
455 // Extract the code from the editor.
456 getCode: function() {
457 if (!this.container.firstChild)
461 select.markSelection(this.win);
462 forEach(traverseDOM(this.container.firstChild), method(accum, "push"));
463 select.selectMarked();
464 return cleanText(accum.join(""));
467 checkLine: function(node) {
468 if (node === false || !(node == null || node.parentNode == this.container))
469 throw parent.CodeMirror.InvalidLineHandle;
472 cursorPosition: function(start) {
473 if (start == null) start = true;
474 var pos = select.cursorPos(this.container, start);
475 if (pos) return {line: pos.node, character: pos.offset};
476 else return {line: null, character: 0};
479 firstLine: function() {
483 lastLine: function() {
484 if (this.container.lastChild) return startOfLine(this.container.lastChild);
488 nextLine: function(line) {
489 this.checkLine(line);
490 var end = endOfLine(line, this.container);
494 prevLine: function(line) {
495 this.checkLine(line);
496 if (line == null) return false;
497 return startOfLine(line.previousSibling);
500 visibleLineCount: function() {
501 var line = this.container.firstChild;
502 while (line && isBR(line)) line = line.nextSibling; // BR heights are unreliable
503 if (!line) return false;
504 var innerHeight = (window.innerHeight
505 || document.documentElement.clientHeight
506 || document.body.clientHeight);
507 return Math.floor(innerHeight / line.offsetHeight);
510 selectLines: function(startLine, startOffset, endLine, endOffset) {
511 this.checkLine(startLine);
512 var start = {node: startLine, offset: startOffset}, end = null;
513 if (endOffset !== undefined) {
514 this.checkLine(endLine);
515 end = {node: endLine, offset: endOffset};
517 select.setCursorPos(this.container, start, end);
518 select.scrollToCursor(this.container);
521 lineContent: function(line) {
523 for (line = line ? line.nextSibling : this.container.firstChild;
524 line && !isBR(line); line = line.nextSibling)
525 accum.push(nodeText(line));
526 return cleanText(accum.join(""));
529 setLineContent: function(line, content) {
530 this.history.commit();
531 this.replaceRange({node: line, offset: 0},
532 {node: line, offset: this.history.textAfter(line).length},
534 this.addDirtyNode(line);
535 this.scheduleHighlight();
538 removeLine: function(line) {
539 var node = line ? line.nextSibling : this.container.firstChild;
541 var next = node.nextSibling;
543 if (isBR(node)) break;
546 this.addDirtyNode(line);
547 this.scheduleHighlight();
550 insertIntoLine: function(line, position, content) {
552 if (position == "end") {
553 before = endOfLine(line, this.container);
556 for (var cur = line ? line.nextSibling : this.container.firstChild; cur; cur = cur.nextSibling) {
561 var text = nodeText(cur);
562 if (text.length > position) {
563 before = cur.nextSibling;
564 content = text.slice(0, position) + content + text.slice(position);
568 position -= text.length;
572 var lines = asEditorLines(content), doc = this.container.ownerDocument;
573 for (var i = 0; i < lines.length; i++) {
574 if (i > 0) this.container.insertBefore(doc.createElement("BR"), before);
575 this.container.insertBefore(makePartSpan(lines[i], doc), before);
577 this.addDirtyNode(line);
578 this.scheduleHighlight();
581 // Retrieve the selected text.
582 selectedText: function() {
583 var h = this.history;
586 var start = select.cursorPos(this.container, true),
587 end = select.cursorPos(this.container, false);
588 if (!start || !end) return "";
590 if (start.node == end.node)
591 return h.textAfter(start.node).slice(start.offset, end.offset);
593 var text = [h.textAfter(start.node).slice(start.offset)];
594 for (var pos = h.nodeAfter(start.node); pos != end.node; pos = h.nodeAfter(pos))
595 text.push(h.textAfter(pos));
596 text.push(h.textAfter(end.node).slice(0, end.offset));
597 return cleanText(text.join("\n"));
600 // Replace the selection with another piece of text.
601 replaceSelection: function(text) {
602 this.history.commit();
604 var start = select.cursorPos(this.container, true),
605 end = select.cursorPos(this.container, false);
606 if (!start || !end) return;
608 end = this.replaceRange(start, end, text);
609 select.setCursorPos(this.container, end);
612 cursorCoords: function(start) {
613 var sel = select.cursorPos(this.container, start);
614 if (!sel) return null;
615 var off = sel.offset, node = sel.node, doc = this.win.document, self = this;
616 function measureFromNode(node, xOffset) {
617 var y = -(self.win.document.body.scrollTop || self.win.document.documentElement.scrollTop || 0),
618 x = -(self.win.document.body.scrollLeft || self.win.document.documentElement.scrollLeft || 0) + xOffset;
619 forEach([node, self.win.frameElement], function(n) {
620 while (n) {x += n.offsetLeft; y += n.offsetTop;n = n.offsetParent;}
622 return {x: x, y: y, yBot: y + node.offsetHeight};
624 function withTempNode(text, f) {
625 var node = doc.createElement("SPAN");
626 node.appendChild(doc.createTextNode(text));
627 try {return f(node);}
628 finally {if (node.parentNode) node.parentNode.removeChild(node);}
632 node = node ? node.nextSibling : this.container.firstChild;
633 var txt = nodeText(node);
634 if (off < txt.length)
635 return withTempNode(txt.substr(0, off), function(tmp) {
636 tmp.style.position = "absolute"; tmp.style.visibility = "hidden";
637 tmp.className = node.className;
638 self.container.appendChild(tmp);
639 return measureFromNode(node, tmp.offsetWidth);
643 if (node && isSpan(node))
644 return measureFromNode(node, node.offsetWidth);
645 else if (node && node.nextSibling && isSpan(node.nextSibling))
646 return measureFromNode(node.nextSibling, 0);
648 return withTempNode("\u200b", function(tmp) {
649 if (node) node.parentNode.insertBefore(tmp, node.nextSibling);
650 else self.container.insertBefore(tmp, self.container.firstChild);
651 return measureFromNode(tmp, 0);
655 reroutePasteEvent: function() {
656 if (this.capturingPaste || window.opera) return;
657 this.capturingPaste = true;
658 var te = window.frameElement.CodeMirror.textareaHack;
664 this.parent.setTimeout(function() {
665 self.capturingPaste = false;
667 if (self.selectionSnapshot) // IE hack
668 self.win.select.setBookmark(self.container, self.selectionSnapshot);
671 self.replaceSelection(text);
672 select.scrollToCursor(self.container);
677 replaceRange: function(from, to, text) {
678 var lines = asEditorLines(text);
679 lines[0] = this.history.textAfter(from.node).slice(0, from.offset) + lines[0];
680 var lastLine = lines[lines.length - 1];
681 lines[lines.length - 1] = lastLine + this.history.textAfter(to.node).slice(to.offset);
682 var end = this.history.nodeAfter(to.node);
683 this.history.push(from.node, end, lines);
684 return {node: this.history.nodeBefore(end),
685 offset: lastLine.length};
688 getSearchCursor: function(string, fromCursor, caseFold) {
689 return new SearchCursor(this, string, fromCursor, caseFold);
692 // Re-indent the whole buffer
693 reindent: function() {
694 if (this.container.firstChild)
695 this.indentRegion(null, this.container.lastChild);
698 reindentSelection: function(direction) {
699 if (!select.somethingSelected(this.win)) {
700 this.indentAtCursor(direction);
703 var start = select.selectionTopNode(this.container, true),
704 end = select.selectionTopNode(this.container, false);
705 if (start === false || end === false) return;
706 this.indentRegion(start, end, direction);
710 grabKeys: function(eventHandler, filter) {
711 this.frozen = eventHandler;
712 this.keyFilter = filter;
714 ungrabKeys: function() {
715 this.frozen = "leave";
718 setParser: function(name, parserConfig) {
719 Editor.Parser = window[name];
720 parserConfig = parserConfig || this.options.parserConfig;
721 if (parserConfig && Editor.Parser.configure)
722 Editor.Parser.configure(parserConfig);
724 if (this.container.firstChild) {
725 forEach(this.container.childNodes, function(n) {
726 if (n.nodeType != 3) n.dirty = true;
728 this.addDirtyNode(this.firstChild);
729 this.scheduleHighlight();
733 // Intercept enter and tab, and assign their new functions.
734 keyDown: function(event) {
735 if (this.frozen == "leave") {this.frozen = null; this.keyFilter = null;}
736 if (this.frozen && (!this.keyFilter || this.keyFilter(event.keyCode, event))) {
742 var code = this.lastKeyDownCode = event.keyCode;
743 // Don't scan when the user is typing.
744 this.delayScanning();
745 // Schedule a paren-highlight event, if configured.
746 if (this.options.autoMatchParens)
747 this.scheduleParenHighlight();
749 // The various checks for !altKey are there because AltGr sets both
750 // ctrlKey and altKey to true, and should not be recognised as
752 if (code == 13) { // enter
753 if (event.ctrlKey && !event.altKey) {
754 this.reparseBuffer();
757 select.insertNewlineAtCursor(this.win);
758 this.indentAtCursor();
759 select.scrollToCursor(this.container);
763 else if (code == 9 && this.options.tabMode != "default" && !event.ctrlKey) { // tab
764 this.handleTab(!event.shiftKey);
767 else if (code == 32 && event.shiftKey && this.options.tabMode == "default") { // space
768 this.handleTab(true);
771 else if (code == 36 && !event.shiftKey && !event.ctrlKey) { // home
772 if (this.home()) event.stop();
774 else if (code == 35 && !event.shiftKey && !event.ctrlKey) { // end
775 if (this.end()) event.stop();
777 // Only in Firefox is the default behavior for PgUp/PgDn correct.
778 else if (code == 33 && !event.shiftKey && !event.ctrlKey && !gecko) { // PgUp
779 if (this.pageUp()) event.stop();
781 else if (code == 34 && !event.shiftKey && !event.ctrlKey && !gecko) { // PgDn
782 if (this.pageDown()) event.stop();
784 else if ((code == 219 || code == 221) && event.ctrlKey && !event.altKey) { // [, ]
785 this.highlightParens(event.shiftKey, true);
788 else if (event.metaKey && !event.shiftKey && (code == 37 || code == 39)) { // Meta-left/right
789 var cursor = select.selectionTopNode(this.container);
790 if (cursor !== false && this.container.firstChild) {
791 if (code == 37) select.focusAfterNode(startOfLine(cursor), this.container);
793 var end = endOfLine(cursor, this.container);
794 select.focusAfterNode(end ? end.previousSibling : this.container.lastChild, this.container);
799 else if ((event.ctrlKey || event.metaKey) && !event.altKey) {
800 if ((event.shiftKey && code == 90) || code == 89) { // shift-Z, Y
801 select.scrollToNode(this.history.redo());
804 else if (code == 90 || (safari && code == 8)) { // Z, backspace
805 select.scrollToNode(this.history.undo());
808 else if (code == 83 && this.options.saveFunction) { // S
809 this.options.saveFunction();
812 else if (internetExplorer && code == 86) {
813 this.reroutePasteEvent();
816 this.keyUpOrPressAfterLastKeyDown = false;
819 // Check for characters that should re-indent the current line,
820 // and prevent Opera from handling enter and tab anyway.
821 keyPress: function(event) {
822 this.keyUpOrPressAfterLastKeyDown = true;
823 var electric = Editor.Parser.electricChars, self = this;
824 // Hack for Opera, and Firefox on OS X, in which stopping a
825 // keydown event does not prevent the associated keypress event
826 // from happening, so we have to cancel enter and tab again
828 if ((this.frozen && (!this.keyFilter || this.keyFilter(event.keyCode || event.code, event))) ||
829 event.code == 13 || (event.code == 9 && this.options.tabMode != "default") ||
830 (event.code == 32 && event.shiftKey && this.options.tabMode == "default"))
832 else if (electric && electric.indexOf(event.character) != -1)
833 this.parent.setTimeout(function(){self.indentAtCursor(null);}, 0);
834 else if ((event.character == "v" || event.character == "V")
835 && (event.ctrlKey || event.metaKey) && !event.altKey) // ctrl-V
836 this.reroutePasteEvent();
837 // Work around a bug where pressing backspace at the end of a
838 // line often causes the cursor to jump to the start of the line
840 else if (brokenOpera && event.code == 8) {
841 var sel = select.selectionTopNode(this.container), self = this,
842 next = sel ? sel.nextSibling : this.container.firstChild;
843 if (sel !== false && next && isBR(next))
844 this.parent.setTimeout(function(){
845 if (select.selectionTopNode(self.container) == next)
846 select.focusAfterNode(next.previousSibling, self.container);
851 // Mark the node at the cursor dirty when a non-safe key is
853 keyUp: function(event) {
854 this.keyUpOrPressAfterLastKeyDown = true;
855 this.cursorActivity(isSafeKey(event.keyCode));
858 // Indent the line following a given <br>, or null for the first
859 // line. If given a <br> element, this must have been highlighted
860 // so that it has an indentation method. Returns the whitespace
861 // element that has been modified or created (if any).
862 indentLineAfter: function(start, direction) {
863 // whiteSpace is the whitespace span at the start of the line,
864 // or null if there is no such node.
865 var whiteSpace = start ? start.nextSibling : this.container.firstChild;
866 if (whiteSpace && !hasClass(whiteSpace, "whitespace"))
869 // Sometimes the start of the line can influence the correct
870 // indentation, so we retrieve it.
871 var firstText = whiteSpace ? whiteSpace.nextSibling : (start ? start.nextSibling : this.container.firstChild);
872 var nextChars = (start && firstText && firstText.currentText) ? firstText.currentText : "";
874 // Ask the lexical context for the correct indentation, and
875 // compute how much this differs from the current indentation.
876 var newIndent = 0, curIndent = whiteSpace ? whiteSpace.currentText.length : 0;
877 if (direction != null && this.options.tabMode == "shift")
878 newIndent = direction ? curIndent + indentUnit : Math.max(0, curIndent - indentUnit)
880 newIndent = start.indentation(nextChars, curIndent, direction);
881 else if (Editor.Parser.firstIndentation)
882 newIndent = Editor.Parser.firstIndentation(nextChars, curIndent, direction);
883 var indentDiff = newIndent - curIndent;
885 // If there is too much, this is just a matter of shrinking a span.
886 if (indentDiff < 0) {
887 if (newIndent == 0) {
888 if (firstText) select.snapshotMove(whiteSpace.firstChild, firstText.firstChild, 0);
889 removeElement(whiteSpace);
893 select.snapshotMove(whiteSpace.firstChild, whiteSpace.firstChild, indentDiff, true);
894 whiteSpace.currentText = makeWhiteSpace(newIndent);
895 whiteSpace.firstChild.nodeValue = whiteSpace.currentText;
899 else if (indentDiff > 0) {
900 // If there is whitespace, we grow it.
902 whiteSpace.currentText = makeWhiteSpace(newIndent);
903 whiteSpace.firstChild.nodeValue = whiteSpace.currentText;
904 select.snapshotMove(whiteSpace.firstChild, whiteSpace.firstChild, indentDiff, true);
906 // Otherwise, we have to add a new whitespace node.
908 whiteSpace = makePartSpan(makeWhiteSpace(newIndent), this.doc);
909 whiteSpace.className = "whitespace";
910 if (start) insertAfter(whiteSpace, start);
911 else this.container.insertBefore(whiteSpace, this.container.firstChild);
912 select.snapshotMove(firstText && (firstText.firstChild || firstText),
913 whiteSpace.firstChild, newIndent, false, true);
916 if (indentDiff != 0) this.addDirtyNode(start);
919 // Re-highlight the selected part of the document.
920 highlightAtCursor: function() {
921 var pos = select.selectionTopNode(this.container, true);
922 var to = select.selectionTopNode(this.container, false);
923 if (pos === false || to === false) return false;
925 select.markSelection(this.win);
926 if (this.highlight(pos, endOfLine(to, this.container), true, 20) === false)
928 select.selectMarked();
932 // When tab is pressed with text selected, the whole selection is
933 // re-indented, when nothing is selected, the line with the cursor
935 handleTab: function(direction) {
936 if (this.options.tabMode == "spaces")
937 select.insertTabAtCursor(this.win);
939 this.reindentSelection(direction);
942 // Custom home behaviour that doesn't land the cursor in front of
943 // leading whitespace unless pressed twice.
945 var cur = select.selectionTopNode(this.container, true), start = cur;
946 if (cur === false || !(!cur || cur.isPart || isBR(cur)) || !this.container.firstChild)
949 while (cur && !isBR(cur)) cur = cur.previousSibling;
950 var next = cur ? cur.nextSibling : this.container.firstChild;
951 if (next && next != start && next.isPart && hasClass(next, "whitespace"))
952 select.focusAfterNode(next, this.container);
954 select.focusAfterNode(cur, this.container);
956 select.scrollToCursor(this.container);
960 // Some browsers (Opera) don't manage to handle the end key
961 // properly in the face of vertical scrolling.
963 var cur = select.selectionTopNode(this.container, true);
964 if (cur === false) return false;
965 cur = endOfLine(cur, this.container);
966 if (!cur) return false;
967 select.focusAfterNode(cur.previousSibling, this.container);
968 select.scrollToCursor(this.container);
973 var line = this.cursorPosition().line, scrollAmount = this.visibleLineCount();
974 if (line === false || scrollAmount === false) return false;
975 // Try to keep one line on the screen.
977 for (var i = 0; i < scrollAmount; i++) {
978 line = this.prevLine(line);
979 if (line === false) break;
981 if (i == 0) return false; // Already at first line
982 select.setCursorPos(this.container, {node: line, offset: 0});
983 select.scrollToCursor(this.container);
987 pageDown: function() {
988 var line = this.cursorPosition().line, scrollAmount = this.visibleLineCount();
989 if (line === false || scrollAmount === false) return false;
990 // Try to move to the last line of the current page.
992 for (var i = 0; i < scrollAmount; i++) {
993 var nextLine = this.nextLine(line);
994 if (nextLine === false) break;
997 if (i == 0) return false; // Already at last line
998 select.setCursorPos(this.container, {node: line, offset: 0});
999 select.scrollToCursor(this.container);
1003 // Delay (or initiate) the next paren highlight event.
1004 scheduleParenHighlight: function() {
1005 if (this.parenEvent) this.parent.clearTimeout(this.parenEvent);
1007 this.parenEvent = this.parent.setTimeout(function(){self.highlightParens();}, 300);
1010 // Take the token before the cursor. If it contains a character in
1011 // '()[]{}', search for the matching paren/brace/bracket, and
1012 // highlight them in green for a moment, or red if no proper match
1014 highlightParens: function(jump, fromKey) {
1016 // give the relevant nodes a colour.
1017 function highlight(node, ok) {
1019 if (self.options.markParen) {
1020 self.options.markParen(node, ok);
1023 node.style.fontWeight = "bold";
1024 node.style.color = ok ? "#8F8" : "#F88";
1027 function unhighlight(node) {
1029 if (self.options.unmarkParen) {
1030 self.options.unmarkParen(node);
1033 node.style.fontWeight = "";
1034 node.style.color = "";
1037 if (!fromKey && self.highlighted) {
1038 unhighlight(self.highlighted[0]);
1039 unhighlight(self.highlighted[1]);
1042 if (!window.select) return;
1043 // Clear the event property.
1044 if (this.parenEvent) this.parent.clearTimeout(this.parenEvent);
1045 this.parenEvent = null;
1047 // Extract a 'paren' from a piece of text.
1048 function paren(node) {
1049 if (node.currentText) {
1050 var match = node.currentText.match(/^[\s\u00a0]*([\(\)\[\]{}])[\s\u00a0]*$/);
1051 return match && match[1];
1054 // Determine the direction a paren is facing.
1055 function forward(ch) {
1056 return /[\(\[\{]/.test(ch);
1059 var ch, cursor = select.selectionTopNode(this.container, true);
1060 if (!cursor || !this.highlightAtCursor()) return;
1061 cursor = select.selectionTopNode(this.container, true);
1062 if (!(cursor && ((ch = paren(cursor)) || (cursor = cursor.nextSibling) && (ch = paren(cursor)))))
1064 // We only look for tokens with the same className.
1065 var className = cursor.className, dir = forward(ch), match = matching[ch];
1067 // Since parts of the document might not have been properly
1068 // highlighted, and it is hard to know in advance which part we
1069 // have to scan, we just try, and when we find dirty nodes we
1070 // abort, parse them, and re-try.
1071 function tryFindMatch() {
1072 var stack = [], ch, ok = true;
1073 for (var runner = cursor; runner; runner = dir ? runner.nextSibling : runner.previousSibling) {
1074 if (runner.className == className && isSpan(runner) && (ch = paren(runner))) {
1075 if (forward(ch) == dir)
1077 else if (!stack.length)
1079 else if (stack.pop() != matching[ch])
1081 if (!stack.length) break;
1083 else if (runner.dirty || !isSpan(runner) && !isBR(runner)) {
1084 return {node: runner, status: "dirty"};
1087 return {node: runner, status: runner && ok};
1091 var found = tryFindMatch();
1092 if (found.status == "dirty") {
1093 this.highlight(found.node, endOfLine(found.node));
1094 // Needed because in some corner cases a highlight does not
1096 found.node.dirty = false;
1100 highlight(cursor, found.status);
1101 highlight(found.node, found.status);
1103 self.parent.setTimeout(function() {unhighlight(cursor); unhighlight(found.node);}, 500);
1105 self.highlighted = [cursor, found.node];
1106 if (jump && found.node)
1107 select.focusAfterNode(found.node.previousSibling, this.container);
1113 // Adjust the amount of whitespace at the start of the line that
1114 // the cursor is on so that it is indented properly.
1115 indentAtCursor: function(direction) {
1116 if (!this.container.firstChild) return;
1117 // The line has to have up-to-date lexical information, so we
1118 // highlight it first.
1119 if (!this.highlightAtCursor()) return;
1120 var cursor = select.selectionTopNode(this.container, false);
1121 // If we couldn't determine the place of the cursor,
1122 // there's nothing to indent.
1123 if (cursor === false)
1125 select.markSelection(this.win);
1126 this.indentLineAfter(startOfLine(cursor), direction);
1127 select.selectMarked();
1130 // Indent all lines whose start falls inside of the current
1132 indentRegion: function(start, end, direction) {
1133 var current = (start = startOfLine(start)), before = start && startOfLine(start.previousSibling);
1134 if (!isBR(end)) end = endOfLine(end, this.container);
1135 this.addDirtyNode(start);
1138 var next = endOfLine(current, this.container);
1139 if (current) this.highlight(before, next, true);
1140 this.indentLineAfter(current, direction);
1143 } while (current != end);
1144 select.setCursorPos(this.container, {node: start, offset: 0}, {node: end, offset: 0});
1147 // Find the node that the cursor is in, mark it as dirty, and make
1148 // sure a highlight pass is scheduled.
1149 cursorActivity: function(safe) {
1150 // pagehide event hack above
1151 if (this.unloaded) {
1152 this.win.document.designMode = "off";
1153 this.win.document.designMode = "on";
1154 this.unloaded = false;
1157 if (internetExplorer) {
1158 this.container.createTextRange().execCommand("unlink");
1159 this.selectionSnapshot = select.getBookmark(this.container);
1162 var activity = this.options.cursorActivity;
1163 if (!safe || activity) {
1164 var cursor = select.selectionTopNode(this.container, false);
1165 if (cursor === false || !this.container.firstChild) return;
1166 cursor = cursor || this.container.firstChild;
1167 if (activity) activity(cursor);
1169 this.scheduleHighlight();
1170 this.addDirtyNode(cursor);
1175 reparseBuffer: function() {
1176 forEach(this.container.childNodes, function(node) {node.dirty = true;});
1177 if (this.container.firstChild)
1178 this.addDirtyNode(this.container.firstChild);
1181 // Add a node to the set of dirty nodes, if it isn't already in
1183 addDirtyNode: function(node) {
1184 node = node || this.container.firstChild;
1187 for (var i = 0; i < this.dirty.length; i++)
1188 if (this.dirty[i] == node) return;
1190 if (node.nodeType != 3)
1192 this.dirty.push(node);
1195 allClean: function() {
1196 return !this.dirty.length;
1199 // Cause a highlight pass to happen in options.passDelay
1200 // milliseconds. Clear the existing timeout, if one exists. This
1201 // way, the passes do not happen while the user is typing, and
1202 // should as unobtrusive as possible.
1203 scheduleHighlight: function() {
1204 // Timeouts are routed through the parent window, because on
1205 // some browsers designMode windows do not fire timeouts.
1207 this.parent.clearTimeout(this.highlightTimeout);
1208 this.highlightTimeout = this.parent.setTimeout(function(){self.highlightDirty();}, this.options.passDelay);
1211 // Fetch one dirty node, and remove it from the dirty set.
1212 getDirtyNode: function() {
1213 while (this.dirty.length > 0) {
1214 var found = this.dirty.pop();
1215 // IE8 sometimes throws an unexplainable 'invalid argument'
1216 // exception for found.parentNode
1218 // If the node has been coloured in the meantime, or is no
1219 // longer in the document, it should not be returned.
1220 while (found && found.parentNode != this.container)
1221 found = found.parentNode;
1222 if (found && (found.dirty || found.nodeType == 3))
1229 // Pick dirty nodes, and highlight them, until options.passTime
1230 // milliseconds have gone by. The highlight method will continue
1231 // to next lines as long as it finds dirty nodes. It returns
1232 // information about the place where it stopped. If there are
1233 // dirty nodes left after this function has spent all its lines,
1234 // it shedules another highlight to finish the job.
1235 highlightDirty: function(force) {
1236 // Prevent FF from raising an error when it is firing timeouts
1237 // on a page that's no longer loaded.
1238 if (!window.select) return false;
1240 if (!this.options.readOnly) select.markSelection(this.win);
1241 var start, endTime = force ? null : time() + this.options.passTime;
1242 while ((time() < endTime || force) && (start = this.getDirtyNode())) {
1243 var result = this.highlight(start, endTime);
1244 if (result && result.node && result.dirty)
1245 this.addDirtyNode(result.node);
1247 if (!this.options.readOnly) select.selectMarked();
1248 if (start) this.scheduleHighlight();
1249 return this.dirty.length == 0;
1252 // Creates a function that, when called through a timeout, will
1253 // continuously re-parse the document.
1254 documentScanner: function(passTime) {
1255 var self = this, pos = null;
1257 // FF timeout weirdness workaround.
1258 if (!window.select) return;
1259 // If the current node is no longer in the document... oh
1260 // well, we start over.
1261 if (pos && pos.parentNode != self.container)
1263 select.markSelection(self.win);
1264 var result = self.highlight(pos, time() + passTime, true);
1265 select.selectMarked();
1266 var newPos = result ? (result.node && result.node.nextSibling) : null;
1267 pos = (pos == newPos) ? null : newPos;
1268 self.delayScanning();
1272 // Starts the continuous scanning process for this document after
1273 // a given interval.
1274 delayScanning: function() {
1276 this.parent.clearTimeout(this.documentScan);
1277 this.documentScan = this.parent.setTimeout(this.scanner, this.options.continuousScanning);
1281 isIMEOn: function() {
1282 // chrome: keyDown keyCode is 229 while IME on
1283 // firefox: no keyUps or keyPresses fires after first keyDown while IME on
1284 return this.lastKeyDownCode == 229 || this.keyUpOrPressAfterLastKeyDown === false;
1287 // The function that does the actual highlighting/colouring (with
1288 // help from the parser and the DOM normalizer). Its interface is
1289 // rather overcomplicated, because it is used in different
1290 // situations: ensuring that a certain line is highlighted, or
1291 // highlighting up to X milliseconds starting from a certain
1292 // point. The 'from' argument gives the node at which it should
1293 // start. If this is null, it will start at the beginning of the
1294 // document. When a timestamp is given with the 'target' argument,
1295 // it will stop highlighting at that time. If this argument holds
1296 // a DOM node, it will highlight until it reaches that node. If at
1297 // any time it comes across two 'clean' lines (no dirty nodes), it
1298 // will stop, except when 'cleanLines' is true. maxBacktrack is
1299 // the maximum number of lines to backtrack to find an existing
1300 // parser instance. This is used to give up in situations where a
1301 // highlight would take too long and freeze the browser interface.
1302 highlight: function(from, target, cleanLines, maxBacktrack){
1303 var container = this.container, self = this, active = this.options.activeTokens;
1304 var endTime = (typeof target == "number" ? target : null);
1306 if (!container.firstChild || this.isIMEOn())
1308 // Backtrack to the first node before from that has a partial
1310 while (from && (!from.parserFromHere || from.dirty)) {
1311 if (maxBacktrack != null && isBR(from) && (--maxBacktrack) < 0)
1313 from = from.previousSibling;
1315 // If we are at the end of the document, do nothing.
1316 if (from && !from.nextSibling)
1319 // Check whether a part (<span> node) and the corresponding token
1321 function correctPart(token, part){
1322 return !part.reduced && part.currentText == token.value && part.className == token.style;
1324 // Shorten the text associated with a part by chopping off
1325 // characters from the front. Note that only the currentText
1326 // property gets changed. For efficiency reasons, we leave the
1327 // nodeValue alone -- we set the reduced flag to indicate that
1328 // this part must be replaced.
1329 function shortenPart(part, minus){
1330 part.currentText = part.currentText.substring(minus);
1331 part.reduced = true;
1333 // Create a part corresponding to a given token.
1334 function tokenPart(token){
1335 var part = makePartSpan(token.value, self.doc);
1336 part.className = token.style;
1340 function maybeTouch(node) {
1342 var old = node.oldNextSibling;
1343 if (lineDirty || old === undefined || node.nextSibling != old)
1344 self.history.touch(node);
1345 node.oldNextSibling = node.nextSibling;
1348 var old = self.container.oldFirstChild;
1349 if (lineDirty || old === undefined || self.container.firstChild != old)
1350 self.history.touch(null);
1351 self.container.oldFirstChild = self.container.firstChild;
1355 // Get the token stream. If from is null, we start with a new
1356 // parser from the start of the frame, otherwise a partial parse
1358 var traversal = traverseDOM(from ? from.nextSibling : container.firstChild),
1359 stream = stringStream(traversal),
1360 parsed = from ? from.parserFromHere(stream) : Editor.Parser.make(stream);
1362 function surroundedByBRs(node) {
1363 return (node.previousSibling == null || isBR(node.previousSibling)) &&
1364 (node.nextSibling == null || isBR(node.nextSibling));
1367 // parts is an interface to make it possible to 'delay' fetching
1368 // the next DOM node until we are completely done with the one
1369 // before it. This is necessary because often the next node is
1370 // not yet available when we want to proceed past the current
1374 // Fetch current node.
1377 this.current = traversal.nodes.shift();
1378 return this.current;
1380 // Advance to the next part (do not fetch it yet).
1382 this.current = null;
1384 // Remove the current part from the DOM tree, and move to the
1387 container.removeChild(this.get());
1388 this.current = null;
1390 // Advance to the next part that is not empty, discarding empty
1392 getNonEmpty: function(){
1393 var part = this.get();
1394 // Allow empty nodes when they are alone on a line, needed
1395 // for the FF cursor bug workaround (see select.js,
1396 // insertNewlineAtCursor).
1397 while (part && isSpan(part) && part.currentText == "") {
1398 // Leave empty nodes that are alone on a line alone in
1399 // Opera, since that browsers doesn't deal well with
1400 // having 2 BRs in a row.
1401 if (window.opera && surroundedByBRs(part)) {
1409 // Adjust selection information, if any. See select.js for details.
1410 select.snapshotMove(old.firstChild, part && (part.firstChild || part), 0);
1418 var lineDirty = false, prevLineDirty = true, lineNodes = 0;
1420 // This forEach loops over the tokens from the parsed stream, and
1421 // at the same time uses the parts object to proceed through the
1422 // corresponding DOM nodes.
1423 forEach(parsed, function(token){
1424 var part = parts.getNonEmpty();
1426 if (token.value == "\n"){
1427 // The idea of the two streams actually staying synchronized
1428 // is such a long shot that we explicitly check.
1430 throw "Parser out of sync. Expected BR.";
1432 if (part.dirty || !part.indentation) lineDirty = true;
1436 // Every <br> gets a copy of the parser state and a lexical
1437 // context assigned to it. The first is used to be able to
1438 // later resume parsing from this point, the second is used
1440 part.parserFromHere = parsed.copy();
1441 part.indentation = token.indentation;
1444 // If the target argument wasn't an integer, go at least
1446 if (endTime == null && part == target) throw StopIteration;
1448 // A clean line with more than one node means we are done.
1449 // Throwing a StopIteration is the way to break out of a
1450 // MochiKit forEach loop.
1451 if ((endTime != null && time() >= endTime) || (!lineDirty && !prevLineDirty && lineNodes > 1 && !cleanLines))
1452 throw StopIteration;
1453 prevLineDirty = lineDirty; lineDirty = false; lineNodes = 0;
1458 throw "Parser out of sync. Expected SPAN.";
1463 // If the part matches the token, we can leave it alone.
1464 if (correctPart(token, part)){
1468 // Otherwise, we have to fix it.
1471 // Insert the correct part.
1472 var newPart = tokenPart(token);
1473 container.insertBefore(newPart, part);
1474 if (active) active(newPart, token, self);
1475 var tokensize = token.value.length;
1477 // Eat up parts until the text for this token has been
1478 // removed, adjusting the stored selection info (see
1479 // select.js) in the process.
1480 while (tokensize > 0) {
1482 var partsize = part.currentText.length;
1483 select.snapshotReplaceNode(part.firstChild, newPart.firstChild, tokensize, offset);
1484 if (partsize > tokensize){
1485 shortenPart(part, tokensize);
1489 tokensize -= partsize;
1499 // The function returns some status information that is used by
1500 // hightlightDirty to determine whether and where it has to
1502 return {node: parts.getNonEmpty(),
1510 addEventHandler(window, "load", function() {
1511 var CodeMirror = window.frameElement.CodeMirror;
1512 var e = CodeMirror.editor = new Editor(CodeMirror.options);
1513 this.parent.setTimeout(method(CodeMirror, "init"), 0);