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