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