JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.1
[ckeditor.git] / _source / plugins / pastefromword / filter / default.js
diff --git a/_source/plugins/pastefromword/filter/default.js b/_source/plugins/pastefromword/filter/default.js
new file mode 100644 (file)
index 0000000..745bda3
--- /dev/null
@@ -0,0 +1,1176 @@
+/*\r
+Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.\r
+For licensing, see LICENSE.html or http://ckeditor.com/license\r
+*/\r
+\r
+(function()\r
+{\r
+       var fragmentPrototype = CKEDITOR.htmlParser.fragment.prototype,\r
+               elementPrototype = CKEDITOR.htmlParser.element.prototype;\r
+\r
+       fragmentPrototype.onlyChild = elementPrototype.onlyChild = function()\r
+       {\r
+               var children = this.children,\r
+                       count = children.length,\r
+                       firstChild = ( count == 1 ) && children[ 0 ];\r
+               return firstChild || null;\r
+       };\r
+\r
+       elementPrototype.removeAnyChildWithName = function( tagName )\r
+       {\r
+               var children = this.children,\r
+                       childs = [],\r
+                       child;\r
+\r
+               for ( var i = 0; i < children.length; i++ )\r
+               {\r
+                       child = children[ i ];\r
+                       if( !child.name )\r
+                               continue;\r
+\r
+                       if ( child.name == tagName )\r
+                       {\r
+                               childs.push( child );\r
+                               children.splice( i--, 1 );\r
+                       }\r
+                       childs = childs.concat( child.removeAnyChildWithName( tagName ) );\r
+               }\r
+               return childs;\r
+       };\r
+\r
+       elementPrototype.getAncestor = function( tagNameRegex )\r
+       {\r
+               var parent = this.parent;\r
+               while ( parent && !( parent.name && parent.name.match( tagNameRegex ) ) )\r
+                       parent = parent.parent;\r
+               return parent;\r
+       };\r
+\r
+       fragmentPrototype.firstChild = elementPrototype.firstChild = function( evaluator )\r
+       {\r
+               var child;\r
+\r
+               for ( var i = 0 ; i < this.children.length ; i++ )\r
+               {\r
+                       child = this.children[ i ];\r
+                       if ( evaluator( child ) )\r
+                               return child;\r
+                       else if ( child.name )\r
+                       {\r
+                               child = child.firstChild( evaluator );\r
+                               if ( child )\r
+                                       return child;\r
+                               else\r
+                                       continue;\r
+                       }\r
+               }\r
+\r
+               return null;\r
+       };\r
+\r
+       // Adding a (set) of styles to the element's attributes.\r
+       elementPrototype.addStyle = function( name, value, isPrepend )\r
+       {\r
+               var styleText, addingStyleText = '';\r
+               // name/value pair.\r
+               if ( typeof value == 'string' )\r
+                       addingStyleText += name + ':' + value + ';';\r
+               else\r
+               {\r
+                       // style literal.\r
+                       if ( typeof name == 'object' )\r
+                       {\r
+                               for( var style in name )\r
+                               {\r
+                                       if( name.hasOwnProperty( style ) )\r
+                                               addingStyleText += style + ':' + name[ style ] + ';';\r
+                               }\r
+                       }\r
+                       // raw style text form.\r
+                       else\r
+                               addingStyleText += name;\r
+\r
+                       isPrepend = value;\r
+               }\r
+\r
+               if( !this.attributes )\r
+                       this.attributes = {};\r
+\r
+               styleText = this.attributes.style || '';\r
+\r
+               styleText = ( isPrepend ?\r
+                             [ addingStyleText, styleText ]\r
+                                         : [ styleText, addingStyleText ] ).join( ';' );\r
+\r
+               this.attributes.style = styleText.replace( /^;|;(?=;)/, '' );\r
+       };\r
+\r
+       /**\r
+        * Return the DTD-valid parent tag names of the specified one.\r
+        * @param tagName\r
+        */\r
+       CKEDITOR.dtd.parentOf = function( tagName )\r
+       {\r
+               var result = {};\r
+               for ( var tag in this )\r
+               {\r
+                       if ( tag.indexOf( '$' ) == -1 && this[ tag ][ tagName ] )\r
+                               result[ tag ] = 1;\r
+               }\r
+               return result;\r
+       };\r
+\r
+       var cssLengthRelativeUnit = /^(\d[.\d]*)+(em|ex|px|gd|rem|vw|vh|vm|ch|mm|cm|in|pt|pc|deg|rad|ms|s|hz|khz){1}?/i;\r
+       var emptyMarginRegex = /^(?:\b0[^\s]*\s*){1,4}$/;\r
+\r
+       var listBaseIndent = 0,\r
+                previousListItemMargin;\r
+\r
+       CKEDITOR.plugins.pastefromword =\r
+       {\r
+               utils :\r
+               {\r
+                       // Create a <cke:listbullet> which indicate an list item type.\r
+                       createListBulletMarker : function ( bulletStyle, bulletText )\r
+                       {\r
+                               var marker = new CKEDITOR.htmlParser.element( 'cke:listbullet' ),\r
+                                       listType;\r
+\r
+                               // TODO: Support more list style type from MS-Word.\r
+                               if ( !bulletStyle )\r
+                               {\r
+                                       bulletStyle = 'decimal';\r
+                                       listType = 'ol';\r
+                               }\r
+                               else if ( bulletStyle[ 2 ] )\r
+                               {\r
+                                       if ( !isNaN( bulletStyle[ 1 ] ) )\r
+                                               bulletStyle = 'decimal';\r
+                                       // No way to distinguish between Roman numerals and Alphas,\r
+                                       // detect them as a whole.\r
+                                       else if ( /^[a-z]+$/.test( bulletStyle[ 1 ] ) )\r
+                                               bulletStyle = 'lower-alpha';\r
+                                       else if ( /^[A-Z]+$/.test( bulletStyle[ 1 ] ) )\r
+                                               bulletStyle = 'upper-alpha';\r
+                                       // Simply use decimal for the rest forms of unrepresentable\r
+                                       // numerals, e.g. Chinese...\r
+                                       else\r
+                                               bulletStyle = 'decimal';\r
+\r
+                                       listType = 'ol';\r
+                               }\r
+                               else\r
+                               {\r
+                                       if ( /[l\u00B7\u2002]/.test( bulletStyle[ 1 ] ) ) //l·•\r
+                                               bulletStyle = 'disc';\r
+                                       else if ( /[\u006F\u00D8]/.test( bulletStyle[ 1 ] ) )  //oØ\r
+                                               bulletStyle = 'circle';\r
+                                       else if ( /[\u006E\u25C6]/.test( bulletStyle[ 1 ] ) ) //n◆\r
+                                               bulletStyle = 'square';\r
+                                       else\r
+                                               bulletStyle = 'disc';\r
+\r
+                                       listType = 'ul';\r
+                               }\r
+\r
+                               // Represent list type as CSS style.\r
+                               marker.attributes =\r
+                               {\r
+                                       'cke:listtype' : listType,\r
+                                       'style' : 'list-style-type:' + bulletStyle + ';'\r
+                               };\r
+                               marker.add( new CKEDITOR.htmlParser.text( bulletText ) );\r
+                               return marker;\r
+                       },\r
+\r
+                       isListBulletIndicator : function( element )\r
+                       {\r
+                               var styleText = element.attributes && element.attributes.style;\r
+                               if( /mso-list\s*:\s*Ignore/i.test( styleText ) )\r
+                                       return true;\r
+                       },\r
+\r
+                       isContainingOnlySpaces : function( element )\r
+                       {\r
+                               var text;\r
+                               return ( ( text = element.onlyChild() )\r
+                                           && ( /^(:?\s|&nbsp;)+$/ ).test( text.value ) );\r
+                       },\r
+\r
+                       resolveList : function( element )\r
+                       {\r
+                               // <cke:listbullet> indicate a list item.\r
+                               var children = element.children,\r
+                                       attrs = element.attributes,\r
+                                       listMarker;\r
+\r
+                               if ( ( listMarker = element.removeAnyChildWithName( 'cke:listbullet' ) )\r
+                                         && listMarker.length\r
+                                         && ( listMarker = listMarker[ 0 ] ) )\r
+                               {\r
+                                       element.name = 'cke:li';\r
+\r
+                                       if ( attrs.style )\r
+                                       {\r
+                                               attrs.style = CKEDITOR.plugins.pastefromword.filters.stylesFilter(\r
+                                                               [\r
+                                                                       // Text-indent is not representing list item level any more.\r
+                                                                       [ 'text-indent' ],\r
+                                                                       [ 'line-height' ],\r
+                                                                       // Resolve indent level from 'margin-left' value.\r
+                                                                       [ ( /^margin(:?-left)?$/ ), null, function( margin )\r
+                                                                       {\r
+                                                                               // Be able to deal with component/short-hand form style.\r
+                                                                               var values = margin.split( ' ' );\r
+                                                                               margin = values[ 3 ] || values[ 1 ] || values [ 0 ];\r
+                                                                               margin = parseInt( margin, 10 );\r
+\r
+                                                                               // Figure out the indent unit by looking at the first increament.\r
+                                                                               if ( !listBaseIndent && previousListItemMargin && margin > previousListItemMargin )\r
+                                                                                       listBaseIndent = margin - previousListItemMargin;\r
+\r
+                                                                               attrs[ 'cke:margin' ] = previousListItemMargin = margin;\r
+                                                                       } ]\r
+                                                       ] )( attrs.style, element ) || '' ;\r
+                                       }\r
+\r
+                                       // Inherit list-type-style from bullet.\r
+                                       var listBulletAttrs = listMarker.attributes,\r
+                                               listBulletStyle = listBulletAttrs.style;\r
+\r
+                                       element.addStyle( listBulletStyle );\r
+                                       CKEDITOR.tools.extend( attrs, listBulletAttrs );\r
+                                       return true;\r
+                               }\r
+\r
+                               return false;\r
+                       },\r
+\r
+                       // Convert various length units to 'px' in ignorance of DPI.\r
+                       convertToPx : ( function ()\r
+                       {\r
+                               var calculator = CKEDITOR.dom.element.createFromHtml(\r
+                                                               '<div style="position:absolute;left:-9999px;' +\r
+                                                               'top:-9999px;margin:0px;padding:0px;border:0px;"' +\r
+                                                               '></div>', CKEDITOR.document );\r
+                               CKEDITOR.document.getBody().append( calculator );\r
+\r
+                               return function( cssLength )\r
+                               {\r
+                                       if( cssLengthRelativeUnit.test( cssLength ) )\r
+                                       {\r
+                                               calculator.setStyle( 'width', cssLength );\r
+                                               return calculator.$.clientWidth + 'px';\r
+                                       }\r
+\r
+                                       return cssLength;\r
+                               };\r
+                       } )(),\r
+\r
+                       // Providing a shorthand style then retrieve one or more style component values.\r
+                       getStyleComponents : ( function()\r
+                       {\r
+                               var calculator = CKEDITOR.dom.element.createFromHtml(\r
+                                                               '<div style="position:absolute;left:-9999px;top:-9999px;"></div>',\r
+                                                               CKEDITOR.document );\r
+                               CKEDITOR.document.getBody().append( calculator );\r
+\r
+                               return function( name, styleValue, fetchList )\r
+                               {\r
+                                       calculator.setStyle( name, styleValue );\r
+                                       var styles = {},\r
+                                               count = fetchList.length;\r
+                                       for ( var i = 0; i < count; i++ )\r
+                                               styles[ fetchList[ i ] ]  = calculator.getStyle( fetchList[ i ] );\r
+\r
+                                       return styles;\r
+                               };\r
+                       } )(),\r
+\r
+                       listDtdParents : CKEDITOR.dtd.parentOf( 'ol' )\r
+               },\r
+\r
+               filters :\r
+               {\r
+                               // Transform a normal list into flat list items only presentation.\r
+                               // E.g. <ul><li>level1<ol><li>level2</li></ol></li> =>\r
+                               // <cke:li cke:listtype="ul" cke:indent="1">level1</cke:li>\r
+                               // <cke:li cke:listtype="ol" cke:indent="2">level2</cke:li>\r
+                               flattenList : function( element )\r
+                               {\r
+                                       var     attrs = element.attributes,\r
+                                               parent = element.parent;\r
+\r
+                                       var listStyleType,\r
+                                               indentLevel = 1;\r
+\r
+                                       // Resolve how many level nested.\r
+                                       while ( parent )\r
+                                       {\r
+                                               parent.attributes && parent.attributes[ 'cke:list'] && indentLevel++;\r
+                                               parent = parent.parent;\r
+                                       }\r
+\r
+                                       // All list items are of the same type.\r
+                                       switch ( attrs.type )\r
+                                       {\r
+                                               case 'a' :\r
+                                                       listStyleType = 'lower-alpha';\r
+                                                       break;\r
+                                               // TODO: Support more list style type from MS-Word.\r
+                                       }\r
+\r
+                                       var children = element.children,\r
+                                               child;\r
+\r
+                                       for ( var i = 0; i < children.length; i++ )\r
+                                       {\r
+                                               child = children[ i ];\r
+                                               var attributes = child.attributes;\r
+\r
+                                               if( child.name in CKEDITOR.dtd.$listItem )\r
+                                               {\r
+                                                       var listItemChildren = child.children,\r
+                                                               count = listItemChildren.length,\r
+                                                               last = listItemChildren[ count - 1 ];\r
+\r
+                                                       // Move out nested list.\r
+                                                       if( last.name in CKEDITOR.dtd.$list )\r
+                                                       {\r
+                                                               children.splice( i + 1, 0, last );\r
+                                                               last.parent = element;\r
+\r
+                                                               // Remove the parent list item if it's just a holder.\r
+                                                               if ( !--listItemChildren.length )\r
+                                                                       children.splice( i, 1 );\r
+                                                       }\r
+\r
+                                                       child.name = 'cke:li';\r
+                                                       attributes[ 'cke:indent' ] = indentLevel;\r
+                                                       previousListItemMargin = 0;\r
+                                                       attributes[ 'cke:listtype' ] = element.name;\r
+                                                       listStyleType && child.addStyle( 'list-style-type', listStyleType, true );\r
+                                               }\r
+                                       }\r
+\r
+                                       delete element.name;\r
+\r
+                                       // We're loosing tag name here, signalize this element as a list.\r
+                                       attrs[ 'cke:list' ] = 1;\r
+                               },\r
+\r
+                               /**\r
+                                *  Try to collect all list items among the children and establish one\r
+                                *  or more HTML list structures for them.\r
+                                * @param element\r
+                                */\r
+                               assembleList : function( element )\r
+                               {\r
+                                       var children = element.children, child,\r
+                                                       listItem,   // The current processing cke:li element.\r
+                                                       listItemAttrs,\r
+                                                       listType,   // Determine the root type of the list.\r
+                                                       listItemIndent, // Indent level of current list item.\r
+                                                       lastListItem, // The previous one just been added to the list.\r
+                                                       list, parentList, // Current staging list and it's parent list if any.\r
+                                                       indent;\r
+\r
+                                       for ( var i = 0; i < children.length; i++ )\r
+                                       {\r
+                                               child = children[ i ];\r
+\r
+                                               if ( 'cke:li' == child.name )\r
+                                               {\r
+                                                       child.name = 'li';\r
+                                                       listItem = child;\r
+                                                       listItemAttrs = listItem.attributes;\r
+                                                       listType = listItem.attributes[ 'cke:listtype' ];\r
+\r
+                                                       // List item indent level might come from a real list indentation or\r
+                                                       // been resolved from a pseudo list item's margin value, even get\r
+                                                       // no indentation at all.\r
+                                                       listItemIndent = parseInt( listItemAttrs[ 'cke:indent' ], 10 )\r
+                                                                                                       || listBaseIndent && ( Math.ceil( listItemAttrs[ 'cke:margin' ] / listBaseIndent ) )\r
+                                                                                                       || 1;\r
+\r
+                                                       // Ignore the 'list-style-type' attribute if it's matched with\r
+                                                       // the list root element's default style type.\r
+                                                       listItemAttrs.style && ( listItemAttrs.style =\r
+                                                               CKEDITOR.plugins.pastefromword.filters.stylesFilter(\r
+                                                                       [\r
+                                                                               [ 'list-style-type', listType == 'ol' ? 'decimal' : 'disc' ]\r
+                                                                       ] )( listItemAttrs.style )\r
+                                                                       || '' );\r
+\r
+                                                       if ( !list )\r
+                                                       {\r
+                                                               list = new CKEDITOR.htmlParser.element( listType );\r
+                                                               list.add( listItem );\r
+                                                               children[ i ] = list;\r
+                                                       }\r
+                                                       else\r
+                                                       {\r
+                                                               if ( listItemIndent > indent )\r
+                                                               {\r
+                                                                       list = new CKEDITOR.htmlParser.element( listType );\r
+                                                                       list.add( listItem );\r
+                                                                       lastListItem.add( list );\r
+                                                               }\r
+                                                               else if ( listItemIndent < indent )\r
+                                                               {\r
+                                                                       // There might be a negative gap between two list levels. (#4944)\r
+                                                                       var diff = indent - listItemIndent,\r
+                                                                               parent = list.parent;\r
+                                                                       while( diff-- && parent )\r
+                                                                               list = parent.parent;\r
+\r
+                                                                       list.add( listItem );\r
+                                                               }\r
+                                                               else\r
+                                                                       list.add( listItem );\r
+\r
+                                                               children.splice( i--, 1 );\r
+                                                       }\r
+\r
+                                                       lastListItem = listItem;\r
+                                                       indent = listItemIndent;\r
+                                               }\r
+                                               else\r
+                                                       list = null;\r
+                                       }\r
+\r
+                                       listBaseIndent = 0;\r
+                               },\r
+\r
+                               /**\r
+                                * A simple filter which always rejecting.\r
+                                */\r
+                               falsyFilter : function( value )\r
+                               {\r
+                                       return false;\r
+                               },\r
+\r
+                               /**\r
+                                * A filter dedicated on the 'style' attribute filtering, e.g. dropping/replacing style properties.\r
+                                * @param styles {Array} in form of [ styleNameRegexp, styleValueRegexp,\r
+                                *  newStyleValue/newStyleGenerator, newStyleName ] where only the first\r
+                                *  parameter is mandatory.\r
+                                * @param whitelist {Boolean} Whether the {@param styles} will be considered as a white-list.\r
+                                */\r
+                               stylesFilter : function( styles, whitelist )\r
+                               {\r
+                                       return function( styleText, element )\r
+                                       {\r
+                                                var rules = [];\r
+                                               // html-encoded quote might be introduced by 'font-family'\r
+                                               // from MS-Word which confused the following regexp. e.g.\r
+                                               //'font-family: &quot;Lucida, Console&quot;'\r
+                                                styleText\r
+                                                       .replace( /&quot;/g, '"' )\r
+                                                       .replace( /\s*([^ :;]+)\s*:\s*([^;]+)\s*(?=;|$)/g,\r
+                                                                function( match, name, value )\r
+                                                                {\r
+                                                                        name = name.toLowerCase();\r
+                                                                        name == 'font-family' && ( value = value.replace( /["']/g, '' ) );\r
+\r
+                                                                        var namePattern,\r
+                                                                                valuePattern,\r
+                                                                                newValue,\r
+                                                                                newName;\r
+                                                                        for( var i = 0 ; i < styles.length; i++ )\r
+                                                                        {\r
+                                                                               if( styles[ i ] )\r
+                                                                               {\r
+                                                                                       namePattern = styles[ i ][ 0 ];\r
+                                                                                       valuePattern = styles[ i ][ 1 ];\r
+                                                                                       newValue = styles[ i ][ 2 ];\r
+                                                                                       newName = styles[ i ][ 3 ];\r
+\r
+                                                                                       if ( name.match( namePattern )\r
+                                                                                                && ( !valuePattern || value.match( valuePattern ) ) )\r
+                                                                                       {\r
+                                                                                               name = newName || name;\r
+                                                                                               whitelist && ( newValue = newValue || value );\r
+\r
+                                                                                               if( typeof newValue == 'function' )\r
+                                                                                                       newValue = newValue( value, element, name );\r
+\r
+                                                                                               // Return an couple indicate both name and value\r
+                                                                                               // changed.\r
+                                                                                               if( newValue && newValue.push )\r
+                                                                                                       name = newValue[ 0 ], newValue = newValue[ 1 ];\r
+\r
+                                                                                               if( typeof newValue == 'string' )\r
+                                                                                                       rules.push( [ name, newValue ] );\r
+                                                                                               return;\r
+                                                                                       }\r
+                                                                               }\r
+                                                                        }\r
+\r
+                                                                        !whitelist && rules.push( [ name, value ] );\r
+\r
+                                                                });\r
+\r
+                                               for ( var i = 0 ; i < rules.length ; i++ )\r
+                                                        rules[ i ] = rules[ i ].join( ':' );\r
+                                               return rules.length ?\r
+                                                        ( rules.join( ';' ) + ';' ) : false;\r
+                                        };\r
+                               },\r
+\r
+                               /**\r
+                                * Migrate the element by decorate styles on it.\r
+                                * @param styleDefiniton\r
+                                * @param variables\r
+                                */\r
+                               elementMigrateFilter : function ( styleDefiniton, variables )\r
+                               {\r
+                                       return function( element )\r
+                                               {\r
+                                                       var styleDef =\r
+                                                                       variables ?\r
+                                                                               new CKEDITOR.style( styleDefiniton, variables )._.definition\r
+                                                                               : styleDefiniton;\r
+                                                       element.name = styleDef.element;\r
+                                                       CKEDITOR.tools.extend( element.attributes, CKEDITOR.tools.clone( styleDef.attributes ) );\r
+                                                       element.addStyle( CKEDITOR.style.getStyleText( styleDef ) );\r
+                                               };\r
+                               },\r
+\r
+                               /**\r
+                                * Migrate styles by creating a new nested stylish element.\r
+                                * @param styleDefinition\r
+                                */\r
+                               styleMigrateFilter : function( styleDefinition, variableName )\r
+                               {\r
+\r
+                                       var elementMigrateFilter = this.elementMigrateFilter;\r
+                                       return function( value, element )\r
+                                       {\r
+                                               // Build an stylish element first.\r
+                                               var styleElement = new CKEDITOR.htmlParser.element( null ),\r
+                                                       variables = {};\r
+\r
+                                               variables[ variableName ] = value;\r
+                                               elementMigrateFilter( styleDefinition, variables )( styleElement );\r
+                                               // Place the new element inside the existing span.\r
+                                               styleElement.children = element.children;\r
+                                               element.children = [ styleElement ];\r
+                                       };\r
+                               },\r
+\r
+                               /**\r
+                                * A filter which remove cke-namespaced-attribute on\r
+                                * all none-cke-namespaced elements.\r
+                                * @param value\r
+                                * @param element\r
+                                */\r
+                               bogusAttrFilter : function( value, element )\r
+                               {\r
+                                       if( element.name.indexOf( 'cke:' ) == -1 )\r
+                                               return false;\r
+                               },\r
+\r
+                               /**\r
+                                * A filter which will be used to apply inline css style according the stylesheet\r
+                                * definition rules, is generated lazily when filtering.\r
+                                */\r
+                               applyStyleFilter : null\r
+\r
+                       },\r
+\r
+               getRules : function( editor )\r
+               {\r
+                       var dtd = CKEDITOR.dtd,\r
+                               blockLike = CKEDITOR.tools.extend( {}, dtd.$block, dtd.$listItem, dtd.$tableContent ),\r
+                               config = editor.config,\r
+                               filters = this.filters,\r
+                               falsyFilter = filters.falsyFilter,\r
+                               stylesFilter = filters.stylesFilter,\r
+                               elementMigrateFilter = filters.elementMigrateFilter,\r
+                               styleMigrateFilter = CKEDITOR.tools.bind( this.filters.styleMigrateFilter, this.filters ),\r
+                               bogusAttrFilter = filters.bogusAttrFilter,\r
+                               createListBulletMarker = this.utils.createListBulletMarker,\r
+                               flattenList = filters.flattenList,\r
+                               assembleList = filters.assembleList,\r
+                               isListBulletIndicator = this.utils.isListBulletIndicator,\r
+                               containsNothingButSpaces = this.utils.isContainingOnlySpaces,\r
+                               resolveListItem = this.utils.resolveList,\r
+                               convertToPx = this.utils.convertToPx,\r
+                               getStyleComponents = this.utils.getStyleComponents,\r
+                               listDtdParents = this.utils.listDtdParents,\r
+                               removeFontStyles = config.pasteFromWordRemoveFontStyles !== false,\r
+                               removeStyles = config.pasteFromWordRemoveStyles !== false;\r
+\r
+                       return {\r
+\r
+                               elementNames :\r
+                               [\r
+                                       // Remove script, meta and link elements.\r
+                                       [ ( /meta|link|script/ ), '' ]\r
+                               ],\r
+\r
+                               root : function( element )\r
+                               {\r
+                                       element.filterChildren();\r
+                                       assembleList( element );\r
+                               },\r
+\r
+                               elements :\r
+                               {\r
+                                       '^' : function( element )\r
+                                       {\r
+                                               // Transform CSS style declaration to inline style.\r
+                                               var applyStyleFilter;\r
+                                               if ( CKEDITOR.env.gecko && ( applyStyleFilter = filters.applyStyleFilter ) )\r
+                                                       applyStyleFilter( element );\r
+                                       },\r
+\r
+                                       $ : function( element )\r
+                                       {\r
+                                               var tagName = element.name || '',\r
+                                                       attrs = element.attributes;\r
+\r
+                                               // Convert length unit of width/height on blocks to\r
+                                               // a more editor-friendly way (px).\r
+                                               if ( tagName in blockLike\r
+                                                       && attrs.style )\r
+                                               {\r
+                                                       attrs.style = stylesFilter(\r
+                                                                               [ [ ( /^(:?width|height)$/ ), null, convertToPx ] ] )( attrs.style ) || '';\r
+                                               }\r
+\r
+                                               // Processing headings.\r
+                                               if ( tagName.match( /h\d/ ) )\r
+                                               {\r
+                                                       element.filterChildren();\r
+                                                       // Is the heading actually a list item?\r
+                                                       if( resolveListItem( element ) )\r
+                                                               return;\r
+\r
+                                                       // Adapt heading styles to editor's convention.\r
+                                                       elementMigrateFilter( config[ 'format_' + tagName ] )( element );\r
+                                               }\r
+                                               // Remove inline elements which contain only empty spaces.\r
+                                               else if ( tagName in dtd.$inline )\r
+                                               {\r
+                                                       element.filterChildren();\r
+                                                       if ( containsNothingButSpaces( element ) )\r
+                                                               delete element.name;\r
+                                               }\r
+                                               // Remove element with ms-office namespace,\r
+                                               // with it's content preserved, e.g. 'o:p'.\r
+                                               else if ( tagName.indexOf( ':' ) != -1\r
+                                                                && tagName.indexOf( 'cke' ) == -1 )\r
+                                               {\r
+                                                       element.filterChildren();\r
+\r
+                                                       // Restore image real link from vml.\r
+                                                       if ( tagName == 'v:imagedata' )\r
+                                                       {\r
+                                                               var href = element.attributes[ 'o:href' ];\r
+                                                               if ( href )\r
+                                                                       element.attributes.src = href;\r
+                                                               element.name = 'img';\r
+                                                               return;\r
+                                                       }\r
+                                                       delete element.name;\r
+                                               }\r
+\r
+                                               // Assembling list items into a whole list.\r
+                                               if ( tagName in listDtdParents )\r
+                                               {\r
+                                                       element.filterChildren();\r
+                                                       assembleList( element );\r
+                                               }\r
+                                       },\r
+\r
+                                       // We'll drop any style sheet, but Firefox conclude\r
+                                       // certain styles in a single style element, which are\r
+                                       // required to be changed into inline ones.\r
+                                       'style' : function( element )\r
+                                       {\r
+                                               if ( CKEDITOR.env.gecko )\r
+                                               {\r
+                                                       // Grab only the style definition section.\r
+                                                       var styleDefSection = element.onlyChild().value.match( /\/\* Style Definitions \*\/([\s\S]*?)\/\*/ ),\r
+                                                               styleDefText = styleDefSection && styleDefSection[ 1 ],\r
+                                                               rules = {}; // Storing the parsed result.\r
+\r
+                                                       if ( styleDefText )\r
+                                                       {\r
+                                                               styleDefText\r
+                                                                       // Remove line-breaks.\r
+                                                                       .replace(/[\n\r]/g,'')\r
+                                                                       // Extract selectors and style properties.\r
+                                                                       .replace( /(.+?)\{(.+?)\}/g,\r
+                                                                               function( rule, selectors, styleBlock )\r
+                                                                               {\r
+                                                                                       selectors = selectors.split( ',' );\r
+                                                                                       var length = selectors.length, selector;\r
+                                                                                       for ( var i = 0; i < length; i++ )\r
+                                                                                       {\r
+                                                                                               // Assume MS-Word mostly generate only simple\r
+                                                                                               // selector( [Type selector][Class selector]).\r
+                                                                                               CKEDITOR.tools.trim( selectors[ i ] )\r
+                                                                                                                         .replace( /^(\w+)(\.[\w-]+)?$/g,\r
+                                                                                               function( match, tagName, className )\r
+                                                                                               {\r
+                                                                                                       tagName = tagName || '*';\r
+                                                                                                       className = className.substring( 1, className.length );\r
+\r
+                                                                                                       // Reject MS-Word Normal styles.\r
+                                                                                                       if( className.match( /MsoNormal/ ) )\r
+                                                                                                               return;\r
+\r
+                                                                                                       if( !rules[ tagName ] )\r
+                                                                                                               rules[ tagName ] = {};\r
+                                                                                                       if( className )\r
+                                                                                                               rules[ tagName ][ className ] = styleBlock;\r
+                                                                                                       else\r
+                                                                                                               rules[ tagName ] = styleBlock;\r
+                                                                                               } );\r
+                                                                                       }\r
+                                                                               });\r
+\r
+                                                               filters.applyStyleFilter = function( element )\r
+                                                               {\r
+                                                                       var name = rules[ '*' ] ? '*' : element.name,\r
+                                                                               className = element.attributes && element.attributes[ 'class' ],\r
+                                                                               style;\r
+                                                                       if( name in rules )\r
+                                                                       {\r
+                                                                               style = rules[ name ];\r
+                                                                               if( typeof style == 'object' )\r
+                                                                                       style = style[ className ];\r
+                                                                               // Maintain style rules priorities.\r
+                                                                               style && element.addStyle( style, true );\r
+                                                                       }\r
+                                                               };\r
+                                                       }\r
+                                               }\r
+                                               return false;\r
+                                       },\r
+\r
+                                       'p' : function( element )\r
+                                       {\r
+                                               element.filterChildren();\r
+\r
+                                               var attrs = element.attributes,\r
+                                                       parent = element.parent,\r
+                                                       children = element.children;\r
+\r
+                                               // Is the paragraph actually a list item?\r
+                                               if ( resolveListItem( element ) )\r
+                                                       return;\r
+\r
+                                               // Adapt paragraph formatting to editor's convention\r
+                                               // according to enter-mode.\r
+                                               if ( config.enterMode == CKEDITOR.ENTER_BR )\r
+                                               {\r
+                                                       // We suffer from attribute/style lost in this situation.\r
+                                                       delete element.name;\r
+                                                       element.add( new CKEDITOR.htmlParser.element( 'br' ) );\r
+                                               }\r
+                                               else\r
+                                                       elementMigrateFilter( config[ 'format_' + ( config.enterMode == CKEDITOR.ENTER_P ? 'p' : 'div' ) ] )( element );\r
+                                       },\r
+\r
+                                       'div' : function( element )\r
+                                       {\r
+                                               // Aligned table with no text surrounded is represented by a wrapper div, from which\r
+                                               // table cells inherit as text-align styles, which is wrong.\r
+                                               // Instead we use a clear-float div after the table to properly achieve the same layout.\r
+                                               var singleChild = element.onlyChild();\r
+                                               if( singleChild && singleChild.name == 'table' )\r
+                                               {\r
+                                                       var attrs = element.attributes;\r
+                                                       singleChild.attributes = CKEDITOR.tools.extend( singleChild.attributes, attrs );\r
+                                                       attrs.style && singleChild.addStyle( attrs.style );\r
+\r
+                                                       var clearFloatDiv = new CKEDITOR.htmlParser.element( 'div' );\r
+                                                       clearFloatDiv.addStyle( 'clear' ,'both' );\r
+                                                       element.add( clearFloatDiv );\r
+                                                       delete element.name;\r
+                                               }\r
+                                       },\r
+\r
+                                       'td' : function ( element )\r
+                                       {\r
+                                               // 'td' in 'thead' is actually <th>.\r
+                                               if ( element.getAncestor( 'thead') )\r
+                                                       element.name = 'th';\r
+                                       },\r
+\r
+                                       // MS-Word sometimes present list as a mixing of normal list\r
+                                       // and pseudo-list, normalize the previous ones into pseudo form.\r
+                                       'ol' : flattenList,\r
+                                       'ul' : flattenList,\r
+                                       'dl' : flattenList,\r
+\r
+                                       'font' : function( element )\r
+                                       {\r
+                                               // IE/Safari: drop the font tag if it comes from list bullet text.\r
+                                               if ( !CKEDITOR.env.gecko && isListBulletIndicator( element.parent ) )\r
+                                               {\r
+                                                       delete element.name;\r
+                                                       return;\r
+                                               }\r
+\r
+                                               element.filterChildren();\r
+\r
+                                               var attrs = element.attributes,\r
+                                                       styleText = attrs.style,\r
+                                                       parent = element.parent;\r
+\r
+                                               if ( 'font' == parent.name )     // Merge nested <font> tags.\r
+                                               {\r
+                                                       CKEDITOR.tools.extend( parent.attributes,\r
+                                                                       element.attributes );\r
+                                                       styleText && parent.addStyle( styleText );\r
+                                                       delete element.name;\r
+                                                       return;\r
+                                               }\r
+                                               // Convert the merged into a span with all attributes preserved.\r
+                                               else\r
+                                               {\r
+                                                       styleText = styleText || '';\r
+                                                       // IE's having those deprecated attributes, normalize them.\r
+                                                       if ( attrs.color )\r
+                                                       {\r
+                                                               attrs.color != '#000000' && ( styleText += 'color:' + attrs.color + ';' );\r
+                                                               delete attrs.color;\r
+                                                       }\r
+                                                       if ( attrs.face )\r
+                                                       {\r
+                                                               styleText += 'font-family:' + attrs.face + ';';\r
+                                                               delete attrs.face;\r
+                                                       }\r
+                                                       // TODO: Mapping size in ranges of xx-small,\r
+                                                       // x-small, small, medium, large, x-large, xx-large.\r
+                                                       if ( attrs.size )\r
+                                                       {\r
+                                                               styleText += 'font-size:' +\r
+                                                                            ( attrs.size > 3 ? 'large'\r
+                                                                                            : ( attrs.size < 3 ? 'small' : 'medium' ) ) + ';';\r
+                                                               delete attrs.size;\r
+                                                       }\r
+\r
+                                                       element.name = 'span';\r
+                                                       element.addStyle( styleText );\r
+                                               }\r
+                                       },\r
+\r
+                                       'span' : function( element )\r
+                                       {\r
+                                               // IE/Safari: remove the span if it comes from list bullet text.\r
+                                               if ( !CKEDITOR.env.gecko && isListBulletIndicator( element.parent ) )\r
+                                                       return false;\r
+\r
+                                               element.filterChildren();\r
+                                               if ( containsNothingButSpaces( element ) )\r
+                                               {\r
+                                                       delete element.name;\r
+                                                       return null;\r
+                                               }\r
+\r
+                                               // For IE/Safari: List item bullet type is supposed to be indicated by\r
+                                               // the text of a span with style 'mso-list : Ignore' or an image.\r
+                                               if ( !CKEDITOR.env.gecko && isListBulletIndicator( element ) )\r
+                                               {\r
+                                                       var listSymbolNode = element.firstChild( function( node )\r
+                                                       {\r
+                                                               return node.value || node.name == 'img';\r
+                                                       });\r
+\r
+                                                       var listSymbol =  listSymbolNode && ( listSymbolNode.value || 'l.' ),\r
+                                                               listType = listSymbol.match( /^([^\s]+?)([.)]?)$/ );\r
+                                                       return createListBulletMarker( listType, listSymbol );\r
+                                               }\r
+\r
+                                               // Update the src attribute of image element with href.\r
+                                               var children = element.children,\r
+                                                       attrs = element.attributes,\r
+                                                       styleText = attrs && attrs.style,\r
+                                                       firstChild = children && children[ 0 ];\r
+\r
+                                               // Assume MS-Word mostly carry font related styles on <span>,\r
+                                               // adapting them to editor's convention.\r
+                                               if ( styleText )\r
+                                               {\r
+                                                       attrs.style = stylesFilter(\r
+                                                                       [\r
+                                                                               // Drop 'inline-height' style which make lines overlapping.\r
+                                                                               [ 'line-height' ],\r
+                                                                               [ ( /^font-family$/ ), null, !removeFontStyles ? styleMigrateFilter( config[ 'font_style' ], 'family' ) : null ] ,\r
+                                                                               [ ( /^font-size$/ ), null, !removeFontStyles ? styleMigrateFilter( config[ 'fontSize_style' ], 'size' ) : null ] ,\r
+                                                                               [ ( /^color$/ ), null, !removeFontStyles ? styleMigrateFilter( config[ 'colorButton_foreStyle' ], 'color' ) : null ] ,\r
+                                                                               [ ( /^background-color$/ ), null, !removeFontStyles ? styleMigrateFilter( config[ 'colorButton_backStyle' ], 'color' ) : null ]\r
+                                                                       ] )( styleText, element ) || '';\r
+                                               }\r
+\r
+                                               return null;\r
+                                       },\r
+\r
+                                       // Migrate basic style formats to editor configured ones.\r
+                                       'b' : elementMigrateFilter( config[ 'coreStyles_bold' ] ),\r
+                                       'i' : elementMigrateFilter( config[ 'coreStyles_italic' ] ),\r
+                                       'u' : elementMigrateFilter( config[ 'coreStyles_underline' ] ),\r
+                                       's' : elementMigrateFilter( config[ 'coreStyles_strike' ] ),\r
+                                       'sup' : elementMigrateFilter( config[ 'coreStyles_superscript' ] ),\r
+                                       'sub' : elementMigrateFilter( config[ 'coreStyles_subscript' ] ),\r
+                                       // Editor doesn't support anchor with content currently (#3582),\r
+                                       // drop such anchors with content preserved.\r
+                                       'a' : function( element )\r
+                                       {\r
+                                               var attrs = element.attributes;\r
+                                               if( attrs && !attrs.href && attrs.name )\r
+                                                       delete element.name;\r
+                                       },\r
+                                       'cke:listbullet' : function( element )\r
+                                       {\r
+                                               if ( element.getAncestor( /h\d/ ) && !config.pasteFromWordNumberedHeadingToList )\r
+                                                       delete element.name;\r
+                                               }\r
+                               },\r
+\r
+                               attributeNames :\r
+                               [\r
+                                       // Remove onmouseover and onmouseout events (from MS Word comments effect)\r
+                                       [ ( /^onmouse(:?out|over)/ ), '' ],\r
+                                       // Onload on image element.\r
+                                       [ ( /^onload$/ ), '' ],\r
+                                       // Remove office and vml attribute from elements.\r
+                                       [ ( /(?:v|o):\w+/ ), '' ],\r
+                                       // Remove lang/language attributes.\r
+                                       [ ( /^lang/ ), '' ]\r
+                               ],\r
+\r
+                               attributes :\r
+                               {\r
+                                       'style' : stylesFilter(\r
+                                       removeStyles ?\r
+                                       // Provide a white-list of styles that we preserve, those should\r
+                                       // be the ones that could later be altered with editor tools.\r
+                                       [\r
+                                               // Preserve margin-left/right which used as default indent style in the editor.\r
+                                               [ ( /^margin$|^margin-(?!bottom|top)/ ), null, function( value, element, name )\r
+                                                       {\r
+                                                               if ( element.name in { p : 1, div : 1 } )\r
+                                                               {\r
+                                                                       var indentStyleName = config.contentsLangDirection == 'ltr' ?\r
+                                                                                       'margin-left' : 'margin-right';\r
+\r
+                                                                       // Extract component value from 'margin' shorthand.\r
+                                                                       if ( name == 'margin' )\r
+                                                                       {\r
+                                                                               value = getStyleComponents( name, value,\r
+                                                                                               [ indentStyleName ] )[ indentStyleName ];\r
+                                                                       }\r
+                                                                       else if ( name != indentStyleName )\r
+                                                                               return null;\r
+\r
+                                                                       if ( value && !emptyMarginRegex.test( value ) )\r
+                                                                               return [ indentStyleName, value ];\r
+                                                               }\r
+\r
+                                                               return null;\r
+                                                       } ],\r
+\r
+                                               // Preserve clear float style.\r
+                                               [ ( /^clear$/ ) ],\r
+\r
+                                               [ ( /^border.*|margin.*|vertical-align|float$/ ), null,\r
+                                                       function( value, element )\r
+                                                       {\r
+                                                               if( element.name == 'img' )\r
+                                                                       return value;\r
+                                                       } ],\r
+\r
+                                               [ (/^width|height$/ ), null,\r
+                                                       function( value, element )\r
+                                                       {\r
+                                                               if( element.name in { table : 1, td : 1, th : 1, img : 1 } )\r
+                                                                       return value;\r
+                                                       } ]\r
+                                       ] :\r
+                                       // Otherwise provide a black-list of styles that we remove.\r
+                                       [\r
+                                               [ ( /^mso-/ ) ],\r
+                                               // Fixing color values.\r
+                                               [ ( /-color$/ ), null, function( value )\r
+                                               {\r
+                                                       if( value == 'transparent' )\r
+                                                               return false;\r
+                                                       if( CKEDITOR.env.gecko )\r
+                                                               return value.replace( /-moz-use-text-color/g, 'transparent' );\r
+                                               } ],\r
+                                               // Remove empty margin values, e.g. 0.00001pt 0em 0pt\r
+                                               [ ( /^margin$/ ), emptyMarginRegex ],\r
+                                               [ 'text-indent', '0cm' ],\r
+                                               [ 'page-break-before' ],\r
+                                               [ 'tab-stops' ],\r
+                                               [ 'display', 'none' ],\r
+                                               removeFontStyles ? [ ( /font-?/ ) ] : null\r
+                                       ], removeStyles ),\r
+\r
+                                       // Prefer width styles over 'width' attributes.\r
+                                       'width' : function( value, element )\r
+                                       {\r
+                                               if( element.name in dtd.$tableContent )\r
+                                                       return false;\r
+                                       },\r
+                                       // Prefer border styles over table 'border' attributes.\r
+                                       'border' : function( value, element )\r
+                                       {\r
+                                               if( element.name in dtd.$tableContent )\r
+                                                       return false;\r
+                                       },\r
+\r
+                                       // Only Firefox carry style sheet from MS-Word, which\r
+                                       // will be applied by us manually. For other browsers\r
+                                       // the css className is useless.\r
+                                       'class' : falsyFilter,\r
+\r
+                                       // MS-Word always generate 'background-color' along with 'bgcolor',\r
+                                       // simply drop the deprecated attributes.\r
+                                       'bgcolor' : falsyFilter,\r
+\r
+                                       // Deprecate 'valign' attribute in favor of 'vertical-align'.\r
+                                       'valign' : removeStyles ? falsyFilter : function( value, element )\r
+                                       {\r
+                                               element.addStyle( 'vertical-align', value );\r
+                                               return false;\r
+                                       }\r
+                               },\r
+\r
+                               // Fore none-IE, some useful data might be buried under these IE-conditional\r
+                               // comments where RegExp were the right approach to dig them out where usual approach\r
+                               // is transform it into a fake element node which hold the desired data.\r
+                               comment :\r
+                                       !CKEDITOR.env.ie ?\r
+                                               function( value, node )\r
+                                               {\r
+                                                       var imageInfo = value.match( /<img.*?>/ ),\r
+                                                               listInfo = value.match( /^\[if !supportLists\]([\s\S]*?)\[endif\]$/ );\r
+\r
+                                                       // Seek for list bullet indicator.\r
+                                                       if ( listInfo )\r
+                                                       {\r
+                                                               // Bullet symbol could be either text or an image.\r
+                                                               var listSymbol = listInfo[ 1 ] || ( imageInfo && 'l.' ),\r
+                                                                       listType = listSymbol && listSymbol.match( />([^\s]+?)([.)]?)</ );\r
+                                                               return createListBulletMarker( listType, listSymbol );\r
+                                                       }\r
+\r
+                                                       // Reveal the <img> element in conditional comments for Firefox.\r
+                                                       if( CKEDITOR.env.gecko && imageInfo )\r
+                                                       {\r
+                                                               var img = CKEDITOR.htmlParser.fragment.fromHtml( imageInfo[ 0 ] ).children[ 0 ],\r
+                                                                       previousComment = node.previous,\r
+                                                                       // Try to dig the real image link from vml markup from previous comment text.\r
+                                                                       imgSrcInfo = previousComment && previousComment.value.match( /<v:imagedata[^>]*o:href=['"](.*?)['"]/ ),\r
+                                                                       imgSrc = imgSrcInfo && imgSrcInfo[ 1 ];\r
+\r
+                                                               // Is there a real 'src' url to be used?\r
+                                                               imgSrc && ( img.attributes.src = imgSrc );\r
+                                                               return img;\r
+                                                       }\r
+\r
+                                                       return false;\r
+                                               }\r
+                                       : falsyFilter\r
+                       };\r
+               }\r
+       };\r
+\r
+       // The paste processor here is just a reduced copy of html data processor.\r
+       var pasteProcessor = function()\r
+       {\r
+               this.dataFilter = new CKEDITOR.htmlParser.filter();\r
+       };\r
+\r
+       pasteProcessor.prototype =\r
+       {\r
+               toHtml : function( data )\r
+               {\r
+                       var fragment = CKEDITOR.htmlParser.fragment.fromHtml( data, false ),\r
+                               writer = new CKEDITOR.htmlParser.basicWriter();\r
+\r
+                       fragment.writeHtml( writer, this.dataFilter );\r
+                       return writer.getHtml( true );\r
+               }\r
+       };\r
+\r
+       CKEDITOR.cleanWord = function( data, editor )\r
+       {\r
+               // Firefox will be confused by those downlevel-revealed IE conditional\r
+               // comments, fixing them first( convert it to upperlevel-revealed one ).\r
+               // e.g. <![if !vml]>...<![endif]>\r
+               if( CKEDITOR.env.gecko )\r
+                       data = data.replace( /(<!--\[if[^<]*?\])-->([\S\s]*?)<!--(\[endif\]-->)/gi, '$1$2$3' );\r
+\r
+               var dataProcessor = new pasteProcessor(),\r
+                       dataFilter = dataProcessor.dataFilter;\r
+\r
+               // These rules will have higher priorities than default ones.\r
+               dataFilter.addRules( CKEDITOR.plugins.pastefromword.getRules( editor ) );\r
+\r
+               // Allow extending data filter rules.\r
+               editor.fire( 'beforeCleanWord', { filter : dataFilter } );\r
+\r
+               try\r
+               {\r
+                       data = dataProcessor.toHtml( data, false );\r
+               }\r
+               catch ( e )\r
+               {\r
+                       alert( editor.lang.pastefromword.error );\r
+               }\r
+\r
+               /* Below post processing those things that are unable to delivered by filter rules. */\r
+\r
+               // Remove 'cke' namespaced attribute used in filter rules as marker.\r
+               data = data.replace( /cke:.*?".*?"/g, '' );\r
+\r
+               // Remove empty style attribute.\r
+               data = data.replace( /style=""/g, '' );\r
+\r
+               // Remove the dummy spans ( having no inline style ).\r
+               data = data.replace( /<span>/g, '' );\r
+\r
+               return data;\r
+       };\r
+})();\r
+\r
+/**\r
+ * Whether the ignore all font-related format styles, including:\r
+ * - font size;\r
+ * - font family;\r
+ * - font fore/background color;\r
+ * @name CKEDITOR.config.pasteFromWordRemoveFontStyles\r
+ * @type Boolean\r
+ * @default true\r
+ * @example\r
+ * config.pasteFromWordRemoveFontStyles = false;\r
+ */\r
+\r
+/**\r
+ * Whether transform MS-Word Outline Numbered Heading into html list.\r
+ * @name CKEDITOR.config.pasteFromWordNumberedHeadingToList\r
+ * @type Boolean\r
+ * @default false\r
+ * @example\r
+ * config.pasteFromWordNumberedHeadingToList = true;\r
+ */\r
+\r
+/**\r
+ * Whether remove element styles that can't be managed with editor, note that this\r
+ * this doesn't handle the font-specific styles, which depends on\r
+ * how {@link CKEDITOR.config.pasteFromWordRemoveFontStyles} is configured.\r
+ * @name CKEDITOR.config.pasteFromWordRemoveStyles\r
+ * @type Boolean\r
+ * @default true\r
+ * @example\r
+ * config.pasteFromWordRemoveStyles = false;\r
+ */\r