JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.6.3
[ckeditor.git] / _source / plugins / pastefromword / plugin.js
1 /*\r
2 Copyright (c) 2003-2012, CKSource - Frederico Knabben. All rights reserved.\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license\r
4 */\r
5 (function()\r
6 {\r
7         function forceHtmlMode( evt ) { evt.data.mode = 'html'; }\r
8 \r
9         CKEDITOR.plugins.add( 'pastefromword',\r
10         {\r
11                 init : function( editor )\r
12                 {\r
13 \r
14                         // Flag indicate this command is actually been asked instead of a generic\r
15                         // pasting.\r
16                         var forceFromWord = 0;\r
17                         var resetFromWord = function( evt )\r
18                                 {\r
19                                         evt && evt.removeListener();\r
20                                         editor.removeListener( 'beforePaste', forceHtmlMode );\r
21                                         forceFromWord && setTimeout( function() { forceFromWord = 0; }, 0 );\r
22                                 };\r
23 \r
24                         // Features bring by this command beside the normal process:\r
25                         // 1. No more bothering of user about the clean-up.\r
26                         // 2. Perform the clean-up even if content is not from MS-Word.\r
27                         // (e.g. from a MS-Word similar application.)\r
28                         editor.addCommand( 'pastefromword',\r
29                         {\r
30                                 canUndo : false,\r
31                                 exec : function()\r
32                                 {\r
33                                         // Ensure the received data format is HTML and apply content filtering. (#6718)\r
34                                         forceFromWord = 1;\r
35                                         editor.on( 'beforePaste', forceHtmlMode );\r
36 \r
37                                         if ( editor.execCommand( 'paste', 'html' ) === false )\r
38                                         {\r
39                                                 editor.on( 'dialogShow', function ( evt )\r
40                                                 {\r
41                                                         evt.removeListener();\r
42                                                         evt.data.on( 'cancel', resetFromWord );\r
43                                                 });\r
44 \r
45                                                 editor.on( 'dialogHide', function( evt )\r
46                                                 {\r
47                                                         evt.data.removeListener( 'cancel', resetFromWord );\r
48                                                 } );\r
49                                         }\r
50 \r
51                                         editor.on( 'afterPaste', resetFromWord );\r
52                                 }\r
53                         });\r
54 \r
55                         // Register the toolbar button.\r
56                         editor.ui.addButton( 'PasteFromWord',\r
57                                 {\r
58                                         label : editor.lang.pastefromword.toolbar,\r
59                                         command : 'pastefromword'\r
60                                 });\r
61 \r
62                         editor.on( 'pasteState', function( evt )\r
63                                 {\r
64                                         editor.getCommand( 'pastefromword' ).setState( evt.data );\r
65                                 });\r
66 \r
67                         editor.on( 'paste', function( evt )\r
68                         {\r
69                                 var data = evt.data,\r
70                                         mswordHtml;\r
71 \r
72                                 // MS-WORD format sniffing.\r
73                                 if ( ( mswordHtml = data[ 'html' ] )\r
74                                          && ( forceFromWord || ( /(class=\"?Mso|style=\"[^\"]*\bmso\-|w:WordDocument)/ ).test( mswordHtml ) ) )\r
75                                 {\r
76                                         var isLazyLoad = this.loadFilterRules( function()\r
77                                                 {\r
78                                                         // Event continuation with the original data.\r
79                                                         if ( isLazyLoad )\r
80                                                                 editor.fire( 'paste', data );\r
81                                                         else if ( !editor.config.pasteFromWordPromptCleanup\r
82                                                           || ( forceFromWord || confirm( editor.lang.pastefromword.confirmCleanup ) ) )\r
83                                                          {\r
84                                                                 data[ 'html' ] = CKEDITOR.cleanWord( mswordHtml, editor );\r
85                                                         }\r
86                                                 });\r
87 \r
88                                         // The cleanup rules are to be loaded, we should just cancel\r
89                                         // this event.\r
90                                         isLazyLoad && evt.cancel();\r
91                                 }\r
92                         }, this );\r
93                 },\r
94 \r
95                 loadFilterRules : function( callback )\r
96                 {\r
97 \r
98                         var isLoaded = CKEDITOR.cleanWord;\r
99 \r
100                         if ( isLoaded )\r
101                                 callback();\r
102                         else\r
103                         {\r
104                                 var filterFilePath = CKEDITOR.getUrl(\r
105                                                 CKEDITOR.config.pasteFromWordCleanupFile\r
106                                                 || ( this.path + 'filter/default.js' ) );\r
107 \r
108                                 // Load with busy indicator.\r
109                                 CKEDITOR.scriptLoader.load( filterFilePath, callback, null, true );\r
110                         }\r
111 \r
112                         return !isLoaded;\r
113                 },\r
114 \r
115                 requires : [ 'clipboard' ]\r
116         });\r
117 })();\r
118 \r
119 /**\r
120  * Whether to prompt the user about the clean up of content being pasted from\r
121  * MS Word.\r
122  * @name CKEDITOR.config.pasteFromWordPromptCleanup\r
123  * @since 3.1\r
124  * @type Boolean\r
125  * @default undefined\r
126  * @example\r
127  * config.pasteFromWordPromptCleanup = true;\r
128  */\r
129 \r
130 /**\r
131  * The file that provides the MS Word cleanup function for pasting operations.\r
132  * Note: This is a global configuration shared by all editor instances present\r
133  * in the page.\r
134  * @name CKEDITOR.config.pasteFromWordCleanupFile\r
135  * @since 3.1\r
136  * @type String\r
137  * @default 'default'\r
138  * @example\r
139  * // Load from 'pastefromword' plugin 'filter' sub folder (custom.js file).\r
140  * CKEDITOR.config.pasteFromWordCleanupFile = 'custom';\r
141  */\r