JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.6.3
[ckeditor.git] / _source / plugins / removeformat / plugin.js
1 /*\r
2 Copyright (c) 2003-2012, 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( 1 ),\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                                                 range.enlarge( CKEDITOR.ENLARGE_ELEMENT );\r
46 \r
47                                         // Bookmark the range so we can re-select it after processing.\r
48                                         var bookmark = range.createBookmark(),\r
49                                                 // The style will be applied within the bookmark boundaries.\r
50                                                 startNode       = bookmark.startNode,\r
51                                                 endNode         = bookmark.endNode,\r
52                                                 currentNode;\r
53 \r
54                                         // We need to check the selection boundaries (bookmark spans) to break\r
55                                         // the code in a way that we can properly remove partially selected nodes.\r
56                                         // For example, removing a <b> style from\r
57                                         //              <b>This is [some text</b> to show <b>the] problem</b>\r
58                                         // ... where [ and ] represent the selection, must result:\r
59                                         //              <b>This is </b>[some text to show the]<b> problem</b>\r
60                                         // The strategy is simple, we just break the partial nodes before the\r
61                                         // removal logic, having something that could be represented this way:\r
62                                         //              <b>This is </b>[<b>some text</b> to show <b>the</b>]<b> problem</b>\r
63 \r
64                                         var breakParent = function( node )\r
65                                         {\r
66                                                 // Let's start checking the start boundary.\r
67                                                 var path = new CKEDITOR.dom.elementPath( node ),\r
68                                                         pathElements = path.elements;\r
69 \r
70                                                 for ( var i = 1, pathElement ; pathElement = pathElements[ i ] ; i++ )\r
71                                                 {\r
72                                                         if ( pathElement.equals( path.block ) || pathElement.equals( path.blockLimit ) )\r
73                                                                 break;\r
74 \r
75                                                         // If this element can be removed (even partially).\r
76                                                         if ( tagsRegex.test( pathElement.getName() ) && filter( editor, pathElement ) )\r
77                                                                 node.breakParent( pathElement );\r
78                                                 }\r
79                                         };\r
80 \r
81                                         breakParent( startNode );\r
82                                         if ( endNode )\r
83                                         {\r
84                                                 breakParent( endNode );\r
85 \r
86                                                 // Navigate through all nodes between the bookmarks.\r
87                                                 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.data( '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( 1 );\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 \r
118                                         range.moveToBookmark( bookmark );\r
119                                 }\r
120 \r
121                                 editor.getSelection().selectRanges( ranges );\r
122                         }\r
123                 }\r
124         },\r
125 \r
126         /**\r
127          * Perform the remove format filters on the passed element.\r
128          * @param {CKEDITOR.editor} editor\r
129          * @param {CKEDITOR.dom.element} element\r
130          */\r
131         filter : function ( editor, element )\r
132         {\r
133                 var filters = editor._.removeFormat.filters;\r
134                 for ( var i = 0; i < filters.length; i++ )\r
135                 {\r
136                         if ( filters[ i ]( element ) === false )\r
137                                 return false;\r
138                 }\r
139                 return true;\r
140         }\r
141 };\r
142 \r
143 /**\r
144  * Add to a collection of functions to decide whether a specific\r
145  * element should be considered as formatting element and thus\r
146  * could be removed during <b>removeFormat</b> command,\r
147  * Note: Only available with the existence of 'removeformat' plugin.\r
148  * @since 3.3\r
149  * @param {Function} func The function to be called, which will be passed a {CKEDITOR.dom.element} element to test.\r
150  * @example\r
151  *  // Don't remove empty span\r
152  *  editor.addRemoveFormatFilter.push( function( element )\r
153  *              {\r
154  *                      return !( element.is( 'span' ) && CKEDITOR.tools.isEmpty( element.getAttributes() ) );\r
155  *              });\r
156  */\r
157 CKEDITOR.editor.prototype.addRemoveFormatFilter = function( func )\r
158 {\r
159         this._.removeFormat.filters.push( func );\r
160 };\r
161 \r
162 /**\r
163  * A comma separated list of elements to be removed when executing the "remove\r
164  " format" command. Note that only inline elements are allowed.\r
165  * @type String\r
166  * @default 'b,big,code,del,dfn,em,font,i,ins,kbd,q,samp,small,span,strike,strong,sub,sup,tt,u,var'\r
167  * @example\r
168  */\r
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';\r
170 \r
171 /**\r
172  * A comma separated list of elements attributes to be removed when executing\r
173  * the "remove format" command.\r
174  * @type String\r
175  * @default 'class,style,lang,width,height,align,hspace,valign'\r
176  * @example\r
177  */\r
178 CKEDITOR.config.removeFormatAttributes = 'class,style,lang,width,height,align,hspace,valign';\r
179 \r
180 /**\r
181  * Fired after an element was cleaned by the removeFormat plugin.\r
182  * @name CKEDITOR.editor#removeFormatCleanup\r
183  * @event\r
184  * @param {Object} data.element The element that was cleaned up.\r
185  */\r