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