1 /*
  2 Copyright (c) 2003-2012, CKSource - Frederico Knabben. All rights reserved.
  3 For licensing, see LICENSE.html or http://ckeditor.com/license
  4 */
  5 
  6 CKEDITOR.plugins.add( 'removeformat',
  7 {
  8 	requires : [ 'selection' ],
  9 
 10 	init : function( editor )
 11 	{
 12 		editor.addCommand( 'removeFormat', CKEDITOR.plugins.removeformat.commands.removeformat );
 13 		editor.ui.addButton( 'RemoveFormat',
 14 			{
 15 				label : editor.lang.removeFormat,
 16 				command : 'removeFormat'
 17 			});
 18 
 19 		editor._.removeFormat = { filters: [] };
 20 	}
 21 });
 22 
 23 CKEDITOR.plugins.removeformat =
 24 {
 25 	commands :
 26 	{
 27 		removeformat :
 28 		{
 29 			exec : function( editor )
 30 			{
 31 				var tagsRegex = editor._.removeFormatRegex ||
 32 					( editor._.removeFormatRegex = new RegExp( '^(?:' + editor.config.removeFormatTags.replace( /,/g,'|' ) + ')$', 'i' ) );
 33 
 34 				var removeAttributes = editor._.removeAttributes ||
 35 					( editor._.removeAttributes = editor.config.removeFormatAttributes.split( ',' ) );
 36 
 37 				var filter = CKEDITOR.plugins.removeformat.filter;
 38 				var ranges = editor.getSelection().getRanges( 1 ),
 39 					iterator = ranges.createIterator(),
 40 					range;
 41 
 42 				while ( ( range = iterator.getNextRange() ) )
 43 				{
 44 					if ( ! range.collapsed )
 45 						range.enlarge( CKEDITOR.ENLARGE_ELEMENT );
 46 
 47 					// Bookmark the range so we can re-select it after processing.
 48 					var bookmark = range.createBookmark(),
 49 						// The style will be applied within the bookmark boundaries.
 50 						startNode	= bookmark.startNode,
 51 						endNode		= bookmark.endNode,
 52 						currentNode;
 53 
 54 					// We need to check the selection boundaries (bookmark spans) to break
 55 					// the code in a way that we can properly remove partially selected nodes.
 56 					// For example, removing a <b> style from
 57 					//		<b>This is [some text</b> to show <b>the] problem</b>
 58 					// ... where [ and ] represent the selection, must result:
 59 					//		<b>This is </b>[some text to show the]<b> problem</b>
 60 					// The strategy is simple, we just break the partial nodes before the
 61 					// removal logic, having something that could be represented this way:
 62 					//		<b>This is </b>[<b>some text</b> to show <b>the</b>]<b> problem</b>
 63 
 64 					var breakParent = function( node )
 65 					{
 66 						// Let's start checking the start boundary.
 67 						var path = new CKEDITOR.dom.elementPath( node ),
 68 							pathElements = path.elements;
 69 
 70 						for ( var i = 1, pathElement ; pathElement = pathElements[ i ] ; i++ )
 71 						{
 72 							if ( pathElement.equals( path.block ) || pathElement.equals( path.blockLimit ) )
 73 								break;
 74 
 75 							// If this element can be removed (even partially).
 76 							if ( tagsRegex.test( pathElement.getName() ) && filter( editor, pathElement ) )
 77 								node.breakParent( pathElement );
 78 						}
 79 					};
 80 
 81 					breakParent( startNode );
 82 					if ( endNode )
 83 					{
 84 						breakParent( endNode );
 85 
 86 						// Navigate through all nodes between the bookmarks.
 87 						currentNode = startNode.getNextSourceNode( true, CKEDITOR.NODE_ELEMENT );
 88 
 89 						while ( currentNode )
 90 						{
 91 							// If we have reached the end of the selection, stop looping.
 92 							if ( currentNode.equals( endNode ) )
 93 								break;
 94 
 95 							// Cache the next node to be processed. Do it now, because
 96 							// currentNode may be removed.
 97 							var nextNode = currentNode.getNextSourceNode( false, CKEDITOR.NODE_ELEMENT );
 98 
 99 							// This node must not be a fake element.
100 							if ( !( currentNode.getName() == 'img'
101 								&& currentNode.data( 'cke-realelement' ) )
102 								&& filter( editor, currentNode ) )
103 							{
104 								// Remove elements nodes that match with this style rules.
105 								if ( tagsRegex.test( currentNode.getName() ) )
106 									currentNode.remove( 1 );
107 								else
108 								{
109 									currentNode.removeAttributes( removeAttributes );
110 									editor.fire( 'removeFormatCleanup', currentNode );
111 								}
112 							}
113 
114 							currentNode = nextNode;
115 						}
116 					}
117 
118 					range.moveToBookmark( bookmark );
119 				}
120 
121 				editor.getSelection().selectRanges( ranges );
122 			}
123 		}
124 	},
125 
126 	/**
127 	 * Perform the remove format filters on the passed element.
128 	 * @param {CKEDITOR.editor} editor
129 	 * @param {CKEDITOR.dom.element} element
130 	 */
131 	filter : function ( editor, element )
132 	{
133 		var filters = editor._.removeFormat.filters;
134 		for ( var i = 0; i < filters.length; i++ )
135 		{
136 			if ( filters[ i ]( element ) === false )
137 				return false;
138 		}
139 		return true;
140 	}
141 };
142 
143 /**
144  * Add to a collection of functions to decide whether a specific
145  * element should be considered as formatting element and thus
146  * could be removed during <b>removeFormat</b> command,
147  * Note: Only available with the existence of 'removeformat' plugin.
148  * @since 3.3
149  * @param {Function} func The function to be called, which will be passed a {CKEDITOR.dom.element} element to test.
150  * @example
151  *  // Don't remove empty span
152  *  editor.addRemoveFormatFilter.push( function( element )
153  *		{
154  *			return !( element.is( 'span' ) && CKEDITOR.tools.isEmpty( element.getAttributes() ) );
155  *		});
156  */
157 CKEDITOR.editor.prototype.addRemoveFormatFilter = function( func )
158 {
159 	this._.removeFormat.filters.push( func );
160 };
161 
162 /**
163  * A comma separated list of elements to be removed when executing the "remove
164  " format" command. Note that only inline elements are allowed.
165  * @type String
166  * @default 'b,big,code,del,dfn,em,font,i,ins,kbd,q,samp,small,span,strike,strong,sub,sup,tt,u,var'
167  * @example
168  */
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';
170 
171 /**
172  * A comma separated list of elements attributes to be removed when executing
173  * the "remove format" command.
174  * @type String
175  * @default 'class,style,lang,width,height,align,hspace,valign'
176  * @example
177  */
178 CKEDITOR.config.removeFormatAttributes = 'class,style,lang,width,height,align,hspace,valign';
179 
180 /**
181  * Fired after an element was cleaned by the removeFormat plugin.
182  * @name CKEDITOR.editor#removeFormatCleanup
183  * @event
184  * @param {Object} data.element The element that was cleaned up.
185  */
186