JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
b8231ddebefc5773b085fb6f8732eb40724e29a8
[ckeditor.git] / _source / plugins / removeformat / plugin.js
1 /*\r
2 Copyright (c) 2003-2009, 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 });\r
20 \r
21 CKEDITOR.plugins.removeformat =\r
22 {\r
23         commands :\r
24         {\r
25                 removeformat :\r
26                 {\r
27                         exec : function( editor )\r
28                         {\r
29                                 var tagsRegex = editor._.removeFormatRegex ||\r
30                                         ( editor._.removeFormatRegex = new RegExp( '^(?:' + editor.config.removeFormatTags.replace( /,/g,'|' ) + ')$', 'i' ) );\r
31 \r
32                                 var removeAttributes = editor._.removeAttributes ||\r
33                                         ( editor._.removeAttributes = editor.config.removeFormatAttributes.split( ',' ) );\r
34 \r
35                                 var ranges = editor.getSelection().getRanges();\r
36 \r
37                                 for ( var i = 0, range ; range = ranges[ i ] ; i++ )\r
38                                 {\r
39                                         if ( range.collapsed )\r
40                                                 continue;\r
41 \r
42                                         range.enlarge( CKEDITOR.ENLARGE_ELEMENT );\r
43 \r
44                                         // Bookmark the range so we can re-select it after processing.\r
45                                         var bookmark = range.createBookmark();\r
46 \r
47                                         // The style will be applied within the bookmark boundaries.\r
48                                         var startNode   = bookmark.startNode;\r
49                                         var endNode             = bookmark.endNode;\r
50 \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
60 \r
61                                         var breakParent = function( node )\r
62                                         {\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
66 \r
67                                                 for ( var i = 1, pathElement ; pathElement = pathElements[ i ] ; i++ )\r
68                                                 {\r
69                                                         if ( pathElement.equals( path.block ) || pathElement.equals( path.blockLimit ) )\r
70                                                                 break;\r
71 \r
72                                                         // If this element can be removed (even partially).\r
73                                                         if ( tagsRegex.test( pathElement.getName() ) )\r
74                                                                 node.breakParent( pathElement );\r
75                                                 }\r
76                                         };\r
77 \r
78                                         breakParent( startNode );\r
79                                         breakParent( endNode );\r
80 \r
81                                         // Navigate through all nodes between the bookmarks.\r
82                                         var currentNode = startNode.getNextSourceNode( true, CKEDITOR.NODE_ELEMENT );\r
83 \r
84                                         while ( currentNode )\r
85                                         {\r
86                                                 // If we have reached the end of the selection, stop looping.\r
87                                                 if ( currentNode.equals( endNode ) )\r
88                                                         break;\r
89 \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
93 \r
94                                                 // This node must not be a fake element.\r
95                                                 if ( !( currentNode.getName() == 'img' && currentNode.getAttribute( '_cke_realelement' ) ) )\r
96                                                 {\r
97                                                         // Remove elements nodes that match with this style rules.\r
98                                                         if ( tagsRegex.test( currentNode.getName() ) )\r
99                                                                 currentNode.remove( true );\r
100                                                         else\r
101                                                                 currentNode.removeAttributes( removeAttributes );\r
102                                                 }\r
103 \r
104                                                 currentNode = nextNode;\r
105                                         }\r
106 \r
107                                         range.moveToBookmark( bookmark );\r
108                                 }\r
109 \r
110                                 editor.getSelection().selectRanges( ranges );\r
111                         }\r
112                 }\r
113         }\r
114 };\r
115 \r
116 /**\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
119  * @type String\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
121  * @example\r
122  */\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
124 \r
125 /**\r
126  * A comma separated list of elements attributes to be removed when executing\r
127  * the "remove format" command.\r
128  * @type String\r
129  * @default 'class,style,lang,width,height,align,hspace,valign'\r
130  * @example\r
131  */\r
132 CKEDITOR.config.removeFormatAttributes = 'class,style,lang,width,height,align,hspace,valign';\r