      1 /*
      2 Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.
      3 For licensing, see LICENSE.html or http://ckeditor.com/license
      4 */
      5
      6 CKEDITOR.dialog.add( 'pastefromword', function( editor )
      7 {
      8 	return {
      9 		title : editor.lang.pastefromword.title,
     10 		minWidth : CKEDITOR.env.ie && CKEDITOR.env.quirks ? 370 : 350,
     11 		minHeight : CKEDITOR.env.ie && CKEDITOR.env.quirks ? 270 : 260,
     12 		htmlToLoad : '<!doctype html><script type="text/javascript">'
     13 				+ 'window.onload = function()'
     14 				+ '{'
     15 					+ 'if ( ' + CKEDITOR.env.ie + ' ) '
     16 						+ 'document.body.contentEditable = "true";'
     17 					+ 'else '
     18 						+ 'document.designMode = "on";'
     19 					+ 'var iframe = new window.parent.CKEDITOR.dom.element( frameElement );'
     20 					+ 'var dialog = iframe.getCustomData( "dialog" );'
     21 					+ 'dialog.fire( "iframeAdded", { iframe : iframe } );'
     22 				+ '};'
     23 				+ '</script><style>body { margin: 3px; height: 95%; } </style><body></body>',
     24 		cleanWord : function( editor, html, ignoreFont, removeStyles )
     25 		{
     26 			html = html.replace(/<o:p>\s*<\/o:p>/g, '') ;
     27 			html = html.replace(/<o:p>[\s\S]*?<\/o:p>/g, ' ') ;
     28
     29 			// Remove mso-xxx styles.
     30 			html = html.replace( /\s*mso-[^:]+:[^;"]+;?/gi, '' ) ;
     31
     32 			// Remove margin styles.
     33 			html = html.replace( /\s*MARGIN: 0cm 0cm 0pt\s*;/gi, '' ) ;
     34 			html = html.replace( /\s*MARGIN: 0cm 0cm 0pt\s*"/gi, "\"" ) ;
     35
     36 			html = html.replace( /\s*TEXT-INDENT: 0cm\s*;/gi, '' ) ;
     37 			html = html.replace( /\s*TEXT-INDENT: 0cm\s*"/gi, "\"" ) ;
     38
     39 			html = html.replace( /\s*TEXT-ALIGN: [^\s;]+;?"/gi, "\"" ) ;
     40
     41 			html = html.replace( /\s*PAGE-BREAK-BEFORE: [^\s;]+;?"/gi, "\"" ) ;
     42
     43 			html = html.replace( /\s*FONT-VARIANT: [^\s;]+;?"/gi, "\"" ) ;
     44
     45 			html = html.replace( /\s*tab-stops:[^;"]*;?/gi, '' ) ;
     46 			html = html.replace( /\s*tab-stops:[^"]*/gi, '' ) ;
     47
     48 			// Remove FONT face attributes.
     49 			if ( ignoreFont )
     50 			{
     51 				html = html.replace( /\s*face="[^"]*"/gi, '' ) ;
     52 				html = html.replace( /\s*face=[^ >]*/gi, '' ) ;
     53
     54 				html = html.replace( /\s*FONT-FAMILY:[^;"]*;?/gi, '' ) ;
     55 			}
     56
     57 			// Remove Class attributes
     58 			html = html.replace(/<(\w[^>]*) class=([^ |>]*)([^>]*)/gi, "<$1$3") ;
     59
     60 			// Remove styles.
     61 			if ( removeStyles )
     62 				html = html.replace( /<(\w[^>]*) style="([^\"]*)"([^>]*)/gi, "<$1$3" ) ;
     63
     64 			// Remove style, meta and link tags
     65 			html = html.replace( /<STYLE[^>]*>[\s\S]*?<\/STYLE[^>]*>/gi, '' ) ;
     66 			html = html.replace( /<(?:META|LINK)[^>]*>\s*/gi, '' ) ;
     67
     68 			// Remove empty styles.
     69 			html =  html.replace( /\s*style="\s*"/gi, '' ) ;
     70
     71 			html = html.replace( /<SPAN\s*[^>]*>\s* \s*<\/SPAN>/gi, ' ' ) ;
     72
     73 			html = html.replace( /<SPAN\s*[^>]*><\/SPAN>/gi, '' ) ;
     74
     75 			// Remove Lang attributes
     76 			html = html.replace(/<(\w[^>]*) lang=([^ |>]*)([^>]*)/gi, "<$1$3") ;
     77
     78 			html = html.replace( /<SPAN\s*>([\s\S]*?)<\/SPAN>/gi, '$1' ) ;
     79
     80 			html = html.replace( /<FONT\s*>([\s\S]*?)<\/FONT>/gi, '$1' ) ;
     81
     82 			// Remove XML elements and declarations
     83 			html = html.replace(/<\\?\?xml[^>]*>/gi, '' ) ;
     84
     85 			// Remove w: tags with contents.
     86 			html = html.replace( /<w:[^>]*>[\s\S]*?<\/w:[^>]*>/gi, '' ) ;
     87
     88 			// Remove Tags with XML namespace declarations: <o:p><\/o:p>
     89 			html = html.replace(/<\/?\w+:[^>]*>/gi, '' ) ;
     90
     91 			// Remove comments [SF BUG-1481861].
     92 			html = html.replace(/<\!--[\s\S]*?-->/g, '' ) ;
     93
     94 			html = html.replace( /<(U|I|STRIKE)> <\/\1>/g, ' ' ) ;
     95
     96 			html = html.replace( /<H\d>\s*<\/H\d>/gi, '' ) ;
     97
     98 			// Remove "display:none" tags.
     99 			html = html.replace( /<(\w+)[^>]*\sstyle="[^"]*DISPLAY\s?:\s?none[\s\S]*?<\/\1>/ig, '' ) ;
    100
    101 			// Remove language tags
    102 			html = html.replace( /<(\w[^>]*) language=([^ |>]*)([^>]*)/gi, "<$1$3") ;
    103
    104 			// Remove onmouseover and onmouseout events (from MS Word comments effect)
    105 			html = html.replace( /<(\w[^>]*) onmouseover="([^\"]*)"([^>]*)/gi, "<$1$3") ;
    106 			html = html.replace( /<(\w[^>]*) onmouseout="([^\"]*)"([^>]*)/gi, "<$1$3") ;
    107
    108 			if ( editor.config.pasteFromWordKeepsStructure )
    109 			{
    110 				// The original <Hn> tag send from Word is something like this: <Hn style="margin-top:0px;margin-bottom:0px">
    111 				html = html.replace( /<H(\d)([^>]*)>/gi, '<h$1>' ) ;
    112
    113 				// Word likes to insert extra <font> tags, when using MSIE. (Wierd).
    114 				html = html.replace( /<(H\d)><FONT[^>]*>([\s\S]*?)<\/FONT><\/\1>/gi, '<$1>$2<\/$1>' );
    115 				html = html.replace( /<(H\d)><EM>([\s\S]*?)<\/EM><\/\1>/gi, '<$1>$2<\/$1>' );
    116 			}
    117 			else
    118 			{
    119 				html = html.replace( /<H1([^>]*)>/gi, '<div$1><b><font size="6">' ) ;
    120 				html = html.replace( /<H2([^>]*)>/gi, '<div$1><b><font size="5">' ) ;
    121 				html = html.replace( /<H3([^>]*)>/gi, '<div$1><b><font size="4">' ) ;
    122 				html = html.replace( /<H4([^>]*)>/gi, '<div$1><b><font size="3">' ) ;
    123 				html = html.replace( /<H5([^>]*)>/gi, '<div$1><b><font size="2">' ) ;
    124 				html = html.replace( /<H6([^>]*)>/gi, '<div$1><b><font size="1">' ) ;
    125
    126 				html = html.replace( /<\/H\d>/gi, '<\/font><\/b><\/div>' ) ;
    127
    128 				// Transform <P> to <DIV>
    129 				var re = new RegExp( '(<P)([^>]*>[\\s\\S]*?)(<\/P>)', 'gi' ) ;	// Different because of a IE 5.0 error
    130 				html = html.replace( re, '<div$2<\/div>' ) ;
    131
    132 				// Remove empty tags (three times, just to be sure).
    133 				// This also removes any empty anchor
    134 				html = html.replace( /<([^\s>]+)(\s[^>]*)?>\s*<\/\1>/g, '' ) ;
    135 				html = html.replace( /<([^\s>]+)(\s[^>]*)?>\s*<\/\1>/g, '' ) ;
    136 				html = html.replace( /<([^\s>]+)(\s[^>]*)?>\s*<\/\1>/g, '' ) ;
    137 			}
    138
    139 			return html ;
    140 		},
    141 		onShow : function()
    142 		{
    143 			// To avoid JAWS putting virtual cursor back to the editor document,
    144 			// disable main document 'contentEditable' during dialog opening.
    145 			if ( CKEDITOR.env.ie )
    146 				this.getParentEditor().document.getBody().$.contentEditable = 'false';
    147
    148 			// FIREFOX BUG: Force the browser to render the dialog to make the to-be-
    149 			// inserted iframe editable. (#3366)
    150 			this.parts.dialog.$.offsetHeight;
    151
    152 			var container = this.getContentElement( 'general', 'editing_area' ).getElement(),
    153 				iframe = CKEDITOR.dom.element.createFromHtml( '<iframe src="javascript:void(0)" frameborder="0" allowtransparency="1"></iframe>' );
    154
    155 			var lang = this.getParentEditor().lang;
    156
    157 			iframe.setStyles(
    158 				{
    159 					width : '346px',
    160 					height : '152px',
    161 					'background-color' : 'white',
    162 					border : '1px solid black'
    163 				} );
    164 			iframe.setCustomData( 'dialog', this );
    165
    166 			var accTitle = lang.editorTitle.replace( '%1', lang.pastefromword.title );
    167
    168 			if ( CKEDITOR.env.ie )
    169 				container.setHtml( '<legend style="position:absolute;top:-1000000px;left:-1000000px;">'
    170 						+ CKEDITOR.tools.htmlEncode( accTitle )
    171 						+ '</legend>' );
    172 			else
    173 			{
    174 				container.setHtml( '' );
    175 				container.setAttributes(
    176 					{
    177 						role : 'region',
    178 						title : accTitle
    179 					} );
    180 				iframe.setAttributes(
    181 					{
    182 						role : 'region',
    183 						title : ' '
    184 					} );
    185 			}
    186 			container.append( iframe );
    187 			if ( CKEDITOR.env.ie )
    188 				container.setStyle( 'height', ( iframe.$.offsetHeight + 2 ) + 'px' );
    189
    190 			var isCustomDomain = CKEDITOR.env.ie && document.domain != window.location.hostname;
    191 			if ( isCustomDomain )
    192 			{
    193 				CKEDITOR._cke_htmlToLoad = this.definition.htmlToLoad;
    194 				iframe.setAttribute( 'src',
    195 					'javascript:void( (function(){' +
    196 						   'document.open();' +
    197 						   'document.domain="' + document.domain + '";' +
    198 						   'document.write( window.parent.CKEDITOR._cke_htmlToLoad );' +
    199 						   'delete window.parent.CKEDITOR._cke_htmlToLoad;' +
    200 						   'document.close();' +
    201 					'})() )' );
    202 			}
    203 			else
    204 			{
    205 				var doc = iframe.$.contentWindow.document;
    206 				doc.open();
    207 				doc.write( this.definition.htmlToLoad );
    208 				doc.close();
    209 			}
    210 		},
    211 		onOk : function()
    212 		{
    213 			var container = this.getContentElement( 'general', 'editing_area' ).getElement(),
    214 				iframe = container.getElementsByTag( 'iframe' ).getItem( 0 ),
    215 				editor = this.getParentEditor(),
    216 				html = this.definition.cleanWord( editor, iframe.$.contentWindow.document.body.innerHTML,
    217 						this.getValueOf( 'general', 'ignoreFontFace' ),
    218 						this.getValueOf( 'general', 'removeStyle' ) );
    219
    220 				// Insertion should happen after main document design mode turned on.
    221 				setTimeout( function(){
    222 					editor.insertHtml( html );
    223 				}, 0 );
    224 		},
    225 		onHide : function()
    226 		{
    227 			if ( CKEDITOR.env.ie )
    228 				this.getParentEditor().document.getBody().$.contentEditable = 'true';
    229 		},
    230 		contents :
    231 		[
    232 			{
    233 				id : 'general',
    234 				label : editor.lang.pastefromword.title,
    235 				elements :
    236 				[
    237 					{
    238 						type : 'html',
    239 						style : 'white-space: normal;',
    240 						onShow : function()
    241 						{
    242 							/*
    243 							 * SAFARI BUG: The advice label would overflow if the table layout
    244 							 * isn't fixed.
    245 							 */
    246 							if ( CKEDITOR.env.webkit )
    247 								this.getElement().getAscendant( 'table' ).setStyle( 'table-layout', 'fixed' );
    248 						},
    249 						html : editor.lang.pastefromword.advice
    250 					},
    251 					{
    252 						type : 'html',
    253 						id : 'editing_area',
    254 						style : 'width: 100%; height: 100%;',
    255 						html : '<fieldset></fieldset>',
    256 						focus : function()
    257 						{
    258 							var div = this.getElement();
    259 							var iframe = div.getElementsByTag( 'iframe' );
    260 							if ( iframe.count() < 1 )
    261 								return;
    262 							iframe = iframe.getItem( 0 );
    263
    264 							// #3291 : JAWS needs the 500ms delay to detect that the editor iframe
    265 							// iframe is no longer editable. So that it will put the focus into the
    266 							// Paste from Word dialog's editable area instead.
    267 							setTimeout( function()
    268 							{
    269 								iframe.$.contentWindow.focus();
    270 							}, 500 );
    271 						}
    272 					},
    273 					{
    274 						type : 'vbox',
    275 						padding : 0,
    276 						children :
    277 						[
    278 							{
    279 								type : 'checkbox',
    280 								id : 'ignoreFontFace',
    281 								label : editor.lang.pastefromword.ignoreFontFace,
    282 								'default' : true
    283 							},
    284 							{
    285 								type : 'checkbox',
    286 								id : 'removeStyle',
    287 								label : editor.lang.pastefromword.removeStyle
    288 							}
    289 						]
    290 					}
    291 				]
    292 			}
    293 		]
    294 	};
    295 } );
    296