JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.0
[ckeditor.git] / _source / plugins / blockquote / plugin.js
diff --git a/_source/plugins/blockquote/plugin.js b/_source/plugins/blockquote/plugin.js
new file mode 100644 (file)
index 0000000..4015bee
--- /dev/null
@@ -0,0 +1,301 @@
+/*\r
+Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.\r
+For licensing, see LICENSE.html or http://ckeditor.com/license\r
+*/\r
+\r
+/**\r
+ * @file Blockquote.\r
+ */\r
+\r
+(function()\r
+{\r
+       function getState( editor, path )\r
+       {\r
+               var firstBlock = path.block || path.blockLimit;\r
+\r
+               if ( !firstBlock || firstBlock.getName() == 'body' )\r
+                       return CKEDITOR.TRISTATE_OFF;\r
+\r
+               // See if the first block has a blockquote parent.\r
+               if ( firstBlock.getAscendant( 'blockquote', true ) )\r
+                       return CKEDITOR.TRISTATE_ON;\r
+\r
+               return CKEDITOR.TRISTATE_OFF;\r
+       }\r
+\r
+       function onSelectionChange( evt )\r
+       {\r
+               var editor = evt.editor,\r
+                       command = editor.getCommand( 'blockquote' );\r
+               command.state = getState( editor, evt.data.path );\r
+               command.fire( 'state' );\r
+       }\r
+\r
+       function noBlockLeft( bqBlock )\r
+       {\r
+               for ( var i = 0, length = bqBlock.getChildCount(), child ; i < length && ( child = bqBlock.getChild( i ) ) ; i++ )\r
+               {\r
+                       if ( child.type == CKEDITOR.NODE_ELEMENT && child.isBlockBoundary() )\r
+                               return false;\r
+               }\r
+               return true;\r
+       }\r
+\r
+       var commandObject =\r
+       {\r
+               exec : function( editor )\r
+               {\r
+                       var state = editor.getCommand( 'blockquote' ).state,\r
+                               selection = editor.getSelection(),\r
+                               range = selection && selection.getRanges()[0];\r
+\r
+                       if ( !range )\r
+                               return;\r
+\r
+                       var bookmarks = selection.createBookmarks();\r
+\r
+                       // Kludge for #1592: if the bookmark nodes are in the beginning of\r
+                       // blockquote, then move them to the nearest block element in the\r
+                       // blockquote.\r
+                       if ( CKEDITOR.env.ie )\r
+                       {\r
+                               var bookmarkStart = bookmarks[0].startNode,\r
+                                       bookmarkEnd = bookmarks[0].endNode,\r
+                                       cursor;\r
+\r
+                               if ( bookmarkStart && bookmarkStart.getParent().getName() == 'blockquote' )\r
+                               {\r
+                                       cursor = bookmarkStart;\r
+                                       while ( ( cursor = cursor.getNext() ) )\r
+                                       {\r
+                                               if ( cursor.type == CKEDITOR.NODE_ELEMENT &&\r
+                                                               cursor.isBlockBoundary() )\r
+                                               {\r
+                                                       bookmarkStart.move( cursor, true );\r
+                                                       break;\r
+                                               }\r
+                                       }\r
+                               }\r
+\r
+                               if ( bookmarkEnd\r
+                                               && bookmarkEnd.getParent().getName() == 'blockquote' )\r
+                               {\r
+                                       cursor = bookmarkEnd;\r
+                                       while ( ( cursor = cursor.getPrevious() ) )\r
+                                       {\r
+                                               if ( cursor.type == CKEDITOR.NODE_ELEMENT &&\r
+                                                               cursor.isBlockBoundary() )\r
+                                               {\r
+                                                       bookmarkEnd.move( cursor );\r
+                                                       break;\r
+                                               }\r
+                                       }\r
+                               }\r
+                       }\r
+\r
+                       var iterator = range.createIterator(),\r
+                               block;\r
+\r
+                       if ( state == CKEDITOR.TRISTATE_OFF )\r
+                       {\r
+                               var paragraphs = [];\r
+                               while ( ( block = iterator.getNextParagraph() ) )\r
+                                       paragraphs.push( block );\r
+\r
+                               // If no paragraphs, create one from the current selection position.\r
+                               if ( paragraphs.length < 1 )\r
+                               {\r
+                                       var para = editor.document.createElement( editor.config.enterMode == CKEDITOR.ENTER_P ? 'p' : 'div' ),\r
+                                               firstBookmark = bookmarks.shift();\r
+                                       range.insertNode( para );\r
+                                       para.append( new CKEDITOR.dom.text( '\ufeff', editor.document ) );\r
+                                       range.moveToBookmark( firstBookmark );\r
+                                       range.selectNodeContents( para );\r
+                                       range.collapse( true );\r
+                                       firstBookmark = range.createBookmark();\r
+                                       paragraphs.push( para );\r
+                                       bookmarks.unshift( firstBookmark );\r
+                               }\r
+\r
+                               // Make sure all paragraphs have the same parent.\r
+                               var commonParent = paragraphs[0].getParent(),\r
+                                       tmp = [];\r
+                               for ( var i = 0 ; i < paragraphs.length ; i++ )\r
+                               {\r
+                                       block = paragraphs[i];\r
+                                       commonParent = commonParent.getCommonAncestor( block.getParent() );\r
+                               }\r
+\r
+                               // The common parent must not be the following tags: table, tbody, tr, ol, ul.\r
+                               var denyTags = { table : 1, tbody : 1, tr : 1, ol : 1, ul : 1 };\r
+                               while ( denyTags[ commonParent.getName() ] )\r
+                                       commonParent = commonParent.getParent();\r
+\r
+                               // Reconstruct the block list to be processed such that all resulting blocks\r
+                               // satisfy parentNode.equals( commonParent ).\r
+                               var lastBlock = null;\r
+                               while ( paragraphs.length > 0 )\r
+                               {\r
+                                       block = paragraphs.shift();\r
+                                       while ( !block.getParent().equals( commonParent ) )\r
+                                               block = block.getParent();\r
+                                       if ( !block.equals( lastBlock ) )\r
+                                               tmp.push( block );\r
+                                       lastBlock = block;\r
+                               }\r
+\r
+                               // If any of the selected blocks is a blockquote, remove it to prevent\r
+                               // nested blockquotes.\r
+                               while ( tmp.length > 0 )\r
+                               {\r
+                                       block = tmp.shift();\r
+                                       if ( block.getName() == 'blockquote' )\r
+                                       {\r
+                                               var docFrag = new CKEDITOR.dom.documentFragment( editor.document );\r
+                                               while ( block.getFirst() )\r
+                                               {\r
+                                                       docFrag.append( block.getFirst().remove() );\r
+                                                       paragraphs.push( docFrag.getLast() );\r
+                                               }\r
+\r
+                                               docFrag.replace( block );\r
+                                       }\r
+                                       else\r
+                                               paragraphs.push( block );\r
+                               }\r
+\r
+                               // Now we have all the blocks to be included in a new blockquote node.\r
+                               var bqBlock = editor.document.createElement( 'blockquote' );\r
+                               bqBlock.insertBefore( paragraphs[0] );\r
+                               while ( paragraphs.length > 0 )\r
+                               {\r
+                                       block = paragraphs.shift();\r
+                                       bqBlock.append( block );\r
+                               }\r
+                       }\r
+                       else if ( state == CKEDITOR.TRISTATE_ON )\r
+                       {\r
+                               var moveOutNodes = [],\r
+                                       database = {};\r
+\r
+                               while ( ( block = iterator.getNextParagraph() ) )\r
+                               {\r
+                                       var bqParent = null,\r
+                                               bqChild = null;\r
+                                       while ( block.getParent() )\r
+                                       {\r
+                                               if ( block.getParent().getName() == 'blockquote' )\r
+                                               {\r
+                                                       bqParent = block.getParent();\r
+                                                       bqChild = block;\r
+                                                       break;\r
+                                               }\r
+                                               block = block.getParent();\r
+                                       }\r
+\r
+                                       // Remember the blocks that were recorded down in the moveOutNodes array\r
+                                       // to prevent duplicates.\r
+                                       if ( bqParent && bqChild && !bqChild.getCustomData( 'blockquote_moveout' ) )\r
+                                       {\r
+                                               moveOutNodes.push( bqChild );\r
+                                               CKEDITOR.dom.element.setMarker( database, bqChild, 'blockquote_moveout', true );\r
+                                       }\r
+                               }\r
+\r
+                               CKEDITOR.dom.element.clearAllMarkers( database );\r
+\r
+                               var movedNodes = [],\r
+                                       processedBlockquoteBlocks = [];\r
+\r
+                               database = {};\r
+                               while ( moveOutNodes.length > 0 )\r
+                               {\r
+                                       var node = moveOutNodes.shift();\r
+                                       bqBlock = node.getParent();\r
+\r
+                                       // If the node is located at the beginning or the end, just take it out\r
+                                       // without splitting. Otherwise, split the blockquote node and move the\r
+                                       // paragraph in between the two blockquote nodes.\r
+                                       if ( !node.getPrevious() )\r
+                                               node.remove().insertBefore( bqBlock );\r
+                                       else if ( !node.getNext() )\r
+                                               node.remove().insertAfter( bqBlock );\r
+                                       else\r
+                                       {\r
+                                               node.breakParent( node.getParent() );\r
+                                               processedBlockquoteBlocks.push( node.getNext() );\r
+                                       }\r
+\r
+                                       // Remember the blockquote node so we can clear it later (if it becomes empty).\r
+                                       if ( !bqBlock.getCustomData( 'blockquote_processed' ) )\r
+                                       {\r
+                                               processedBlockquoteBlocks.push( bqBlock );\r
+                                               CKEDITOR.dom.element.setMarker( database, bqBlock, 'blockquote_processed', true );\r
+                                       }\r
+\r
+                                       movedNodes.push( node );\r
+                               }\r
+\r
+                               CKEDITOR.dom.element.clearAllMarkers( database );\r
+\r
+                               // Clear blockquote nodes that have become empty.\r
+                               for ( i = processedBlockquoteBlocks.length - 1 ; i >= 0 ; i-- )\r
+                               {\r
+                                       bqBlock = processedBlockquoteBlocks[i];\r
+                                       if ( noBlockLeft( bqBlock ) )\r
+                                               bqBlock.remove();\r
+                               }\r
+\r
+                               if ( editor.config.enterMode == CKEDITOR.ENTER_BR )\r
+                               {\r
+                                       var firstTime = true;\r
+                                       while ( movedNodes.length )\r
+                                       {\r
+                                               node = movedNodes.shift();\r
+\r
+                                               if ( node.getName() == 'div' )\r
+                                               {\r
+                                                       docFrag = new CKEDITOR.dom.documentFragment( editor.document );\r
+                                                       var needBeginBr = firstTime && node.getPrevious() &&\r
+                                                                       !( node.getPrevious().type == CKEDITOR.NODE_ELEMENT && node.getPrevious().isBlockBoundary() );\r
+                                                       if ( needBeginBr )\r
+                                                               docFrag.append( editor.document.createElement( 'br' ) );\r
+\r
+                                                       var needEndBr = node.getNext() &&\r
+                                                               !( node.getNext().type == CKEDITOR.NODE_ELEMENT && node.getNext().isBlockBoundary() );\r
+                                                       while ( node.getFirst() )\r
+                                                               node.getFirst().remove().appendTo( docFrag );\r
+\r
+                                                       if ( needEndBr )\r
+                                                               docFrag.append( editor.document.createElement( 'br' ) );\r
+\r
+                                                       docFrag.replace( node );\r
+                                                       firstTime = false;\r
+                                               }\r
+                                       }\r
+                               }\r
+                       }\r
+\r
+                       selection.selectBookmarks( bookmarks );\r
+                       editor.focus();\r
+               }\r
+       };\r
+\r
+       CKEDITOR.plugins.add( 'blockquote',\r
+       {\r
+               init : function( editor )\r
+               {\r
+                       editor.addCommand( 'blockquote', commandObject );\r
+\r
+                       editor.ui.addButton( 'Blockquote',\r
+                               {\r
+                                       label : editor.lang.blockquote,\r
+                                       command : 'blockquote'\r
+                               } );\r
+\r
+                       editor.on( 'selectionChange', onSelectionChange );\r
+               },\r
+\r
+               requires : [ 'domiterator' ]\r
+       } );\r
+})();\r