TinyMCE_ForceParagraphs.class.js
Summary
No overview generated for 'TinyMCE_ForceParagraphs.class.js'
var TinyMCE_ForceParagraphs = {
_insertPara : function(inst, e) {
function isEmpty(para) {
function isEmptyHTML(html) {
return html.replace(new RegExp('[ \t\r\n]+', 'g'), '').toLowerCase() == "";
}
if (para.getElementsByTagName("img").length > 0)
return false;
if (para.getElementsByTagName("table").length > 0)
return false;
if (para.getElementsByTagName("hr").length > 0)
return false;
var nodes = tinyMCE.getNodeTree(para, new Array(), 3);
for (var i=0; i<nodes.length; i++) {
if (!isEmptyHTML(nodes[i].nodeValue))
return false;
}
return true;
}
var doc = inst.getDoc();
var sel = inst.getSel();
var win = inst.contentWindow;
var rng = sel.getRangeAt(0);
var body = doc.body;
var rootElm = doc.documentElement;
var blockName = "P";
var rngBefore = doc.createRange();
rngBefore.setStart(sel.anchorNode, sel.anchorOffset);
rngBefore.collapse(true);
var rngAfter = doc.createRange();
rngAfter.setStart(sel.focusNode, sel.focusOffset);
rngAfter.collapse(true);
var direct = rngBefore.compareBoundaryPoints(rngBefore.START_TO_END, rngAfter) < 0;
var startNode = direct ? sel.anchorNode : sel.focusNode;
var startOffset = direct ? sel.anchorOffset : sel.focusOffset;
var endNode = direct ? sel.focusNode : sel.anchorNode;
var endOffset = direct ? sel.focusOffset : sel.anchorOffset;
startNode = startNode.nodeName == "BODY" ? startNode.firstChild : startNode;
endNode = endNode.nodeName == "BODY" ? endNode.firstChild : endNode;
var startBlock = tinyMCE.getParentBlockElement(startNode);
var endBlock = tinyMCE.getParentBlockElement(endNode);
if (startBlock != null) {
blockName = startBlock.nodeName;
if (blockName == "TD" || blockName == "TABLE" || (blockName == "DIV" && new RegExp('left|right', 'gi').test(startBlock.style.cssFloat)))
blockName = "P";
}
if (tinyMCE.getParentElement(startBlock, "OL,UL") != null)
return false;
if ((startBlock != null && startBlock.nodeName == "TABLE") || (endBlock != null && endBlock.nodeName == "TABLE"))
startBlock = endBlock = null;
var paraBefore = (startBlock != null && startBlock.nodeName == blockName) ? startBlock.cloneNode(false) : doc.createElement(blockName);
var paraAfter = (endBlock != null && endBlock.nodeName == blockName) ? endBlock.cloneNode(false) : doc.createElement(blockName);
if (/^(H[1-6])$/.test(blockName))
paraAfter = doc.createElement("p");
var startChop = startNode;
var endChop = endNode;
node = startChop;
do {
if (node == body || node.nodeType == 9 || tinyMCE.isBlockElement(node))
break;
startChop = node;
} while ((node = node.previousSibling ? node.previousSibling : node.parentNode));
node = endChop;
do {
if (node == body || node.nodeType == 9 || tinyMCE.isBlockElement(node))
break;
endChop = node;
} while ((node = node.nextSibling ? node.nextSibling : node.parentNode));
if (startChop.nodeName == "TD")
startChop = startChop.firstChild;
if (endChop.nodeName == "TD")
endChop = endChop.lastChild;
if (startBlock == null) {
rng.deleteContents();
sel.removeAllRanges();
if (startChop != rootElm && endChop != rootElm) {
rngBefore = rng.cloneRange();
if (startChop == body)
rngBefore.setStart(startChop, 0);
else
rngBefore.setStartBefore(startChop);
paraBefore.appendChild(rngBefore.cloneContents());
if (endChop.parentNode.nodeName == blockName)
endChop = endChop.parentNode;
rng.setEndAfter(endChop);
if (endChop.nodeName != "#text" && endChop.nodeName != "BODY")
rngBefore.setEndAfter(endChop);
var contents = rng.cloneContents();
if (contents.firstChild && (contents.firstChild.nodeName == blockName || contents.firstChild.nodeName == "BODY"))
paraAfter.innerHTML = contents.firstChild.innerHTML;
else
paraAfter.appendChild(contents);
if (isEmpty(paraBefore))
paraBefore.innerHTML = " ";
if (isEmpty(paraAfter))
paraAfter.innerHTML = " ";
rng.deleteContents();
rngAfter.deleteContents();
rngBefore.deleteContents();
paraAfter.normalize();
rngBefore.insertNode(paraAfter);
paraBefore.normalize();
rngBefore.insertNode(paraBefore);
} else {
body.innerHTML = "<" + blockName + "> </" + blockName + "><" + blockName + "> </" + blockName + ">";
paraAfter = body.childNodes[1];
}
inst.selection.selectNode(paraAfter, true, true);
return true;
}
if (startChop.nodeName == blockName)
rngBefore.setStart(startChop, 0);
else
rngBefore.setStartBefore(startChop);
rngBefore.setEnd(startNode, startOffset);
paraBefore.appendChild(rngBefore.cloneContents());
rngAfter.setEndAfter(endChop);
rngAfter.setStart(endNode, endOffset);
var contents = rngAfter.cloneContents();
if (contents.firstChild && contents.firstChild.nodeName == blockName) {
paraAfter.innerHTML = contents.firstChild.innerHTML;
} else
paraAfter.appendChild(contents);
if (isEmpty(paraBefore))
paraBefore.innerHTML = " ";
if (isEmpty(paraAfter))
paraAfter.innerHTML = " ";
var rng = doc.createRange();
if (!startChop.previousSibling && startChop.parentNode.nodeName.toUpperCase() == blockName) {
rng.setStartBefore(startChop.parentNode);
} else {
if (rngBefore.startContainer.nodeName.toUpperCase() == blockName && rngBefore.startOffset == 0)
rng.setStartBefore(rngBefore.startContainer);
else
rng.setStart(rngBefore.startContainer, rngBefore.startOffset);
}
if (!endChop.nextSibling && endChop.parentNode.nodeName.toUpperCase() == blockName)
rng.setEndAfter(endChop.parentNode);
else
rng.setEnd(rngAfter.endContainer, rngAfter.endOffset);
rng.deleteContents();
rng.insertNode(paraAfter);
rng.insertNode(paraBefore);
paraAfter.normalize();
paraBefore.normalize();
inst.selection.selectNode(paraAfter, true, true);
return true;
},
_handleBackSpace : function(inst) {
var r = inst.getRng();
var sn = r.startContainer;
if (sn && sn.nextSibling && sn.nextSibling.nodeName == "BR")
sn.nextSibling.parentNode.removeChild(sn.nextSibling);
return false;
}
};
Documentation generated by
JSDoc on Fri Feb 24 13:38:20 2006