JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.0
[ckeditor.git] / _source / plugins / removeformat / plugin.js
diff --git a/_source/plugins/removeformat/plugin.js b/_source/plugins/removeformat/plugin.js
new file mode 100644 (file)
index 0000000..227ae05
--- /dev/null
@@ -0,0 +1,132 @@
+/*\r
+Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.\r
+For licensing, see LICENSE.html or http://ckeditor.com/license\r
+*/\r
+\r
+CKEDITOR.plugins.add( 'removeformat',\r
+{\r
+       requires : [ 'selection' ],\r
+\r
+       init : function( editor )\r
+       {\r
+               editor.addCommand( 'removeFormat', CKEDITOR.plugins.removeformat.commands.removeformat );\r
+               editor.ui.addButton( 'RemoveFormat',\r
+                       {\r
+                               label : editor.lang.removeFormat,\r
+                               command : 'removeFormat'\r
+                       });\r
+       }\r
+});\r
+\r
+CKEDITOR.plugins.removeformat =\r
+{\r
+       commands :\r
+       {\r
+               removeformat :\r
+               {\r
+                       exec : function( editor )\r
+                       {\r
+                               var tagsRegex = editor._.removeFormatRegex ||\r
+                                       ( editor._.removeFormatRegex = new RegExp( '^(?:' + editor.config.removeFormatTags.replace( /,/g,'|' ) + ')$', 'i' ) );\r
+\r
+                               var removeAttributes = editor._.removeAttributes ||\r
+                                       ( editor._.removeAttributes = editor.config.removeFormatAttributes.split( ',' ) );\r
+\r
+                               var ranges = editor.getSelection().getRanges();\r
+\r
+                               for ( var i = 0, range ; range = ranges[ i ] ; i++ )\r
+                               {\r
+                                       if ( range.collapsed )\r
+                                               continue;\r
+\r
+                                       range.enlarge( CKEDITOR.ENLARGE_ELEMENT );\r
+\r
+                                       // Bookmark the range so we can re-select it after processing.\r
+                                       var bookmark = range.createBookmark();\r
+\r
+                                       // The style will be applied within the bookmark boundaries.\r
+                                       var startNode   = bookmark.startNode;\r
+                                       var endNode             = bookmark.endNode;\r
+\r
+                                       // We need to check the selection boundaries (bookmark spans) to break\r
+                                       // the code in a way that we can properly remove partially selected nodes.\r
+                                       // For example, removing a <b> style from\r
+                                       //              <b>This is [some text</b> to show <b>the] problem</b>\r
+                                       // ... where [ and ] represent the selection, must result:\r
+                                       //              <b>This is </b>[some text to show the]<b> problem</b>\r
+                                       // The strategy is simple, we just break the partial nodes before the\r
+                                       // removal logic, having something that could be represented this way:\r
+                                       //              <b>This is </b>[<b>some text</b> to show <b>the</b>]<b> problem</b>\r
+\r
+                                       var breakParent = function( node )\r
+                                       {\r
+                                               // Let's start checking the start boundary.\r
+                                               var path = new CKEDITOR.dom.elementPath( node );\r
+                                               var pathElements = path.elements;\r
+\r
+                                               for ( var i = 1, pathElement ; pathElement = pathElements[ i ] ; i++ )\r
+                                               {\r
+                                                       if ( pathElement.equals( path.block ) || pathElement.equals( path.blockLimit ) )\r
+                                                               break;\r
+\r
+                                                       // If this element can be removed (even partially).\r
+                                                       if ( tagsRegex.test( pathElement.getName() ) )\r
+                                                               node.breakParent( pathElement );\r
+                                               }\r
+                                       };\r
+\r
+                                       breakParent( startNode );\r
+                                       breakParent( endNode );\r
+\r
+                                       // Navigate through all nodes between the bookmarks.\r
+                                       var currentNode = startNode.getNextSourceNode( true, CKEDITOR.NODE_ELEMENT );\r
+\r
+                                       while ( currentNode )\r
+                                       {\r
+                                               // If we have reached the end of the selection, stop looping.\r
+                                               if ( currentNode.equals( endNode ) )\r
+                                                       break;\r
+\r
+                                               // Cache the next node to be processed. Do it now, because\r
+                                               // currentNode may be removed.\r
+                                               var nextNode = currentNode.getNextSourceNode( false, CKEDITOR.NODE_ELEMENT );\r
+\r
+                                               // This node must not be a fake element.\r
+                                               if ( currentNode.getName() != 'img' || !currentNode.getAttribute( '_cke_protected_html' ) )\r
+                                               {\r
+                                                       // Remove elements nodes that match with this style rules.\r
+                                                       if ( tagsRegex.test( currentNode.getName() ) )\r
+                                                               currentNode.remove( true );\r
+                                                       else\r
+                                                               currentNode.removeAttributes( removeAttributes );\r
+                                               }\r
+\r
+                                               currentNode = nextNode;\r
+                                       }\r
+\r
+                                       range.moveToBookmark( bookmark );\r
+                               }\r
+\r
+                               editor.getSelection().selectRanges( ranges );\r
+                       }\r
+               }\r
+       }\r
+};\r
+\r
+/**\r
+ * A comma separated list of elements to be removed when executing the "remove\r
+ " format" command. Note that only inline elements are allowed.\r
+ * @type String\r
+ * @default 'b,big,code,del,dfn,em,font,i,ins,kbd,q,samp,small,span,strike,strong,sub,sup,tt,u,var'\r
+ * @example\r
+ */\r
+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
+\r
+/**\r
+ * A comma separated list of elements attributes to be removed when executing\r
+ * the "remove format" command.\r
+ * @type String\r
+ * @default 'class,style,lang,width,height,align,hspace,valign'\r
+ * @example\r
+ */\r
+CKEDITOR.config.removeFormatAttributes = 'class,style,lang,width,height,align,hspace,valign';\r