|
Line 0
Link Here
|
|
|
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 |
| 5 |
*/ |
| 6 |
|
| 7 |
var safeWhiteSpace, splitSpaces; |
| 8 |
function setWhiteSpaceModel(collapsing) { |
| 9 |
safeWhiteSpace = collapsing ? |
| 10 |
// Make sure a string does not contain two consecutive 'collapseable' |
| 11 |
// whitespace characters. |
| 12 |
function(n) { |
| 13 |
var buffer = [], nb = true; |
| 14 |
for (; n > 0; n--) { |
| 15 |
buffer.push((nb || n == 1) ? nbsp : " "); |
| 16 |
nb = !nb; |
| 17 |
} |
| 18 |
return buffer.join(""); |
| 19 |
} : |
| 20 |
function(n) { |
| 21 |
var buffer = []; |
| 22 |
for (; n > 0; n--) buffer.push(" "); |
| 23 |
return buffer.join(""); |
| 24 |
}; |
| 25 |
splitSpaces = collapsing ? |
| 26 |
// Create a set of white-space characters that will not be collapsed |
| 27 |
// by the browser, but will not break text-wrapping either. |
| 28 |
function(string) { |
| 29 |
if (string.charAt(0) == " ") string = nbsp + string.slice(1); |
| 30 |
return string.replace(/[\t \u00a0]{2,}/g, function(s) {return safeWhiteSpace(s.length);}); |
| 31 |
} : |
| 32 |
function(string) {return string;}; |
| 33 |
} |
| 34 |
|
| 35 |
function makePartSpan(value, doc) { |
| 36 |
var text = value; |
| 37 |
if (value.nodeType == 3) text = value.nodeValue; |
| 38 |
else value = doc.createTextNode(text); |
| 39 |
|
| 40 |
var span = doc.createElement("SPAN"); |
| 41 |
span.isPart = true; |
| 42 |
span.appendChild(value); |
| 43 |
span.currentText = text; |
| 44 |
return span; |
| 45 |
} |
| 46 |
|
| 47 |
var Editor = (function(){ |
| 48 |
// The HTML elements whose content should be suffixed by a newline |
| 49 |
// when converting them to flat text. |
| 50 |
var newlineElements = {"P": true, "DIV": true, "LI": true}; |
| 51 |
|
| 52 |
function asEditorLines(string) { |
| 53 |
return splitSpaces(string.replace(/\t/g, " ").replace(/\u00a0/g, " ")).replace(/\r\n?/g, "\n").split("\n"); |
| 54 |
} |
| 55 |
|
| 56 |
var internetExplorer = document.selection && window.ActiveXObject && /MSIE/.test(navigator.userAgent); |
| 57 |
|
| 58 |
// Helper function for traverseDOM. Flattens an arbitrary DOM node |
| 59 |
// into an array of textnodes and <br> tags. |
| 60 |
function simplifyDOM(root) { |
| 61 |
var doc = root.ownerDocument; |
| 62 |
var result = []; |
| 63 |
var leaving = false; |
| 64 |
|
| 65 |
function simplifyNode(node) { |
| 66 |
if (node.nodeType == 3) { |
| 67 |
var text = node.nodeValue = splitSpaces(node.nodeValue.replace(/[\n\r]/g, "")); |
| 68 |
if (text.length) leaving = false; |
| 69 |
result.push(node); |
| 70 |
} |
| 71 |
else if (node.nodeName == "BR" && node.childNodes.length == 0) { |
| 72 |
leaving = true; |
| 73 |
result.push(node); |
| 74 |
} |
| 75 |
else { |
| 76 |
forEach(node.childNodes, simplifyNode); |
| 77 |
if (!leaving && newlineElements.hasOwnProperty(node.nodeName)) { |
| 78 |
leaving = true; |
| 79 |
result.push(doc.createElement("BR")); |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
simplifyNode(root); |
| 85 |
return result; |
| 86 |
} |
| 87 |
|
| 88 |
// Creates a MochiKit-style iterator that goes over a series of DOM |
| 89 |
// nodes. The values it yields are strings, the textual content of |
| 90 |
// the nodes. It makes sure that all nodes up to and including the |
| 91 |
// one whose text is being yielded have been 'normalized' to be just |
| 92 |
// <span> and <br> elements. |
| 93 |
// See the story.html file for some short remarks about the use of |
| 94 |
// continuation-passing style in this iterator. |
| 95 |
function traverseDOM(start){ |
| 96 |
function yield(value, c){cc = c; return value;} |
| 97 |
function push(fun, arg, c){return function(){return fun(arg, c);};} |
| 98 |
function stop(){cc = stop; throw StopIteration;}; |
| 99 |
var cc = push(scanNode, start, stop); |
| 100 |
var owner = start.ownerDocument; |
| 101 |
var nodeQueue = []; |
| 102 |
|
| 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); |
| 110 |
}; |
| 111 |
} |
| 112 |
var point = null; |
| 113 |
|
| 114 |
// Insert a normalized node at the current point. If it is a text |
| 115 |
// node, wrap it in a <span>, and give that span a currentText |
| 116 |
// property -- this is used to cache the nodeValue, because |
| 117 |
// directly accessing nodeValue is horribly slow on some browsers. |
| 118 |
// The dirty property is used by the highlighter to determine |
| 119 |
// which parts of the document have to be re-highlighted. |
| 120 |
function insertPart(part){ |
| 121 |
var text = "\n"; |
| 122 |
if (part.nodeType == 3) { |
| 123 |
select.snapshotChanged(); |
| 124 |
part = makePartSpan(part, owner); |
| 125 |
text = part.currentText; |
| 126 |
} |
| 127 |
part.dirty = true; |
| 128 |
nodeQueue.push(part); |
| 129 |
point(part); |
| 130 |
return text; |
| 131 |
} |
| 132 |
|
| 133 |
// Extract the text and newlines from a DOM node, insert them into |
| 134 |
// the document, and yield the textual content. Used to replace |
| 135 |
// non-normalized nodes. |
| 136 |
function writeNode(node, c){ |
| 137 |
var toYield = []; |
| 138 |
forEach(simplifyDOM(node), function(part) { |
| 139 |
toYield.push(insertPart(part)); |
| 140 |
}); |
| 141 |
return yield(toYield.join(""), c); |
| 142 |
} |
| 143 |
|
| 144 |
// Check whether a node is a normalized <span> element. |
| 145 |
function partNode(node){ |
| 146 |
if (node.nodeName == "SPAN" && node.childNodes.length == 1 && node.firstChild.nodeType == 3 && node.isPart) { |
| 147 |
node.currentText = node.firstChild.nodeValue; |
| 148 |
return !/[\n\t\r]/.test(node.currentText); |
| 149 |
} |
| 150 |
return false; |
| 151 |
} |
| 152 |
|
| 153 |
// Handle a node. Add its successor to the continuation if there |
| 154 |
// is one, find out whether the node is normalized. If it is, |
| 155 |
// yield its content, otherwise, normalize it (writeNode will take |
| 156 |
// care of yielding). |
| 157 |
function scanNode(node, c){ |
| 158 |
if (node.nextSibling) |
| 159 |
c = push(scanNode, node.nextSibling, c); |
| 160 |
|
| 161 |
if (partNode(node)){ |
| 162 |
nodeQueue.push(node); |
| 163 |
return yield(node.currentText, c); |
| 164 |
} |
| 165 |
else if (node.nodeName == "BR") { |
| 166 |
nodeQueue.push(node); |
| 167 |
return yield("\n", c); |
| 168 |
} |
| 169 |
else { |
| 170 |
point = pointAt(node); |
| 171 |
removeElement(node); |
| 172 |
return writeNode(node, c); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
// MochiKit iterators are objects with a next function that |
| 177 |
// returns the next value or throws StopIteration when there are |
| 178 |
// no more values. |
| 179 |
return {next: function(){return cc();}, nodes: nodeQueue}; |
| 180 |
} |
| 181 |
|
| 182 |
// Determine the text size of a processed node. |
| 183 |
function nodeSize(node) { |
| 184 |
if (node.nodeName == "BR") |
| 185 |
return 1; |
| 186 |
else |
| 187 |
return node.currentText.length; |
| 188 |
} |
| 189 |
|
| 190 |
// Search backwards through the top-level nodes until the next BR or |
| 191 |
// the start of the frame. |
| 192 |
function startOfLine(node) { |
| 193 |
while (node && node.nodeName != "BR") node = node.previousSibling; |
| 194 |
return node; |
| 195 |
} |
| 196 |
function endOfLine(node, container) { |
| 197 |
if (!node) node = container.firstChild; |
| 198 |
while (node && node.nodeName != "BR") node = node.nextSibling; |
| 199 |
return node; |
| 200 |
} |
| 201 |
|
| 202 |
function cleanText(text) { |
| 203 |
return text.replace(/\u00a0/g, " "); |
| 204 |
} |
| 205 |
|
| 206 |
// Client interface for searching the content of the editor. Create |
| 207 |
// these by calling CodeMirror.getSearchCursor. To use, call |
| 208 |
// findNext on the resulting object -- this returns a boolean |
| 209 |
// indicating whether anything was found, and can be called again to |
| 210 |
// skip to the next find. Use the select and replace methods to |
| 211 |
// actually do something with the found locations. |
| 212 |
function SearchCursor(editor, string, fromCursor) { |
| 213 |
this.editor = editor; |
| 214 |
this.history = editor.history; |
| 215 |
this.history.commit(); |
| 216 |
|
| 217 |
// Are we currently at an occurrence of the search string? |
| 218 |
this.atOccurrence = false; |
| 219 |
// The object stores a set of nodes coming after its current |
| 220 |
// position, so that when the current point is taken out of the |
| 221 |
// DOM tree, we can still try to continue. |
| 222 |
this.fallbackSize = 15; |
| 223 |
var cursor; |
| 224 |
// Start from the cursor when specified and a cursor can be found. |
| 225 |
if (fromCursor && (cursor = select.cursorPos(this.editor.container))) { |
| 226 |
this.line = cursor.node; |
| 227 |
this.offset = cursor.offset; |
| 228 |
} |
| 229 |
else { |
| 230 |
this.line = null; |
| 231 |
this.offset = 0; |
| 232 |
} |
| 233 |
this.valid = !!string; |
| 234 |
|
| 235 |
// Create a matcher function based on the kind of string we have. |
| 236 |
var target = string.split("\n"), self = this;; |
| 237 |
this.matches = (target.length == 1) ? |
| 238 |
// For one-line strings, searching can be done simply by calling |
| 239 |
// indexOf on the current line. |
| 240 |
function() { |
| 241 |
var match = cleanText(self.history.textAfter(self.line).slice(self.offset)).indexOf(string); |
| 242 |
if (match > -1) |
| 243 |
return {from: {node: self.line, offset: self.offset + match}, |
| 244 |
to: {node: self.line, offset: self.offset + match + string.length}}; |
| 245 |
} : |
| 246 |
// Multi-line strings require internal iteration over lines, and |
| 247 |
// some clunky checks to make sure the first match ends at the |
| 248 |
// end of the line and the last match starts at the start. |
| 249 |
function() { |
| 250 |
var firstLine = cleanText(self.history.textAfter(self.line).slice(self.offset)); |
| 251 |
var match = firstLine.lastIndexOf(target[0]); |
| 252 |
if (match == -1 || match != firstLine.length - target[0].length) |
| 253 |
return false; |
| 254 |
var startOffset = self.offset + match; |
| 255 |
|
| 256 |
var line = self.history.nodeAfter(self.line); |
| 257 |
for (var i = 1; i < target.length - 1; i++) { |
| 258 |
if (cleanText(self.history.textAfter(line)) != target[i]) |
| 259 |
return false; |
| 260 |
line = self.history.nodeAfter(line); |
| 261 |
} |
| 262 |
|
| 263 |
if (cleanText(self.history.textAfter(line)).indexOf(target[target.length - 1]) != 0) |
| 264 |
return false; |
| 265 |
|
| 266 |
return {from: {node: self.line, offset: startOffset}, |
| 267 |
to: {node: line, offset: target[target.length - 1].length}}; |
| 268 |
}; |
| 269 |
} |
| 270 |
|
| 271 |
SearchCursor.prototype = { |
| 272 |
findNext: function() { |
| 273 |
if (!this.valid) return false; |
| 274 |
this.atOccurrence = false; |
| 275 |
var self = this; |
| 276 |
|
| 277 |
// Go back to the start of the document if the current line is |
| 278 |
// no longer in the DOM tree. |
| 279 |
if (this.line && !this.line.parentNode) { |
| 280 |
this.line = null; |
| 281 |
this.offset = 0; |
| 282 |
} |
| 283 |
|
| 284 |
// Set the cursor's position one character after the given |
| 285 |
// position. |
| 286 |
function saveAfter(pos) { |
| 287 |
if (self.history.textAfter(pos.node).length < pos.offset) { |
| 288 |
self.line = pos.node; |
| 289 |
self.offset = pos.offset + 1; |
| 290 |
} |
| 291 |
else { |
| 292 |
self.line = self.history.nodeAfter(pos.node); |
| 293 |
self.offset = 0; |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
while (true) { |
| 298 |
var match = this.matches(); |
| 299 |
// Found the search string. |
| 300 |
if (match) { |
| 301 |
this.atOccurrence = match; |
| 302 |
saveAfter(match.from); |
| 303 |
return true; |
| 304 |
} |
| 305 |
this.line = this.history.nodeAfter(this.line); |
| 306 |
this.offset = 0; |
| 307 |
// End of document. |
| 308 |
if (!this.line) { |
| 309 |
this.valid = false; |
| 310 |
return false; |
| 311 |
} |
| 312 |
} |
| 313 |
}, |
| 314 |
|
| 315 |
select: function() { |
| 316 |
if (this.atOccurrence) { |
| 317 |
select.setCursorPos(this.editor.container, this.atOccurrence.from, this.atOccurrence.to); |
| 318 |
select.scrollToCursor(this.editor.container); |
| 319 |
} |
| 320 |
}, |
| 321 |
|
| 322 |
replace: function(string) { |
| 323 |
if (this.atOccurrence) { |
| 324 |
var end = this.editor.replaceRange(this.atOccurrence.from, this.atOccurrence.to, string); |
| 325 |
this.line = end.node; |
| 326 |
this.offset = end.offset; |
| 327 |
this.atOccurrence = false; |
| 328 |
} |
| 329 |
} |
| 330 |
}; |
| 331 |
|
| 332 |
// The Editor object is the main inside-the-iframe interface. |
| 333 |
function Editor(options) { |
| 334 |
this.options = options; |
| 335 |
this.parent = parent; |
| 336 |
this.doc = document; |
| 337 |
this.container = this.doc.body; |
| 338 |
this.win = window; |
| 339 |
this.history = new History(this.container, options.undoDepth, options.undoDelay, |
| 340 |
this, options.onChange); |
| 341 |
var self = this; |
| 342 |
|
| 343 |
if (!Editor.Parser) |
| 344 |
throw "No parser loaded."; |
| 345 |
if (options.parserConfig && Editor.Parser.configure) |
| 346 |
Editor.Parser.configure(options.parserConfig); |
| 347 |
|
| 348 |
if (!options.textWrapping) |
| 349 |
this.container.style.whiteSpace = "pre"; |
| 350 |
setWhiteSpaceModel(options.textWrapping); |
| 351 |
|
| 352 |
if (!options.readOnly) |
| 353 |
select.setCursorPos(this.container, {node: null, offset: 0}); |
| 354 |
|
| 355 |
this.dirty = []; |
| 356 |
if (options.content) |
| 357 |
this.importCode(options.content); |
| 358 |
else // FF acts weird when the editable document is completely empty |
| 359 |
this.container.appendChild(this.doc.createElement("BR")); |
| 360 |
|
| 361 |
if (!options.readOnly) { |
| 362 |
if (options.continuousScanning !== false) { |
| 363 |
this.scanner = this.documentScanner(options.linesPerPass); |
| 364 |
this.delayScanning(); |
| 365 |
} |
| 366 |
|
| 367 |
function setEditable() { |
| 368 |
// In IE, designMode frames can not run any scripts, so we use |
| 369 |
// contentEditable instead. |
| 370 |
if (document.body.contentEditable != undefined && /MSIE/.test(navigator.userAgent)) |
| 371 |
document.body.contentEditable = "true"; |
| 372 |
else |
| 373 |
document.designMode = "on"; |
| 374 |
} |
| 375 |
|
| 376 |
// If setting the frame editable fails, try again when the user |
| 377 |
// focus it (happens when the frame is not visible on |
| 378 |
// initialisation, in Firefox). |
| 379 |
try { |
| 380 |
setEditable(); |
| 381 |
} |
| 382 |
catch(e) { |
| 383 |
var focusEvent = addEventHandler(document, "focus", function() { |
| 384 |
focusEvent(); |
| 385 |
setEditable(); |
| 386 |
}, true); |
| 387 |
} |
| 388 |
|
| 389 |
addEventHandler(document, "keydown", method(this, "keyDown")); |
| 390 |
addEventHandler(document, "keypress", method(this, "keyPress")); |
| 391 |
addEventHandler(document, "keyup", method(this, "keyUp")); |
| 392 |
|
| 393 |
function cursorActivity() {self.cursorActivity(false);} |
| 394 |
addEventHandler(document.body, "paste", cursorActivity); |
| 395 |
addEventHandler(document.body, "cut", cursorActivity); |
| 396 |
addEventHandler(document.body, "mouseup", cursorActivity); |
| 397 |
|
| 398 |
if (this.options.autoMatchParens) |
| 399 |
addEventHandler(document.body, "click", method(this, "scheduleParenBlink")); |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
function isSafeKey(code) { |
| 404 |
return (code >= 16 && code <= 18) || // shift, control, alt |
| 405 |
(code >= 33 && code <= 40); // arrows, home, end |
| 406 |
} |
| 407 |
|
| 408 |
Editor.prototype = { |
| 409 |
// Import a piece of code into the editor. |
| 410 |
importCode: function(code) { |
| 411 |
this.history.push(null, null, asEditorLines(code)); |
| 412 |
this.history.reset(); |
| 413 |
}, |
| 414 |
|
| 415 |
// Extract the code from the editor. |
| 416 |
getCode: function() { |
| 417 |
if (!this.container.firstChild) |
| 418 |
return ""; |
| 419 |
|
| 420 |
var accum = []; |
| 421 |
select.markSelection(this.win); |
| 422 |
forEach(traverseDOM(this.container.firstChild), method(accum, "push")); |
| 423 |
select.selectMarked(); |
| 424 |
return cleanText(accum.join("")); |
| 425 |
}, |
| 426 |
|
| 427 |
checkLine: function(node) { |
| 428 |
if (node === false || !(node == null || node.parentNode == this.container)) |
| 429 |
throw parent.CodeMirror.InvalidLineHandle; |
| 430 |
}, |
| 431 |
|
| 432 |
cursorPosition: function(start) { |
| 433 |
if (start == null) start = true; |
| 434 |
var pos = select.cursorPos(this.container, start); |
| 435 |
if (pos) return {line: pos.node, character: pos.offset}; |
| 436 |
else return {line: null, character: 0}; |
| 437 |
}, |
| 438 |
|
| 439 |
firstLine: function() { |
| 440 |
return null; |
| 441 |
}, |
| 442 |
|
| 443 |
lastLine: function() { |
| 444 |
if (this.container.lastChild) return startOfLine(this.container.lastChild); |
| 445 |
else return null; |
| 446 |
}, |
| 447 |
|
| 448 |
nextLine: function(line) { |
| 449 |
this.checkLine(line); |
| 450 |
var end = endOfLine(line ? line.nextSibling : this.container.firstChild, this.container); |
| 451 |
return end || false; |
| 452 |
}, |
| 453 |
|
| 454 |
prevLine: function(line) { |
| 455 |
this.checkLine(line); |
| 456 |
if (line == null) return false; |
| 457 |
return startOfLine(line.previousSibling); |
| 458 |
}, |
| 459 |
|
| 460 |
selectLines: function(startLine, startOffset, endLine, endOffset) { |
| 461 |
this.checkLine(startLine); |
| 462 |
var start = {node: startLine, offset: startOffset}, end = null; |
| 463 |
if (endOffset !== undefined) { |
| 464 |
this.checkLine(endLine); |
| 465 |
end = {node: endLine, offset: endOffset}; |
| 466 |
} |
| 467 |
select.setCursorPos(this.container, start, end); |
| 468 |
}, |
| 469 |
|
| 470 |
lineContent: function(line) { |
| 471 |
this.checkLine(line); |
| 472 |
var accum = []; |
| 473 |
for (line = line ? line.nextSibling : this.container.firstChild; |
| 474 |
line && line.nodeName != "BR"; line = line.nextSibling) |
| 475 |
accum.push(line.innerText || line.textContent || line.nodeValue || ""); |
| 476 |
return cleanText(accum.join("")); |
| 477 |
}, |
| 478 |
|
| 479 |
setLineContent: function(line, content) { |
| 480 |
this.history.commit(); |
| 481 |
this.replaceRange({node: line, offset: 0}, |
| 482 |
{node: line, offset: this.history.textAfter(line).length}, |
| 483 |
content); |
| 484 |
this.addDirtyNode(line); |
| 485 |
this.scheduleHighlight(); |
| 486 |
}, |
| 487 |
|
| 488 |
insertIntoLine: function(line, position, content) { |
| 489 |
var before = null; |
| 490 |
if (position == "end") { |
| 491 |
before = endOfLine(line ? line.nextSibling : this.container.firstChild, this.container); |
| 492 |
} |
| 493 |
else { |
| 494 |
for (var cur = line ? line.nextSibling : this.container.firstChild; cur; cur = cur.nextSibling) { |
| 495 |
if (position == 0) { |
| 496 |
before = cur; |
| 497 |
break; |
| 498 |
} |
| 499 |
var text = (cur.innerText || cur.textContent || cur.nodeValue || ""); |
| 500 |
if (text.length > position) { |
| 501 |
before = cur.nextSibling; |
| 502 |
content = text.slice(0, position) + content + text.slice(position); |
| 503 |
removeElement(cur); |
| 504 |
break; |
| 505 |
} |
| 506 |
position -= text.length; |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
var lines = asEditorLines(content), doc = this.container.ownerDocument; |
| 511 |
for (var i = 0; i < lines.length; i++) { |
| 512 |
if (i > 0) this.container.insertBefore(doc.createElement("BR"), before); |
| 513 |
this.container.insertBefore(makePartSpan(lines[i], doc), before); |
| 514 |
} |
| 515 |
this.addDirtyNode(line); |
| 516 |
this.scheduleHighlight(); |
| 517 |
}, |
| 518 |
|
| 519 |
// Retrieve the selected text. |
| 520 |
selectedText: function() { |
| 521 |
var h = this.history; |
| 522 |
h.commit(); |
| 523 |
|
| 524 |
var start = select.cursorPos(this.container, true), |
| 525 |
end = select.cursorPos(this.container, false); |
| 526 |
if (!start || !end) return ""; |
| 527 |
|
| 528 |
if (start.node == end.node) |
| 529 |
return h.textAfter(start.node).slice(start.offset, end.offset); |
| 530 |
|
| 531 |
var text = [h.textAfter(start.node).slice(start.offset)]; |
| 532 |
for (pos = h.nodeAfter(start.node); pos != end.node; pos = h.nodeAfter(pos)) |
| 533 |
text.push(h.textAfter(pos)); |
| 534 |
text.push(h.textAfter(end.node).slice(0, end.offset)); |
| 535 |
return cleanText(text.join("\n")); |
| 536 |
}, |
| 537 |
|
| 538 |
// Replace the selection with another piece of text. |
| 539 |
replaceSelection: function(text) { |
| 540 |
this.history.commit(); |
| 541 |
var start = select.cursorPos(this.container, true), |
| 542 |
end = select.cursorPos(this.container, false); |
| 543 |
if (!start || !end) return; |
| 544 |
|
| 545 |
end = this.replaceRange(start, end, text); |
| 546 |
select.setCursorPos(this.container, start, end); |
| 547 |
}, |
| 548 |
|
| 549 |
replaceRange: function(from, to, text) { |
| 550 |
var lines = asEditorLines(text); |
| 551 |
lines[0] = this.history.textAfter(from.node).slice(0, from.offset) + lines[0]; |
| 552 |
var lastLine = lines[lines.length - 1]; |
| 553 |
lines[lines.length - 1] = lastLine + this.history.textAfter(to.node).slice(to.offset); |
| 554 |
var end = this.history.nodeAfter(to.node); |
| 555 |
this.history.push(from.node, end, lines); |
| 556 |
return {node: this.history.nodeBefore(end), |
| 557 |
offset: lastLine.length}; |
| 558 |
}, |
| 559 |
|
| 560 |
getSearchCursor: function(string, fromCursor) { |
| 561 |
return new SearchCursor(this, string, fromCursor); |
| 562 |
}, |
| 563 |
|
| 564 |
// Re-indent the whole buffer |
| 565 |
reindent: function() { |
| 566 |
if (this.container.firstChild) |
| 567 |
this.indentRegion(null, this.container.lastChild); |
| 568 |
}, |
| 569 |
|
| 570 |
// Intercept enter and tab, and assign their new functions. |
| 571 |
keyDown: function(event) { |
| 572 |
// Don't scan when the user is typing. |
| 573 |
this.delayScanning(); |
| 574 |
// Schedule a paren-highlight event, if configured. |
| 575 |
if (this.options.autoMatchParens) |
| 576 |
this.scheduleParenBlink(); |
| 577 |
|
| 578 |
if (event.keyCode == 13) { // enter |
| 579 |
if (event.ctrlKey) { |
| 580 |
this.reparseBuffer(); |
| 581 |
} |
| 582 |
else { |
| 583 |
select.insertNewlineAtCursor(this.win); |
| 584 |
this.indentAtCursor(); |
| 585 |
select.scrollToCursor(this.container); |
| 586 |
} |
| 587 |
event.stop(); |
| 588 |
} |
| 589 |
else if (event.keyCode == 9) { // tab |
| 590 |
this.handleTab(!event.ctrlKey && !event.shiftKey); |
| 591 |
event.stop(); |
| 592 |
} |
| 593 |
else if (event.ctrlKey || event.metaKey) { |
| 594 |
if (event.keyCode == 90 || event.keyCode == 8) { // Z, backspace |
| 595 |
this.history.undo(); |
| 596 |
event.stop(); |
| 597 |
} |
| 598 |
else if (event.keyCode == 89) { // Y |
| 599 |
this.history.redo(); |
| 600 |
event.stop(); |
| 601 |
} |
| 602 |
else if (event.keyCode == 83 && this.options.saveFunction) { // S |
| 603 |
this.options.saveFunction(); |
| 604 |
event.stop(); |
| 605 |
} |
| 606 |
} |
| 607 |
}, |
| 608 |
|
| 609 |
// Check for characters that should re-indent the current line, |
| 610 |
// and prevent Opera from handling enter and tab anyway. |
| 611 |
keyPress: function(event) { |
| 612 |
var electric = Editor.Parser.electricChars; |
| 613 |
// Hack for Opera, and Firefox on OS X, in which stopping a |
| 614 |
// keydown event does not prevent the associated keypress event |
| 615 |
// from happening, so we have to cancel enter and tab again |
| 616 |
// here. |
| 617 |
if (event.code == 13 || event.code == 9) |
| 618 |
event.stop(); |
| 619 |
else if ((event.character == "[" || event.character == "]") && event.ctrlKey) |
| 620 |
event.stop(), this.blinkParens(); |
| 621 |
else if (electric && electric.indexOf(event.character) != -1) |
| 622 |
this.parent.setTimeout(method(this, "indentAtCursor"), 0); |
| 623 |
}, |
| 624 |
|
| 625 |
// Mark the node at the cursor dirty when a non-safe key is |
| 626 |
// released. |
| 627 |
keyUp: function(event) { |
| 628 |
this.cursorActivity(isSafeKey(event.keyCode)); |
| 629 |
}, |
| 630 |
|
| 631 |
// Indent the line following a given <br>, or null for the first |
| 632 |
// line. If given a <br> element, this must have been highlighted |
| 633 |
// so that it has an indentation method. Returns the whitespace |
| 634 |
// element that has been modified or created (if any). |
| 635 |
indentLineAfter: function(start, direction) { |
| 636 |
// whiteSpace is the whitespace span at the start of the line, |
| 637 |
// or null if there is no such node. |
| 638 |
var whiteSpace = start ? start.nextSibling : this.container.firstChild; |
| 639 |
if (whiteSpace && !hasClass(whiteSpace, "whitespace")) |
| 640 |
whiteSpace = null; |
| 641 |
|
| 642 |
// Sometimes the start of the line can influence the correct |
| 643 |
// indentation, so we retrieve it. |
| 644 |
var firstText = whiteSpace ? whiteSpace.nextSibling : (start ? start.nextSibling : this.container.firstChild); |
| 645 |
var nextChars = (start && firstText && firstText.currentText) ? firstText.currentText : ""; |
| 646 |
|
| 647 |
// Ask the lexical context for the correct indentation, and |
| 648 |
// compute how much this differs from the current indentation. |
| 649 |
var newIndent = 0, curIndent = whiteSpace ? whiteSpace.currentText.length : 0; |
| 650 |
if (start) newIndent = start.indentation(nextChars, curIndent, direction); |
| 651 |
else if (Editor.Parser.firstIndentation) newIndent = Editor.Parser.firstIndentation(nextChars, curIndent, direction); |
| 652 |
var indentDiff = newIndent - curIndent; |
| 653 |
|
| 654 |
// If there is too much, this is just a matter of shrinking a span. |
| 655 |
if (indentDiff < 0) { |
| 656 |
if (newIndent == 0) { |
| 657 |
if (firstText) select.snapshotMove(whiteSpace.firstChild, firstText.firstChild, 0); |
| 658 |
removeElement(whiteSpace); |
| 659 |
whiteSpace = null; |
| 660 |
} |
| 661 |
else { |
| 662 |
select.snapshotMove(whiteSpace.firstChild, whiteSpace.firstChild, indentDiff, true); |
| 663 |
whiteSpace.currentText = safeWhiteSpace(newIndent); |
| 664 |
whiteSpace.firstChild.nodeValue = whiteSpace.currentText; |
| 665 |
} |
| 666 |
} |
| 667 |
// Not enough... |
| 668 |
else if (indentDiff > 0) { |
| 669 |
// If there is whitespace, we grow it. |
| 670 |
if (whiteSpace) { |
| 671 |
whiteSpace.currentText = safeWhiteSpace(newIndent); |
| 672 |
whiteSpace.firstChild.nodeValue = whiteSpace.currentText; |
| 673 |
} |
| 674 |
// Otherwise, we have to add a new whitespace node. |
| 675 |
else { |
| 676 |
whiteSpace = makePartSpan(safeWhiteSpace(newIndent), this.doc); |
| 677 |
whiteSpace.className = "whitespace"; |
| 678 |
if (start) insertAfter(whiteSpace, start); |
| 679 |
else this.container.insertBefore(whiteSpace, this.container.firstChild); |
| 680 |
} |
| 681 |
if (firstText) select.snapshotMove(firstText.firstChild, whiteSpace.firstChild, curIndent, false, true); |
| 682 |
} |
| 683 |
if (indentDiff != 0) this.addDirtyNode(start); |
| 684 |
return whiteSpace; |
| 685 |
}, |
| 686 |
|
| 687 |
// Re-highlight the selected part of the document. |
| 688 |
highlightAtCursor: function() { |
| 689 |
var pos = select.selectionTopNode(this.container, true); |
| 690 |
var to = select.selectionTopNode(this.container, false); |
| 691 |
if (pos === false || !to) return; |
| 692 |
// Skip one node ahead to make sure the cursor itself is |
| 693 |
// *inside* a highlighted line. |
| 694 |
if (to.nextSibling) to = to.nextSibling; |
| 695 |
|
| 696 |
select.markSelection(this.win); |
| 697 |
var toIsText = to.nodeType == 3; |
| 698 |
if (!toIsText) to.dirty = true; |
| 699 |
|
| 700 |
// Highlight lines as long as to is in the document and dirty. |
| 701 |
while (to.parentNode == this.container && (toIsText || to.dirty)) { |
| 702 |
var result = this.highlight(pos, 1, true); |
| 703 |
if (result) pos = result.node; |
| 704 |
if (!result || result.left) break; |
| 705 |
} |
| 706 |
select.selectMarked(); |
| 707 |
}, |
| 708 |
|
| 709 |
// When tab is pressed with text selected, the whole selection is |
| 710 |
// re-indented, when nothing is selected, the line with the cursor |
| 711 |
// is re-indented. |
| 712 |
handleTab: function(direction) { |
| 713 |
if (this.options.dumbTabs) { |
| 714 |
select.insertTabAtCursor(this.win); |
| 715 |
} |
| 716 |
else if (!select.somethingSelected(this.win)) { |
| 717 |
this.indentAtCursor(direction); |
| 718 |
} |
| 719 |
else { |
| 720 |
var start = select.selectionTopNode(this.container, true), |
| 721 |
end = select.selectionTopNode(this.container, false); |
| 722 |
if (start === false || end === false) return; |
| 723 |
this.indentRegion(start, end, direction); |
| 724 |
} |
| 725 |
}, |
| 726 |
|
| 727 |
// Delay (or initiate) the next paren blink event. |
| 728 |
scheduleParenBlink: function() { |
| 729 |
if (this.parenEvent) this.parent.clearTimeout(this.parenEvent); |
| 730 |
this.parenEvent = this.parent.setTimeout(method(this, "blinkParens"), 300); |
| 731 |
}, |
| 732 |
|
| 733 |
isNearParsedNode: function(node) { |
| 734 |
var distance = 0; |
| 735 |
while (node && (!node.parserFromHere || node.dirty)) { |
| 736 |
distance += (node.textContent || node.innerText || "-").length; |
| 737 |
if (distance > 800) return false; |
| 738 |
node = node.previousSibling; |
| 739 |
} |
| 740 |
return true; |
| 741 |
}, |
| 742 |
|
| 743 |
// Take the token before the cursor. If it contains a character in |
| 744 |
// '()[]{}', search for the matching paren/brace/bracket, and |
| 745 |
// highlight them in green for a moment, or red if no proper match |
| 746 |
// was found. |
| 747 |
blinkParens: function() { |
| 748 |
// Clear the event property. |
| 749 |
if (this.parenEvent) this.parent.clearTimeout(this.parenEvent); |
| 750 |
this.parenEvent = null; |
| 751 |
|
| 752 |
// Extract a 'paren' from a piece of text. |
| 753 |
function paren(node) { |
| 754 |
if (node.currentText) { |
| 755 |
var match = node.currentText.match(/^[\s\u00a0]*([\(\)\[\]{}])[\s\u00a0]*$/); |
| 756 |
return match && match[1]; |
| 757 |
} |
| 758 |
} |
| 759 |
// Determine the direction a paren is facing. |
| 760 |
function forward(ch) { |
| 761 |
return /[\(\[\{]/.test(ch); |
| 762 |
} |
| 763 |
|
| 764 |
var ch, self = this, cursor = select.selectionTopNode(this.container, true); |
| 765 |
if (!cursor || !this.isNearParsedNode(cursor)) return; |
| 766 |
this.highlightAtCursor(); |
| 767 |
cursor = select.selectionTopNode(this.container, true); |
| 768 |
if (!cursor || !(ch = paren(cursor))) return; |
| 769 |
// We only look for tokens with the same className. |
| 770 |
var className = cursor.className, dir = forward(ch), match = matching[ch]; |
| 771 |
|
| 772 |
// Since parts of the document might not have been properly |
| 773 |
// highlighted, and it is hard to know in advance which part we |
| 774 |
// have to scan, we just try, and when we find dirty nodes we |
| 775 |
// abort, parse them, and re-try. |
| 776 |
function tryFindMatch() { |
| 777 |
var stack = [], ch, ok = true;; |
| 778 |
for (var runner = cursor; runner; runner = dir ? runner.nextSibling : runner.previousSibling) { |
| 779 |
if (runner.className == className && runner.nodeName == "SPAN" && (ch = paren(runner))) { |
| 780 |
if (forward(ch) == dir) |
| 781 |
stack.push(ch); |
| 782 |
else if (!stack.length) |
| 783 |
ok = false; |
| 784 |
else if (stack.pop() != matching[ch]) |
| 785 |
ok = false; |
| 786 |
if (!stack.length) break; |
| 787 |
} |
| 788 |
else if (runner.dirty || runner.nodeName != "SPAN" && runner.nodeName != "BR") { |
| 789 |
return {node: runner, status: "dirty"}; |
| 790 |
} |
| 791 |
} |
| 792 |
return {node: runner, status: runner && ok}; |
| 793 |
} |
| 794 |
// Temporarily give the relevant nodes a colour. |
| 795 |
function blink(node, ok) { |
| 796 |
node.style.fontWeight = "bold"; |
| 797 |
node.style.color = ok ? "#8F8" : "#F88"; |
| 798 |
self.parent.setTimeout(function() {node.style.fontWeight = ""; node.style.color = "";}, 500); |
| 799 |
} |
| 800 |
|
| 801 |
while (true) { |
| 802 |
var found = tryFindMatch(); |
| 803 |
if (found.status == "dirty") { |
| 804 |
this.highlight(found.node, 1); |
| 805 |
// Needed because in some corner cases a highlight does not |
| 806 |
// reach a node. |
| 807 |
found.node.dirty = false; |
| 808 |
continue; |
| 809 |
} |
| 810 |
else { |
| 811 |
blink(cursor, found.status); |
| 812 |
if (found.node) blink(found.node, found.status); |
| 813 |
break; |
| 814 |
} |
| 815 |
} |
| 816 |
}, |
| 817 |
|
| 818 |
// Adjust the amount of whitespace at the start of the line that |
| 819 |
// the cursor is on so that it is indented properly. |
| 820 |
indentAtCursor: function(direction) { |
| 821 |
if (!this.container.firstChild) return; |
| 822 |
// The line has to have up-to-date lexical information, so we |
| 823 |
// highlight it first. |
| 824 |
this.highlightAtCursor(); |
| 825 |
var cursor = select.selectionTopNode(this.container, false); |
| 826 |
// If we couldn't determine the place of the cursor, |
| 827 |
// there's nothing to indent. |
| 828 |
if (cursor === false) |
| 829 |
return; |
| 830 |
var lineStart = startOfLine(cursor); |
| 831 |
var whiteSpace = this.indentLineAfter(lineStart, direction); |
| 832 |
if (cursor == lineStart && whiteSpace) |
| 833 |
cursor = whiteSpace; |
| 834 |
// This means the indentation has probably messed up the cursor. |
| 835 |
if (cursor == whiteSpace) |
| 836 |
select.focusAfterNode(cursor, this.container); |
| 837 |
}, |
| 838 |
|
| 839 |
// Indent all lines whose start falls inside of the current |
| 840 |
// selection. |
| 841 |
indentRegion: function(current, end, direction) { |
| 842 |
select.markSelection(this.win); |
| 843 |
current = startOfLine(current); |
| 844 |
end = endOfLine(end, this.container); |
| 845 |
|
| 846 |
do { |
| 847 |
this.highlight(current); |
| 848 |
var hl = this.highlight(current, 1); |
| 849 |
this.indentLineAfter(current, direction); |
| 850 |
current = hl ? hl.node : null; |
| 851 |
} while (current != end); |
| 852 |
select.selectMarked(); |
| 853 |
}, |
| 854 |
|
| 855 |
// Find the node that the cursor is in, mark it as dirty, and make |
| 856 |
// sure a highlight pass is scheduled. |
| 857 |
cursorActivity: function(safe) { |
| 858 |
if (internetExplorer) { |
| 859 |
this.container.createTextRange().execCommand("unlink"); |
| 860 |
this.selectionSnapshot = select.selectionCoords(this.win); |
| 861 |
} |
| 862 |
|
| 863 |
var activity = this.options.cursorActivity; |
| 864 |
if (!safe || activity) { |
| 865 |
var cursor = select.selectionTopNode(this.container, false); |
| 866 |
if (cursor === false || !this.container.firstChild) return; |
| 867 |
cursor = cursor || this.container.firstChild; |
| 868 |
if (activity) activity(cursor); |
| 869 |
if (!safe) { |
| 870 |
this.scheduleHighlight(); |
| 871 |
this.addDirtyNode(cursor); |
| 872 |
} |
| 873 |
} |
| 874 |
}, |
| 875 |
|
| 876 |
reparseBuffer: function() { |
| 877 |
forEach(this.container.childNodes, function(node) {node.dirty = true;}); |
| 878 |
if (this.container.firstChild) |
| 879 |
this.addDirtyNode(this.container.firstChild); |
| 880 |
}, |
| 881 |
|
| 882 |
// Add a node to the set of dirty nodes, if it isn't already in |
| 883 |
// there. |
| 884 |
addDirtyNode: function(node) { |
| 885 |
node = node || this.container.firstChild; |
| 886 |
if (!node) return; |
| 887 |
|
| 888 |
for (var i = 0; i < this.dirty.length; i++) |
| 889 |
if (this.dirty[i] == node) return; |
| 890 |
|
| 891 |
if (node.nodeType != 3) |
| 892 |
node.dirty = true; |
| 893 |
this.dirty.push(node); |
| 894 |
}, |
| 895 |
|
| 896 |
// Cause a highlight pass to happen in options.passDelay |
| 897 |
// milliseconds. Clear the existing timeout, if one exists. This |
| 898 |
// way, the passes do not happen while the user is typing, and |
| 899 |
// should as unobtrusive as possible. |
| 900 |
scheduleHighlight: function() { |
| 901 |
// Timeouts are routed through the parent window, because on |
| 902 |
// some browsers designMode windows do not fire timeouts. |
| 903 |
var self = this; |
| 904 |
this.parent.clearTimeout(this.highlightTimeout); |
| 905 |
this.highlightTimeout = this.parent.setTimeout(function(){self.highlightDirty();}, this.options.passDelay); |
| 906 |
}, |
| 907 |
|
| 908 |
// Fetch one dirty node, and remove it from the dirty set. |
| 909 |
getDirtyNode: function() { |
| 910 |
while (this.dirty.length > 0) { |
| 911 |
var found = this.dirty.pop(); |
| 912 |
// IE8 sometimes throws an unexplainable 'invalid argument' |
| 913 |
// exception for found.parentNode |
| 914 |
try { |
| 915 |
// If the node has been coloured in the meantime, or is no |
| 916 |
// longer in the document, it should not be returned. |
| 917 |
while (found && found.parentNode != this.container) |
| 918 |
found = found.parentNode |
| 919 |
if (found && (found.dirty || found.nodeType == 3)) |
| 920 |
return found; |
| 921 |
} catch (e) {} |
| 922 |
} |
| 923 |
return null; |
| 924 |
}, |
| 925 |
|
| 926 |
// Pick dirty nodes, and highlight them, until |
| 927 |
// options.linesPerPass lines have been highlighted. The highlight |
| 928 |
// method will continue to next lines as long as it finds dirty |
| 929 |
// nodes. It returns an object indicating the amount of lines |
| 930 |
// left, and information about the place where it stopped. If |
| 931 |
// there are dirty nodes left after this function has spent all |
| 932 |
// its lines, it shedules another highlight to finish the job. |
| 933 |
highlightDirty: function(force) { |
| 934 |
var lines = force ? Infinity : this.options.linesPerPass; |
| 935 |
if (!this.options.readOnly) select.markSelection(this.win); |
| 936 |
var start; |
| 937 |
while (lines > 0 && (start = this.getDirtyNode())){ |
| 938 |
var result = this.highlight(start, lines); |
| 939 |
if (result) { |
| 940 |
lines = result.left; |
| 941 |
if (result.node && result.dirty) |
| 942 |
this.addDirtyNode(result.node); |
| 943 |
} |
| 944 |
} |
| 945 |
if (!this.options.readOnly) select.selectMarked(); |
| 946 |
if (start) |
| 947 |
this.scheduleHighlight(); |
| 948 |
return this.dirty.length == 0; |
| 949 |
}, |
| 950 |
|
| 951 |
// Creates a function that, when called through a timeout, will |
| 952 |
// continuously re-parse the document. |
| 953 |
documentScanner: function(linesPer) { |
| 954 |
var self = this, pos = null; |
| 955 |
return function() { |
| 956 |
// If the current node is no longer in the document... oh |
| 957 |
// well, we start over. |
| 958 |
if (pos && pos.parentNode != self.container) |
| 959 |
pos = null; |
| 960 |
select.markSelection(self.win); |
| 961 |
var result = self.highlight(pos, linesPer, true); |
| 962 |
select.selectMarked(); |
| 963 |
var newPos = result ? (result.node && result.node.nextSibling) : null; |
| 964 |
pos = (pos == newPos) ? null : newPos; |
| 965 |
self.delayScanning(); |
| 966 |
}; |
| 967 |
}, |
| 968 |
|
| 969 |
// Starts the continuous scanning process for this document after |
| 970 |
// a given interval. |
| 971 |
delayScanning: function() { |
| 972 |
if (this.scanner) { |
| 973 |
this.parent.clearTimeout(this.documentScan); |
| 974 |
this.documentScan = this.parent.setTimeout(this.scanner, this.options.continuousScanning); |
| 975 |
} |
| 976 |
}, |
| 977 |
|
| 978 |
// The function that does the actual highlighting/colouring (with |
| 979 |
// help from the parser and the DOM normalizer). Its interface is |
| 980 |
// rather overcomplicated, because it is used in different |
| 981 |
// situations: ensuring that a certain line is highlighted, or |
| 982 |
// highlighting up to X lines starting from a certain point. The |
| 983 |
// 'from' argument gives the node at which it should start. If |
| 984 |
// this is null, it will start at the beginning of the frame. When |
| 985 |
// a number of lines is given with the 'lines' argument, it will |
| 986 |
// colour no more than that amount. If at any time it comes across |
| 987 |
// a 'clean' line (no dirty nodes), it will stop, except when |
| 988 |
// 'cleanLines' is true. |
| 989 |
highlight: function(from, lines, cleanLines){ |
| 990 |
var container = this.container, self = this, active = this.options.activeTokens, origFrom = from; |
| 991 |
|
| 992 |
if (!container.firstChild) |
| 993 |
return; |
| 994 |
// lines given as null means 'make sure this BR node has up to date parser information' |
| 995 |
if (lines == null) { |
| 996 |
if (!from) return; |
| 997 |
else from = from.previousSibling; |
| 998 |
} |
| 999 |
// Backtrack to the first node before from that has a partial |
| 1000 |
// parse stored. |
| 1001 |
while (from && (!from.parserFromHere || from.dirty)) |
| 1002 |
from = from.previousSibling; |
| 1003 |
// If we are at the end of the document, do nothing. |
| 1004 |
if (from && !from.nextSibling) |
| 1005 |
return; |
| 1006 |
|
| 1007 |
// Check whether a part (<span> node) and the corresponding token |
| 1008 |
// match. |
| 1009 |
function correctPart(token, part){ |
| 1010 |
return !part.reduced && part.currentText == token.value && part.className == token.style; |
| 1011 |
} |
| 1012 |
// Shorten the text associated with a part by chopping off |
| 1013 |
// characters from the front. Note that only the currentText |
| 1014 |
// property gets changed. For efficiency reasons, we leave the |
| 1015 |
// nodeValue alone -- we set the reduced flag to indicate that |
| 1016 |
// this part must be replaced. |
| 1017 |
function shortenPart(part, minus){ |
| 1018 |
part.currentText = part.currentText.substring(minus); |
| 1019 |
part.reduced = true; |
| 1020 |
} |
| 1021 |
// Create a part corresponding to a given token. |
| 1022 |
function tokenPart(token){ |
| 1023 |
var part = makePartSpan(token.value, self.doc); |
| 1024 |
part.className = token.style; |
| 1025 |
return part; |
| 1026 |
} |
| 1027 |
|
| 1028 |
// Get the token stream. If from is null, we start with a new |
| 1029 |
// parser from the start of the frame, otherwise a partial parse |
| 1030 |
// is resumed. |
| 1031 |
var traversal = traverseDOM(from ? from.nextSibling : container.firstChild), |
| 1032 |
stream = stringStream(traversal), |
| 1033 |
parsed = from ? from.parserFromHere(stream) : Editor.Parser.make(stream); |
| 1034 |
|
| 1035 |
// parts is an interface to make it possible to 'delay' fetching |
| 1036 |
// the next DOM node until we are completely done with the one |
| 1037 |
// before it. This is necessary because often the next node is |
| 1038 |
// not yet available when we want to proceed past the current |
| 1039 |
// one. |
| 1040 |
var parts = { |
| 1041 |
current: null, |
| 1042 |
// Fetch current node. |
| 1043 |
get: function(){ |
| 1044 |
if (!this.current) |
| 1045 |
this.current = traversal.nodes.shift(); |
| 1046 |
return this.current; |
| 1047 |
}, |
| 1048 |
// Advance to the next part (do not fetch it yet). |
| 1049 |
next: function(){ |
| 1050 |
this.current = null; |
| 1051 |
}, |
| 1052 |
// Remove the current part from the DOM tree, and move to the |
| 1053 |
// next. |
| 1054 |
remove: function(){ |
| 1055 |
container.removeChild(this.get()); |
| 1056 |
this.current = null; |
| 1057 |
}, |
| 1058 |
// Advance to the next part that is not empty, discarding empty |
| 1059 |
// parts. |
| 1060 |
getNonEmpty: function(){ |
| 1061 |
var part = this.get(); |
| 1062 |
// Allow empty nodes when they are alone on a line, needed |
| 1063 |
// for the FF cursor bug workaround (see select.js, |
| 1064 |
// insertNewlineAtCursor). |
| 1065 |
while (part && part.nodeName == "SPAN" && part.currentText == "") { |
| 1066 |
var old = part; |
| 1067 |
this.remove(); |
| 1068 |
part = this.get(); |
| 1069 |
// Adjust selection information, if any. See select.js for details. |
| 1070 |
select.snapshotMove(old.firstChild, part.firstChild || part, 0); |
| 1071 |
} |
| 1072 |
return part; |
| 1073 |
} |
| 1074 |
}; |
| 1075 |
|
| 1076 |
var lineDirty = false, prevLineDirty = true, lineNodes = 0; |
| 1077 |
|
| 1078 |
// This forEach loops over the tokens from the parsed stream, and |
| 1079 |
// at the same time uses the parts object to proceed through the |
| 1080 |
// corresponding DOM nodes. |
| 1081 |
forEach(parsed, function(token){ |
| 1082 |
var part = parts.getNonEmpty(); |
| 1083 |
|
| 1084 |
if (token.value == "\n"){ |
| 1085 |
// The idea of the two streams actually staying synchronized |
| 1086 |
// is such a long shot that we explicitly check. |
| 1087 |
if (part.nodeName != "BR") |
| 1088 |
throw "Parser out of sync. Expected BR."; |
| 1089 |
|
| 1090 |
if (part.dirty || !part.indentation) lineDirty = true; |
| 1091 |
if (lineDirty) self.history.touch(from); |
| 1092 |
from = part; |
| 1093 |
|
| 1094 |
// Every <br> gets a copy of the parser state and a lexical |
| 1095 |
// context assigned to it. The first is used to be able to |
| 1096 |
// later resume parsing from this point, the second is used |
| 1097 |
// for indentation. |
| 1098 |
part.parserFromHere = parsed.copy(); |
| 1099 |
part.indentation = token.indentation; |
| 1100 |
part.dirty = false; |
| 1101 |
|
| 1102 |
// No line argument passed means 'go at least until this node'. |
| 1103 |
if (lines == null && part == origFrom) throw StopIteration; |
| 1104 |
|
| 1105 |
// A clean line with more than one node means we are done. |
| 1106 |
// Throwing a StopIteration is the way to break out of a |
| 1107 |
// MochiKit forEach loop. |
| 1108 |
if ((lines !== undefined && --lines <= 0) || (!lineDirty && !prevLineDirty && lineNodes > 1 && !cleanLines)) |
| 1109 |
throw StopIteration; |
| 1110 |
prevLineDirty = lineDirty; lineDirty = false; lineNodes = 0; |
| 1111 |
parts.next(); |
| 1112 |
} |
| 1113 |
else { |
| 1114 |
if (part.nodeName != "SPAN") |
| 1115 |
throw "Parser out of sync. Expected SPAN."; |
| 1116 |
if (part.dirty) |
| 1117 |
lineDirty = true; |
| 1118 |
lineNodes++; |
| 1119 |
|
| 1120 |
// If the part matches the token, we can leave it alone. |
| 1121 |
if (correctPart(token, part)){ |
| 1122 |
part.dirty = false; |
| 1123 |
parts.next(); |
| 1124 |
} |
| 1125 |
// Otherwise, we have to fix it. |
| 1126 |
else { |
| 1127 |
lineDirty = true; |
| 1128 |
// Insert the correct part. |
| 1129 |
var newPart = tokenPart(token); |
| 1130 |
container.insertBefore(newPart, part); |
| 1131 |
if (active) active(newPart, token, self); |
| 1132 |
var tokensize = token.value.length; |
| 1133 |
var offset = 0; |
| 1134 |
// Eat up parts until the text for this token has been |
| 1135 |
// removed, adjusting the stored selection info (see |
| 1136 |
// select.js) in the process. |
| 1137 |
while (tokensize > 0) { |
| 1138 |
part = parts.get(); |
| 1139 |
var partsize = part.currentText.length; |
| 1140 |
select.snapshotReplaceNode(part.firstChild, newPart.firstChild, tokensize, offset); |
| 1141 |
if (partsize > tokensize){ |
| 1142 |
shortenPart(part, tokensize); |
| 1143 |
tokensize = 0; |
| 1144 |
} |
| 1145 |
else { |
| 1146 |
tokensize -= partsize; |
| 1147 |
offset += partsize; |
| 1148 |
parts.remove(); |
| 1149 |
} |
| 1150 |
} |
| 1151 |
} |
| 1152 |
} |
| 1153 |
}); |
| 1154 |
if (lineDirty) this.history.touch(from); |
| 1155 |
|
| 1156 |
// The function returns some status information that is used by |
| 1157 |
// hightlightDirty to determine whether and where it has to |
| 1158 |
// continue. |
| 1159 |
return {left: lines, |
| 1160 |
node: parts.get(), |
| 1161 |
dirty: lineDirty}; |
| 1162 |
} |
| 1163 |
}; |
| 1164 |
|
| 1165 |
return Editor; |
| 1166 |
})(); |
| 1167 |
|
| 1168 |
addEventHandler(window, "load", function() { |
| 1169 |
var CodeMirror = window.frameElement.CodeMirror; |
| 1170 |
CodeMirror.editor = new Editor(CodeMirror.options); |
| 1171 |
if (CodeMirror.options.initCallback) { |
| 1172 |
this.parent.setTimeout(function(){ |
| 1173 |
CodeMirror.options.initCallback(CodeMirror); |
| 1174 |
}, 0); |
| 1175 |
} |
| 1176 |
}); |