source: [view]
try{
var ed = this.editor;
ed.focus();
var quoteIt = this.button.get("checked");
var sel = dijit.range.getSelection(ed.window);
var range, elem, start, end;
if(sel && sel.rangeCount > 0){
range = sel.getRangeAt(0);
}
if(range){
ed.beginEditing();
if(quoteIt){
// Lets see what we've got as a selection...
var bq, tag;
if(range.startContainer === range.endContainer){
// No selection, just cursor point, we need to see if we're
// in an indentable block, or similar.
if(this._isRootInline(range.startContainer)){
// Text at the 'root' of the document, so we need to gather all of it.,
// First, we need to find the toplevel inline element that is rooted
// to the document 'editNode'
start = range.startContainer;
while(start && start.parentNode !== ed.editNode){
start = start.parentNode;
}
// Now we need to walk up its siblings and look for the first one in the rooting
// that isn't inline or text, as we want to grab all of that for indent.
while(start && start.previousSibling && (
this._isTextElement(start) ||
(start.nodeType === 1 &&
this._isInlineFormat(this._getTagName(start))
))){
start = start.previousSibling;
}
if(start && start.nodeType === 1 &&
!this._isInlineFormat(this._getTagName(start))){
// Adjust slightly, we're one node too far back in this case.
start = start.nextSibling;
}
// Okay, we have a configured start, lets grab everything following it that's
// inline and make it part of the blockquote!
if(start){
bq = ed.document.createElement("blockquote");
dojo.place(bq, start, "after");
bq.appendChild(start);
end = bq.nextSibling;
while(end && (
this._isTextElement(end) ||
(end.nodeType === 1 &&
this._isInlineFormat(this._getTagName(end)))
)){
// Add it.
bq.appendChild(end);
end = bq.nextSibling;
}
}
}else{
// Figure out what to do when not root inline....
var node = range.startContainer;
while ((this._isTextElement(node) ||
this._isInlineFormat(this._getTagName(node))
|| this._getTagName(node) === "li") &&
node !== ed.editNode && node !== ed.document.body){
node = node.parentNode;
}
if(node !== ed.editNode && node !== node.ownerDocument.documentElement){
bq = ed.document.createElement("blockquote");
dojo.place(bq, node, "after");
bq.appendChild(node);
}
}
if(bq){
dojo.withGlobal(ed.window,
"selectElementChildren", dijit._editor.selection, [bq]);
dojo.withGlobal(ed.window,
"collapse", dijit._editor.selection, [true]);
}
}else{
var curNode;
// multi-node select. We need to scan over them.
// Find the two containing nodes at start and end.
// then move the end one node past. Then ... lets see
// what we can blockquote!
start = range.startContainer;
end = range.endContainer;
// Find the non-text nodes.
while(start && this._isTextElement(start) && start.parentNode !== ed.editNode){
start = start.parentNode;
}
// Try to find the end node. We have to check the selection junk
curNode = start;
while(curNode.nextSibling && dojo.withGlobal(ed.window,
"inSelection", dijit._editor.selection, [curNode])){
curNode = curNode.nextSibling;
}
end = curNode;
if(end === ed.editNode || end === ed.document.body){
// Unable to determine real selection end, so just make it
// a single node indent of start + all following inline styles, if
// present, then just exit.
bq = ed.document.createElement("blockquote");
dojo.place(bq, start, "after");
tag = this._getTagName(start);
if(this._isTextElement(start) || this._isInlineFormat(tag)){
// inline element or textnode
// Find and move all inline tags following the one we inserted also into the
// blockquote so we don't split up content funny.
var next = start;
while(next && (
this._isTextElement(next) ||
(next.nodeType === 1 &&
this._isInlineFormat(this._getTagName(next))))){
bq.appendChild(next);
next = bq.nextSibling;
}
}else{
bq.appendChild(start);
}
return;
}
// Has a definite end somewhere, so lets try to blockquote up to it.
// requires looking at the selections and in some cases, moving nodes
// into separate blockquotes.
end = end.nextSibling;
curNode = start;
while(curNode && curNode !== end){
if(curNode.nodeType === 1){
tag = this._getTagName(curNode);
if(tag !== "br"){
if(!window.getSelection){
// IE sometimes inserts blank P tags, which we want to skip
// as they end up blockquoted, which messes up layout.
if(tag === "p" && this._isEmpty(curNode)){
curNode = curNode.nextSibling;
continue;
}
}
if(this._isInlineFormat(tag)){
// inline tag.
if(!bq){
bq = ed.document.createElement("blockquote");
dojo.place(bq, curNode, "after");
bq.appendChild(curNode);
}else{
bq.appendChild(curNode);
}
curNode = bq;
}else{
if(bq){
if(this._isEmpty(bq)){
bq.parentNode.removeChild(bq);
}
}
bq = ed.document.createElement("blockquote");
dojo.place(bq, curNode, "after");
bq.appendChild(curNode);
curNode = bq;
}
}
}else if(this._isTextElement(curNode)){
if(!bq){
bq = ed.document.createElement("blockquote");
dojo.place(bq, curNode, "after");
bq.appendChild(curNode);
}else{
bq.appendChild(curNode);
}
curNode = bq;
}
curNode = curNode.nextSibling;
}
// Okay, check the last bq, remove it if no content.
if(bq){
if(this._isEmpty(bq)){
bq.parentNode.removeChild(bq);
}else{
dojo.withGlobal(ed.window,
"selectElementChildren", dijit._editor.selection, [bq]);
dojo.withGlobal(ed.window,
"collapse", dijit._editor.selection, [true]);
}
bq = null;
}
}
}else{
var found = false;
if(range.startContainer === range.endContainer){
elem = range.endContainer;
// Okay, now see if we can find one of the formatting types we're in.
while(elem && elem !== ed.editNode && elem !== ed.document.body){
var tg = elem.tagName?elem.tagName.toLowerCase():"";
if(tg === "blockquote"){
found = true;
break;
}
elem = elem.parentNode;
}
if(found){
var lastChild;
while(elem.firstChild){
lastChild = elem.firstChild;
dojo.place(lastChild, elem, "before");
}
elem.parentNode.removeChild(elem);
if(lastChild){
dojo.withGlobal(ed.window,
"selectElementChildren", dijit._editor.selection, [lastChild]);
dojo.withGlobal(ed.window,
"collapse", dijit._editor.selection, [true]);
}
}
}else{
// Multi-select! Gotta find all the blockquotes contained within the selection area.
start = range.startContainer;
end = range.endContainer;
while(start && this._isTextElement(start) && start.parentNode !== ed.editNode){
start = start.parentNode;
}
var selectedNodes = [];
var cNode = start;
while(cNode && cNode.nextSibling && dojo.withGlobal(ed.window,
"inSelection", dijit._editor.selection, [cNode])){
if(cNode.parentNode && this._getTagName(cNode.parentNode) === "blockquote"){
cNode = cNode.parentNode;
}
selectedNodes.push(cNode);
cNode = cNode.nextSibling;
}
// Find all the blocknodes now that we know the selection area.
var bnNodes = this._findBlockQuotes(selectedNodes);
while(bnNodes.length){
var bn = bnNodes.pop();
if(bn.parentNode){
// Make sure we haven't seen this before and removed it.
while(bn.firstChild){
dojo.place(bn.firstChild, bn, "before");
}
bn.parentNode.removeChild(bn);
}
}
}
}
ed.endEditing();
}
ed.onNormalizedDisplayChanged();
}catch(e){ /* Squelch */ }