2 Copyright (c) 2003-2009, 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
21 CKEDITOR.plugins.removeformat =
\r
27 exec : function( editor )
\r
29 var tagsRegex = editor._.removeFormatRegex ||
\r
30 ( editor._.removeFormatRegex = new RegExp( '^(?:' + editor.config.removeFormatTags.replace( /,/g,'|' ) + ')$', 'i' ) );
\r
32 var removeAttributes = editor._.removeAttributes ||
\r
33 ( editor._.removeAttributes = editor.config.removeFormatAttributes.split( ',' ) );
\r
35 var ranges = editor.getSelection().getRanges();
\r
37 for ( var i = 0, range ; range = ranges[ i ] ; i++ )
\r
39 if ( range.collapsed )
\r
42 range.enlarge( CKEDITOR.ENLARGE_ELEMENT );
\r
44 // Bookmark the range so we can re-select it after processing.
\r
45 var bookmark = range.createBookmark();
\r
47 // The style will be applied within the bookmark boundaries.
\r
48 var startNode = bookmark.startNode;
\r
49 var endNode = bookmark.endNode;
\r
51 // We need to check the selection boundaries (bookmark spans) to break
\r
52 // the code in a way that we can properly remove partially selected nodes.
\r
53 // For example, removing a <b> style from
\r
54 // <b>This is [some text</b> to show <b>the] problem</b>
\r
55 // ... where [ and ] represent the selection, must result:
\r
56 // <b>This is </b>[some text to show the]<b> problem</b>
\r
57 // The strategy is simple, we just break the partial nodes before the
\r
58 // removal logic, having something that could be represented this way:
\r
59 // <b>This is </b>[<b>some text</b> to show <b>the</b>]<b> problem</b>
\r
61 var breakParent = function( node )
\r
63 // Let's start checking the start boundary.
\r
64 var path = new CKEDITOR.dom.elementPath( node );
\r
65 var pathElements = path.elements;
\r
67 for ( var i = 1, pathElement ; pathElement = pathElements[ i ] ; i++ )
\r
69 if ( pathElement.equals( path.block ) || pathElement.equals( path.blockLimit ) )
\r
72 // If this element can be removed (even partially).
\r
73 if ( tagsRegex.test( pathElement.getName() ) )
\r
74 node.breakParent( pathElement );
\r
78 breakParent( startNode );
\r
79 breakParent( endNode );
\r
81 // Navigate through all nodes between the bookmarks.
\r
82 var currentNode = startNode.getNextSourceNode( true, CKEDITOR.NODE_ELEMENT );
\r
84 while ( currentNode )
\r
86 // If we have reached the end of the selection, stop looping.
\r
87 if ( currentNode.equals( endNode ) )
\r
90 // Cache the next node to be processed. Do it now, because
\r
91 // currentNode may be removed.
\r
92 var nextNode = currentNode.getNextSourceNode( false, CKEDITOR.NODE_ELEMENT );
\r
94 // This node must not be a fake element.
\r
95 if ( currentNode.getName() != 'img' || !currentNode.getAttribute( '_cke_protected_html' ) )
\r
97 // Remove elements nodes that match with this style rules.
\r
98 if ( tagsRegex.test( currentNode.getName() ) )
\r
99 currentNode.remove( true );
\r
101 currentNode.removeAttributes( removeAttributes );
\r
104 currentNode = nextNode;
\r
107 range.moveToBookmark( bookmark );
\r
110 editor.getSelection().selectRanges( ranges );
\r
117 * A comma separated list of elements to be removed when executing the "remove
\r
118 " format" command. Note that only inline elements are allowed.
\r
120 * @default 'b,big,code,del,dfn,em,font,i,ins,kbd,q,samp,small,span,strike,strong,sub,sup,tt,u,var'
\r
123 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
126 * A comma separated list of elements attributes to be removed when executing
\r
127 * the "remove format" command.
\r
129 * @default 'class,style,lang,width,height,align,hspace,valign'
\r
132 CKEDITOR.config.removeFormatAttributes = 'class,style,lang,width,height,align,hspace,valign';
\r