JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.2.2
[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();\r
39 \r
40                                 for ( var i = 0, range ; range = ranges[ i ] ; i++ )\r
41                                 {\r
42                                         if ( range.collapsed )\r
43                                                 continue;\r
44 \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 \r
50                                         // The style will be applied within the bookmark boundaries.\r
51                                         var startNode   = bookmark.startNode;\r
52                                         var endNode             = bookmark.endNode;\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                                                 var 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                                         breakParent( endNode );\r
83 \r
84                                         // Navigate through all nodes between the bookmarks.\r
85                                         var currentNode = startNode.getNextSourceNode( true, CKEDITOR.NODE_ELEMENT );\r
86 \r
87                                         while ( currentNode )\r
88                                         {\r
89                                                 // If we have reached the end of the selection, stop looping.\r
90                                                 if ( currentNode.equals( endNode ) )\r
91                                                         break;\r
92 \r
93                                                 // Cache the next node to be processed. Do it now, because\r
94                                                 // currentNode may be removed.\r
95                                                 var nextNode = currentNode.getNextSourceNode( false, CKEDITOR.NODE_ELEMENT );\r
96 \r
97                                                 // This node must not be a fake element.\r
98                                                 if ( !( currentNode.getName() == 'img'\r
99                                                         && currentNode.getAttribute( '_cke_realelement' ) )\r
100                                                         && filter( editor, currentNode ) )\r
101                                                 {\r
102                                                         // Remove elements nodes that match with this style rules.\r
103                                                         if ( tagsRegex.test( currentNode.getName() ) )\r
104                                                                 currentNode.remove( true );\r
105                                                         else\r
106                                                                 currentNode.removeAttributes( removeAttributes );\r
107                                                 }\r
108 \r
109                                                 currentNode = nextNode;\r
110                                         }\r
111 \r
112                                         range.moveToBookmark( bookmark );\r
113                                 }\r
114 \r
115                                 editor.getSelection().selectRanges( ranges );\r
116                         }\r
117                 }\r
118         },\r
119 \r
120         /**\r
121          * Perform the remove format filters on the passed element.\r
122          * @param {CKEDITOR.editor} editor\r
123          * @param {CKEDITOR.dom.element} element\r
124          */\r
125         filter : function ( editor, element )\r
126         {\r
127                 var filters = editor._.removeFormat.filters;\r
128                 for ( var i = 0; i < filters.length; i++ )\r
129                 {\r
130                         if ( filters[ i ]( element ) === false )\r
131                                 return false;\r
132                 }\r
133                 return true;\r
134         }\r
135 };\r
136 \r
137 /**\r
138  * Add to a collection of functions to decide whether a specific\r
139  * element should be considered as formatting element and thus\r
140  * could be removed during <b>removeFormat</b> command,\r
141  * Note: Only available with the existence of 'removeformat' plugin.\r
142  * @since 3.3\r
143  * @param {Function} func The function to be called, which will be passed a {CKEDITOR.dom.element} element to test.\r
144  * @example\r
145  *  // Don't remove empty span\r
146  *  editor.addRemoveFormatFilter.push( function( element )\r
147  *              {\r
148  *                      return !( element.is( 'span' ) && CKEDITOR.tools.isEmpty( element.getAttributes() ) );\r
149  *              });\r
150  */\r
151 CKEDITOR.editor.prototype.addRemoveFormatFilter = function( func )\r
152 {\r
153         this._.removeFormat.filters.push( func );\r
154 };\r
155 \r
156 /**\r
157  * A comma separated list of elements to be removed when executing the "remove\r
158  " format" command. Note that only inline elements are allowed.\r
159  * @type String\r
160  * @default 'b,big,code,del,dfn,em,font,i,ins,kbd,q,samp,small,span,strike,strong,sub,sup,tt,u,var'\r
161  * @example\r
162  */\r
163 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
164 \r
165 /**\r
166  * A comma separated list of elements attributes to be removed when executing\r
167  * the "remove format" command.\r
168  * @type String\r
169  * @default 'class,style,lang,width,height,align,hspace,valign'\r
170  * @example\r
171  */\r
172 CKEDITOR.config.removeFormatAttributes = 'class,style,lang,width,height,align,hspace,valign';\r