JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.6.3
[ckeditor.git] / _source / plugins / removeformat / plugin.js
index 97b858a..d031a65 100644 (file)
@@ -1,5 +1,5 @@
 /*\r
-Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.\r
+Copyright (c) 2003-2012, CKSource - Frederico Knabben. All rights reserved.\r
 For licensing, see LICENSE.html or http://ckeditor.com/license\r
 */\r
 \r
@@ -15,6 +15,8 @@ CKEDITOR.plugins.add( 'removeformat',
                                label : editor.lang.removeFormat,\r
                                command : 'removeFormat'\r
                        });\r
+\r
+               editor._.removeFormat = { filters: [] };\r
        }\r
 });\r
 \r
@@ -32,21 +34,22 @@ CKEDITOR.plugins.removeformat =
                                var removeAttributes = editor._.removeAttributes ||\r
                                        ( editor._.removeAttributes = editor.config.removeFormatAttributes.split( ',' ) );\r
 \r
-                               var ranges = editor.getSelection().getRanges();\r
+                               var filter = CKEDITOR.plugins.removeformat.filter;\r
+                               var ranges = editor.getSelection().getRanges( 1 ),\r
+                                       iterator = ranges.createIterator(),\r
+                                       range;\r
 \r
-                               for ( var i = 0, range ; range = ranges[ i ] ; i++ )\r
+                               while ( ( range = iterator.getNextRange() ) )\r
                                {\r
-                                       if ( range.collapsed )\r
-                                               continue;\r
-\r
-                                       range.enlarge( CKEDITOR.ENLARGE_ELEMENT );\r
+                                       if ( ! range.collapsed )\r
+                                               range.enlarge( CKEDITOR.ENLARGE_ELEMENT );\r
 \r
                                        // Bookmark the range so we can re-select it after processing.\r
-                                       var bookmark = range.createBookmark();\r
-\r
-                                       // The style will be applied within the bookmark boundaries.\r
-                                       var startNode   = bookmark.startNode;\r
-                                       var endNode             = bookmark.endNode;\r
+                                       var bookmark = range.createBookmark(),\r
+                                               // The style will be applied within the bookmark boundaries.\r
+                                               startNode       = bookmark.startNode,\r
+                                               endNode         = bookmark.endNode,\r
+                                               currentNode;\r
 \r
                                        // We need to check the selection boundaries (bookmark spans) to break\r
                                        // the code in a way that we can properly remove partially selected nodes.\r
@@ -61,8 +64,8 @@ CKEDITOR.plugins.removeformat =
                                        var breakParent = function( node )\r
                                        {\r
                                                // Let's start checking the start boundary.\r
-                                               var path = new CKEDITOR.dom.elementPath( node );\r
-                                               var pathElements = path.elements;\r
+                                               var path = new CKEDITOR.dom.elementPath( node ),\r
+                                                       pathElements = path.elements;\r
 \r
                                                for ( var i = 1, pathElement ; pathElement = pathElements[ i ] ; i++ )\r
                                                {\r
@@ -70,38 +73,46 @@ CKEDITOR.plugins.removeformat =
                                                                break;\r
 \r
                                                        // If this element can be removed (even partially).\r
-                                                       if ( tagsRegex.test( pathElement.getName() ) )\r
+                                                       if ( tagsRegex.test( pathElement.getName() ) && filter( editor, pathElement ) )\r
                                                                node.breakParent( pathElement );\r
                                                }\r
                                        };\r
 \r
                                        breakParent( startNode );\r
-                                       breakParent( endNode );\r
-\r
-                                       // Navigate through all nodes between the bookmarks.\r
-                                       var currentNode = startNode.getNextSourceNode( true, CKEDITOR.NODE_ELEMENT );\r
-\r
-                                       while ( currentNode )\r
+                                       if ( endNode )\r
                                        {\r
-                                               // If we have reached the end of the selection, stop looping.\r
-                                               if ( currentNode.equals( endNode ) )\r
-                                                       break;\r
+                                               breakParent( endNode );\r
 \r
-                                               // Cache the next node to be processed. Do it now, because\r
-                                               // currentNode may be removed.\r
-                                               var nextNode = currentNode.getNextSourceNode( false, CKEDITOR.NODE_ELEMENT );\r
+                                               // Navigate through all nodes between the bookmarks.\r
+                                               currentNode = startNode.getNextSourceNode( true, CKEDITOR.NODE_ELEMENT );\r
 \r
-                                               // This node must not be a fake element.\r
-                                               if ( !( currentNode.getName() == 'img' && currentNode.getAttribute( '_cke_realelement' ) ) )\r
+                                               while ( currentNode )\r
                                                {\r
-                                                       // Remove elements nodes that match with this style rules.\r
-                                                       if ( tagsRegex.test( currentNode.getName() ) )\r
-                                                               currentNode.remove( true );\r
-                                                       else\r
-                                                               currentNode.removeAttributes( removeAttributes );\r
-                                               }\r
+                                                       // If we have reached the end of the selection, stop looping.\r
+                                                       if ( currentNode.equals( endNode ) )\r
+                                                               break;\r
 \r
-                                               currentNode = nextNode;\r
+                                                       // Cache the next node to be processed. Do it now, because\r
+                                                       // currentNode may be removed.\r
+                                                       var nextNode = currentNode.getNextSourceNode( false, CKEDITOR.NODE_ELEMENT );\r
+\r
+                                                       // This node must not be a fake element.\r
+                                                       if ( !( currentNode.getName() == 'img'\r
+                                                               && currentNode.data( 'cke-realelement' ) )\r
+                                                               && filter( editor, currentNode ) )\r
+                                                       {\r
+                                                               // Remove elements nodes that match with this style rules.\r
+                                                               if ( tagsRegex.test( currentNode.getName() ) )\r
+                                                                       currentNode.remove( 1 );\r
+                                                               else\r
+                                                               {\r
+                                                                       currentNode.removeAttributes( removeAttributes );\r
+                                                                       editor.fire( 'removeFormatCleanup', currentNode );\r
+                                                               }\r
+                                                       }\r
+\r
+                                                       currentNode = nextNode;\r
+                                               }\r
                                        }\r
 \r
                                        range.moveToBookmark( bookmark );\r
@@ -110,10 +121,45 @@ CKEDITOR.plugins.removeformat =
                                editor.getSelection().selectRanges( ranges );\r
                        }\r
                }\r
+       },\r
+\r
+       /**\r
+        * Perform the remove format filters on the passed element.\r
+        * @param {CKEDITOR.editor} editor\r
+        * @param {CKEDITOR.dom.element} element\r
+        */\r
+       filter : function ( editor, element )\r
+       {\r
+               var filters = editor._.removeFormat.filters;\r
+               for ( var i = 0; i < filters.length; i++ )\r
+               {\r
+                       if ( filters[ i ]( element ) === false )\r
+                               return false;\r
+               }\r
+               return true;\r
        }\r
 };\r
 \r
 /**\r
+ * Add to a collection of functions to decide whether a specific\r
+ * element should be considered as formatting element and thus\r
+ * could be removed during <b>removeFormat</b> command,\r
+ * Note: Only available with the existence of 'removeformat' plugin.\r
+ * @since 3.3\r
+ * @param {Function} func The function to be called, which will be passed a {CKEDITOR.dom.element} element to test.\r
+ * @example\r
+ *  // Don't remove empty span\r
+ *  editor.addRemoveFormatFilter.push( function( element )\r
+ *             {\r
+ *                     return !( element.is( 'span' ) && CKEDITOR.tools.isEmpty( element.getAttributes() ) );\r
+ *             });\r
+ */\r
+CKEDITOR.editor.prototype.addRemoveFormatFilter = function( func )\r
+{\r
+       this._.removeFormat.filters.push( func );\r
+};\r
+\r
+/**\r
  * A comma separated list of elements to be removed when executing the "remove\r
  " format" command. Note that only inline elements are allowed.\r
  * @type String\r
@@ -130,3 +176,10 @@ CKEDITOR.config.removeFormatTags = 'b,big,code,del,dfn,em,font,i,ins,kbd,q,samp,
  * @example\r
  */\r
 CKEDITOR.config.removeFormatAttributes = 'class,style,lang,width,height,align,hspace,valign';\r
+\r
+/**\r
+ * Fired after an element was cleaned by the removeFormat plugin.\r
+ * @name CKEDITOR.editor#removeFormatCleanup\r
+ * @event\r
+ * @param {Object} data.element The element that was cleaned up.\r
+ */\r