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
69 // Adding a (set) of styles to the element's 'style' attributes.
\r
70 elementPrototype.addStyle = function( name, value, isPrepend )
\r
72 var styleText, addingStyleText = '';
\r
74 if ( typeof value == 'string' )
\r
75 addingStyleText += name + ':' + value + ';';
\r
79 if ( typeof name == 'object' )
\r
81 for ( var style in name )
\r
83 if ( name.hasOwnProperty( style ) )
\r
84 addingStyleText += style + ':' + name[ style ] + ';';
\r
87 // raw style text form.
\r
89 addingStyleText += name;
\r
94 if ( !this.attributes )
\r
95 this.attributes = {};
\r
97 styleText = this.attributes.style || '';
\r
99 styleText = ( isPrepend ?
\r
100 [ addingStyleText, styleText ]
\r
101 : [ styleText, addingStyleText ] ).join( ';' );
\r
103 this.attributes.style = styleText.replace( /^;|;(?=;)/, '' );
\r
107 * Return the DTD-valid parent tag names of the specified one.
\r
110 CKEDITOR.dtd.parentOf = function( tagName )
\r
113 for ( var tag in this )
\r
115 if ( tag.indexOf( '$' ) == -1 && this[ tag ][ tagName ] )
\r
121 var cssLengthRelativeUnit = /^([.\d]*)+(em|ex|px|gd|rem|vw|vh|vm|ch|mm|cm|in|pt|pc|deg|rad|ms|s|hz|khz){1}?/i;
\r
122 var emptyMarginRegex = /^(?:\b0[^\s]*\s*){1,4}$/; // e.g. 0px 0pt 0px
\r
124 var listBaseIndent = 0,
\r
125 previousListItemMargin;
\r
127 CKEDITOR.plugins.pastefromword =
\r
131 // Create a <cke:listbullet> which indicate an list item type.
\r
132 createListBulletMarker : function ( bulletStyle, bulletText )
\r
134 var marker = new CKEDITOR.htmlParser.element( 'cke:listbullet' ),
\r
137 // TODO: Support more list style type from MS-Word.
\r
138 if ( !bulletStyle )
\r
140 bulletStyle = 'decimal';
\r
143 else if ( bulletStyle[ 2 ] )
\r
145 if ( !isNaN( bulletStyle[ 1 ] ) )
\r
146 bulletStyle = 'decimal';
\r
147 // No way to distinguish between Roman numerals and Alphas,
\r
148 // detect them as a whole.
\r
149 else if ( /^[a-z]+$/.test( bulletStyle[ 1 ] ) )
\r
150 bulletStyle = 'lower-alpha';
\r
151 else if ( /^[A-Z]+$/.test( bulletStyle[ 1 ] ) )
\r
152 bulletStyle = 'upper-alpha';
\r
153 // Simply use decimal for the rest forms of unrepresentable
\r
154 // numerals, e.g. Chinese...
\r
156 bulletStyle = 'decimal';
\r
162 if ( /[l\u00B7\u2002]/.test( bulletStyle[ 1 ] ) )
\r
163 bulletStyle = 'disc';
\r
164 else if ( /[\u006F\u00D8]/.test( bulletStyle[ 1 ] ) )
\r
165 bulletStyle = 'circle';
\r
166 else if ( /[\u006E\u25C6]/.test( bulletStyle[ 1 ] ) )
\r
167 bulletStyle = 'square';
\r
169 bulletStyle = 'disc';
\r
174 // Represent list type as CSS style.
\r
175 marker.attributes =
\r
177 'cke:listtype' : listType,
\r
178 'style' : 'list-style-type:' + bulletStyle + ';'
\r
180 marker.add( new CKEDITOR.htmlParser.text( bulletText ) );
\r
184 isListBulletIndicator : function( element )
\r
186 var styleText = element.attributes && element.attributes.style;
\r
187 if ( /mso-list\s*:\s*Ignore/i.test( styleText ) )
\r
191 isContainingOnlySpaces : function( element )
\r
194 return ( ( text = element.onlyChild() )
\r
195 && ( /^(:?\s| )+$/ ).test( text.value ) );
\r
198 resolveList : function( element )
\r
200 // <cke:listbullet> indicate a list item.
\r
201 var attrs = element.attributes,
\r
204 if ( ( listMarker = element.removeAnyChildWithName( 'cke:listbullet' ) )
\r
205 && listMarker.length
\r
206 && ( listMarker = listMarker[ 0 ] ) )
\r
208 element.name = 'cke:li';
\r
212 attrs.style = CKEDITOR.plugins.pastefromword.filters.stylesFilter(
\r
214 // Text-indent is not representing list item level any more.
\r
217 // Resolve indent level from 'margin-left' value.
\r
218 [ ( /^margin(:?-left)?$/ ), null, function( margin )
\r
220 // Be able to deal with component/short-hand form style.
\r
221 var values = margin.split( ' ' );
\r
222 margin = CKEDITOR.plugins.pastefromword.utils.convertToPx( values[ 3 ] || values[ 1 ] || values [ 0 ] );
\r
223 margin = parseInt( margin, 10 );
\r
225 // Figure out the indent unit by looking at the first increament.
\r
226 if ( !listBaseIndent && previousListItemMargin && margin > previousListItemMargin )
\r
227 listBaseIndent = margin - previousListItemMargin;
\r
229 attrs[ 'cke:margin' ] = previousListItemMargin = margin;
\r
231 ] )( attrs.style, element ) || '' ;
\r
234 // Inherit list-type-style from bullet.
\r
235 var listBulletAttrs = listMarker.attributes,
\r
236 listBulletStyle = listBulletAttrs.style;
\r
238 element.addStyle( listBulletStyle );
\r
239 CKEDITOR.tools.extend( attrs, listBulletAttrs );
\r
246 // Convert various length units to 'px' in ignorance of DPI.
\r
247 convertToPx : ( function ()
\r
249 var calculator = CKEDITOR.dom.element.createFromHtml(
\r
250 '<div style="position:absolute;left:-9999px;' +
\r
251 'top:-9999px;margin:0px;padding:0px;border:0px;"' +
\r
252 '></div>', CKEDITOR.document );
\r
253 CKEDITOR.document.getBody().append( calculator );
\r
255 return function( cssLength )
\r
257 if ( cssLengthRelativeUnit.test( cssLength ) )
\r
259 calculator.setStyle( 'width', cssLength );
\r
260 return calculator.$.clientWidth + 'px';
\r
267 // Providing a shorthand style then retrieve one or more style component values.
\r
268 getStyleComponents : ( function()
\r
270 var calculator = CKEDITOR.dom.element.createFromHtml(
\r
271 '<div style="position:absolute;left:-9999px;top:-9999px;"></div>',
\r
272 CKEDITOR.document );
\r
273 CKEDITOR.document.getBody().append( calculator );
\r
275 return function( name, styleValue, fetchList )
\r
277 calculator.setStyle( name, styleValue );
\r
279 count = fetchList.length;
\r
280 for ( var i = 0; i < count; i++ )
\r
281 styles[ fetchList[ i ] ] = calculator.getStyle( fetchList[ i ] );
\r
287 listDtdParents : CKEDITOR.dtd.parentOf( 'ol' )
\r
292 // Transform a normal list into flat list items only presentation.
\r
293 // E.g. <ul><li>level1<ol><li>level2</li></ol></li> =>
\r
294 // <cke:li cke:listtype="ul" cke:indent="1">level1</cke:li>
\r
295 // <cke:li cke:listtype="ol" cke:indent="2">level2</cke:li>
\r
296 flattenList : function( element )
\r
298 var attrs = element.attributes,
\r
299 parent = element.parent;
\r
304 // Resolve how many level nested.
\r
307 parent.attributes && parent.attributes[ 'cke:list'] && indentLevel++;
\r
308 parent = parent.parent;
\r
311 // All list items are of the same type.
\r
312 switch ( attrs.type )
\r
315 listStyleType = 'lower-alpha';
\r
317 // TODO: Support more list style type from MS-Word.
\r
320 var children = element.children,
\r
323 for ( var i = 0; i < children.length; i++ )
\r
325 child = children[ i ];
\r
326 var attributes = child.attributes;
\r
328 if ( child.name in CKEDITOR.dtd.$listItem )
\r
330 var listItemChildren = child.children,
\r
331 count = listItemChildren.length,
\r
332 last = listItemChildren[ count - 1 ];
\r
334 // Move out nested list.
\r
335 if ( last.name in CKEDITOR.dtd.$list )
\r
337 children.splice( i + 1, 0, last );
\r
338 last.parent = element;
\r
340 // Remove the parent list item if it's just a holder.
\r
341 if ( !--listItemChildren.length )
\r
342 children.splice( i, 1 );
\r
345 child.name = 'cke:li';
\r
346 attributes[ 'cke:indent' ] = indentLevel;
\r
347 previousListItemMargin = 0;
\r
348 attributes[ 'cke:listtype' ] = element.name;
\r
349 listStyleType && child.addStyle( 'list-style-type', listStyleType, true );
\r
353 delete element.name;
\r
355 // We're loosing tag name here, signalize this element as a list.
\r
356 attrs[ 'cke:list' ] = 1;
\r
360 * Try to collect all list items among the children and establish one
\r
361 * or more HTML list structures for them.
\r
364 assembleList : function( element )
\r
366 var children = element.children, child,
\r
367 listItem, // The current processing cke:li element.
\r
369 listType, // Determine the root type of the list.
\r
370 listItemIndent, // Indent level of current list item.
\r
371 lastListItem, // The previous one just been added to the list.
\r
372 list, parentList, // Current staging list and it's parent list if any.
\r
375 for ( var i = 0; i < children.length; i++ )
\r
377 child = children[ i ];
\r
379 if ( 'cke:li' == child.name )
\r
383 listItemAttrs = listItem.attributes;
\r
384 listType = listItem.attributes[ 'cke:listtype' ];
\r
386 // List item indent level might come from a real list indentation or
\r
387 // been resolved from a pseudo list item's margin value, even get
\r
388 // no indentation at all.
\r
389 listItemIndent = parseInt( listItemAttrs[ 'cke:indent' ], 10 )
\r
390 || listBaseIndent && ( Math.ceil( listItemAttrs[ 'cke:margin' ] / listBaseIndent ) )
\r
393 // Ignore the 'list-style-type' attribute if it's matched with
\r
394 // the list root element's default style type.
\r
395 listItemAttrs.style && ( listItemAttrs.style =
\r
396 CKEDITOR.plugins.pastefromword.filters.stylesFilter(
\r
398 [ 'list-style-type', listType == 'ol' ? 'decimal' : 'disc' ]
\r
399 ] )( listItemAttrs.style )
\r
404 list = new CKEDITOR.htmlParser.element( listType );
\r
405 list.add( listItem );
\r
406 children[ i ] = list;
\r
410 if ( listItemIndent > indent )
\r
412 list = new CKEDITOR.htmlParser.element( listType );
\r
413 list.add( listItem );
\r
414 lastListItem.add( list );
\r
416 else if ( listItemIndent < indent )
\r
418 // There might be a negative gap between two list levels. (#4944)
\r
419 var diff = indent - listItemIndent,
\r
421 while ( diff-- && ( parent = list.parent ) )
\r
422 list = parent.parent;
\r
424 list.add( listItem );
\r
427 list.add( listItem );
\r
429 children.splice( i--, 1 );
\r
432 lastListItem = listItem;
\r
433 indent = listItemIndent;
\r
439 listBaseIndent = 0;
\r
443 * A simple filter which always rejecting.
\r
445 falsyFilter : function( value )
\r
451 * A filter dedicated on the 'style' attribute filtering, e.g. dropping/replacing style properties.
\r
452 * @param styles {Array} in form of [ styleNameRegexp, styleValueRegexp,
\r
453 * newStyleValue/newStyleGenerator, newStyleName ] where only the first
\r
454 * parameter is mandatory.
\r
455 * @param whitelist {Boolean} Whether the {@param styles} will be considered as a white-list.
\r
457 stylesFilter : function( styles, whitelist )
\r
459 return function( styleText, element )
\r
462 // html-encoded quote might be introduced by 'font-family'
\r
463 // from MS-Word which confused the following regexp. e.g.
\r
464 //'font-family: "Lucida, Console"'
\r
466 .replace( /"/g, '"' )
\r
467 .replace( /\s*([^ :;]+)\s*:\s*([^;]+)\s*(?=;|$)/g,
\r
468 function( match, name, value )
\r
470 name = name.toLowerCase();
\r
471 name == 'font-family' && ( value = value.replace( /["']/g, '' ) );
\r
477 for ( var i = 0 ; i < styles.length; i++ )
\r
481 namePattern = styles[ i ][ 0 ];
\r
482 valuePattern = styles[ i ][ 1 ];
\r
483 newValue = styles[ i ][ 2 ];
\r
484 newName = styles[ i ][ 3 ];
\r
486 if ( name.match( namePattern )
\r
487 && ( !valuePattern || value.match( valuePattern ) ) )
\r
489 name = newName || name;
\r
490 whitelist && ( newValue = newValue || value );
\r
492 if ( typeof newValue == 'function' )
\r
493 newValue = newValue( value, element, name );
\r
495 // Return an couple indicate both name and value
\r
497 if ( newValue && newValue.push )
\r
498 name = newValue[ 0 ], newValue = newValue[ 1 ];
\r
500 if ( typeof newValue == 'string' )
\r
501 rules.push( [ name, newValue ] );
\r
507 !whitelist && rules.push( [ name, value ] );
\r
511 for ( var i = 0 ; i < rules.length ; i++ )
\r
512 rules[ i ] = rules[ i ].join( ':' );
\r
513 return rules.length ?
\r
514 ( rules.join( ';' ) + ';' ) : false;
\r
519 * Migrate the element by decorate styles on it.
\r
520 * @param styleDefiniton
\r
523 elementMigrateFilter : function ( styleDefiniton, variables )
\r
525 return function( element )
\r
529 new CKEDITOR.style( styleDefiniton, variables )._.definition
\r
531 element.name = styleDef.element;
\r
532 CKEDITOR.tools.extend( element.attributes, CKEDITOR.tools.clone( styleDef.attributes ) );
\r
533 element.addStyle( CKEDITOR.style.getStyleText( styleDef ) );
\r
538 * Migrate styles by creating a new nested stylish element.
\r
539 * @param styleDefinition
\r
541 styleMigrateFilter : function( styleDefinition, variableName )
\r
544 var elementMigrateFilter = this.elementMigrateFilter;
\r
545 return function( value, element )
\r
547 // Build an stylish element first.
\r
548 var styleElement = new CKEDITOR.htmlParser.element( null ),
\r
551 variables[ variableName ] = value;
\r
552 elementMigrateFilter( styleDefinition, variables )( styleElement );
\r
553 // Place the new element inside the existing span.
\r
554 styleElement.children = element.children;
\r
555 element.children = [ styleElement ];
\r
560 * A filter which remove cke-namespaced-attribute on
\r
561 * all none-cke-namespaced elements.
\r
565 bogusAttrFilter : function( value, element )
\r
567 if ( element.name.indexOf( 'cke:' ) == -1 )
\r
572 * A filter which will be used to apply inline css style according the stylesheet
\r
573 * definition rules, is generated lazily when filtering.
\r
575 applyStyleFilter : null
\r
579 getRules : function( editor )
\r
581 var dtd = CKEDITOR.dtd,
\r
582 blockLike = CKEDITOR.tools.extend( {}, dtd.$block, dtd.$listItem, dtd.$tableContent ),
\r
583 config = editor.config,
\r
584 filters = this.filters,
\r
585 falsyFilter = filters.falsyFilter,
\r
586 stylesFilter = filters.stylesFilter,
\r
587 elementMigrateFilter = filters.elementMigrateFilter,
\r
588 styleMigrateFilter = CKEDITOR.tools.bind( this.filters.styleMigrateFilter, this.filters ),
\r
589 createListBulletMarker = this.utils.createListBulletMarker,
\r
590 flattenList = filters.flattenList,
\r
591 assembleList = filters.assembleList,
\r
592 isListBulletIndicator = this.utils.isListBulletIndicator,
\r
593 containsNothingButSpaces = this.utils.isContainingOnlySpaces,
\r
594 resolveListItem = this.utils.resolveList,
\r
595 convertToPx = this.utils.convertToPx,
\r
596 getStyleComponents = this.utils.getStyleComponents,
\r
597 listDtdParents = this.utils.listDtdParents,
\r
598 removeFontStyles = config.pasteFromWordRemoveFontStyles !== false,
\r
599 removeStyles = config.pasteFromWordRemoveStyles !== false;
\r
605 // Remove script, meta and link elements.
\r
606 [ ( /meta|link|script/ ), '' ]
\r
609 root : function( element )
\r
611 element.filterChildren();
\r
612 assembleList( element );
\r
617 '^' : function( element )
\r
619 // Transform CSS style declaration to inline style.
\r
620 var applyStyleFilter;
\r
621 if ( CKEDITOR.env.gecko && ( applyStyleFilter = filters.applyStyleFilter ) )
\r
622 applyStyleFilter( element );
\r
625 $ : function( element )
\r
627 var tagName = element.name || '',
\r
628 attrs = element.attributes;
\r
630 // Convert length unit of width/height on blocks to
\r
631 // a more editor-friendly way (px).
\r
632 if ( tagName in blockLike
\r
635 attrs.style = stylesFilter(
\r
636 [ [ ( /^(:?width|height)$/ ), null, convertToPx ] ] )( attrs.style ) || '';
\r
639 // Processing headings.
\r
640 if ( tagName.match( /h\d/ ) )
\r
642 element.filterChildren();
\r
643 // Is the heading actually a list item?
\r
644 if ( resolveListItem( element ) )
\r
647 // Adapt heading styles to editor's convention.
\r
648 elementMigrateFilter( config[ 'format_' + tagName ] )( element );
\r
650 // Remove inline elements which contain only empty spaces.
\r
651 else if ( tagName in dtd.$inline )
\r
653 element.filterChildren();
\r
654 if ( containsNothingButSpaces( element ) )
\r
655 delete element.name;
\r
657 // Remove element with ms-office namespace,
\r
658 // with it's content preserved, e.g. 'o:p'.
\r
659 else if ( tagName.indexOf( ':' ) != -1
\r
660 && tagName.indexOf( 'cke' ) == -1 )
\r
662 element.filterChildren();
\r
664 // Restore image real link from vml.
\r
665 if ( tagName == 'v:imagedata' )
\r
667 var href = element.attributes[ 'o:href' ];
\r
669 element.attributes.src = href;
\r
670 element.name = 'img';
\r
673 delete element.name;
\r
676 // Assembling list items into a whole list.
\r
677 if ( tagName in listDtdParents )
\r
679 element.filterChildren();
\r
680 assembleList( element );
\r
684 // We'll drop any style sheet, but Firefox conclude
\r
685 // certain styles in a single style element, which are
\r
686 // required to be changed into inline ones.
\r
687 'style' : function( element )
\r
689 if ( CKEDITOR.env.gecko )
\r
691 // Grab only the style definition section.
\r
692 var styleDefSection = element.onlyChild().value.match( /\/\* Style Definitions \*\/([\s\S]*?)\/\*/ ),
\r
693 styleDefText = styleDefSection && styleDefSection[ 1 ],
\r
694 rules = {}; // Storing the parsed result.
\r
696 if ( styleDefText )
\r
699 // Remove line-breaks.
\r
700 .replace(/[\n\r]/g,'')
\r
701 // Extract selectors and style properties.
\r
702 .replace( /(.+?)\{(.+?)\}/g,
\r
703 function( rule, selectors, styleBlock )
\r
705 selectors = selectors.split( ',' );
\r
706 var length = selectors.length, selector;
\r
707 for ( var i = 0; i < length; i++ )
\r
709 // Assume MS-Word mostly generate only simple
\r
710 // selector( [Type selector][Class selector]).
\r
711 CKEDITOR.tools.trim( selectors[ i ] )
\r
712 .replace( /^(\w+)(\.[\w-]+)?$/g,
\r
713 function( match, tagName, className )
\r
715 tagName = tagName || '*';
\r
716 className = className.substring( 1, className.length );
\r
718 // Reject MS-Word Normal styles.
\r
719 if ( className.match( /MsoNormal/ ) )
\r
722 if ( !rules[ tagName ] )
\r
723 rules[ tagName ] = {};
\r
725 rules[ tagName ][ className ] = styleBlock;
\r
727 rules[ tagName ] = styleBlock;
\r
732 filters.applyStyleFilter = function( element )
\r
734 var name = rules[ '*' ] ? '*' : element.name,
\r
735 className = element.attributes && element.attributes[ 'class' ],
\r
737 if ( name in rules )
\r
739 style = rules[ name ];
\r
740 if ( typeof style == 'object' )
\r
741 style = style[ className ];
\r
742 // Maintain style rules priorities.
\r
743 style && element.addStyle( style, true );
\r
751 'p' : function( element )
\r
753 element.filterChildren();
\r
755 // Is the paragraph actually a list item?
\r
756 if ( resolveListItem( element ) )
\r
759 // Adapt paragraph formatting to editor's convention
\r
760 // according to enter-mode.
\r
761 if ( config.enterMode == CKEDITOR.ENTER_BR )
\r
763 // We suffer from attribute/style lost in this situation.
\r
764 delete element.name;
\r
765 element.add( new CKEDITOR.htmlParser.element( 'br' ) );
\r
768 elementMigrateFilter( config[ 'format_' + ( config.enterMode == CKEDITOR.ENTER_P ? 'p' : 'div' ) ] )( element );
\r
771 'div' : function( element )
\r
773 // Aligned table with no text surrounded is represented by a wrapper div, from which
\r
774 // table cells inherit as text-align styles, which is wrong.
\r
775 // Instead we use a clear-float div after the table to properly achieve the same layout.
\r
776 var singleChild = element.onlyChild();
\r
777 if ( singleChild && singleChild.name == 'table' )
\r
779 var attrs = element.attributes;
\r
780 singleChild.attributes = CKEDITOR.tools.extend( singleChild.attributes, attrs );
\r
781 attrs.style && singleChild.addStyle( attrs.style );
\r
783 var clearFloatDiv = new CKEDITOR.htmlParser.element( 'div' );
\r
784 clearFloatDiv.addStyle( 'clear' ,'both' );
\r
785 element.add( clearFloatDiv );
\r
786 delete element.name;
\r
790 'td' : function ( element )
\r
792 // 'td' in 'thead' is actually <th>.
\r
793 if ( element.getAncestor( 'thead') )
\r
794 element.name = 'th';
\r
797 // MS-Word sometimes present list as a mixing of normal list
\r
798 // and pseudo-list, normalize the previous ones into pseudo form.
\r
799 'ol' : flattenList,
\r
800 'ul' : flattenList,
\r
801 'dl' : flattenList,
\r
803 'font' : function( element )
\r
805 // IE/Safari: drop the font tag if it comes from list bullet text.
\r
806 if ( !CKEDITOR.env.gecko && isListBulletIndicator( element.parent ) )
\r
808 delete element.name;
\r
812 element.filterChildren();
\r
814 var attrs = element.attributes,
\r
815 styleText = attrs.style,
\r
816 parent = element.parent;
\r
818 if ( 'font' == parent.name ) // Merge nested <font> tags.
\r
820 CKEDITOR.tools.extend( parent.attributes,
\r
821 element.attributes );
\r
822 styleText && parent.addStyle( styleText );
\r
823 delete element.name;
\r
825 // Convert the merged into a span with all attributes preserved.
\r
828 styleText = styleText || '';
\r
829 // IE's having those deprecated attributes, normalize them.
\r
832 attrs.color != '#000000' && ( styleText += 'color:' + attrs.color + ';' );
\r
833 delete attrs.color;
\r
837 styleText += 'font-family:' + attrs.face + ';';
\r
840 // TODO: Mapping size in ranges of xx-small,
\r
841 // x-small, small, medium, large, x-large, xx-large.
\r
844 styleText += 'font-size:' +
\r
845 ( attrs.size > 3 ? 'large'
\r
846 : ( attrs.size < 3 ? 'small' : 'medium' ) ) + ';';
\r
850 element.name = 'span';
\r
851 element.addStyle( styleText );
\r
855 'span' : function( element )
\r
857 // IE/Safari: remove the span if it comes from list bullet text.
\r
858 if ( !CKEDITOR.env.gecko && isListBulletIndicator( element.parent ) )
\r
861 element.filterChildren();
\r
862 if ( containsNothingButSpaces( element ) )
\r
864 delete element.name;
\r
868 // For IE/Safari: List item bullet type is supposed to be indicated by
\r
869 // the text of a span with style 'mso-list : Ignore' or an image.
\r
870 if ( !CKEDITOR.env.gecko && isListBulletIndicator( element ) )
\r
872 var listSymbolNode = element.firstChild( function( node )
\r
874 return node.value || node.name == 'img';
\r
877 var listSymbol = listSymbolNode && ( listSymbolNode.value || 'l.' ),
\r
878 listType = listSymbol.match( /^([^\s]+?)([.)]?)$/ );
\r
879 return createListBulletMarker( listType, listSymbol );
\r
882 // Update the src attribute of image element with href.
\r
883 var children = element.children,
\r
884 attrs = element.attributes,
\r
885 styleText = attrs && attrs.style,
\r
886 firstChild = children && children[ 0 ];
\r
888 // Assume MS-Word mostly carry font related styles on <span>,
\r
889 // adapting them to editor's convention.
\r
892 attrs.style = stylesFilter(
\r
894 // Drop 'inline-height' style which make lines overlapping.
\r
896 [ ( /^font-family$/ ), null, !removeFontStyles ? styleMigrateFilter( config[ 'font_style' ], 'family' ) : null ] ,
\r
897 [ ( /^font-size$/ ), null, !removeFontStyles ? styleMigrateFilter( config[ 'fontSize_style' ], 'size' ) : null ] ,
\r
898 [ ( /^color$/ ), null, !removeFontStyles ? styleMigrateFilter( config[ 'colorButton_foreStyle' ], 'color' ) : null ] ,
\r
899 [ ( /^background-color$/ ), null, !removeFontStyles ? styleMigrateFilter( config[ 'colorButton_backStyle' ], 'color' ) : null ]
\r
900 ] )( styleText, element ) || '';
\r
906 // Migrate basic style formats to editor configured ones.
\r
907 'b' : elementMigrateFilter( config[ 'coreStyles_bold' ] ),
\r
908 'i' : elementMigrateFilter( config[ 'coreStyles_italic' ] ),
\r
909 'u' : elementMigrateFilter( config[ 'coreStyles_underline' ] ),
\r
910 's' : elementMigrateFilter( config[ 'coreStyles_strike' ] ),
\r
911 'sup' : elementMigrateFilter( config[ 'coreStyles_superscript' ] ),
\r
912 'sub' : elementMigrateFilter( config[ 'coreStyles_subscript' ] ),
\r
913 // Editor doesn't support anchor with content currently (#3582),
\r
914 // drop such anchors with content preserved.
\r
915 'a' : function( element )
\r
917 var attrs = element.attributes;
\r
918 if ( attrs && !attrs.href && attrs.name )
\r
919 delete element.name;
\r
921 'cke:listbullet' : function( element )
\r
923 if ( element.getAncestor( /h\d/ ) && !config.pasteFromWordNumberedHeadingToList )
\r
924 delete element.name;
\r
930 // Remove onmouseover and onmouseout events (from MS Word comments effect)
\r
931 [ ( /^onmouse(:?out|over)/ ), '' ],
\r
932 // Onload on image element.
\r
933 [ ( /^onload$/ ), '' ],
\r
934 // Remove office and vml attribute from elements.
\r
935 [ ( /(?:v|o):\w+/ ), '' ],
\r
936 // Remove lang/language attributes.
\r
937 [ ( /^lang/ ), '' ]
\r
942 'style' : stylesFilter(
\r
944 // Provide a white-list of styles that we preserve, those should
\r
945 // be the ones that could later be altered with editor tools.
\r
947 // Preserve margin-left/right which used as default indent style in the editor.
\r
948 [ ( /^margin$|^margin-(?!bottom|top)/ ), null, function( value, element, name )
\r
950 if ( element.name in { p : 1, div : 1 } )
\r
952 var indentStyleName = config.contentsLangDirection == 'ltr' ?
\r
953 'margin-left' : 'margin-right';
\r
955 // Extract component value from 'margin' shorthand.
\r
956 if ( name == 'margin' )
\r
958 value = getStyleComponents( name, value,
\r
959 [ indentStyleName ] )[ indentStyleName ];
\r
961 else if ( name != indentStyleName )
\r
964 if ( value && !emptyMarginRegex.test( value ) )
\r
965 return [ indentStyleName, value ];
\r
971 // Preserve clear float style.
\r
974 [ ( /^border.*|margin.*|vertical-align|float$/ ), null,
\r
975 function( value, element )
\r
977 if ( element.name == 'img' )
\r
981 [ (/^width|height$/ ), null,
\r
982 function( value, element )
\r
984 if ( element.name in { table : 1, td : 1, th : 1, img : 1 } )
\r
988 // Otherwise provide a black-list of styles that we remove.
\r
991 // Fixing color values.
\r
992 [ ( /-color$/ ), null, function( value )
\r
994 if ( value == 'transparent' )
\r
996 if ( CKEDITOR.env.gecko )
\r
997 return value.replace( /-moz-use-text-color/g, 'transparent' );
\r
999 // Remove empty margin values, e.g. 0.00001pt 0em 0pt
\r
1000 [ ( /^margin$/ ), emptyMarginRegex ],
\r
1001 [ 'text-indent', '0cm' ],
\r
1002 [ 'page-break-before' ],
\r
1004 [ 'display', 'none' ],
\r
1005 removeFontStyles ? [ ( /font-?/ ) ] : null
\r
1006 ], removeStyles ),
\r
1008 // Prefer width styles over 'width' attributes.
\r
1009 'width' : function( value, element )
\r
1011 if ( element.name in dtd.$tableContent )
\r
1014 // Prefer border styles over table 'border' attributes.
\r
1015 'border' : function( value, element )
\r
1017 if ( element.name in dtd.$tableContent )
\r
1021 // Only Firefox carry style sheet from MS-Word, which
\r
1022 // will be applied by us manually. For other browsers
\r
1023 // the css className is useless.
\r
1024 'class' : falsyFilter,
\r
1026 // MS-Word always generate 'background-color' along with 'bgcolor',
\r
1027 // simply drop the deprecated attributes.
\r
1028 'bgcolor' : falsyFilter,
\r
1030 // Deprecate 'valign' attribute in favor of 'vertical-align'.
\r
1031 'valign' : removeStyles ? falsyFilter : function( value, element )
\r
1033 element.addStyle( 'vertical-align', value );
\r
1038 // Fore none-IE, some useful data might be buried under these IE-conditional
\r
1039 // comments where RegExp were the right approach to dig them out where usual approach
\r
1040 // is transform it into a fake element node which hold the desired data.
\r
1042 !CKEDITOR.env.ie ?
\r
1043 function( value, node )
\r
1045 var imageInfo = value.match( /<img.*?>/ ),
\r
1046 listInfo = value.match( /^\[if !supportLists\]([\s\S]*?)\[endif\]$/ );
\r
1048 // Seek for list bullet indicator.
\r
1051 // Bullet symbol could be either text or an image.
\r
1052 var listSymbol = listInfo[ 1 ] || ( imageInfo && 'l.' ),
\r
1053 listType = listSymbol && listSymbol.match( />([^\s]+?)([.)]?)</ );
\r
1054 return createListBulletMarker( listType, listSymbol );
\r
1057 // Reveal the <img> element in conditional comments for Firefox.
\r
1058 if ( CKEDITOR.env.gecko && imageInfo )
\r
1060 var img = CKEDITOR.htmlParser.fragment.fromHtml( imageInfo[ 0 ] ).children[ 0 ],
\r
1061 previousComment = node.previous,
\r
1062 // Try to dig the real image link from vml markup from previous comment text.
\r
1063 imgSrcInfo = previousComment && previousComment.value.match( /<v:imagedata[^>]*o:href=['"](.*?)['"]/ ),
\r
1064 imgSrc = imgSrcInfo && imgSrcInfo[ 1 ];
\r
1066 // Is there a real 'src' url to be used?
\r
1067 imgSrc && ( img.attributes.src = imgSrc );
\r
1078 // The paste processor here is just a reduced copy of html data processor.
\r
1079 var pasteProcessor = function()
\r
1081 this.dataFilter = new CKEDITOR.htmlParser.filter();
\r
1084 pasteProcessor.prototype =
\r
1086 toHtml : function( data )
\r
1088 var fragment = CKEDITOR.htmlParser.fragment.fromHtml( data, false ),
\r
1089 writer = new CKEDITOR.htmlParser.basicWriter();
\r
1091 fragment.writeHtml( writer, this.dataFilter );
\r
1092 return writer.getHtml( true );
\r
1096 CKEDITOR.cleanWord = function( data, editor )
\r
1098 // Firefox will be confused by those downlevel-revealed IE conditional
\r
1099 // comments, fixing them first( convert it to upperlevel-revealed one ).
\r
1100 // e.g. <![if !vml]>...<![endif]>
\r
1101 if ( CKEDITOR.env.gecko )
\r
1102 data = data.replace( /(<!--\[if[^<]*?\])-->([\S\s]*?)<!--(\[endif\]-->)/gi, '$1$2$3' );
\r
1104 var dataProcessor = new pasteProcessor(),
\r
1105 dataFilter = dataProcessor.dataFilter;
\r
1107 // These rules will have higher priorities than default ones.
\r
1108 dataFilter.addRules( CKEDITOR.plugins.pastefromword.getRules( editor ) );
\r
1110 // Allow extending data filter rules.
\r
1111 editor.fire( 'beforeCleanWord', { filter : dataFilter } );
\r
1115 data = dataProcessor.toHtml( data, false );
\r
1119 alert( editor.lang.pastefromword.error );
\r
1122 /* Below post processing those things that are unable to delivered by filter rules. */
\r
1124 // Remove 'cke' namespaced attribute used in filter rules as marker.
\r
1125 data = data.replace( /cke:.*?".*?"/g, '' );
\r
1127 // Remove empty style attribute.
\r
1128 data = data.replace( /style=""/g, '' );
\r
1130 // Remove the dummy spans ( having no inline style ).
\r
1131 data = data.replace( /<span>/g, '' );
\r
1138 * Whether to ignore all font related formatting styles, including:
\r
1139 * <ul> <li>font size;</li>
\r
1140 * <li>font family;</li>
\r
1141 * <li>font foreground/background color.</li></ul>
\r
1142 * @name CKEDITOR.config.pasteFromWordRemoveFontStyles
\r
1147 * config.pasteFromWordRemoveFontStyles = false;
\r
1151 * Whether to transform MS Word outline numbered headings into lists.
\r
1152 * @name CKEDITOR.config.pasteFromWordNumberedHeadingToList
\r
1157 * config.pasteFromWordNumberedHeadingToList = true;
\r
1161 * Whether to remove element styles that can't be managed with the editor. Note
\r
1162 * that this doesn't handle the font specific styles, which depends on the
\r
1163 * {@link CKEDITOR.config.pasteFromWordRemoveFontStyles} setting instead.
\r
1164 * @name CKEDITOR.config.pasteFromWordRemoveStyles
\r
1169 * config.pasteFromWordRemoveStyles = false;
\r