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