2 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license
\r
6 CKEDITOR.plugins.add( 'removeformat',
\r
8 requires : [ 'selection' ],
\r
10 init : function( editor )
\r
12 editor.addCommand( 'removeFormat', CKEDITOR.plugins.removeformat.commands.removeformat );
\r
13 editor.ui.addButton( 'RemoveFormat',
\r
15 label : editor.lang.removeFormat,
\r
16 command : 'removeFormat'
\r
19 editor._.removeFormat = { filters: [] };
\r
23 CKEDITOR.plugins.removeformat =
\r
29 exec : function( editor )
\r
31 var tagsRegex = editor._.removeFormatRegex ||
\r
32 ( editor._.removeFormatRegex = new RegExp( '^(?:' + editor.config.removeFormatTags.replace( /,/g,'|' ) + ')$', 'i' ) );
\r
34 var removeAttributes = editor._.removeAttributes ||
\r
35 ( editor._.removeAttributes = editor.config.removeFormatAttributes.split( ',' ) );
\r
37 var filter = CKEDITOR.plugins.removeformat.filter;
\r
38 var ranges = editor.getSelection().getRanges( 1 ),
\r
39 iterator = ranges.createIterator(),
\r
42 while ( ( range = iterator.getNextRange() ) )
\r
44 if ( ! range.collapsed )
\r
45 range.enlarge( CKEDITOR.ENLARGE_ELEMENT );
\r
47 // Bookmark the range so we can re-select it after processing.
\r
48 var bookmark = range.createBookmark(),
\r
49 // The style will be applied within the bookmark boundaries.
\r
50 startNode = bookmark.startNode,
\r
51 endNode = bookmark.endNode,
\r
54 // We need to check the selection boundaries (bookmark spans) to break
\r
55 // the code in a way that we can properly remove partially selected nodes.
\r
56 // For example, removing a <b> style from
\r
57 // <b>This is [some text</b> to show <b>the] problem</b>
\r
58 // ... where [ and ] represent the selection, must result:
\r
59 // <b>This is </b>[some text to show the]<b> problem</b>
\r
60 // The strategy is simple, we just break the partial nodes before the
\r
61 // removal logic, having something that could be represented this way:
\r
62 // <b>This is </b>[<b>some text</b> to show <b>the</b>]<b> problem</b>
\r
64 var breakParent = function( node )
\r
66 // Let's start checking the start boundary.
\r
67 var path = new CKEDITOR.dom.elementPath( node ),
\r
68 pathElements = path.elements;
\r
70 for ( var i = 1, pathElement ; pathElement = pathElements[ i ] ; i++ )
\r
72 if ( pathElement.equals( path.block ) || pathElement.equals( path.blockLimit ) )
\r
75 // If this element can be removed (even partially).
\r
76 if ( tagsRegex.test( pathElement.getName() ) && filter( editor, pathElement ) )
\r
77 node.breakParent( pathElement );
\r
81 breakParent( startNode );
\r
84 breakParent( endNode );
\r
86 // Navigate through all nodes between the bookmarks.
\r
87 currentNode = startNode.getNextSourceNode( true, CKEDITOR.NODE_ELEMENT );
\r
89 while ( currentNode )
\r
91 // If we have reached the end of the selection, stop looping.
\r
92 if ( currentNode.equals( endNode ) )
\r
95 // Cache the next node to be processed. Do it now, because
\r
96 // currentNode may be removed.
\r
97 var nextNode = currentNode.getNextSourceNode( false, CKEDITOR.NODE_ELEMENT );
\r
99 // This node must not be a fake element.
\r
100 if ( !( currentNode.getName() == 'img'
\r
101 && currentNode.data( 'cke-realelement' ) )
\r
102 && filter( editor, currentNode ) )
\r
104 // Remove elements nodes that match with this style rules.
\r
105 if ( tagsRegex.test( currentNode.getName() ) )
\r
106 currentNode.remove( 1 );
\r
109 currentNode.removeAttributes( removeAttributes );
\r
110 editor.fire( 'removeFormatCleanup', currentNode );
\r
114 currentNode = nextNode;
\r
118 range.moveToBookmark( bookmark );
\r
121 editor.getSelection().selectRanges( ranges );
\r
127 * Perform the remove format filters on the passed element.
\r
128 * @param {CKEDITOR.editor} editor
\r
129 * @param {CKEDITOR.dom.element} element
\r
131 filter : function ( editor, element )
\r
133 var filters = editor._.removeFormat.filters;
\r
134 for ( var i = 0; i < filters.length; i++ )
\r
136 if ( filters[ i ]( element ) === false )
\r
144 * Add to a collection of functions to decide whether a specific
\r
145 * element should be considered as formatting element and thus
\r
146 * could be removed during <b>removeFormat</b> command,
\r
147 * Note: Only available with the existence of 'removeformat' plugin.
\r
149 * @param {Function} func The function to be called, which will be passed a {CKEDITOR.dom.element} element to test.
\r
151 * // Don't remove empty span
\r
152 * editor.addRemoveFormatFilter.push( function( element )
\r
154 * return !( element.is( 'span' ) && CKEDITOR.tools.isEmpty( element.getAttributes() ) );
\r
157 CKEDITOR.editor.prototype.addRemoveFormatFilter = function( func )
\r
159 this._.removeFormat.filters.push( func );
\r
163 * A comma separated list of elements to be removed when executing the "remove
\r
164 " format" command. Note that only inline elements are allowed.
\r
166 * @default 'b,big,code,del,dfn,em,font,i,ins,kbd,q,samp,small,span,strike,strong,sub,sup,tt,u,var'
\r
169 CKEDITOR.config.removeFormatTags = 'b,big,code,del,dfn,em,font,i,ins,kbd,q,samp,small,span,strike,strong,sub,sup,tt,u,var';
\r
172 * A comma separated list of elements attributes to be removed when executing
\r
173 * the "remove format" command.
\r
175 * @default 'class,style,lang,width,height,align,hspace,valign'
\r
178 CKEDITOR.config.removeFormatAttributes = 'class,style,lang,width,height,align,hspace,valign';
\r
181 * Fired after an element was cleaned by the removeFormat plugin.
\r
182 * @name CKEDITOR.editor#removeFormatCleanup
\r
184 * @param {Object} data.element The element that was cleaned up.
\r