var nextNum = 1, barWidth = null;
function sizeBar() {
- for (var root = frame; root.parentNode; root = root.parentNode);
- if (root != document || !win.Editor) {
- clearInterval(sizeInterval);
- return;
+ if (!frame.offsetWidth || !win.Editor) {
+ for (var cur = frame; cur.parentNode; cur = cur.parentNode) {
+ if (cur != document) {
+ clearInterval(sizeInterval);
+ return;
+ }
+ }
}
if (nums.offsetWidth != barWidth) {
var node = place;
place = function(n){node.appendChild(n);};
}
-
if (options.lineNumbers) place = wrapLineNumberDiv(place);
place(frame);
getCode: function() {return this.editor.getCode();},
setCode: function(code) {this.editor.importCode(code);},
- selection: function() {this.focusIfIE(); return this.editor.selectedText();},
+ selection: function() {return this.editor.selectedText();},
reindent: function() {this.editor.reindent();},
- reindentSelection: function() {this.focusIfIE(); this.editor.reindentSelection(null);},
+ reindentSelection: function() {this.editor.reindentSelection(null);},
- focusIfIE: function() {
- // in IE, a lot of selection-related functionality only works when the frame is focused
- if (this.win.select.ie_selection) this.focus();
- },
focus: function() {
this.win.focus();
if (this.editor.selectionSnapshot) // IE hack
setParser: function(name) {this.editor.setParser(name);},
- cursorPosition: function(start) {this.focusIfIE(); return this.editor.cursorPosition(start);},
+ cursorPosition: function(start) {
+ if (this.win.select.ie_selection) this.focus();
+ return this.editor.cursorPosition(start);
+ },
firstLine: function() {return this.editor.firstLine();},
lastLine: function() {return this.editor.lastLine();},
nextLine: function(line) {return this.editor.nextLine(line);},
if (text.length) leaving = false;
result.push(node);
}
- else if (isBR(node) && node.childNodes.length == 0) {
+ else if (node.nodeName == "BR" && node.childNodes.length == 0) {
leaving = true;
result.push(node);
}
else {
forEach(node.childNodes, simplifyNode);
- if (!leaving && newlineElements.hasOwnProperty(node.nodeName.toUpperCase())) {
+ if (!leaving && newlineElements.hasOwnProperty(node.nodeName)) {
leaving = true;
if (!atEnd || !top)
result.push(doc.createElement("BR"));
nodeQueue.push(node);
return yield(node.currentText, c);
}
- else if (isBR(node)) {
+ else if (node.nodeName == "BR") {
nodeQueue.push(node);
return yield("\n", c);
}
// Determine the text size of a processed node.
function nodeSize(node) {
- return isBR(node) ? 1 : node.currentText.length;
+ if (node.nodeName == "BR")
+ return 1;
+ else
+ return node.currentText.length;
}
// Search backwards through the top-level nodes until the next BR or
// the start of the frame.
function startOfLine(node) {
- while (node && !isBR(node)) node = node.previousSibling;
+ while (node && node.nodeName != "BR") node = node.previousSibling;
return node;
}
function endOfLine(node, container) {
if (!node) node = container.firstChild;
- else if (isBR(node)) node = node.nextSibling;
+ else if (node.nodeName == "BR") node = node.nextSibling;
- while (node && !isBR(node)) node = node.nextSibling;
+ while (node && node.nodeName != "BR") node = node.nextSibling;
return node;
}
this.dirty = [];
if (options.content)
this.importCode(options.content);
+ else // FF acts weird when the editable document is completely empty
+ container.appendChild(this.doc.createElement("BR"));
if (!options.readOnly) {
if (options.continuousScanning !== false) {
this.checkLine(line);
var accum = [];
for (line = line ? line.nextSibling : this.container.firstChild;
- line && !isBR(line); line = line.nextSibling)
+ line && line.nodeName != "BR"; line = line.nextSibling)
accum.push(nodeText(line));
return cleanText(accum.join(""));
},
before = cur;
break;
}
- var text = nodeText(cur);
+ var text = (cur.innerText || cur.textContent || cur.nodeValue || "");
if (text.length > position) {
before = cur.nextSibling;
content = text.slice(0, position) + content + text.slice(position);
return cleanText(text.join("\n"));
},
- // Replace the selection with another piece of text.
+ // Replace the selection with another p
+ // iece of text.
replaceSelection: function(text) {
this.history.commit();
if (!start || !end) return;
end = this.replaceRange(start, end, text);
- select.setCursorPos(this.container, end);
+ // select.setCursorPos(this.container, end);
webkitLastLineHack(this.container);
},
home: function() {
var cur = select.selectionTopNode(this.container, true), start = cur;
- if (cur === false || !(!cur || cur.isPart || isBR(cur)) || !this.container.firstChild)
+ if (cur === false || !(!cur || cur.isPart || cur.nodeName == "BR") || !this.container.firstChild)
return false;
- while (cur && !isBR(cur)) cur = cur.previousSibling;
+ while (cur && cur.nodeName != "BR") cur = cur.previousSibling;
var next = cur ? cur.nextSibling : this.container.firstChild;
if (next && next != start && next.isPart && hasClass(next, "whitespace"))
select.focusAfterNode(next, this.container);
function tryFindMatch() {
var stack = [], ch, ok = true;;
for (var runner = cursor; runner; runner = dir ? runner.nextSibling : runner.previousSibling) {
- if (runner.className == className && isSpan(runner) && (ch = paren(runner))) {
+ if (runner.className == className && runner.nodeName == "SPAN" && (ch = paren(runner))) {
if (forward(ch) == dir)
stack.push(ch);
else if (!stack.length)
ok = false;
if (!stack.length) break;
}
- else if (runner.dirty || !isSpan(runner) && !isBR(runner)) {
+ else if (runner.dirty || runner.nodeName != "SPAN" && runner.nodeName != "BR") {
return {node: runner, status: "dirty"};
}
}
// selection.
indentRegion: function(start, end, direction) {
var current = (start = startOfLine(start)), before = start && startOfLine(start.previousSibling);
- if (!isBR(end)) end = endOfLine(end, this.container);
+ if (end.nodeName != "BR") end = endOfLine(end, this.container);
do {
var next = endOfLine(current, this.container);
// Backtrack to the first node before from that has a partial
// parse stored.
while (from && (!from.parserFromHere || from.dirty)) {
- if (maxBacktrack != null && isBR(from) && (--maxBacktrack) < 0)
+ if (maxBacktrack != null && from.nodeName == "BR" && (--maxBacktrack) < 0)
return false;
from = from.previousSibling;
}
// Allow empty nodes when they are alone on a line, needed
// for the FF cursor bug workaround (see select.js,
// insertNewlineAtCursor).
- while (part && isSpan(part) && part.currentText == "") {
+ while (part && part.nodeName == "SPAN" && part.currentText == "") {
var old = part;
this.remove();
part = this.get();
if (token.value == "\n"){
// The idea of the two streams actually staying synchronized
// is such a long shot that we explicitly check.
- if (!isBR(part))
+ if (part.nodeName != "BR")
throw "Parser out of sync. Expected BR.";
if (part.dirty || !part.indentation) lineDirty = true;
parts.next();
}
else {
- if (!isSpan(part))
+ if (part.nodeName != "SPAN")
throw "Parser out of sync. Expected SPAN.";
if (part.dirty)
lineDirty = true;
while (pos && pos.offsetParent) {
y += pos.offsetTop;
// Don't count X offset for <br> nodes
- if (!isBR(pos))
+ if (pos.nodeName != "BR")
x += pos.offsetLeft;
pos = pos.offsetParent;
}
}
if (cur) {
try{range.moveToElementText(cur);}
- catch(e){return false;}
+ catch(e){}
range.collapse(false);
}
else range.moveToElementText(node.parentNode);
if (count) range.move("character", count);
}
- else {
- try{range.moveToElementText(node);}
- catch(e){return false;}
- }
- return true;
+ else range.moveToElementText(node);
}
// Do a binary search through the container object, comparing
while (start < end) {
var middle = Math.ceil((end + start) / 2), node = container.childNodes[middle];
if (!node) return false; // Don't ask. IE6 manages this sometimes.
- if (!moveToNodeStart(range2, node)) return false;
+ moveToNodeStart(range2, node);
if (range.compareEndPoints("StartToStart", range2) == 1)
start = middle;
else
if (!selection) return null;
var topNode = select.selectionTopNode(container, start);
- while (topNode && !isBR(topNode))
+ while (topNode && topNode.nodeName != "BR")
topNode = topNode.previousSibling;
var range = selection.createRange(), range2 = range.duplicate();
// ancestors with a suitable offset. This goes down the DOM tree
// until a 'leaf' is reached (or is it *up* the DOM tree?).
function normalize(point){
- while (point.node.nodeType != 3 && !isBR(point.node)) {
+ while (point.node.nodeType != 3 && point.node.nodeName != "BR") {
var newNode = point.node.childNodes[point.offset] || point.node.nextSibling;
point.offset = 0;
while (!newNode && point.node.parentNode) {
};
select.selectMarked = function () {
- var cs = currentSelection;
- if (!(cs && (cs.changed || (webkit && cs.start.node == cs.end.node)))) return;
- var win = cs.window, range = win.document.createRange();
+ if (!currentSelection || !currentSelection.changed) return;
+ var win = currentSelection.window, range = win.document.createRange();
function setPoint(point, which) {
if (point.node) {
}
}
- setPoint(cs.end, "End");
- setPoint(cs.start, "Start");
+ setPoint(currentSelection.end, "End");
+ setPoint(currentSelection.start, "Start");
selectRange(range, win);
};
var offset = start ? range.startOffset : range.endOffset;
// Work around (yet another) bug in Opera's selection model.
if (window.opera && !start && range.endContainer == container && range.endOffset == range.startOffset + 1 &&
- container.childNodes[range.startOffset] && isBR(container.childNodes[range.startOffset]))
+ container.childNodes[range.startOffset] && container.childNodes[range.startOffset].nodeName == "BR")
offset--;
// For text nodes, we look at the node itself if the cursor is
// Occasionally, browsers will return the HTML node as
// selection. If the offset is 0, we take the start of the frame
// ('after null'), otherwise, we take the last node.
- else if (node.nodeName.toUpperCase() == "HTML") {
+ else if (node.nodeName == "HTML") {
return (offset == 1 ? null : container.lastChild);
}
// If the given node is our 'container', we just look up the
if (!range) return;
var topNode = select.selectionTopNode(container, start);
- while (topNode && !isBR(topNode))
+ while (topNode && topNode.nodeName != "BR")
topNode = topNode.previousSibling;
range = range.cloneRange();
function buildLine(node) {
var text = [];
for (var cur = node ? node.nextSibling : self.container.firstChild;
- cur && !isBR(cur); cur = cur.nextSibling)
+ cur && cur.nodeName != "BR"; cur = cur.nextSibling)
if (cur.currentText) text.push(cur.currentText);
return {from: node, to: cur, text: cleanText(text.join(""))};
}
// Get the BR element after/before the given node.
function nextBR(node, dir) {
var link = dir + "Sibling", search = node[link];
- while (search && !isBR(search))
+ while (search && search.nodeName != "BR")
search = search[link];
return search;
}
}
return top;
}
-
-function isBR(node) {
- var nn = node.nodeName;
- return nn == "BR" || nn == "br";
-}
-function isSpan(node) {
- var nn = node.nodeName;
- return nn == "SPAN" || nn == "span";
-}