/
plugin.js
465 lines (400 loc) · 14.2 KB
/
plugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/**
* @license Copyright (c) 2003-2014, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or http://ckeditor.com/license
*/
/**
* @fileOverview Rich code snippets for CKEditor.
*/
'use strict';
( function() {
var isBrowserSupported = !CKEDITOR.env.ie || CKEDITOR.env.version > 8;
CKEDITOR.plugins.add( 'codesnippet', {
requires: 'widget,dialog',
lang: 'bg,ca,cs,da,de,el,en,en-gb,eo,es,et,fa,fr,fr-ca,hr,hu,it,ja,ku,lt,lv,nb,nl,no,pl,pt,ro,ru,sk,sl,sq,sv,th,tt,ug,uk,vi', // %REMOVE_LINE_CORE%
icons: 'codesnippet', // %REMOVE_LINE_CORE%
hidpi: true, // %REMOVE_LINE_CORE%
beforeInit: function( editor ) {
editor._.codesnippet = {};
/**
* Sets the custom syntax highlighter. See {@link CKEDITOR.plugins.codesnippet.highlighter}
* to learn how to register a custom highlighter.
*
* @since 4.4
* @member CKEDITOR.plugins.codesnippet
* @param {CKEDITOR.plugins.codesnippet.highlighter} highlighter
*/
this.setHighlighter = function( highlighter ) {
editor._.codesnippet.highlighter = highlighter;
var langs = editor._.codesnippet.langs =
editor.config.codeSnippet_languages || highlighter.languages;
// We might escape special regex chars below, but we expect that there
// should be no crazy values used as lang keys.
editor._.codesnippet.langsRegex = new RegExp( '(?:^|\\s)language-(' +
CKEDITOR.tools.objectKeys( langs ).join( '|' ) + ')(?:\\s|$)' );
};
},
onLoad: function( editor ) {
CKEDITOR.dialog.add( 'codeSnippet', this.path + 'dialogs/codesnippet.js' );
},
init: function( editor ) {
editor.ui.addButton && editor.ui.addButton( 'CodeSnippet', {
label: editor.lang.codesnippet.button,
command: 'codeSnippet',
toolbar: 'insert,10'
} );
},
afterInit: function( editor ) {
var path = this.path;
registerWidget( editor );
// At the very end, if no custom highlighter was set so far (by plugin#setHighlighter)
// we will set default one.
if ( !editor._.codesnippet.highlighter ) {
var hljsHighlighter = new CKEDITOR.plugins.codesnippet.highlighter( {
languages: {
apache: 'Apache',
bash: 'Bash',
coffeescript: 'CoffeeScript',
cpp: 'C++',
cs: 'C#',
css: 'CSS',
diff: 'Diff',
html: 'HTML',
http: 'HTTP',
ini: 'INI',
java: 'Java',
javascript: 'JavaScript',
json: 'JSON',
makefile: 'Makefile',
markdown: 'Markdown',
nginx: 'Nginx',
objectivec: 'Objective-C',
perl: 'Perl',
php: 'PHP',
python: 'Python',
ruby: 'Ruby',
sql: 'SQL',
vbscript: 'VBScript',
xhtml: 'XHTML',
xml: 'XML'
},
init: function( callback ) {
var that = this;
if ( isBrowserSupported ) {
CKEDITOR.scriptLoader.load( path + 'lib/highlight/highlight.pack.js', function() {
that.hljs = window.hljs;
callback();
} );
}
// Note: This will work for framed editor only.
editor.addContentsCss( path + 'lib/highlight/styles/' + editor.config.codeSnippet_theme + '.css' );
},
highlighter: function( code, language, callback ) {
var highlighted = this.hljs.highlightAuto( code,
this.hljs.getLanguage( language ) ? [ language ] : undefined );
if ( highlighted )
callback( highlighted.value );
}
} );
this.setHighlighter( hljsHighlighter );
}
}
} );
/**
* Global helpers and classes of the Code Snippet plugin.
*
* For more information see the [Code Snippet Guide](#!/guide/dev_codesnippet).
*
* @class
* @singleton
*/
CKEDITOR.plugins.codesnippet = {
highlighter: Highlighter
};
/**
* A Code Snippet highlighter. It can be set as a default highlighter
* using {@link CKEDITOR.plugins.codesnippet#setHighlighter}, for example:
*
* // Create a new plugin which registers a custom code highlighter
* // based on customEngine in order to replace the one that comes
* // with the Code Snippet plugin.
* CKEDITOR.plugins.add( 'myCustomHighlighter', {
* afterInit: function( editor ) {
* // Create a new instance of the highlighter.
* var myHighlighter = new CKEDITOR.plugins.codesnippet.highlighter( {
* init: function( ready ) {
* // Asynchronous code to load resources and libraries for customEngine.
* customEngine.loadResources( function() {
* // Let the editor know that everything is ready.
* ready();
* } );
* },
* highlighter: function( code, language, callback ) {
* // Let the customEngine highlight the code.
* customEngine.highlight( code, language, function() {
* callback( highlightedCode );
* } );
* }
* } );
*
* // Check how it performs.
* myHighlighter.highlight( 'foo()', 'javascript', function( highlightedCode ) {
* console.log( highlightedCode ); // -> <span class="pretty">foo()</span>
* } );
*
* // From now on, myHighlighter will be used as a Code Snippet
* // highlighter, overwriting the default engine.
* editor.plugins.codesnippet.setHighlighter( myHighlighter );
* }
* } );
*
* @since 4.4
* @class CKEDITOR.plugins.codesnippet.highlighter
* @extends CKEDITOR.plugins.codesnippet
* @param {Object} def Highlighter definition. See {@link #highlighter}, {@link #init} and {@link #languages}.
*/
function Highlighter( def ) {
CKEDITOR.tools.extend( this, def );
/**
* A queue of {@link #highlight} jobs to be
* done once the highlighter is {@link #ready}.
*
* @readonly
* @property {Array} [=[]]
*/
this.queue = [];
// Async init – execute jobs when ready.
if ( this.init ) {
this.init( CKEDITOR.tools.bind( function() {
// Execute pending jobs.
var job;
while ( ( job = this.queue.pop() ) )
job.call( this );
this.ready = true;
}, this ) );
} else
this.ready = true;
/**
* If specified, this function should asynchronously load highlighter-specific
* resources and execute `ready` when the highlighter is ready.
*
* @property {Function} [init]
* @param {Function} ready The function to be called once
* the highlighter is {@link #ready}.
*/
/**
* A function which highlights given plain text `code` in a given `language` and, once done,
* calls the `callback` function with highlighted markup as an argument.
*
* @property {Function} [highlighter]
* @param {String} code Code to be formatted.
* @param {String} lang Language to be used ({@link CKEDITOR.config#codeSnippet_languages}).
* @param {Function} callback Function which accepts highlighted String as an argument.
*/
/**
* Defines languages supported by the highlighter.
* They can be restricted with the {@link CKEDITOR.config#codeSnippet_languages} configuration option.
*
* **Note**: If {@link CKEDITOR.config#codeSnippet_languages} is set, **it will
* overwrite** the languages listed in `languages`.
*
* languages: {
* coffeescript: 'CoffeeScript',
* cpp: 'C++',
* cs: 'C#',
* css: 'CSS'
* }
*
* More information on how to change the list of languages is available
* in the [Code Snippet documentation](#!/guide/dev_codesnippet-section-changing-languages-list).
*
* @property {Object} languages
*/
/**
* A flag which indicates whether the highlighter is ready to do jobs
* from the {@link #queue}.
*
* @readonly
* @property {Boolean} ready
*/
}
/**
* Executes the {@link #highlighter}. If the highlighter is not ready, it defers the job ({@link #queue})
* and executes it when the highlighter is {@link #ready}.
*
* @param {String} code Code to be formatted.
* @param {String} lang Language to be used ({@link CKEDITOR.config#codeSnippet_languages}).
* @param {Function} callback Function which accepts highlighted String as an argument.
*/
Highlighter.prototype.highlight = function() {
var arg = arguments;
// Highlighter is ready – do it now.
if ( this.ready )
this.highlighter.apply( this, arg );
// Queue the job. It will be done once ready.
else {
this.queue.push( function() {
this.highlighter.apply( this, arg );
} );
}
};
// Encapsulates snippet widget registration code.
// @param {CKEDITOR.editor} editor
function registerWidget( editor ) {
var codeClass = editor.config.codeSnippet_codeClass,
newLineRegex = /\r?\n/g,
textarea = new CKEDITOR.dom.element( 'textarea' );
editor.widgets.add( 'codeSnippet', {
allowedContent: 'pre; code(language-*)',
// Actually we need both - pre and code, but ACF does not make it possible
// to defire required content with "and" operator.
requiredContent: 'pre',
styleableElements: 'pre',
template: '<pre><code class="' + codeClass + '"></code></pre>',
dialog: 'codeSnippet',
mask: true,
parts: {
pre: 'pre',
code: 'code'
},
highlight: function() {
var that = this,
widgetData = this.data,
callback = function( formatted ) {
// IE8 (not supported browser) have issue with new line chars, when using innerHTML.
// It will simply strip it.
that.parts.code.setHtml( isBrowserSupported ?
formatted : formatted.replace( newLineRegex, '<br>' ) );
};
// Set plain code first, so even if custom handler will not call it the code will be there.
callback( CKEDITOR.tools.htmlEncode( widgetData.code ) );
// Call higlighter to apply its custom highlighting.
editor._.codesnippet.highlighter.highlight( widgetData.code, widgetData.lang, function( formatted ) {
editor.fire( 'lockSnapshot' );
callback( formatted );
editor.fire( 'unlockSnapshot' );
} );
},
data: function() {
var newData = this.data,
oldData = this.oldData;
if ( newData.code )
this.parts.code.setHtml( CKEDITOR.tools.htmlEncode( newData.code ) );
// Remove old .language-* class.
if ( oldData && newData.lang != oldData.lang )
this.parts.code.removeClass( 'language-' + oldData.lang );
// Lang needs to be specified in order to apply formatting.
if ( newData.lang ) {
// Apply new .language-* class.
this.parts.code.addClass( 'language-' + newData.lang );
this.highlight();
}
// Save oldData.
this.oldData = CKEDITOR.tools.copy( newData );
},
// Upcasts <pre><code [class="language-*"]>...</code></pre>
upcast: function( el, data ) {
if ( el.name != 'pre' )
return;
var childrenArray = getNonEmptyChildren( el ),
code;
if ( childrenArray.length != 1 || ( code = childrenArray[ 0 ] ).name != 'code' )
return;
// Upcast <code> with text only: http://dev.ckeditor.com/ticket/11926#comment:4
if ( code.children.length != 1 || code.children[ 0 ].type != CKEDITOR.NODE_TEXT )
return;
// Read language-* from <code> class attribute.
var matchResult = editor._.codesnippet.langsRegex.exec( code.attributes[ 'class' ] );
if ( matchResult )
data.lang = matchResult[ 1 ];
// Use textarea to decode HTML entities (#11926).
textarea.setHtml( code.getHtml() );
data.code = textarea.getValue();
code.addClass( codeClass );
return el;
},
// Downcasts to <pre><code [class="language-*"]>...</code></pre>
downcast: function( el ) {
var code = el.getFirst( 'code' );
// Remove pretty formatting from <code>...</code>.
code.children.length = 0;
// Remove config#codeSnippet_codeClass.
code.removeClass( codeClass );
// Set raw text inside <code>...</code>.
code.add( new CKEDITOR.htmlParser.text( CKEDITOR.tools.htmlEncode( this.data.code ) ) );
return el;
}
} );
// Returns an **array** of child elements, with whitespace-only text nodes
// filtered out.
// @param {CKEDITOR.htmlParser.element} parentElement
// @return Array - array of CKEDITOR.htmlParser.node
var whitespaceOnlyRegex = /^[\s\n\r]*$/;
function getNonEmptyChildren( parentElement ) {
var ret = [],
preChildrenList = parentElement.children,
curNode;
// Filter out empty text nodes.
for ( var i = preChildrenList.length - 1; i >= 0; i-- ) {
curNode = preChildrenList[ i ];
if ( curNode.type != CKEDITOR.NODE_TEXT || !curNode.value.match( whitespaceOnlyRegex ) )
ret.push( curNode );
}
return ret;
}
}
} )();
/**
* A CSS class of the `<code>` element used internally for styling
* (by default [highlight.js](http://highlightjs.org) themes, see
* {@link CKEDITOR.config#codeSnippet_theme config.codeSnippet_theme}),
* which means that it is **not present** in the editor output data.
*
* // Changes the class to "myCustomClass".
* config.codeSnippet_codeClass = 'myCustomClass';
*
* **Note**: The class might need to be changed when you are using a custom
* highlighter (the default is [highlight.js](http://highlightjs.org)).
* See {@link CKEDITOR.plugins.codesnippet.highlighter} to read more.
*
* @since 4.4
* @cfg {String} [codeSnippet_codeClass='hljs']
* @member CKEDITOR.config
*/
CKEDITOR.config.codeSnippet_codeClass = 'hljs';
/**
* Restricts languages available in the "Code Snippet" dialog window.
* Node that the empty value is always added to the list.
*
* **Note**: If using a custom highlighter library (the default is [highlight.js](http://highlightjs.org)),
* you may need to refer to external documentation to set `config.codeSnippet_languages` properly.
*
* // Restricts languages to JavaScript and PHP.
* config.codeSnippet_languages = {
* javascript: 'JavaScript',
* php: 'PHP'
* };
*
* More information on how to change the list of languages list is available
* in the [Code Snippet documentation](#!/guide/dev_codesnippet-section-changing-languages-list).
*
* @since 4.4
* @cfg {Object} [codeSnippet_languages=null]
* @member CKEDITOR.config
*/
/**
* A theme used to render code snippets. See [available themes](http://highlightjs.org/static/test.html).
*
* **Note**: This will only work with the default highlighter
* ([highlight.js](http://highlightjs.org/static/test.html)).
*
* // Changes the theme to "pojoaque".
* config.codeSnippet_theme = 'pojoaque';
*
* More information on how to change the highlighter theme is available
* in the [Code Snippet documentation](#!/guide/dev_codesnippet-section-changing-highlighter-theme).
*
* @since 4.4
* @cfg {String} [codeSnippet_theme='default']
* @member CKEDITOR.config
*/
CKEDITOR.config.codeSnippet_theme = 'default';