2 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license
\r
12 function getState( editor, path )
\r
14 var firstBlock = path.block || path.blockLimit;
\r
16 if ( !firstBlock || firstBlock.getName() == 'body' )
\r
17 return CKEDITOR.TRISTATE_OFF;
\r
19 // See if the first block has a blockquote parent.
\r
20 if ( firstBlock.getAscendant( 'blockquote', true ) )
\r
21 return CKEDITOR.TRISTATE_ON;
\r
23 return CKEDITOR.TRISTATE_OFF;
\r
26 function onSelectionChange( evt )
\r
28 var editor = evt.editor,
\r
29 command = editor.getCommand( 'blockquote' );
\r
30 command.state = getState( editor, evt.data.path );
\r
31 command.fire( 'state' );
\r
34 function noBlockLeft( bqBlock )
\r
36 for ( var i = 0, length = bqBlock.getChildCount(), child ; i < length && ( child = bqBlock.getChild( i ) ) ; i++ )
\r
38 if ( child.type == CKEDITOR.NODE_ELEMENT && child.isBlockBoundary() )
\r
46 exec : function( editor )
\r
48 var state = editor.getCommand( 'blockquote' ).state,
\r
49 selection = editor.getSelection(),
\r
50 range = selection && selection.getRanges( true )[0];
\r
55 var bookmarks = selection.createBookmarks();
\r
57 // Kludge for #1592: if the bookmark nodes are in the beginning of
\r
58 // blockquote, then move them to the nearest block element in the
\r
60 if ( CKEDITOR.env.ie )
\r
62 var bookmarkStart = bookmarks[0].startNode,
\r
63 bookmarkEnd = bookmarks[0].endNode,
\r
66 if ( bookmarkStart && bookmarkStart.getParent().getName() == 'blockquote' )
\r
68 cursor = bookmarkStart;
\r
69 while ( ( cursor = cursor.getNext() ) )
\r
71 if ( cursor.type == CKEDITOR.NODE_ELEMENT &&
\r
72 cursor.isBlockBoundary() )
\r
74 bookmarkStart.move( cursor, true );
\r
81 && bookmarkEnd.getParent().getName() == 'blockquote' )
\r
83 cursor = bookmarkEnd;
\r
84 while ( ( cursor = cursor.getPrevious() ) )
\r
86 if ( cursor.type == CKEDITOR.NODE_ELEMENT &&
\r
87 cursor.isBlockBoundary() )
\r
89 bookmarkEnd.move( cursor );
\r
96 var iterator = range.createIterator(),
\r
98 iterator.enlargeBr = editor.config.enterMode != CKEDITOR.ENTER_BR;
\r
100 if ( state == CKEDITOR.TRISTATE_OFF )
\r
102 var paragraphs = [];
\r
103 while ( ( block = iterator.getNextParagraph() ) )
\r
104 paragraphs.push( block );
\r
106 // If no paragraphs, create one from the current selection position.
\r
107 if ( paragraphs.length < 1 )
\r
109 var para = editor.document.createElement( editor.config.enterMode == CKEDITOR.ENTER_P ? 'p' : 'div' ),
\r
110 firstBookmark = bookmarks.shift();
\r
111 range.insertNode( para );
\r
112 para.append( new CKEDITOR.dom.text( '\ufeff', editor.document ) );
\r
113 range.moveToBookmark( firstBookmark );
\r
114 range.selectNodeContents( para );
\r
115 range.collapse( true );
\r
116 firstBookmark = range.createBookmark();
\r
117 paragraphs.push( para );
\r
118 bookmarks.unshift( firstBookmark );
\r
121 // Make sure all paragraphs have the same parent.
\r
122 var commonParent = paragraphs[0].getParent(),
\r
124 for ( var i = 0 ; i < paragraphs.length ; i++ )
\r
126 block = paragraphs[i];
\r
127 commonParent = commonParent.getCommonAncestor( block.getParent() );
\r
130 // The common parent must not be the following tags: table, tbody, tr, ol, ul.
\r
131 var denyTags = { table : 1, tbody : 1, tr : 1, ol : 1, ul : 1 };
\r
132 while ( denyTags[ commonParent.getName() ] )
\r
133 commonParent = commonParent.getParent();
\r
135 // Reconstruct the block list to be processed such that all resulting blocks
\r
136 // satisfy parentNode.equals( commonParent ).
\r
137 var lastBlock = null;
\r
138 while ( paragraphs.length > 0 )
\r
140 block = paragraphs.shift();
\r
141 while ( !block.getParent().equals( commonParent ) )
\r
142 block = block.getParent();
\r
143 if ( !block.equals( lastBlock ) )
\r
148 // If any of the selected blocks is a blockquote, remove it to prevent
\r
149 // nested blockquotes.
\r
150 while ( tmp.length > 0 )
\r
152 block = tmp.shift();
\r
153 if ( block.getName() == 'blockquote' )
\r
155 var docFrag = new CKEDITOR.dom.documentFragment( editor.document );
\r
156 while ( block.getFirst() )
\r
158 docFrag.append( block.getFirst().remove() );
\r
159 paragraphs.push( docFrag.getLast() );
\r
162 docFrag.replace( block );
\r
165 paragraphs.push( block );
\r
168 // Now we have all the blocks to be included in a new blockquote node.
\r
169 var bqBlock = editor.document.createElement( 'blockquote' );
\r
170 bqBlock.insertBefore( paragraphs[0] );
\r
171 while ( paragraphs.length > 0 )
\r
173 block = paragraphs.shift();
\r
174 bqBlock.append( block );
\r
177 else if ( state == CKEDITOR.TRISTATE_ON )
\r
179 var moveOutNodes = [],
\r
182 while ( ( block = iterator.getNextParagraph() ) )
\r
184 var bqParent = null,
\r
186 while ( block.getParent() )
\r
188 if ( block.getParent().getName() == 'blockquote' )
\r
190 bqParent = block.getParent();
\r
194 block = block.getParent();
\r
197 // Remember the blocks that were recorded down in the moveOutNodes array
\r
198 // to prevent duplicates.
\r
199 if ( bqParent && bqChild && !bqChild.getCustomData( 'blockquote_moveout' ) )
\r
201 moveOutNodes.push( bqChild );
\r
202 CKEDITOR.dom.element.setMarker( database, bqChild, 'blockquote_moveout', true );
\r
206 CKEDITOR.dom.element.clearAllMarkers( database );
\r
208 var movedNodes = [],
\r
209 processedBlockquoteBlocks = [];
\r
212 while ( moveOutNodes.length > 0 )
\r
214 var node = moveOutNodes.shift();
\r
215 bqBlock = node.getParent();
\r
217 // If the node is located at the beginning or the end, just take it out
\r
218 // without splitting. Otherwise, split the blockquote node and move the
\r
219 // paragraph in between the two blockquote nodes.
\r
220 if ( !node.getPrevious() )
\r
221 node.remove().insertBefore( bqBlock );
\r
222 else if ( !node.getNext() )
\r
223 node.remove().insertAfter( bqBlock );
\r
226 node.breakParent( node.getParent() );
\r
227 processedBlockquoteBlocks.push( node.getNext() );
\r
230 // Remember the blockquote node so we can clear it later (if it becomes empty).
\r
231 if ( !bqBlock.getCustomData( 'blockquote_processed' ) )
\r
233 processedBlockquoteBlocks.push( bqBlock );
\r
234 CKEDITOR.dom.element.setMarker( database, bqBlock, 'blockquote_processed', true );
\r
237 movedNodes.push( node );
\r
240 CKEDITOR.dom.element.clearAllMarkers( database );
\r
242 // Clear blockquote nodes that have become empty.
\r
243 for ( i = processedBlockquoteBlocks.length - 1 ; i >= 0 ; i-- )
\r
245 bqBlock = processedBlockquoteBlocks[i];
\r
246 if ( noBlockLeft( bqBlock ) )
\r
250 if ( editor.config.enterMode == CKEDITOR.ENTER_BR )
\r
252 var firstTime = true;
\r
253 while ( movedNodes.length )
\r
255 node = movedNodes.shift();
\r
257 if ( node.getName() == 'div' )
\r
259 docFrag = new CKEDITOR.dom.documentFragment( editor.document );
\r
260 var needBeginBr = firstTime && node.getPrevious() &&
\r
261 !( node.getPrevious().type == CKEDITOR.NODE_ELEMENT && node.getPrevious().isBlockBoundary() );
\r
263 docFrag.append( editor.document.createElement( 'br' ) );
\r
265 var needEndBr = node.getNext() &&
\r
266 !( node.getNext().type == CKEDITOR.NODE_ELEMENT && node.getNext().isBlockBoundary() );
\r
267 while ( node.getFirst() )
\r
268 node.getFirst().remove().appendTo( docFrag );
\r
271 docFrag.append( editor.document.createElement( 'br' ) );
\r
273 docFrag.replace( node );
\r
280 selection.selectBookmarks( bookmarks );
\r
285 CKEDITOR.plugins.add( 'blockquote',
\r
287 init : function( editor )
\r
289 editor.addCommand( 'blockquote', commandObject );
\r
291 editor.ui.addButton( 'Blockquote',
\r
293 label : editor.lang.blockquote,
\r
294 command : 'blockquote'
\r
297 editor.on( 'selectionChange', onSelectionChange );
\r
300 requires : [ 'domiterator' ]
\r