2 * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
3 * For licensing, see LICENSE.md or http://ckeditor.com/license
7 CKEDITOR
.plugins
.add( 'pastefromword', {
9 // jscs:disable maximumLineLength
10 lang: 'af,ar,bg,bn,bs,ca,cs,cy,da,de,de-ch,el,en,en-au,en-ca,en-gb,eo,es,et,eu,fa,fi,fo,fr,fr-ca,gl,gu,he,hi,hr,hu,id,is,it,ja,ka,km,ko,ku,lt,lv,mk,mn,ms,nb,nl,no,pl,pt,pt-br,ro,ru,si,sk,sl,sq,sr,sr-latn,sv,th,tr,tt,ug,uk,vi,zh,zh-cn', // %REMOVE_LINE_CORE%
11 // jscs:enable maximumLineLength
12 icons: 'pastefromword,pastefromword-rtl', // %REMOVE_LINE_CORE%
13 hidpi: true, // %REMOVE_LINE_CORE%
14 init: function( editor
) {
15 var commandName
= 'pastefromword',
16 // Flag indicate this command is actually been asked instead of a generic pasting.
20 editor
.addCommand( commandName
, {
21 // Snapshots are done manually by editable.insertXXX methods.
25 exec: function( editor
) {
29 // Force html mode for incomming paste events sequence.
30 editor
.once( 'beforePaste', forceHtmlMode
);
32 editor
.getClipboardData( { title: editor
.lang
.pastefromword
.title
}, function( data
) {
33 // Do not use editor#paste, because it would start from beforePaste event.
34 data
&& editor
.fire( 'paste', {
36 dataValue: data
.dataValue
,
38 dataTransfer: CKEDITOR
.plugins
.clipboard
.initPasteDataTransfer()
41 editor
.fire( 'afterCommandExec', {
50 // Register the toolbar button.
51 editor
.ui
.addButton
&& editor
.ui
.addButton( 'PasteFromWord', {
52 label: editor
.lang
.pastefromword
.toolbar
,
54 toolbar: 'clipboard,50'
57 editor
.on( 'pasteState', function( evt
) {
58 editor
.getCommand( commandName
).setState( evt
.data
);
61 // Features bring by this command beside the normal process:
62 // 1. No more bothering of user about the clean-up.
63 // 2. Perform the clean-up even if content is not from MS-Word.
64 // (e.g. from a MS-Word similar application.)
65 // 3. Listen with high priority (3), so clean up is done before content
66 // type sniffing (priority = 6).
67 editor
.on( 'paste', function( evt
) {
69 mswordHtml
= data
.dataValue
;
71 // MS-WORD format sniffing.
72 if ( mswordHtml
&& ( forceFromWord
|| ( /(class=\"?Mso|style=\"[^\"]*\bmso\-|w:WordDocument)/ ).test( mswordHtml
) ) ) {
73 // Do not apply paste filter to data filtered by the Word filter (#13093).
74 data
.dontFilter
= true;
76 // If filter rules aren't loaded then cancel 'paste' event,
77 // load them and when they'll get loaded fire new paste event
78 // for which data will be filtered in second execution of
80 var isLazyLoad
= loadFilterRules( editor
, path
, function() {
81 // Event continuation with the original data.
83 editor
.fire( 'paste', data
);
84 else if ( !editor
.config
.pasteFromWordPromptCleanup
|| ( forceFromWord
|| confirm( editor
.lang
.pastefromword
.confirmCleanup
) ) ) // jshint ignore:line
85 data
.dataValue
= CKEDITOR
.cleanWord( mswordHtml
, editor
);
87 // Reset forceFromWord.
91 // The cleanup rules are to be loaded, we should just cancel
93 isLazyLoad
&& evt
.cancel();
100 function loadFilterRules( editor
, path
, callback
) {
101 var isLoaded
= CKEDITOR
.cleanWord
;
106 var filterFilePath
= CKEDITOR
.getUrl( editor
.config
.pasteFromWordCleanupFile
|| ( path
+ 'filter/default.js' ) );
108 // Load with busy indicator.
109 CKEDITOR
.scriptLoader
.load( filterFilePath
, callback
, null, true );
115 function forceHtmlMode( evt
) {
116 evt
.data
.type
= 'html';
122 * Whether to prompt the user about the clean up of content being pasted from MS Word.
124 * config.pasteFromWordPromptCleanup = true;
127 * @cfg {Boolean} [pasteFromWordPromptCleanup=false]
128 * @member CKEDITOR.config
132 * The file that provides the MS Word cleanup function for pasting operations.
134 * **Note:** This is a global configuration shared by all editor instances present
137 * // Load from 'pastefromword' plugin 'filter' sub folder (custom.js file) using path relative to CKEditor installation folder.
138 * CKEDITOR.config.pasteFromWordCleanupFile = 'plugins/pastefromword/filter/custom.js';
140 * // Load from 'pastefromword' plugin 'filter' sub folder (custom.js file) using full path (including CKEditor installation folder).
141 * CKEDITOR.config.pasteFromWordCleanupFile = '/ckeditor/plugins/pastefromword/filter/custom.js';
143 * // Load custom.js file from 'customFilerts' folder (located in server's root) using full URL.
144 * CKEDITOR.config.pasteFromWordCleanupFile = 'http://my.example.com/customFilerts/custom.js';
147 * @cfg {String} [pasteFromWordCleanupFile=<plugin path> + 'filter/default.js']
148 * @member CKEDITOR.config