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
123 var romanLiternalPattern = '^m{0,4}(cm|cd|d?c{0,3})(xc|xl|l?x{0,3})(ix|iv|v?i{0,3})$',
\r
124 lowerRomanLiteralRegex = new RegExp( romanLiternalPattern ),
\r
125 upperRomanLiteralRegex = new RegExp( romanLiternalPattern.toUpperCase() );
\r
127 var listBaseIndent = 0,
\r
128 previousListItemMargin;
\r
130 CKEDITOR.plugins.pastefromword =
\r
134 // Create a <cke:listbullet> which indicate an list item type.
\r
135 createListBulletMarker : function ( bulletStyle, bulletText )
\r
137 var marker = new CKEDITOR.htmlParser.element( 'cke:listbullet' ),
\r
140 // TODO: Support more list style type from MS-Word.
\r
141 if ( !bulletStyle )
\r
143 bulletStyle = 'decimal';
\r
146 else if ( bulletStyle[ 2 ] )
\r
148 if ( !isNaN( bulletStyle[ 1 ] ) )
\r
149 bulletStyle = 'decimal';
\r
150 else if ( lowerRomanLiteralRegex.test( bulletStyle[ 1 ] ) )
\r
151 bulletStyle = 'lower-roman';
\r
152 else if ( upperRomanLiteralRegex.test( bulletStyle[ 1 ] ) )
\r
153 bulletStyle = 'upper-roman';
\r
154 else if ( /^[a-z]+$/.test( bulletStyle[ 1 ] ) )
\r
155 bulletStyle = 'lower-alpha';
\r
156 else if ( /^[A-Z]+$/.test( bulletStyle[ 1 ] ) )
\r
157 bulletStyle = 'upper-alpha';
\r
158 // Simply use decimal for the rest forms of unrepresentable
\r
159 // numerals, e.g. Chinese...
\r
161 bulletStyle = 'decimal';
\r
167 if ( /[l\u00B7\u2002]/.test( bulletStyle[ 1 ] ) )
\r
168 bulletStyle = 'disc';
\r
169 else if ( /[\u006F\u00D8]/.test( bulletStyle[ 1 ] ) )
\r
170 bulletStyle = 'circle';
\r
171 else if ( /[\u006E\u25C6]/.test( bulletStyle[ 1 ] ) )
\r
172 bulletStyle = 'square';
\r
174 bulletStyle = 'disc';
\r
179 // Represent list type as CSS style.
\r
180 marker.attributes =
\r
182 'cke:listtype' : listType,
\r
183 'style' : 'list-style-type:' + bulletStyle + ';'
\r
185 marker.add( new CKEDITOR.htmlParser.text( bulletText ) );
\r
189 isListBulletIndicator : function( element )
\r
191 var styleText = element.attributes && element.attributes.style;
\r
192 if ( /mso-list\s*:\s*Ignore/i.test( styleText ) )
\r
196 isContainingOnlySpaces : function( element )
\r
199 return ( ( text = element.onlyChild() )
\r
200 && ( /^(:?\s| )+$/ ).test( text.value ) );
\r
203 resolveList : function( element )
\r
205 // <cke:listbullet> indicate a list item.
\r
206 var attrs = element.attributes,
\r
209 if ( ( listMarker = element.removeAnyChildWithName( 'cke:listbullet' ) )
\r
210 && listMarker.length
\r
211 && ( listMarker = listMarker[ 0 ] ) )
\r
213 element.name = 'cke:li';
\r
217 attrs.style = CKEDITOR.plugins.pastefromword.filters.stylesFilter(
\r
219 // Text-indent is not representing list item level any more.
\r
222 // Resolve indent level from 'margin-left' value.
\r
223 [ ( /^margin(:?-left)?$/ ), null, function( margin )
\r
225 // Be able to deal with component/short-hand form style.
\r
226 var values = margin.split( ' ' );
\r
227 margin = CKEDITOR.plugins.pastefromword.utils.convertToPx( values[ 3 ] || values[ 1 ] || values [ 0 ] );
\r
228 margin = parseInt( margin, 10 );
\r
230 // Figure out the indent unit by looking at the first increament.
\r
231 if ( !listBaseIndent && previousListItemMargin && margin > previousListItemMargin )
\r
232 listBaseIndent = margin - previousListItemMargin;
\r
234 attrs[ 'cke:margin' ] = previousListItemMargin = margin;
\r
236 ] )( attrs.style, element ) || '' ;
\r
239 // Inherit list-type-style from bullet.
\r
240 var listBulletAttrs = listMarker.attributes,
\r
241 listBulletStyle = listBulletAttrs.style;
\r
243 element.addStyle( listBulletStyle );
\r
244 CKEDITOR.tools.extend( attrs, listBulletAttrs );
\r
251 // Convert various length units to 'px' in ignorance of DPI.
\r
252 convertToPx : ( function ()
\r
254 var calculator = CKEDITOR.dom.element.createFromHtml(
\r
255 '<div style="position:absolute;left:-9999px;' +
\r
256 'top:-9999px;margin:0px;padding:0px;border:0px;"' +
\r
257 '></div>', CKEDITOR.document );
\r
258 CKEDITOR.document.getBody().append( calculator );
\r
260 return function( cssLength )
\r
262 if ( cssLengthRelativeUnit.test( cssLength ) )
\r
264 calculator.setStyle( 'width', cssLength );
\r
265 return calculator.$.clientWidth + 'px';
\r
272 // Providing a shorthand style then retrieve one or more style component values.
\r
273 getStyleComponents : ( function()
\r
275 var calculator = CKEDITOR.dom.element.createFromHtml(
\r
276 '<div style="position:absolute;left:-9999px;top:-9999px;"></div>',
\r
277 CKEDITOR.document );
\r
278 CKEDITOR.document.getBody().append( calculator );
\r
280 return function( name, styleValue, fetchList )
\r
282 calculator.setStyle( name, styleValue );
\r
284 count = fetchList.length;
\r
285 for ( var i = 0; i < count; i++ )
\r
286 styles[ fetchList[ i ] ] = calculator.getStyle( fetchList[ i ] );
\r
292 listDtdParents : CKEDITOR.dtd.parentOf( 'ol' )
\r
297 // Transform a normal list into flat list items only presentation.
\r
298 // E.g. <ul><li>level1<ol><li>level2</li></ol></li> =>
\r
299 // <cke:li cke:listtype="ul" cke:indent="1">level1</cke:li>
\r
300 // <cke:li cke:listtype="ol" cke:indent="2">level2</cke:li>
\r
301 flattenList : function( element )
\r
303 var attrs = element.attributes,
\r
304 parent = element.parent;
\r
309 // Resolve how many level nested.
\r
312 parent.attributes && parent.attributes[ 'cke:list'] && indentLevel++;
\r
313 parent = parent.parent;
\r
316 // All list items are of the same type.
\r
317 switch ( attrs.type )
\r
320 listStyleType = 'lower-alpha';
\r
322 // TODO: Support more list style type from MS-Word.
\r
325 var children = element.children,
\r
328 for ( var i = 0; i < children.length; i++ )
\r
330 child = children[ i ];
\r
331 var attributes = child.attributes;
\r
333 if ( child.name in CKEDITOR.dtd.$listItem )
\r
335 var listItemChildren = child.children,
\r
336 count = listItemChildren.length,
\r
337 last = listItemChildren[ count - 1 ];
\r
339 // Move out nested list.
\r
340 if ( last.name in CKEDITOR.dtd.$list )
\r
342 children.splice( i + 1, 0, last );
\r
343 last.parent = element;
\r
345 // Remove the parent list item if it's just a holder.
\r
346 if ( !--listItemChildren.length )
\r
347 children.splice( i, 1 );
\r
350 child.name = 'cke:li';
\r
351 attributes[ 'cke:indent' ] = indentLevel;
\r
352 previousListItemMargin = 0;
\r
353 attributes[ 'cke:listtype' ] = element.name;
\r
354 listStyleType && child.addStyle( 'list-style-type', listStyleType, true );
\r
358 delete element.name;
\r
360 // We're loosing tag name here, signalize this element as a list.
\r
361 attrs[ 'cke:list' ] = 1;
\r
365 * Try to collect all list items among the children and establish one
\r
366 * or more HTML list structures for them.
\r
369 assembleList : function( element )
\r
371 var children = element.children, child,
\r
372 listItem, // The current processing cke:li element.
\r
374 listType, // Determine the root type of the list.
\r
375 listItemIndent, // Indent level of current list item.
\r
376 lastListItem, // The previous one just been added to the list.
\r
377 list, parentList, // Current staging list and it's parent list if any.
\r
380 for ( var i = 0; i < children.length; i++ )
\r
382 child = children[ i ];
\r
384 if ( 'cke:li' == child.name )
\r
388 listItemAttrs = listItem.attributes;
\r
389 listType = listItem.attributes[ 'cke:listtype' ];
\r
391 // List item indent level might come from a real list indentation or
\r
392 // been resolved from a pseudo list item's margin value, even get
\r
393 // no indentation at all.
\r
394 listItemIndent = parseInt( listItemAttrs[ 'cke:indent' ], 10 )
\r
395 || listBaseIndent && ( Math.ceil( listItemAttrs[ 'cke:margin' ] / listBaseIndent ) )
\r
398 // Ignore the 'list-style-type' attribute if it's matched with
\r
399 // the list root element's default style type.
\r
400 listItemAttrs.style && ( listItemAttrs.style =
\r
401 CKEDITOR.plugins.pastefromword.filters.stylesFilter(
\r
403 [ 'list-style-type', listType == 'ol' ? 'decimal' : 'disc' ]
\r
404 ] )( listItemAttrs.style )
\r
409 list = new CKEDITOR.htmlParser.element( listType );
\r
410 list.add( listItem );
\r
411 children[ i ] = list;
\r
415 if ( listItemIndent > indent )
\r
417 list = new CKEDITOR.htmlParser.element( listType );
\r
418 list.add( listItem );
\r
419 lastListItem.add( list );
\r
421 else if ( listItemIndent < indent )
\r
423 // There might be a negative gap between two list levels. (#4944)
\r
424 var diff = indent - listItemIndent,
\r
426 while ( diff-- && ( parent = list.parent ) )
\r
427 list = parent.parent;
\r
429 list.add( listItem );
\r
432 list.add( listItem );
\r
434 children.splice( i--, 1 );
\r
437 lastListItem = listItem;
\r
438 indent = listItemIndent;
\r
444 listBaseIndent = 0;
\r
448 * A simple filter which always rejecting.
\r
450 falsyFilter : function( value )
\r
456 * A filter dedicated on the 'style' attribute filtering, e.g. dropping/replacing style properties.
\r
457 * @param styles {Array} in form of [ styleNameRegexp, styleValueRegexp,
\r
458 * newStyleValue/newStyleGenerator, newStyleName ] where only the first
\r
459 * parameter is mandatory.
\r
460 * @param whitelist {Boolean} Whether the {@param styles} will be considered as a white-list.
\r
462 stylesFilter : function( styles, whitelist )
\r
464 return function( styleText, element )
\r
467 // html-encoded quote might be introduced by 'font-family'
\r
468 // from MS-Word which confused the following regexp. e.g.
\r
469 //'font-family: "Lucida, Console"'
\r
471 .replace( /"/g, '"' )
\r
472 .replace( /\s*([^ :;]+)\s*:\s*([^;]+)\s*(?=;|$)/g,
\r
473 function( match, name, value )
\r
475 name = name.toLowerCase();
\r
476 name == 'font-family' && ( value = value.replace( /["']/g, '' ) );
\r
482 for ( var i = 0 ; i < styles.length; i++ )
\r
486 namePattern = styles[ i ][ 0 ];
\r
487 valuePattern = styles[ i ][ 1 ];
\r
488 newValue = styles[ i ][ 2 ];
\r
489 newName = styles[ i ][ 3 ];
\r
491 if ( name.match( namePattern )
\r
492 && ( !valuePattern || value.match( valuePattern ) ) )
\r
494 name = newName || name;
\r
495 whitelist && ( newValue = newValue || value );
\r
497 if ( typeof newValue == 'function' )
\r
498 newValue = newValue( value, element, name );
\r
500 // Return an couple indicate both name and value
\r
502 if ( newValue && newValue.push )
\r
503 name = newValue[ 0 ], newValue = newValue[ 1 ];
\r
505 if ( typeof newValue == 'string' )
\r
506 rules.push( [ name, newValue ] );
\r
512 !whitelist && rules.push( [ name, value ] );
\r
516 for ( var i = 0 ; i < rules.length ; i++ )
\r
517 rules[ i ] = rules[ i ].join( ':' );
\r
518 return rules.length ?
\r
519 ( rules.join( ';' ) + ';' ) : false;
\r
524 * Migrate the element by decorate styles on it.
\r
525 * @param styleDefiniton
\r
528 elementMigrateFilter : function ( styleDefiniton, variables )
\r
530 return function( element )
\r
534 new CKEDITOR.style( styleDefiniton, variables )._.definition
\r
536 element.name = styleDef.element;
\r
537 CKEDITOR.tools.extend( element.attributes, CKEDITOR.tools.clone( styleDef.attributes ) );
\r
538 element.addStyle( CKEDITOR.style.getStyleText( styleDef ) );
\r
543 * Migrate styles by creating a new nested stylish element.
\r
544 * @param styleDefinition
\r
546 styleMigrateFilter : function( styleDefinition, variableName )
\r
549 var elementMigrateFilter = this.elementMigrateFilter;
\r
550 return function( value, element )
\r
552 // Build an stylish element first.
\r
553 var styleElement = new CKEDITOR.htmlParser.element( null ),
\r
556 variables[ variableName ] = value;
\r
557 elementMigrateFilter( styleDefinition, variables )( styleElement );
\r
558 // Place the new element inside the existing span.
\r
559 styleElement.children = element.children;
\r
560 element.children = [ styleElement ];
\r
565 * A filter which remove cke-namespaced-attribute on
\r
566 * all none-cke-namespaced elements.
\r
570 bogusAttrFilter : function( value, element )
\r
572 if ( element.name.indexOf( 'cke:' ) == -1 )
\r
577 * A filter which will be used to apply inline css style according the stylesheet
\r
578 * definition rules, is generated lazily when filtering.
\r
580 applyStyleFilter : null
\r
584 getRules : function( editor )
\r
586 var dtd = CKEDITOR.dtd,
\r
587 blockLike = CKEDITOR.tools.extend( {}, dtd.$block, dtd.$listItem, dtd.$tableContent ),
\r
588 config = editor.config,
\r
589 filters = this.filters,
\r
590 falsyFilter = filters.falsyFilter,
\r
591 stylesFilter = filters.stylesFilter,
\r
592 elementMigrateFilter = filters.elementMigrateFilter,
\r
593 styleMigrateFilter = CKEDITOR.tools.bind( this.filters.styleMigrateFilter, this.filters ),
\r
594 createListBulletMarker = this.utils.createListBulletMarker,
\r
595 flattenList = filters.flattenList,
\r
596 assembleList = filters.assembleList,
\r
597 isListBulletIndicator = this.utils.isListBulletIndicator,
\r
598 containsNothingButSpaces = this.utils.isContainingOnlySpaces,
\r
599 resolveListItem = this.utils.resolveList,
\r
600 convertToPx = this.utils.convertToPx,
\r
601 getStyleComponents = this.utils.getStyleComponents,
\r
602 listDtdParents = this.utils.listDtdParents,
\r
603 removeFontStyles = config.pasteFromWordRemoveFontStyles !== false,
\r
604 removeStyles = config.pasteFromWordRemoveStyles !== false;
\r
610 // Remove script, meta and link elements.
\r
611 [ ( /meta|link|script/ ), '' ]
\r
614 root : function( element )
\r
616 element.filterChildren();
\r
617 assembleList( element );
\r
622 '^' : function( element )
\r
624 // Transform CSS style declaration to inline style.
\r
625 var applyStyleFilter;
\r
626 if ( CKEDITOR.env.gecko && ( applyStyleFilter = filters.applyStyleFilter ) )
\r
627 applyStyleFilter( element );
\r
630 $ : function( element )
\r
632 var tagName = element.name || '',
\r
633 attrs = element.attributes;
\r
635 // Convert length unit of width/height on blocks to
\r
636 // a more editor-friendly way (px).
\r
637 if ( tagName in blockLike
\r
640 attrs.style = stylesFilter(
\r
641 [ [ ( /^(:?width|height)$/ ), null, convertToPx ] ] )( attrs.style ) || '';
\r
644 // Processing headings.
\r
645 if ( tagName.match( /h\d/ ) )
\r
647 element.filterChildren();
\r
648 // Is the heading actually a list item?
\r
649 if ( resolveListItem( element ) )
\r
652 // Adapt heading styles to editor's convention.
\r
653 elementMigrateFilter( config[ 'format_' + tagName ] )( element );
\r
655 // Remove inline elements which contain only empty spaces.
\r
656 else if ( tagName in dtd.$inline )
\r
658 element.filterChildren();
\r
659 if ( containsNothingButSpaces( element ) )
\r
660 delete element.name;
\r
662 // Remove element with ms-office namespace,
\r
663 // with it's content preserved, e.g. 'o:p'.
\r
664 else if ( tagName.indexOf( ':' ) != -1
\r
665 && tagName.indexOf( 'cke' ) == -1 )
\r
667 element.filterChildren();
\r
669 // Restore image real link from vml.
\r
670 if ( tagName == 'v:imagedata' )
\r
672 var href = element.attributes[ 'o:href' ];
\r
674 element.attributes.src = href;
\r
675 element.name = 'img';
\r
678 delete element.name;
\r
681 // Assembling list items into a whole list.
\r
682 if ( tagName in listDtdParents )
\r
684 element.filterChildren();
\r
685 assembleList( element );
\r
689 // We'll drop any style sheet, but Firefox conclude
\r
690 // certain styles in a single style element, which are
\r
691 // required to be changed into inline ones.
\r
692 'style' : function( element )
\r
694 if ( CKEDITOR.env.gecko )
\r
696 // Grab only the style definition section.
\r
697 var styleDefSection = element.onlyChild().value.match( /\/\* Style Definitions \*\/([\s\S]*?)\/\*/ ),
\r
698 styleDefText = styleDefSection && styleDefSection[ 1 ],
\r
699 rules = {}; // Storing the parsed result.
\r
701 if ( styleDefText )
\r
704 // Remove line-breaks.
\r
705 .replace(/[\n\r]/g,'')
\r
706 // Extract selectors and style properties.
\r
707 .replace( /(.+?)\{(.+?)\}/g,
\r
708 function( rule, selectors, styleBlock )
\r
710 selectors = selectors.split( ',' );
\r
711 var length = selectors.length, selector;
\r
712 for ( var i = 0; i < length; i++ )
\r
714 // Assume MS-Word mostly generate only simple
\r
715 // selector( [Type selector][Class selector]).
\r
716 CKEDITOR.tools.trim( selectors[ i ] )
\r
717 .replace( /^(\w+)(\.[\w-]+)?$/g,
\r
718 function( match, tagName, className )
\r
720 tagName = tagName || '*';
\r
721 className = className.substring( 1, className.length );
\r
723 // Reject MS-Word Normal styles.
\r
724 if ( className.match( /MsoNormal/ ) )
\r
727 if ( !rules[ tagName ] )
\r
728 rules[ tagName ] = {};
\r
730 rules[ tagName ][ className ] = styleBlock;
\r
732 rules[ tagName ] = styleBlock;
\r
737 filters.applyStyleFilter = function( element )
\r
739 var name = rules[ '*' ] ? '*' : element.name,
\r
740 className = element.attributes && element.attributes[ 'class' ],
\r
742 if ( name in rules )
\r
744 style = rules[ name ];
\r
745 if ( typeof style == 'object' )
\r
746 style = style[ className ];
\r
747 // Maintain style rules priorities.
\r
748 style && element.addStyle( style, true );
\r
756 'p' : function( element )
\r
758 element.filterChildren();
\r
760 // Is the paragraph actually a list item?
\r
761 if ( resolveListItem( element ) )
\r
764 // Adapt paragraph formatting to editor's convention
\r
765 // according to enter-mode.
\r
766 if ( config.enterMode == CKEDITOR.ENTER_BR )
\r
768 // We suffer from attribute/style lost in this situation.
\r
769 delete element.name;
\r
770 element.add( new CKEDITOR.htmlParser.element( 'br' ) );
\r
773 elementMigrateFilter( config[ 'format_' + ( config.enterMode == CKEDITOR.ENTER_P ? 'p' : 'div' ) ] )( element );
\r
776 'div' : function( element )
\r
778 // Aligned table with no text surrounded is represented by a wrapper div, from which
\r
779 // table cells inherit as text-align styles, which is wrong.
\r
780 // Instead we use a clear-float div after the table to properly achieve the same layout.
\r
781 var singleChild = element.onlyChild();
\r
782 if ( singleChild && singleChild.name == 'table' )
\r
784 var attrs = element.attributes;
\r
785 singleChild.attributes = CKEDITOR.tools.extend( singleChild.attributes, attrs );
\r
786 attrs.style && singleChild.addStyle( attrs.style );
\r
788 var clearFloatDiv = new CKEDITOR.htmlParser.element( 'div' );
\r
789 clearFloatDiv.addStyle( 'clear' ,'both' );
\r
790 element.add( clearFloatDiv );
\r
791 delete element.name;
\r
795 'td' : function ( element )
\r
797 // 'td' in 'thead' is actually <th>.
\r
798 if ( element.getAncestor( 'thead') )
\r
799 element.name = 'th';
\r
802 // MS-Word sometimes present list as a mixing of normal list
\r
803 // and pseudo-list, normalize the previous ones into pseudo form.
\r
804 'ol' : flattenList,
\r
805 'ul' : flattenList,
\r
806 'dl' : flattenList,
\r
808 'font' : function( element )
\r
810 // IE/Safari: drop the font tag if it comes from list bullet text.
\r
811 if ( !CKEDITOR.env.gecko && isListBulletIndicator( element.parent ) )
\r
813 delete element.name;
\r
817 element.filterChildren();
\r
819 var attrs = element.attributes,
\r
820 styleText = attrs.style,
\r
821 parent = element.parent;
\r
823 if ( 'font' == parent.name ) // Merge nested <font> tags.
\r
825 CKEDITOR.tools.extend( parent.attributes,
\r
826 element.attributes );
\r
827 styleText && parent.addStyle( styleText );
\r
828 delete element.name;
\r
830 // Convert the merged into a span with all attributes preserved.
\r
833 styleText = styleText || '';
\r
834 // IE's having those deprecated attributes, normalize them.
\r
837 attrs.color != '#000000' && ( styleText += 'color:' + attrs.color + ';' );
\r
838 delete attrs.color;
\r
842 styleText += 'font-family:' + attrs.face + ';';
\r
845 // TODO: Mapping size in ranges of xx-small,
\r
846 // x-small, small, medium, large, x-large, xx-large.
\r
849 styleText += 'font-size:' +
\r
850 ( attrs.size > 3 ? 'large'
\r
851 : ( attrs.size < 3 ? 'small' : 'medium' ) ) + ';';
\r
855 element.name = 'span';
\r
856 element.addStyle( styleText );
\r
860 'span' : function( element )
\r
862 // IE/Safari: remove the span if it comes from list bullet text.
\r
863 if ( !CKEDITOR.env.gecko && isListBulletIndicator( element.parent ) )
\r
866 element.filterChildren();
\r
867 if ( containsNothingButSpaces( element ) )
\r
869 delete element.name;
\r
873 // For IE/Safari: List item bullet type is supposed to be indicated by
\r
874 // the text of a span with style 'mso-list : Ignore' or an image.
\r
875 if ( !CKEDITOR.env.gecko && isListBulletIndicator( element ) )
\r
877 var listSymbolNode = element.firstChild( function( node )
\r
879 return node.value || node.name == 'img';
\r
882 var listSymbol = listSymbolNode && ( listSymbolNode.value || 'l.' ),
\r
883 listType = listSymbol.match( /^([^\s]+?)([.)]?)$/ );
\r
884 return createListBulletMarker( listType, listSymbol );
\r
887 // Update the src attribute of image element with href.
\r
888 var children = element.children,
\r
889 attrs = element.attributes,
\r
890 styleText = attrs && attrs.style,
\r
891 firstChild = children && children[ 0 ];
\r
893 // Assume MS-Word mostly carry font related styles on <span>,
\r
894 // adapting them to editor's convention.
\r
897 attrs.style = stylesFilter(
\r
899 // Drop 'inline-height' style which make lines overlapping.
\r
901 [ ( /^font-family$/ ), null, !removeFontStyles ? styleMigrateFilter( config[ 'font_style' ], 'family' ) : null ] ,
\r
902 [ ( /^font-size$/ ), null, !removeFontStyles ? styleMigrateFilter( config[ 'fontSize_style' ], 'size' ) : null ] ,
\r
903 [ ( /^color$/ ), null, !removeFontStyles ? styleMigrateFilter( config[ 'colorButton_foreStyle' ], 'color' ) : null ] ,
\r
904 [ ( /^background-color$/ ), null, !removeFontStyles ? styleMigrateFilter( config[ 'colorButton_backStyle' ], 'color' ) : null ]
\r
905 ] )( styleText, element ) || '';
\r
911 // Migrate basic style formats to editor configured ones.
\r
912 'b' : elementMigrateFilter( config[ 'coreStyles_bold' ] ),
\r
913 'i' : elementMigrateFilter( config[ 'coreStyles_italic' ] ),
\r
914 'u' : elementMigrateFilter( config[ 'coreStyles_underline' ] ),
\r
915 's' : elementMigrateFilter( config[ 'coreStyles_strike' ] ),
\r
916 'sup' : elementMigrateFilter( config[ 'coreStyles_superscript' ] ),
\r
917 'sub' : elementMigrateFilter( config[ 'coreStyles_subscript' ] ),
\r
918 // Editor doesn't support anchor with content currently (#3582),
\r
919 // drop such anchors with content preserved.
\r
920 'a' : function( element )
\r
922 var attrs = element.attributes;
\r
923 if ( attrs && !attrs.href && attrs.name )
\r
924 delete element.name;
\r
926 'cke:listbullet' : function( element )
\r
928 if ( element.getAncestor( /h\d/ ) && !config.pasteFromWordNumberedHeadingToList )
\r
929 delete element.name;
\r
935 // Remove onmouseover and onmouseout events (from MS Word comments effect)
\r
936 [ ( /^onmouse(:?out|over)/ ), '' ],
\r
937 // Onload on image element.
\r
938 [ ( /^onload$/ ), '' ],
\r
939 // Remove office and vml attribute from elements.
\r
940 [ ( /(?:v|o):\w+/ ), '' ],
\r
941 // Remove lang/language attributes.
\r
942 [ ( /^lang/ ), '' ]
\r
947 'style' : stylesFilter(
\r
949 // Provide a white-list of styles that we preserve, those should
\r
950 // be the ones that could later be altered with editor tools.
\r
952 // Preserve margin-left/right which used as default indent style in the editor.
\r
953 [ ( /^margin$|^margin-(?!bottom|top)/ ), null, function( value, element, name )
\r
955 if ( element.name in { p : 1, div : 1 } )
\r
957 var indentStyleName = config.contentsLangDirection == 'ltr' ?
\r
958 'margin-left' : 'margin-right';
\r
960 // Extract component value from 'margin' shorthand.
\r
961 if ( name == 'margin' )
\r
963 value = getStyleComponents( name, value,
\r
964 [ indentStyleName ] )[ indentStyleName ];
\r
966 else if ( name != indentStyleName )
\r
969 if ( value && !emptyMarginRegex.test( value ) )
\r
970 return [ indentStyleName, value ];
\r
976 // Preserve clear float style.
\r
979 [ ( /^border.*|margin.*|vertical-align|float$/ ), null,
\r
980 function( value, element )
\r
982 if ( element.name == 'img' )
\r
986 [ (/^width|height$/ ), null,
\r
987 function( value, element )
\r
989 if ( element.name in { table : 1, td : 1, th : 1, img : 1 } )
\r
993 // Otherwise provide a black-list of styles that we remove.
\r
996 // Fixing color values.
\r
997 [ ( /-color$/ ), null, function( value )
\r
999 if ( value == 'transparent' )
\r
1001 if ( CKEDITOR.env.gecko )
\r
1002 return value.replace( /-moz-use-text-color/g, 'transparent' );
\r
1004 // Remove empty margin values, e.g. 0.00001pt 0em 0pt
\r
1005 [ ( /^margin$/ ), emptyMarginRegex ],
\r
1006 [ 'text-indent', '0cm' ],
\r
1007 [ 'page-break-before' ],
\r
1009 [ 'display', 'none' ],
\r
1010 removeFontStyles ? [ ( /font-?/ ) ] : null
\r
1011 ], removeStyles ),
\r
1013 // Prefer width styles over 'width' attributes.
\r
1014 'width' : function( value, element )
\r
1016 if ( element.name in dtd.$tableContent )
\r
1019 // Prefer border styles over table 'border' attributes.
\r
1020 'border' : function( value, element )
\r
1022 if ( element.name in dtd.$tableContent )
\r
1026 // Only Firefox carry style sheet from MS-Word, which
\r
1027 // will be applied by us manually. For other browsers
\r
1028 // the css className is useless.
\r
1029 'class' : falsyFilter,
\r
1031 // MS-Word always generate 'background-color' along with 'bgcolor',
\r
1032 // simply drop the deprecated attributes.
\r
1033 'bgcolor' : falsyFilter,
\r
1035 // Deprecate 'valign' attribute in favor of 'vertical-align'.
\r
1036 'valign' : removeStyles ? falsyFilter : function( value, element )
\r
1038 element.addStyle( 'vertical-align', value );
\r
1043 // Fore none-IE, some useful data might be buried under these IE-conditional
\r
1044 // comments where RegExp were the right approach to dig them out where usual approach
\r
1045 // is transform it into a fake element node which hold the desired data.
\r
1047 !CKEDITOR.env.ie ?
\r
1048 function( value, node )
\r
1050 var imageInfo = value.match( /<img.*?>/ ),
\r
1051 listInfo = value.match( /^\[if !supportLists\]([\s\S]*?)\[endif\]$/ );
\r
1053 // Seek for list bullet indicator.
\r
1056 // Bullet symbol could be either text or an image.
\r
1057 var listSymbol = listInfo[ 1 ] || ( imageInfo && 'l.' ),
\r
1058 listType = listSymbol && listSymbol.match( />([^\s]+?)([.)]?)</ );
\r
1059 return createListBulletMarker( listType, listSymbol );
\r
1062 // Reveal the <img> element in conditional comments for Firefox.
\r
1063 if ( CKEDITOR.env.gecko && imageInfo )
\r
1065 var img = CKEDITOR.htmlParser.fragment.fromHtml( imageInfo[ 0 ] ).children[ 0 ],
\r
1066 previousComment = node.previous,
\r
1067 // Try to dig the real image link from vml markup from previous comment text.
\r
1068 imgSrcInfo = previousComment && previousComment.value.match( /<v:imagedata[^>]*o:href=['"](.*?)['"]/ ),
\r
1069 imgSrc = imgSrcInfo && imgSrcInfo[ 1 ];
\r
1071 // Is there a real 'src' url to be used?
\r
1072 imgSrc && ( img.attributes.src = imgSrc );
\r
1083 // The paste processor here is just a reduced copy of html data processor.
\r
1084 var pasteProcessor = function()
\r
1086 this.dataFilter = new CKEDITOR.htmlParser.filter();
\r
1089 pasteProcessor.prototype =
\r
1091 toHtml : function( data )
\r
1093 var fragment = CKEDITOR.htmlParser.fragment.fromHtml( data, false ),
\r
1094 writer = new CKEDITOR.htmlParser.basicWriter();
\r
1096 fragment.writeHtml( writer, this.dataFilter );
\r
1097 return writer.getHtml( true );
\r
1101 CKEDITOR.cleanWord = function( data, editor )
\r
1103 // Firefox will be confused by those downlevel-revealed IE conditional
\r
1104 // comments, fixing them first( convert it to upperlevel-revealed one ).
\r
1105 // e.g. <![if !vml]>...<![endif]>
\r
1106 if ( CKEDITOR.env.gecko )
\r
1107 data = data.replace( /(<!--\[if[^<]*?\])-->([\S\s]*?)<!--(\[endif\]-->)/gi, '$1$2$3' );
\r
1109 var dataProcessor = new pasteProcessor(),
\r
1110 dataFilter = dataProcessor.dataFilter;
\r
1112 // These rules will have higher priorities than default ones.
\r
1113 dataFilter.addRules( CKEDITOR.plugins.pastefromword.getRules( editor ) );
\r
1115 // Allow extending data filter rules.
\r
1116 editor.fire( 'beforeCleanWord', { filter : dataFilter } );
\r
1120 data = dataProcessor.toHtml( data, false );
\r
1124 alert( editor.lang.pastefromword.error );
\r
1127 /* Below post processing those things that are unable to delivered by filter rules. */
\r
1129 // Remove 'cke' namespaced attribute used in filter rules as marker.
\r
1130 data = data.replace( /cke:.*?".*?"/g, '' );
\r
1132 // Remove empty style attribute.
\r
1133 data = data.replace( /style=""/g, '' );
\r
1135 // Remove the dummy spans ( having no inline style ).
\r
1136 data = data.replace( /<span>/g, '' );
\r
1143 * Whether to ignore all font related formatting styles, including:
\r
1144 * <ul> <li>font size;</li>
\r
1145 * <li>font family;</li>
\r
1146 * <li>font foreground/background color.</li></ul>
\r
1147 * @name CKEDITOR.config.pasteFromWordRemoveFontStyles
\r
1152 * config.pasteFromWordRemoveFontStyles = false;
\r
1156 * Whether to transform MS Word outline numbered headings into lists.
\r
1157 * @name CKEDITOR.config.pasteFromWordNumberedHeadingToList
\r
1162 * config.pasteFromWordNumberedHeadingToList = true;
\r
1166 * Whether to remove element styles that can't be managed with the editor. Note
\r
1167 * that this doesn't handle the font specific styles, which depends on the
\r
1168 * {@link CKEDITOR.config.pasteFromWordRemoveFontStyles} setting instead.
\r
1169 * @name CKEDITOR.config.pasteFromWordRemoveStyles
\r
1174 * config.pasteFromWordRemoveStyles = false;
\r