2 Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.
\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license
\r
8 var fragmentPrototype = CKEDITOR.htmlParser.fragment.prototype,
\r
9 elementPrototype = CKEDITOR.htmlParser.element.prototype;
\r
11 fragmentPrototype.onlyChild = elementPrototype.onlyChild = function()
\r
13 var children = this.children,
\r
14 count = children.length,
\r
15 firstChild = ( count == 1 ) && children[ 0 ];
\r
16 return firstChild || null;
\r
19 elementPrototype.removeAnyChildWithName = function( tagName )
\r
21 var children = this.children,
\r
25 for ( var i = 0; i < children.length; i++ )
\r
27 child = children[ i ];
\r
31 if ( child.name == tagName )
\r
33 childs.push( child );
\r
34 children.splice( i--, 1 );
\r
36 childs = childs.concat( child.removeAnyChildWithName( tagName ) );
\r
41 elementPrototype.getAncestor = function( tagNameRegex )
\r
43 var parent = this.parent;
\r
44 while ( parent && !( parent.name && parent.name.match( tagNameRegex ) ) )
\r
45 parent = parent.parent;
\r
49 fragmentPrototype.firstChild = elementPrototype.firstChild = function( evaluator )
\r
53 for ( var i = 0 ; i < this.children.length ; i++ )
\r
55 child = this.children[ i ];
\r
56 if ( evaluator( child ) )
\r
58 else if ( child.name )
\r
60 child = child.firstChild( evaluator );
\r
71 // Adding a (set) of styles to the element's attributes.
\r
72 elementPrototype.addStyle = function( name, value, isPrepend )
\r
74 var styleText, addingStyleText = '';
\r
76 if ( typeof value == 'string' )
\r
77 addingStyleText += name + ':' + value + ';';
\r
81 if ( typeof name == 'object' )
\r
83 for( var style in name )
\r
85 if( name.hasOwnProperty( style ) )
\r
86 addingStyleText += style + ':' + name[ style ] + ';';
\r
89 // raw style text form.
\r
91 addingStyleText += name;
\r
96 if( !this.attributes )
\r
97 this.attributes = {};
\r
99 styleText = this.attributes.style || '';
\r
101 styleText = ( isPrepend ?
\r
102 [ addingStyleText, styleText ]
\r
103 : [ styleText, addingStyleText ] ).join( ';' );
\r
105 this.attributes.style = styleText.replace( /^;|;(?=;)/, '' );
\r
109 * Return the DTD-valid parent tag names of the specified one.
\r
112 CKEDITOR.dtd.parentOf = function( tagName )
\r
115 for ( var tag in this )
\r
117 if ( tag.indexOf( '$' ) == -1 && this[ tag ][ tagName ] )
\r
123 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
124 var emptyMarginRegex = /^(?:\b0[^\s]*\s*){1,4}$/;
\r
126 var listBaseIndent = 0,
\r
127 previousListItemMargin;
\r
129 CKEDITOR.plugins.pastefromword =
\r
133 // Create a <cke:listbullet> which indicate an list item type.
\r
134 createListBulletMarker : function ( bulletStyle, bulletText )
\r
136 var marker = new CKEDITOR.htmlParser.element( 'cke:listbullet' ),
\r
139 // TODO: Support more list style type from MS-Word.
\r
140 if ( !bulletStyle )
\r
142 bulletStyle = 'decimal';
\r
145 else if ( bulletStyle[ 2 ] )
\r
147 if ( !isNaN( bulletStyle[ 1 ] ) )
\r
148 bulletStyle = 'decimal';
\r
149 // No way to distinguish between Roman numerals and Alphas,
\r
150 // detect them as a whole.
\r
151 else if ( /^[a-z]+$/.test( bulletStyle[ 1 ] ) )
\r
152 bulletStyle = 'lower-alpha';
\r
153 else if ( /^[A-Z]+$/.test( bulletStyle[ 1 ] ) )
\r
154 bulletStyle = 'upper-alpha';
\r
155 // Simply use decimal for the rest forms of unrepresentable
\r
156 // numerals, e.g. Chinese...
\r
158 bulletStyle = 'decimal';
\r
164 if ( /[l\u00B7\u2002]/.test( bulletStyle[ 1 ] ) ) //l·•
\r
165 bulletStyle = 'disc';
\r
166 else if ( /[\u006F\u00D8]/.test( bulletStyle[ 1 ] ) ) //oØ
\r
167 bulletStyle = 'circle';
\r
168 else if ( /[\u006E\u25C6]/.test( bulletStyle[ 1 ] ) ) //n◆
\r
169 bulletStyle = 'square';
\r
171 bulletStyle = 'disc';
\r
176 // Represent list type as CSS style.
\r
177 marker.attributes =
\r
179 'cke:listtype' : listType,
\r
180 'style' : 'list-style-type:' + bulletStyle + ';'
\r
182 marker.add( new CKEDITOR.htmlParser.text( bulletText ) );
\r
186 isListBulletIndicator : function( element )
\r
188 var styleText = element.attributes && element.attributes.style;
\r
189 if( /mso-list\s*:\s*Ignore/i.test( styleText ) )
\r
193 isContainingOnlySpaces : function( element )
\r
196 return ( ( text = element.onlyChild() )
\r
197 && ( /^(:?\s| )+$/ ).test( text.value ) );
\r
200 resolveList : function( element )
\r
202 // <cke:listbullet> indicate a list item.
\r
203 var children = element.children,
\r
204 attrs = element.attributes,
\r
207 if ( ( listMarker = element.removeAnyChildWithName( 'cke:listbullet' ) )
\r
208 && listMarker.length
\r
209 && ( listMarker = listMarker[ 0 ] ) )
\r
211 element.name = 'cke:li';
\r
215 attrs.style = CKEDITOR.plugins.pastefromword.filters.stylesFilter(
\r
217 // Text-indent is not representing list item level any more.
\r
220 // Resolve indent level from 'margin-left' value.
\r
221 [ ( /^margin(:?-left)?$/ ), null, function( margin )
\r
223 // Be able to deal with component/short-hand form style.
\r
224 var values = margin.split( ' ' );
\r
225 margin = values[ 3 ] || values[ 1 ] || values [ 0 ];
\r
226 margin = parseInt( margin, 10 );
\r
228 // Figure out the indent unit by looking at the first increament.
\r
229 if ( !listBaseIndent && previousListItemMargin && margin > previousListItemMargin )
\r
230 listBaseIndent = margin - previousListItemMargin;
\r
232 attrs[ 'cke:margin' ] = previousListItemMargin = margin;
\r
234 ] )( attrs.style, element ) || '' ;
\r
237 // Inherit list-type-style from bullet.
\r
238 var listBulletAttrs = listMarker.attributes,
\r
239 listBulletStyle = listBulletAttrs.style;
\r
241 element.addStyle( listBulletStyle );
\r
242 CKEDITOR.tools.extend( attrs, listBulletAttrs );
\r
249 // Convert various length units to 'px' in ignorance of DPI.
\r
250 convertToPx : ( function ()
\r
252 var calculator = CKEDITOR.dom.element.createFromHtml(
\r
253 '<div style="position:absolute;left:-9999px;' +
\r
254 'top:-9999px;margin:0px;padding:0px;border:0px;"' +
\r
255 '></div>', CKEDITOR.document );
\r
256 CKEDITOR.document.getBody().append( calculator );
\r
258 return function( cssLength )
\r
260 if( cssLengthRelativeUnit.test( cssLength ) )
\r
262 calculator.setStyle( 'width', cssLength );
\r
263 return calculator.$.clientWidth + 'px';
\r
270 // Providing a shorthand style then retrieve one or more style component values.
\r
271 getStyleComponents : ( function()
\r
273 var calculator = CKEDITOR.dom.element.createFromHtml(
\r
274 '<div style="position:absolute;left:-9999px;top:-9999px;"></div>',
\r
275 CKEDITOR.document );
\r
276 CKEDITOR.document.getBody().append( calculator );
\r
278 return function( name, styleValue, fetchList )
\r
280 calculator.setStyle( name, styleValue );
\r
282 count = fetchList.length;
\r
283 for ( var i = 0; i < count; i++ )
\r
284 styles[ fetchList[ i ] ] = calculator.getStyle( fetchList[ i ] );
\r
290 listDtdParents : CKEDITOR.dtd.parentOf( 'ol' )
\r
295 // Transform a normal list into flat list items only presentation.
\r
296 // E.g. <ul><li>level1<ol><li>level2</li></ol></li> =>
\r
297 // <cke:li cke:listtype="ul" cke:indent="1">level1</cke:li>
\r
298 // <cke:li cke:listtype="ol" cke:indent="2">level2</cke:li>
\r
299 flattenList : function( element )
\r
301 var attrs = element.attributes,
\r
302 parent = element.parent;
\r
307 // Resolve how many level nested.
\r
310 parent.attributes && parent.attributes[ 'cke:list'] && indentLevel++;
\r
311 parent = parent.parent;
\r
314 // All list items are of the same type.
\r
315 switch ( attrs.type )
\r
318 listStyleType = 'lower-alpha';
\r
320 // TODO: Support more list style type from MS-Word.
\r
323 var children = element.children,
\r
326 for ( var i = 0; i < children.length; i++ )
\r
328 child = children[ i ];
\r
329 var attributes = child.attributes;
\r
331 if( child.name in CKEDITOR.dtd.$listItem )
\r
333 var listItemChildren = child.children,
\r
334 count = listItemChildren.length,
\r
335 last = listItemChildren[ count - 1 ];
\r
337 // Move out nested list.
\r
338 if( last.name in CKEDITOR.dtd.$list )
\r
340 children.splice( i + 1, 0, last );
\r
341 last.parent = element;
\r
343 // Remove the parent list item if it's just a holder.
\r
344 if ( !--listItemChildren.length )
\r
345 children.splice( i, 1 );
\r
348 child.name = 'cke:li';
\r
349 attributes[ 'cke:indent' ] = indentLevel;
\r
350 previousListItemMargin = 0;
\r
351 attributes[ 'cke:listtype' ] = element.name;
\r
352 listStyleType && child.addStyle( 'list-style-type', listStyleType, true );
\r
356 delete element.name;
\r
358 // We're loosing tag name here, signalize this element as a list.
\r
359 attrs[ 'cke:list' ] = 1;
\r
363 * Try to collect all list items among the children and establish one
\r
364 * or more HTML list structures for them.
\r
367 assembleList : function( element )
\r
369 var children = element.children, child,
\r
370 listItem, // The current processing cke:li element.
\r
372 listType, // Determine the root type of the list.
\r
373 listItemIndent, // Indent level of current list item.
\r
374 lastListItem, // The previous one just been added to the list.
\r
375 list, parentList, // Current staging list and it's parent list if any.
\r
378 for ( var i = 0; i < children.length; i++ )
\r
380 child = children[ i ];
\r
382 if ( 'cke:li' == child.name )
\r
386 listItemAttrs = listItem.attributes;
\r
387 listType = listItem.attributes[ 'cke:listtype' ];
\r
389 // List item indent level might come from a real list indentation or
\r
390 // been resolved from a pseudo list item's margin value, even get
\r
391 // no indentation at all.
\r
392 listItemIndent = parseInt( listItemAttrs[ 'cke:indent' ], 10 )
\r
393 || listBaseIndent && ( Math.ceil( listItemAttrs[ 'cke:margin' ] / listBaseIndent ) )
\r
396 // Ignore the 'list-style-type' attribute if it's matched with
\r
397 // the list root element's default style type.
\r
398 listItemAttrs.style && ( listItemAttrs.style =
\r
399 CKEDITOR.plugins.pastefromword.filters.stylesFilter(
\r
401 [ 'list-style-type', listType == 'ol' ? 'decimal' : 'disc' ]
\r
402 ] )( listItemAttrs.style )
\r
407 list = new CKEDITOR.htmlParser.element( listType );
\r
408 list.add( listItem );
\r
409 children[ i ] = list;
\r
413 if ( listItemIndent > indent )
\r
415 list = new CKEDITOR.htmlParser.element( listType );
\r
416 list.add( listItem );
\r
417 lastListItem.add( list );
\r
419 else if ( listItemIndent < indent )
\r
421 // There might be a negative gap between two list levels. (#4944)
\r
422 var diff = indent - listItemIndent,
\r
423 parent = list.parent;
\r
424 while( diff-- && parent )
\r
425 list = parent.parent;
\r
427 list.add( listItem );
\r
430 list.add( listItem );
\r
432 children.splice( i--, 1 );
\r
435 lastListItem = listItem;
\r
436 indent = listItemIndent;
\r
442 listBaseIndent = 0;
\r
446 * A simple filter which always rejecting.
\r
448 falsyFilter : function( value )
\r
454 * A filter dedicated on the 'style' attribute filtering, e.g. dropping/replacing style properties.
\r
455 * @param styles {Array} in form of [ styleNameRegexp, styleValueRegexp,
\r
456 * newStyleValue/newStyleGenerator, newStyleName ] where only the first
\r
457 * parameter is mandatory.
\r
458 * @param whitelist {Boolean} Whether the {@param styles} will be considered as a white-list.
\r
460 stylesFilter : function( styles, whitelist )
\r
462 return function( styleText, element )
\r
465 // html-encoded quote might be introduced by 'font-family'
\r
466 // from MS-Word which confused the following regexp. e.g.
\r
467 //'font-family: "Lucida, Console"'
\r
469 .replace( /"/g, '"' )
\r
470 .replace( /\s*([^ :;]+)\s*:\s*([^;]+)\s*(?=;|$)/g,
\r
471 function( match, name, value )
\r
473 name = name.toLowerCase();
\r
474 name == 'font-family' && ( value = value.replace( /["']/g, '' ) );
\r
480 for( var i = 0 ; i < styles.length; i++ )
\r
484 namePattern = styles[ i ][ 0 ];
\r
485 valuePattern = styles[ i ][ 1 ];
\r
486 newValue = styles[ i ][ 2 ];
\r
487 newName = styles[ i ][ 3 ];
\r
489 if ( name.match( namePattern )
\r
490 && ( !valuePattern || value.match( valuePattern ) ) )
\r
492 name = newName || name;
\r
493 whitelist && ( newValue = newValue || value );
\r
495 if( typeof newValue == 'function' )
\r
496 newValue = newValue( value, element, name );
\r
498 // Return an couple indicate both name and value
\r
500 if( newValue && newValue.push )
\r
501 name = newValue[ 0 ], newValue = newValue[ 1 ];
\r
503 if( typeof newValue == 'string' )
\r
504 rules.push( [ name, newValue ] );
\r
510 !whitelist && rules.push( [ name, value ] );
\r
514 for ( var i = 0 ; i < rules.length ; i++ )
\r
515 rules[ i ] = rules[ i ].join( ':' );
\r
516 return rules.length ?
\r
517 ( rules.join( ';' ) + ';' ) : false;
\r
522 * Migrate the element by decorate styles on it.
\r
523 * @param styleDefiniton
\r
526 elementMigrateFilter : function ( styleDefiniton, variables )
\r
528 return function( element )
\r
532 new CKEDITOR.style( styleDefiniton, variables )._.definition
\r
534 element.name = styleDef.element;
\r
535 CKEDITOR.tools.extend( element.attributes, CKEDITOR.tools.clone( styleDef.attributes ) );
\r
536 element.addStyle( CKEDITOR.style.getStyleText( styleDef ) );
\r
541 * Migrate styles by creating a new nested stylish element.
\r
542 * @param styleDefinition
\r
544 styleMigrateFilter : function( styleDefinition, variableName )
\r
547 var elementMigrateFilter = this.elementMigrateFilter;
\r
548 return function( value, element )
\r
550 // Build an stylish element first.
\r
551 var styleElement = new CKEDITOR.htmlParser.element( null ),
\r
554 variables[ variableName ] = value;
\r
555 elementMigrateFilter( styleDefinition, variables )( styleElement );
\r
556 // Place the new element inside the existing span.
\r
557 styleElement.children = element.children;
\r
558 element.children = [ styleElement ];
\r
563 * A filter which remove cke-namespaced-attribute on
\r
564 * all none-cke-namespaced elements.
\r
568 bogusAttrFilter : function( value, element )
\r
570 if( element.name.indexOf( 'cke:' ) == -1 )
\r
575 * A filter which will be used to apply inline css style according the stylesheet
\r
576 * definition rules, is generated lazily when filtering.
\r
578 applyStyleFilter : null
\r
582 getRules : function( editor )
\r
584 var dtd = CKEDITOR.dtd,
\r
585 blockLike = CKEDITOR.tools.extend( {}, dtd.$block, dtd.$listItem, dtd.$tableContent ),
\r
586 config = editor.config,
\r
587 filters = this.filters,
\r
588 falsyFilter = filters.falsyFilter,
\r
589 stylesFilter = filters.stylesFilter,
\r
590 elementMigrateFilter = filters.elementMigrateFilter,
\r
591 styleMigrateFilter = CKEDITOR.tools.bind( this.filters.styleMigrateFilter, this.filters ),
\r
592 bogusAttrFilter = filters.bogusAttrFilter,
\r
593 createListBulletMarker = this.utils.createListBulletMarker,
\r
594 flattenList = filters.flattenList,
\r
595 assembleList = filters.assembleList,
\r
596 isListBulletIndicator = this.utils.isListBulletIndicator,
\r
597 containsNothingButSpaces = this.utils.isContainingOnlySpaces,
\r
598 resolveListItem = this.utils.resolveList,
\r
599 convertToPx = this.utils.convertToPx,
\r
600 getStyleComponents = this.utils.getStyleComponents,
\r
601 listDtdParents = this.utils.listDtdParents,
\r
602 removeFontStyles = config.pasteFromWordRemoveFontStyles !== false,
\r
603 removeStyles = config.pasteFromWordRemoveStyles !== false;
\r
609 // Remove script, meta and link elements.
\r
610 [ ( /meta|link|script/ ), '' ]
\r
613 root : function( element )
\r
615 element.filterChildren();
\r
616 assembleList( element );
\r
621 '^' : function( element )
\r
623 // Transform CSS style declaration to inline style.
\r
624 var applyStyleFilter;
\r
625 if ( CKEDITOR.env.gecko && ( applyStyleFilter = filters.applyStyleFilter ) )
\r
626 applyStyleFilter( element );
\r
629 $ : function( element )
\r
631 var tagName = element.name || '',
\r
632 attrs = element.attributes;
\r
634 // Convert length unit of width/height on blocks to
\r
635 // a more editor-friendly way (px).
\r
636 if ( tagName in blockLike
\r
639 attrs.style = stylesFilter(
\r
640 [ [ ( /^(:?width|height)$/ ), null, convertToPx ] ] )( attrs.style ) || '';
\r
643 // Processing headings.
\r
644 if ( tagName.match( /h\d/ ) )
\r
646 element.filterChildren();
\r
647 // Is the heading actually a list item?
\r
648 if( resolveListItem( element ) )
\r
651 // Adapt heading styles to editor's convention.
\r
652 elementMigrateFilter( config[ 'format_' + tagName ] )( element );
\r
654 // Remove inline elements which contain only empty spaces.
\r
655 else if ( tagName in dtd.$inline )
\r
657 element.filterChildren();
\r
658 if ( containsNothingButSpaces( element ) )
\r
659 delete element.name;
\r
661 // Remove element with ms-office namespace,
\r
662 // with it's content preserved, e.g. 'o:p'.
\r
663 else if ( tagName.indexOf( ':' ) != -1
\r
664 && tagName.indexOf( 'cke' ) == -1 )
\r
666 element.filterChildren();
\r
668 // Restore image real link from vml.
\r
669 if ( tagName == 'v:imagedata' )
\r
671 var href = element.attributes[ 'o:href' ];
\r
673 element.attributes.src = href;
\r
674 element.name = 'img';
\r
677 delete element.name;
\r
680 // Assembling list items into a whole list.
\r
681 if ( tagName in listDtdParents )
\r
683 element.filterChildren();
\r
684 assembleList( element );
\r
688 // We'll drop any style sheet, but Firefox conclude
\r
689 // certain styles in a single style element, which are
\r
690 // required to be changed into inline ones.
\r
691 'style' : function( element )
\r
693 if ( CKEDITOR.env.gecko )
\r
695 // Grab only the style definition section.
\r
696 var styleDefSection = element.onlyChild().value.match( /\/\* Style Definitions \*\/([\s\S]*?)\/\*/ ),
\r
697 styleDefText = styleDefSection && styleDefSection[ 1 ],
\r
698 rules = {}; // Storing the parsed result.
\r
700 if ( styleDefText )
\r
703 // Remove line-breaks.
\r
704 .replace(/[\n\r]/g,'')
\r
705 // Extract selectors and style properties.
\r
706 .replace( /(.+?)\{(.+?)\}/g,
\r
707 function( rule, selectors, styleBlock )
\r
709 selectors = selectors.split( ',' );
\r
710 var length = selectors.length, selector;
\r
711 for ( var i = 0; i < length; i++ )
\r
713 // Assume MS-Word mostly generate only simple
\r
714 // selector( [Type selector][Class selector]).
\r
715 CKEDITOR.tools.trim( selectors[ i ] )
\r
716 .replace( /^(\w+)(\.[\w-]+)?$/g,
\r
717 function( match, tagName, className )
\r
719 tagName = tagName || '*';
\r
720 className = className.substring( 1, className.length );
\r
722 // Reject MS-Word Normal styles.
\r
723 if( className.match( /MsoNormal/ ) )
\r
726 if( !rules[ tagName ] )
\r
727 rules[ tagName ] = {};
\r
729 rules[ tagName ][ className ] = styleBlock;
\r
731 rules[ tagName ] = styleBlock;
\r
736 filters.applyStyleFilter = function( element )
\r
738 var name = rules[ '*' ] ? '*' : element.name,
\r
739 className = element.attributes && element.attributes[ 'class' ],
\r
741 if( name in rules )
\r
743 style = rules[ name ];
\r
744 if( typeof style == 'object' )
\r
745 style = style[ className ];
\r
746 // Maintain style rules priorities.
\r
747 style && element.addStyle( style, true );
\r
755 'p' : function( element )
\r
757 element.filterChildren();
\r
759 var attrs = element.attributes,
\r
760 parent = element.parent,
\r
761 children = element.children;
\r
763 // Is the paragraph actually a list item?
\r
764 if ( resolveListItem( element ) )
\r
767 // Adapt paragraph formatting to editor's convention
\r
768 // according to enter-mode.
\r
769 if ( config.enterMode == CKEDITOR.ENTER_BR )
\r
771 // We suffer from attribute/style lost in this situation.
\r
772 delete element.name;
\r
773 element.add( new CKEDITOR.htmlParser.element( 'br' ) );
\r
776 elementMigrateFilter( config[ 'format_' + ( config.enterMode == CKEDITOR.ENTER_P ? 'p' : 'div' ) ] )( element );
\r
779 'div' : function( element )
\r
781 // Aligned table with no text surrounded is represented by a wrapper div, from which
\r
782 // table cells inherit as text-align styles, which is wrong.
\r
783 // Instead we use a clear-float div after the table to properly achieve the same layout.
\r
784 var singleChild = element.onlyChild();
\r
785 if( singleChild && singleChild.name == 'table' )
\r
787 var attrs = element.attributes;
\r
788 singleChild.attributes = CKEDITOR.tools.extend( singleChild.attributes, attrs );
\r
789 attrs.style && singleChild.addStyle( attrs.style );
\r
791 var clearFloatDiv = new CKEDITOR.htmlParser.element( 'div' );
\r
792 clearFloatDiv.addStyle( 'clear' ,'both' );
\r
793 element.add( clearFloatDiv );
\r
794 delete element.name;
\r
798 'td' : function ( element )
\r
800 // 'td' in 'thead' is actually <th>.
\r
801 if ( element.getAncestor( 'thead') )
\r
802 element.name = 'th';
\r
805 // MS-Word sometimes present list as a mixing of normal list
\r
806 // and pseudo-list, normalize the previous ones into pseudo form.
\r
807 'ol' : flattenList,
\r
808 'ul' : flattenList,
\r
809 'dl' : flattenList,
\r
811 'font' : function( element )
\r
813 // IE/Safari: drop the font tag if it comes from list bullet text.
\r
814 if ( !CKEDITOR.env.gecko && isListBulletIndicator( element.parent ) )
\r
816 delete element.name;
\r
820 element.filterChildren();
\r
822 var attrs = element.attributes,
\r
823 styleText = attrs.style,
\r
824 parent = element.parent;
\r
826 if ( 'font' == parent.name ) // Merge nested <font> tags.
\r
828 CKEDITOR.tools.extend( parent.attributes,
\r
829 element.attributes );
\r
830 styleText && parent.addStyle( styleText );
\r
831 delete element.name;
\r
834 // Convert the merged into a span with all attributes preserved.
\r
837 styleText = styleText || '';
\r
838 // IE's having those deprecated attributes, normalize them.
\r
841 attrs.color != '#000000' && ( styleText += 'color:' + attrs.color + ';' );
\r
842 delete attrs.color;
\r
846 styleText += 'font-family:' + attrs.face + ';';
\r
849 // TODO: Mapping size in ranges of xx-small,
\r
850 // x-small, small, medium, large, x-large, xx-large.
\r
853 styleText += 'font-size:' +
\r
854 ( attrs.size > 3 ? 'large'
\r
855 : ( attrs.size < 3 ? 'small' : 'medium' ) ) + ';';
\r
859 element.name = 'span';
\r
860 element.addStyle( styleText );
\r
864 'span' : function( element )
\r
866 // IE/Safari: remove the span if it comes from list bullet text.
\r
867 if ( !CKEDITOR.env.gecko && isListBulletIndicator( element.parent ) )
\r
870 element.filterChildren();
\r
871 if ( containsNothingButSpaces( element ) )
\r
873 delete element.name;
\r
877 // For IE/Safari: List item bullet type is supposed to be indicated by
\r
878 // the text of a span with style 'mso-list : Ignore' or an image.
\r
879 if ( !CKEDITOR.env.gecko && isListBulletIndicator( element ) )
\r
881 var listSymbolNode = element.firstChild( function( node )
\r
883 return node.value || node.name == 'img';
\r
886 var listSymbol = listSymbolNode && ( listSymbolNode.value || 'l.' ),
\r
887 listType = listSymbol.match( /^([^\s]+?)([.)]?)$/ );
\r
888 return createListBulletMarker( listType, listSymbol );
\r
891 // Update the src attribute of image element with href.
\r
892 var children = element.children,
\r
893 attrs = element.attributes,
\r
894 styleText = attrs && attrs.style,
\r
895 firstChild = children && children[ 0 ];
\r
897 // Assume MS-Word mostly carry font related styles on <span>,
\r
898 // adapting them to editor's convention.
\r
901 attrs.style = stylesFilter(
\r
903 // Drop 'inline-height' style which make lines overlapping.
\r
905 [ ( /^font-family$/ ), null, !removeFontStyles ? styleMigrateFilter( config[ 'font_style' ], 'family' ) : null ] ,
\r
906 [ ( /^font-size$/ ), null, !removeFontStyles ? styleMigrateFilter( config[ 'fontSize_style' ], 'size' ) : null ] ,
\r
907 [ ( /^color$/ ), null, !removeFontStyles ? styleMigrateFilter( config[ 'colorButton_foreStyle' ], 'color' ) : null ] ,
\r
908 [ ( /^background-color$/ ), null, !removeFontStyles ? styleMigrateFilter( config[ 'colorButton_backStyle' ], 'color' ) : null ]
\r
909 ] )( styleText, element ) || '';
\r
915 // Migrate basic style formats to editor configured ones.
\r
916 'b' : elementMigrateFilter( config[ 'coreStyles_bold' ] ),
\r
917 'i' : elementMigrateFilter( config[ 'coreStyles_italic' ] ),
\r
918 'u' : elementMigrateFilter( config[ 'coreStyles_underline' ] ),
\r
919 's' : elementMigrateFilter( config[ 'coreStyles_strike' ] ),
\r
920 'sup' : elementMigrateFilter( config[ 'coreStyles_superscript' ] ),
\r
921 'sub' : elementMigrateFilter( config[ 'coreStyles_subscript' ] ),
\r
922 // Editor doesn't support anchor with content currently (#3582),
\r
923 // drop such anchors with content preserved.
\r
924 'a' : function( element )
\r
926 var attrs = element.attributes;
\r
927 if( attrs && !attrs.href && attrs.name )
\r
928 delete element.name;
\r
930 'cke:listbullet' : function( element )
\r
932 if ( element.getAncestor( /h\d/ ) && !config.pasteFromWordNumberedHeadingToList )
\r
933 delete element.name;
\r
939 // Remove onmouseover and onmouseout events (from MS Word comments effect)
\r
940 [ ( /^onmouse(:?out|over)/ ), '' ],
\r
941 // Onload on image element.
\r
942 [ ( /^onload$/ ), '' ],
\r
943 // Remove office and vml attribute from elements.
\r
944 [ ( /(?:v|o):\w+/ ), '' ],
\r
945 // Remove lang/language attributes.
\r
946 [ ( /^lang/ ), '' ]
\r
951 'style' : stylesFilter(
\r
953 // Provide a white-list of styles that we preserve, those should
\r
954 // be the ones that could later be altered with editor tools.
\r
956 // Preserve margin-left/right which used as default indent style in the editor.
\r
957 [ ( /^margin$|^margin-(?!bottom|top)/ ), null, function( value, element, name )
\r
959 if ( element.name in { p : 1, div : 1 } )
\r
961 var indentStyleName = config.contentsLangDirection == 'ltr' ?
\r
962 'margin-left' : 'margin-right';
\r
964 // Extract component value from 'margin' shorthand.
\r
965 if ( name == 'margin' )
\r
967 value = getStyleComponents( name, value,
\r
968 [ indentStyleName ] )[ indentStyleName ];
\r
970 else if ( name != indentStyleName )
\r
973 if ( value && !emptyMarginRegex.test( value ) )
\r
974 return [ indentStyleName, value ];
\r
980 // Preserve clear float style.
\r
983 [ ( /^border.*|margin.*|vertical-align|float$/ ), null,
\r
984 function( value, element )
\r
986 if( element.name == 'img' )
\r
990 [ (/^width|height$/ ), null,
\r
991 function( value, element )
\r
993 if( element.name in { table : 1, td : 1, th : 1, img : 1 } )
\r
997 // Otherwise provide a black-list of styles that we remove.
\r
1000 // Fixing color values.
\r
1001 [ ( /-color$/ ), null, function( value )
\r
1003 if( value == 'transparent' )
\r
1005 if( CKEDITOR.env.gecko )
\r
1006 return value.replace( /-moz-use-text-color/g, 'transparent' );
\r
1008 // Remove empty margin values, e.g. 0.00001pt 0em 0pt
\r
1009 [ ( /^margin$/ ), emptyMarginRegex ],
\r
1010 [ 'text-indent', '0cm' ],
\r
1011 [ 'page-break-before' ],
\r
1013 [ 'display', 'none' ],
\r
1014 removeFontStyles ? [ ( /font-?/ ) ] : null
\r
1015 ], removeStyles ),
\r
1017 // Prefer width styles over 'width' attributes.
\r
1018 'width' : function( value, element )
\r
1020 if( element.name in dtd.$tableContent )
\r
1023 // Prefer border styles over table 'border' attributes.
\r
1024 'border' : function( value, element )
\r
1026 if( element.name in dtd.$tableContent )
\r
1030 // Only Firefox carry style sheet from MS-Word, which
\r
1031 // will be applied by us manually. For other browsers
\r
1032 // the css className is useless.
\r
1033 'class' : falsyFilter,
\r
1035 // MS-Word always generate 'background-color' along with 'bgcolor',
\r
1036 // simply drop the deprecated attributes.
\r
1037 'bgcolor' : falsyFilter,
\r
1039 // Deprecate 'valign' attribute in favor of 'vertical-align'.
\r
1040 'valign' : removeStyles ? falsyFilter : function( value, element )
\r
1042 element.addStyle( 'vertical-align', value );
\r
1047 // Fore none-IE, some useful data might be buried under these IE-conditional
\r
1048 // comments where RegExp were the right approach to dig them out where usual approach
\r
1049 // is transform it into a fake element node which hold the desired data.
\r
1051 !CKEDITOR.env.ie ?
\r
1052 function( value, node )
\r
1054 var imageInfo = value.match( /<img.*?>/ ),
\r
1055 listInfo = value.match( /^\[if !supportLists\]([\s\S]*?)\[endif\]$/ );
\r
1057 // Seek for list bullet indicator.
\r
1060 // Bullet symbol could be either text or an image.
\r
1061 var listSymbol = listInfo[ 1 ] || ( imageInfo && 'l.' ),
\r
1062 listType = listSymbol && listSymbol.match( />([^\s]+?)([.)]?)</ );
\r
1063 return createListBulletMarker( listType, listSymbol );
\r
1066 // Reveal the <img> element in conditional comments for Firefox.
\r
1067 if( CKEDITOR.env.gecko && imageInfo )
\r
1069 var img = CKEDITOR.htmlParser.fragment.fromHtml( imageInfo[ 0 ] ).children[ 0 ],
\r
1070 previousComment = node.previous,
\r
1071 // Try to dig the real image link from vml markup from previous comment text.
\r
1072 imgSrcInfo = previousComment && previousComment.value.match( /<v:imagedata[^>]*o:href=['"](.*?)['"]/ ),
\r
1073 imgSrc = imgSrcInfo && imgSrcInfo[ 1 ];
\r
1075 // Is there a real 'src' url to be used?
\r
1076 imgSrc && ( img.attributes.src = imgSrc );
\r
1087 // The paste processor here is just a reduced copy of html data processor.
\r
1088 var pasteProcessor = function()
\r
1090 this.dataFilter = new CKEDITOR.htmlParser.filter();
\r
1093 pasteProcessor.prototype =
\r
1095 toHtml : function( data )
\r
1097 var fragment = CKEDITOR.htmlParser.fragment.fromHtml( data, false ),
\r
1098 writer = new CKEDITOR.htmlParser.basicWriter();
\r
1100 fragment.writeHtml( writer, this.dataFilter );
\r
1101 return writer.getHtml( true );
\r
1105 CKEDITOR.cleanWord = function( data, editor )
\r
1107 // Firefox will be confused by those downlevel-revealed IE conditional
\r
1108 // comments, fixing them first( convert it to upperlevel-revealed one ).
\r
1109 // e.g. <![if !vml]>...<![endif]>
\r
1110 if( CKEDITOR.env.gecko )
\r
1111 data = data.replace( /(<!--\[if[^<]*?\])-->([\S\s]*?)<!--(\[endif\]-->)/gi, '$1$2$3' );
\r
1113 var dataProcessor = new pasteProcessor(),
\r
1114 dataFilter = dataProcessor.dataFilter;
\r
1116 // These rules will have higher priorities than default ones.
\r
1117 dataFilter.addRules( CKEDITOR.plugins.pastefromword.getRules( editor ) );
\r
1119 // Allow extending data filter rules.
\r
1120 editor.fire( 'beforeCleanWord', { filter : dataFilter } );
\r
1124 data = dataProcessor.toHtml( data, false );
\r
1128 alert( editor.lang.pastefromword.error );
\r
1131 /* Below post processing those things that are unable to delivered by filter rules. */
\r
1133 // Remove 'cke' namespaced attribute used in filter rules as marker.
\r
1134 data = data.replace( /cke:.*?".*?"/g, '' );
\r
1136 // Remove empty style attribute.
\r
1137 data = data.replace( /style=""/g, '' );
\r
1139 // Remove the dummy spans ( having no inline style ).
\r
1140 data = data.replace( /<span>/g, '' );
\r
1147 * Whether the ignore all font-related format styles, including:
\r
1150 * - font fore/background color;
\r
1151 * @name CKEDITOR.config.pasteFromWordRemoveFontStyles
\r
1155 * config.pasteFromWordRemoveFontStyles = false;
\r
1159 * Whether transform MS-Word Outline Numbered Heading into html list.
\r
1160 * @name CKEDITOR.config.pasteFromWordNumberedHeadingToList
\r
1164 * config.pasteFromWordNumberedHeadingToList = true;
\r
1168 * Whether remove element styles that can't be managed with editor, note that this
\r
1169 * this doesn't handle the font-specific styles, which depends on
\r
1170 * how {@link CKEDITOR.config.pasteFromWordRemoveFontStyles} is configured.
\r
1171 * @name CKEDITOR.config.pasteFromWordRemoveStyles
\r
1175 * config.pasteFromWordRemoveStyles = false;
\r