RichTextElement.php 51.3 KB
Newer Older
Marco Huber's avatar
Marco Huber committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
namespace SGalinski\Tinymce4Rte\Form\Element;

/*
 * This file is part of the TYPO3 CMS project.
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */

17
18
19
20
use SGalinski\Tinymce\Loader;
use SGalinski\Tinymce4Rte\Extension\Typo3Image;
use SGalinski\Tinymce4Rte\Extension\Typo3Link;
use SGalinski\Tinymce4Rte\RteHtmlAreaApi;
21
use SGalinski\Tinymce4Rte\Utility\VersionUtility;
Marco Huber's avatar
Marco Huber committed
22
use TYPO3\CMS\Backend\Form\Element\AbstractFormElement;
23
use TYPO3\CMS\Backend\Form\InlineStackProcessor;
Marco Huber's avatar
Marco Huber committed
24
25
26
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
use TYPO3\CMS\Core\Database\DatabaseConnection;
27
use TYPO3\CMS\Core\FrontendEditing\FrontendEditingController;
Marco Huber's avatar
Marco Huber committed
28
use TYPO3\CMS\Core\Html\RteHtmlParser;
29
30
31
32
33
34
use TYPO3\CMS\Core\Localization\Locales;
use TYPO3\CMS\Core\Localization\LocalizationFactory;
use TYPO3\CMS\Core\Page\PageRenderer;
use TYPO3\CMS\Core\Utility\ArrayUtility;
use TYPO3\CMS\Core\Utility\ClientUtility;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
Marco Huber's avatar
Marco Huber committed
35
36
37
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\MathUtility;
use TYPO3\CMS\Core\Utility\PathUtility;
38
use TYPO3\CMS\Lang\LanguageService;
Marco Huber's avatar
Marco Huber committed
39
40
41
42

/**
 * Render rich text editor in FormEngine
 */
43
class RichTextElement extends AbstractFormElement {
Marco Huber's avatar
Marco Huber committed
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
	/**
	 * Main result array as defined in initializeResultArray() of AbstractNode
	 *
	 * @var array
	 */
	protected $resultArray;

	/**
	 * pid of page record the TSconfig is located at.
	 * This is pid of record if table is not pages, or uid if table is pages
	 *
	 * @var int
	 */
	protected $pidOfPageRecord;

	/**
	 * pid of fixed versioned record.
	 * This is the pid of the record in normal cases, but is changed to the pid
	 * of the "mother" record in case the handled record is a versioned overlay
	 * and "mother" is located at a different pid.
	 *
	 * @var int
	 */
	protected $pidOfVersionedMotherRecord;

	/**
	 * Native, not further processed TsConfig of RTE section for this record on given pid.
	 *
	 * Example:
	 *
	 * RTE = foo
	 * RTE.bar = xy
	 *
	 * array(
78
79
80
81
	 *    'value' => 'foo',
	 *    'properties' => array(
	 *        'bar' => 'xy',
	 *    ),
Marco Huber's avatar
Marco Huber committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95
	 * );
	 *
	 * @var array
	 */
	protected $vanillaRteTsConfig;

	/**
	 * Based on $vanillaRteTsConfig, this property contains "processed" configuration
	 * where table and type specific RTE setup is merged into 'default.' array.
	 *
	 * @var array
	 */
	protected $processedRteConfiguration;

96
97
98
99
100
101
102
	/**
	 * An unique identifier based on field name to have id attributes in HTML referenced in javascript.
	 *
	 * @var string
	 */
	protected $domIdentifier;

Marco Huber's avatar
Marco Huber committed
103
104
105
106
107
108
109
110
	/**
	 * Parsed "defaultExtras" TCA
	 *
	 * @var array
	 */
	protected $defaultExtras;

	/**
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
	 * Some client info containing "user agent", "browser", "version", "system"
	 *
	 * @var array
	 */
	protected $client;

	/**
	 * Selected language
	 *
	 * @var string
	 */
	protected $language;

	/**
	 * TYPO3 language code of the content language
	 *
	 * @var string
	 */
	protected $contentTypo3Language;

	/**
	 * ISO language code of the content language
	 *
	 * @var string
	 */
	protected $contentISOLanguage;

	/**
	 * Uid of chosen content language
	 *
	 * @var int
	 */
	protected $contentLanguageUid;

	/**
	 * The order of the toolbar: the name is the TYPO3-button name
	 *
	 * @var string
	 */
	protected $defaultToolbarOrder;

	/**
	 * Conversion array: TYPO3 button names to htmlArea button names
	 *
	 * @var array
	 */
	protected $convertToolbarForHtmlAreaArray = array(
		'space' => 'space',
		'bar' => 'separator',
		'linebreak' => 'linebreak'
	);

	/**
	 * Final toolbar array
	 *
	 * @var array
	 */
	protected $toolbar = array();

	/**
	 * Save the buttons for the toolbar
	 *
	 * @var array
	 */
	protected $toolbarOrderArray = array();

	/**
	 * Plugin buttons
	 *
	 * @var array
	 */
	protected $pluginButton = array();

	/**
	 * Plugin labels
	 *
	 * @var array
	 */
	protected $pluginLabel = array();

	/**
	 * Array of plugin id's enabled in the current RTE editing area
Marco Huber's avatar
Marco Huber committed
193
	 *
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
	 * @var array
	 */
	protected $pluginEnabledArray = array();

	/**
	 * Cumulative array of plugin id's enabled so far in any of the RTE editing areas of the form
	 *
	 * @var array
	 */
	protected $pluginEnabledCumulativeArray = array();

	/**
	 * Array of registered plugins indexed by their plugin Id's
	 *
	 * @var array
Marco Huber's avatar
Marco Huber committed
209
	 */
210
	protected $registeredPlugins = array();
Marco Huber's avatar
Marco Huber committed
211
212
213
214
215
216
217
218

	/**
	 * This will render a <textarea> OR RTE area form field,
	 * possibly with various control/validation features
	 *
	 * @return array As defined in initializeResultArray() of AbstractNode
	 */
	public function render() {
219
220
221
222
		$table = $this->data['tableName'];
		$fieldName = $this->data['fieldName'];
		$row = $this->data['databaseRow'];
		$parameterArray = $this->data['parameterArray'];
Marco Huber's avatar
Marco Huber committed
223
224
225
226
227

		$backendUser = $this->getBackendUserAuthentication();

		$this->resultArray = $this->initializeResultArray();
		$this->defaultExtras = BackendUtility::getSpecConfParts($parameterArray['fieldConf']['defaultExtras']);
228
		$this->pidOfPageRecord = $this->data['effectivePid'];
Marco Huber's avatar
Marco Huber committed
229
		BackendUtility::fixVersioningPid($table, $row);
230
		$this->pidOfVersionedMotherRecord = (int) $row['pid'];
Marco Huber's avatar
Marco Huber committed
231
232
233
234
235
		$this->vanillaRteTsConfig = $backendUser->getTSConfig('RTE', BackendUtility::getPagesTSconfig($this->pidOfPageRecord));
		$this->processedRteConfiguration = BackendUtility::RTEsetup(
			$this->vanillaRteTsConfig['properties'],
			$table,
			$fieldName,
236
			$this->data['recordTypeValue']
Marco Huber's avatar
Marco Huber committed
237
		);
238
239
240
241
242
243
244
245
246
247
248
249
250
251
		$this->client = $this->clientInfo();
		$this->domIdentifier = preg_replace('/[^a-zA-Z0-9_:.-]/', '_', $parameterArray['itemFormElName']);
		$this->domIdentifier = htmlspecialchars(preg_replace('/^[^a-zA-Z]/', 'x', $this->domIdentifier));
//
//		$this->initializeLanguageRelatedProperties();
//
		// Get skin file name from Page TSConfig if any
//		$skinFilename = trim($this->processedRteConfiguration['skin']) ?: 'EXT:rtehtmlarea/Resources/Public/Css/Skin/htmlarea.css';
//		$skinFilename = $this->getFullFileName($skinFilename);
//		$skinDirectory = dirname($skinFilename);
//
//		// jQuery UI Resizable style sheet and main skin stylesheet
//		$this->resultArray['stylesheetFiles'][] = $skinDirectory . '/jquery-ui-resizable.css';
//		$this->resultArray['stylesheetFiles'][] = $skinFilename;
Marco Huber's avatar
Marco Huber committed
252

253
254
255
256
257
258
259
260
261
262
263
        $this->enableRegisteredPlugins();

//        // Configure toolbar
//        $this->setToolbar();
//
//        // Check if some plugins need to be disabled
//        $this->setPlugins();
//
        // Merge the list of enabled plugins with the lists from the previous RTE editing areas on the same form
        $this->pluginEnabledCumulativeArray = $this->pluginEnabledArray;

264
//        $this->addOnSubmitJavaScriptCode();
265
266

        // Add RTE JavaScript
267
        $this->loadRequireModulesForRTE();
268
269
270
271
272
273

//        // Create language labels
//        $this->createJavaScriptLanguageLabelsFromFiles();

        // Get RTE init JS code
        $this->resultArray['additionalJavaScriptPost'][] = $this->getRteInitJsCode();
274
		$this->addInstanceJavaScriptRegistration();
275
276
277
278
279
280
281
282
283
284
285
286
287
288

		$html = $this->getMainHtml();

		$this->resultArray['html'] = $this->renderWizards(
			array($html),
			$parameterArray['fieldConf']['config']['wizards'],
			$table,
			$row,
			$fieldName,
			$parameterArray,
			$parameterArray['itemFormElName'],
			$this->defaultExtras,
			TRUE
		);
Marco Huber's avatar
Marco Huber committed
289
290
291
292

		return $this->resultArray;
	}

293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
	/**
	 * Create main HTML elements
	 *
	 * @return string Main RTE html
	 */
	protected function getMainHtml() {
		$backendUser = $this->getBackendUserAuthentication();

		if ($this->isInFullScreenMode()) {
			$width = '100%';
			$height = '100%';
			$paddingRight = '0px';
			$editorWrapWidth = '100%';
		} else {
			$options = $backendUser->userTS['options.'];
308
			$width = 610 + (isset($options['RTELargeWidthIncrement']) ? (int) $options['RTELargeWidthIncrement'] : 150);
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
			/** @var InlineStackProcessor $inlineStackProcessor */
			$inlineStackProcessor = GeneralUtility::makeInstance(InlineStackProcessor::class);
			$inlineStackProcessor->initializeByGivenStructure($this->data['inlineStructure']);
			$inlineStructureDepth = $inlineStackProcessor->getStructureDepth();
			$width -= $inlineStructureDepth > 0 ? ($inlineStructureDepth + 1) * 12 : 0;
			$widthOverride = isset($backendUser->uc['rteWidth']) && trim($backendUser->uc['rteWidth']) ?: trim($this->processedRteConfiguration['RTEWidthOverride']);
			if ($widthOverride) {
				if (strstr($widthOverride, '%')) {
					if ($this->client['browser'] !== 'msie') {
						$width = (int) $widthOverride > 0 ? (int) $widthOverride : '100%';
					}
				} else {
					$width = (int) $widthOverride > 0 ? (int) $widthOverride : $width;
				}
			}
			$width = strstr($width, '%') ? $width : $width . 'px';
			$height = 380 + (isset($options['RTELargeHeightIncrement']) ? (int) $options['RTELargeHeightIncrement'] : 0);
			$heightOverride = isset($backendUser->uc['rteHeight']) && (int) $backendUser->uc['rteHeight'] ?: (int) $this->processedRteConfiguration['RTEHeightOverride'];
			$height = $heightOverride > 0 ? $heightOverride . 'px' : $height . 'px';
			$paddingRight = '2';
			$editorWrapWidth = '99%';
		}
		$rteDivStyle = 'position:relative; left:0px; top:0px; height:' . $height . '; width:' . $width . '; border: 1px solid black; padding: 2 ' . $paddingRight . ' 2 2;';

		$itemFormElementName = $this->data['parameterArray']['itemFormElName'];
Marco Huber's avatar
Marco Huber committed
334
335
336
337

		// This seems to result in:
		//	_TRANSFORM_bodytext (the handled field name) in case the field is a direct DB field
		//	_TRANSFORM_vDEF (constant string) in case the RTE is within a flex form
338
339
340
341
		$triggerFieldName = preg_replace('/\\[([^]]+)\\]$/', '[_TRANSFORM_\\1]', $itemFormElementName);

		$value = $this->transformDatabaseContentToEditor($this->data['parameterArray']['itemFormElValue']);

342
343
344
345
346
347
348
349
350
351
		// Remove this empty data attribute, otherwise an error will throw in TYPO3 8.7.
		$validationResults = $this->getValidationDataAsDataAttribute(
			$this->data['parameterArray']['fieldConf']['config']
		);
		if (VersionUtility::isVersion870OrHigher() &&
			trim($validationResults) === 'data-formengine-validation-rules="[]"'
		) {
			$validationResults = '';
		}

352
353
354
		$result = array();
		// The hidden field tells the DataHandler that processing should be done on this value.
		$result[] = '<input type="hidden" name="' . htmlspecialchars($triggerFieldName) . '" value="RTE" />';
355
356
357
//		$result[] = '<div id="pleasewait' . $this->domIdentifier . '" class="pleasewait" style="display: block;" >';
//		$result[] =    $this->getLanguageService()->sL('LLL:EXT:tinymce4_rte/Resources/Private/Language/locallang.xlf:Please wait');
//		$result[] = '</div>';
358
		$result[] = '<div id="editorWrap' . $this->domIdentifier . '" class="editorWrap" style="width:' . $editorWrapWidth . '; height:100%;">';
359
		$result[] = '<textarea ' . $validationResults . ' id="RTEarea' . $this->domIdentifier . '" class="tinymce4_rte" name="' . htmlspecialchars($itemFormElementName) . '" rows="0" cols="0" style="' . htmlspecialchars($rteDivStyle) . '">';
360
361
362
363
364
365
366
367
368
369
370
371
372
		$result[] = htmlspecialchars($value);
		$result[] = '</textarea>';
		$result[] = '</div>';

		return implode(LF, $result);
	}

	/**
	 * Add registered plugins to the array of enabled plugins
	 *
	 * @return void
	 */
	protected function enableRegisteredPlugins() {
373
		$plugins = [
374
375
376
			'TYPO3Image' => [
				'objectReference' => Typo3Image::class,
			],
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
			'TYPO3Link' => [
				'objectReference' => TYPO3Link::class,
			],
		];
		foreach ($plugins as $pluginId => $pluginObjectConfiguration) {
			if (is_array($pluginObjectConfiguration) && isset($pluginObjectConfiguration['objectReference'])) {
				/** @var RteHtmlAreaApi $plugin */
				$plugin = GeneralUtility::makeInstance($pluginObjectConfiguration['objectReference']);
				$configuration = array(
					'language' => $this->language,
					'contentTypo3Language' => $this->contentTypo3Language,
					'contentISOLanguage' => $this->contentISOLanguage,
					'contentLanguageUid' => $this->contentLanguageUid,
					'RTEsetup' => $this->vanillaRteTsConfig,
					'client' => $this->client,
					'thisConfig' => $this->processedRteConfiguration,
					'specConf' => $this->defaultExtras,
				);
				if ($plugin->main($configuration)) {
					$this->registeredPlugins[$pluginId] = $plugin;
					// Override buttons from previously registered plugins
					$pluginButtons = GeneralUtility::trimExplode(',', $plugin->getPluginButtons(), TRUE);
					foreach ($this->pluginButton as $previousPluginId => $buttonList) {
						$this->pluginButton[$previousPluginId] = implode(',', array_diff(GeneralUtility::trimExplode(',', $this->pluginButton[$previousPluginId], TRUE), $pluginButtons));
					}
					$this->pluginButton[$pluginId] = $plugin->getPluginButtons();
					$pluginLabels = GeneralUtility::trimExplode(',', $plugin->getPluginLabels(), TRUE);
					foreach ($this->pluginLabel as $previousPluginId => $labelList) {
						$this->pluginLabel[$previousPluginId] = implode(',', array_diff(GeneralUtility::trimExplode(',', $this->pluginLabel[$previousPluginId], TRUE), $pluginLabels));
					}
					$this->pluginLabel[$pluginId] = $plugin->getPluginLabels();
					$this->pluginEnabledArray[] = $pluginId;
				}
			}
		}

		// Process overrides
		$hidePlugins = array();
		foreach ($this->registeredPlugins as $pluginId => $plugin) {
			/** @var RteHtmlAreaApi $plugin */
			if ($plugin->addsButtons() && !$this->pluginButton[$pluginId]) {
				$hidePlugins[] = $pluginId;
			}
		}
		$this->pluginEnabledArray = array_unique(array_diff($this->pluginEnabledArray, $hidePlugins));
422
	}
Marco Huber's avatar
Marco Huber committed
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
	/**
	 * Set the toolbar config (only in this PHP-Object, not in JS):
	 *
	 * @return void
	 */
	protected function setToolbar() {
		$backendUser = $this->getBackendUserAuthentication();

		if ($this->client['browser'] === 'msie' || $this->client['browser'] === 'opera') {
			$this->processedRteConfiguration['keepButtonGroupTogether'] = 0;
		}
		$this->defaultToolbarOrder = 'bar, blockstylelabel, blockstyle, textstylelabel, textstyle, linebreak,
			bar, formattext, bold,  strong, italic, emphasis, big, small, insertedtext, deletedtext, citation, code,'
			. 'definition, keyboard, monospaced, quotation, sample, variable, bidioverride, strikethrough, subscript, superscript, underline, span,
			bar, fontstyle, fontsize, bar, formatblock, insertparagraphbefore, insertparagraphafter, blockquote, line,
			bar, left, center, right, justifyfull,
			bar, orderedlist, unorderedlist, definitionlist, definitionitem, outdent, indent,
			bar, language, showlanguagemarks,lefttoright, righttoleft,
			bar, textcolor, bgcolor, textindicator,
			bar, editelement, showmicrodata,
			bar, image, emoticon, insertcharacter, insertsofthyphen, abbreviation, user,
			bar, link, unlink,
			bar, table,'
			. ($this->processedRteConfiguration['hideTableOperationsInToolbar']
			&& is_array($this->processedRteConfiguration['buttons.'])
			&& is_array($this->processedRteConfiguration['buttons.']['toggleborders.'])
			&& $this->processedRteConfiguration['buttons.']['toggleborders.']['keepInToolbar'] ? ' toggleborders,' : '')
			. 'bar, findreplace, spellcheck,
			bar, chMode, inserttag, removeformat, bar, copy, cut, paste, pastetoggle, pastebehaviour, bar, undo, redo, bar, about, linebreak,'
			. ($this->processedRteConfiguration['hideTableOperationsInToolbar'] ? '' : 'bar, toggleborders,')
			. ' bar, tableproperties, tablerestyle, bar, rowproperties, rowinsertabove, rowinsertunder, rowdelete, rowsplit, bar,
			columnproperties, columninsertbefore, columninsertafter, columndelete, columnsplit, bar,
			cellproperties, cellinsertbefore, cellinsertafter, celldelete, cellsplit, cellmerge';

		// Additional buttons from registered plugins
		foreach ($this->registeredPlugins as $pluginId => $plugin) {
			/** @var RteHtmlAreaApi $plugin */
			if ($this->isPluginEnabled($pluginId)) {
				$pluginButtons = $plugin->getPluginButtons();
				//Add only buttons not yet in the default toolbar order
				$addButtons = implode(
					',',
					array_diff(
						GeneralUtility::trimExplode(',', $pluginButtons, TRUE),
						GeneralUtility::trimExplode(',', $this->defaultToolbarOrder, TRUE)
					)
				);
				$this->defaultToolbarOrder = ($addButtons ? 'bar,' . $addButtons . ',linebreak,' : '') . $this->defaultToolbarOrder;
			}
		}
		$toolbarOrder = $this->processedRteConfiguration['toolbarOrder'] ?: $this->defaultToolbarOrder;
		// Getting rid of undefined buttons
		$this->toolbarOrderArray = array_intersect(GeneralUtility::trimExplode(',', $toolbarOrder, TRUE), GeneralUtility::trimExplode(',', $this->defaultToolbarOrder, TRUE));
		$toolbarOrder = array_unique(array_values($this->toolbarOrderArray));
		// Fetching specConf for field from backend
		$pList = is_array($this->defaultExtras['richtext']['parameters']) ? implode(',', $this->defaultExtras['richtext']['parameters']) : '';
		if ($pList !== '*') {
			// If not all
			$show = is_array($this->defaultExtras['richtext']['parameters']) ? $this->defaultExtras['richtext']['parameters'] : array();
			if ($this->processedRteConfiguration['showButtons']) {
				if (!GeneralUtility::inList($this->processedRteConfiguration['showButtons'], '*')) {
					$show = array_unique(array_merge($show, GeneralUtility::trimExplode(',', $this->processedRteConfiguration['showButtons'], TRUE)));
				} else {
					$show = array_unique(array_merge($show, $toolbarOrder));
				}
			}
			if (is_array($this->processedRteConfiguration['showButtons.'])) {
				foreach ($this->processedRteConfiguration['showButtons.'] as $buttonId => $value) {
					if ($value) {
						$show[] = $buttonId;
					}
Marco Huber's avatar
Marco Huber committed
495
				}
496
				$show = array_unique($show);
Marco Huber's avatar
Marco Huber committed
497
			}
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
		} else {
			$show = $toolbarOrder;
		}
		$RTEkeyList = isset($backendUser->userTS['options.']['RTEkeyList']) ? $backendUser->userTS['options.']['RTEkeyList'] : '*';
		if ($RTEkeyList !== '*') {
			// If not all
			$show = array_intersect($show, GeneralUtility::trimExplode(',', $RTEkeyList, TRUE));
		}
		// Hiding buttons of disabled plugins
		$hideButtons = array('space', 'bar', 'linebreak');
		foreach ($this->pluginButton as $pluginId => $buttonList) {
			if (!$this->isPluginEnabled($pluginId)) {
				$buttonArray = GeneralUtility::trimExplode(',', $buttonList, TRUE);
				foreach ($buttonArray as $button) {
					$hideButtons[] = $button;
				}
			}
		}
		// Hiding labels of disabled plugins
		foreach ($this->pluginLabel as $pluginId => $label) {
			if (!$this->isPluginEnabled($pluginId)) {
				$hideButtons[] = $label;
			}
		}
		// Hiding buttons
		$show = array_diff($show, GeneralUtility::trimExplode(',', $this->processedRteConfiguration['hideButtons'], TRUE));
		// Apply toolbar constraints from registered plugins
		foreach ($this->registeredPlugins as $pluginId => $plugin) {
			if ($this->isPluginEnabled($pluginId) && method_exists($plugin, 'applyToolbarConstraints')) {
				$show = $plugin->applyToolbarConstraints($show);
			}
		}
		// Getting rid of the buttons for which we have no position
		$show = array_intersect($show, $toolbarOrder);
		foreach ($this->registeredPlugins as $pluginId => $plugin) {
			/** @var RteHtmlAreaApi $plugin */
			$plugin->setToolbar($show);
		}
		$this->toolbar = $show;
	}
Marco Huber's avatar
Marco Huber committed
538

539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
	/**
	 * Disable some plugins
	 *
	 * @return void
	 */
	protected function setPlugins() {
		// Disabling a plugin that adds buttons if none of its buttons is in the toolbar
		$hidePlugins = array();
		foreach ($this->pluginButton as $pluginId => $buttonList) {
			/** @var RteHtmlAreaApi $plugin */
			$plugin = $this->registeredPlugins[$pluginId];
			if ($plugin->addsButtons()) {
				$showPlugin = FALSE;
				$buttonArray = GeneralUtility::trimExplode(',', $buttonList, TRUE);
				foreach ($buttonArray as $button) {
					if (in_array($button, $this->toolbar)) {
						$showPlugin = TRUE;
Marco Huber's avatar
Marco Huber committed
556
					}
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
				}
				if (!$showPlugin) {
					$hidePlugins[] = $pluginId;
				}
			}
		}
		$this->pluginEnabledArray = array_diff($this->pluginEnabledArray, $hidePlugins);
		// Hiding labels of disabled plugins
		$hideLabels = array();
		foreach ($this->pluginLabel as $pluginId => $label) {
			if (!$this->isPluginEnabled($pluginId)) {
				$hideLabels[] = $label;
			}
		}
		$this->toolbar = array_diff($this->toolbar, $hideLabels);
		// Adding plugins declared as prerequisites by enabled plugins
		$requiredPlugins = array();
		foreach ($this->registeredPlugins as $pluginId => $plugin) {
			/** @var RteHtmlAreaApi $plugin */
			if ($this->isPluginEnabled($pluginId)) {
				$requiredPlugins = array_merge($requiredPlugins, GeneralUtility::trimExplode(',', $plugin->getRequiredPlugins(), TRUE));
			}
		}
		$requiredPlugins = array_unique($requiredPlugins);
		foreach ($requiredPlugins as $pluginId) {
			if (is_object($this->registeredPlugins[$pluginId]) && !$this->isPluginEnabled($pluginId)) {
				$this->pluginEnabledArray[] = $pluginId;
			}
		}
		$this->pluginEnabledArray = array_unique($this->pluginEnabledArray);
		// Completing the toolbar conversion array for htmlArea
		foreach ($this->registeredPlugins as $pluginId => $plugin) {
			/** @var RteHtmlAreaApi $plugin */
			if ($this->isPluginEnabled($pluginId)) {
				$this->convertToolbarForHtmlAreaArray = array_unique(array_merge($this->convertToolbarForHtmlAreaArray, $plugin->getConvertToolbarForHtmlAreaArray()));
			}
		}
	}

	/**
	 * Add RTE main scripts and plugin scripts
	 *
	 * @return void
	 */
	protected function loadRequireModulesForRTE() {
602
603
604
		/** @var Loader $tinyMCE */
		$tinyMCE = GeneralUtility::makeInstance(Loader::class);
		$tinyMCE->loadConfiguration($this->vanillaRteTsConfig['properties']['default.']['tinymceConfiguration']);
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623

		$contentCssArray = [];
		if (VersionUtility::isVersion870OrHigher()) {
			$contentCssArray = is_array($this->vanillaRteTsConfig['properties']['default.']['contentCSS.']) ?
				$this->vanillaRteTsConfig['properties']['default.']['contentCSS.'] : [];
		} else {
			if ($this->vanillaRteTsConfig['properties']['default.']['contentCSS'] !== '') {
				$contentCssArray = is_array($this->vanillaRteTsConfig['properties']['default.']['contentCSS.']) ?
					$this->vanillaRteTsConfig['properties']['default.']['contentCSS.'] :
					(array) $this->vanillaRteTsConfig['properties']['default.']['contentCSS'];
			}
		}

		if (!empty($contentCssArray)) {
			$contentCssFileArray = [];
			foreach ($contentCssArray as $contentCssKey => $contentCssFile) {
				$contentCssFileAbs = GeneralUtility::getFileAbsFileName(trim($contentCssFile));
				if (is_file($contentCssFileAbs)) {
					$contentCssFileArray[] = GeneralUtility::getIndpEnv('TYPO3_SITE_URL') . PathUtility::stripPathSitePrefix($contentCssFileAbs) . '?' . filemtime($contentCssFileAbs);
624
				}
625
			}
626
			$tinyMCE->addConfigurationOption('content_css', implode(',', $contentCssFileArray));
627
		}
628
629

		$tinyMCE->addConfigurationOption(
630
631
632
633
634
635
			'changeMethod', 'function() {
				var TBE_EDITOR = window.TBE_EDITOR || null;
				if (TBE_EDITOR && TBE_EDITOR.fieldChanged && typeof TBE_EDITOR.fieldChanged === \'function\') {
					TBE_EDITOR.fieldChanged();
				}
			}'
636
637
638
639
		);

		$tinyMCE->addConfigurationOption('editornumber', $this->domIdentifier);

640
		// IRRE
641
		$this->resultArray['requireJsModules'] = $tinyMCE->loadJsViaRequireJS();
642
643
644
645
646
647
648
649
650
651
652
653
654
	}

	/**
	 * Return RTE initialization inline JavaScript code
	 *
	 * @return string RTE initialization inline JavaScript code
	 */
	protected function getRteInitJsCode() {
		return 'if (typeof RTEarea === "undefined") {
			RTEarea = new Object();
			RTEarea[0] = new Object();
			RTEarea[0].version = "' . $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['tinymce4_rte']['version'] . '";
			RTEarea[0].editorUrl = "' . ExtensionManagementUtility::extRelPath('tinymce4_rte') . '";
655
		}';
656
657
658
659
660
661
662
663
664
665
666
	}

	/**
	 * Return the Javascript code for configuring the RTE
	 *
	 * @return void
	 */
	protected function addInstanceJavaScriptRegistration() {
		$backendUser = $this->getBackendUserAuthentication();

		$jsArray = array();
667
668
669
670
671
672
673
//		$jsArray[] = 'if (typeof configureEditorInstance === "undefined") {';
//		$jsArray[] = '	configureEditorInstance = new Object();';
//		$jsArray[] = '}';
//		$jsArray[] = 'configureEditorInstance["' . $this->domIdentifier . '"] = function() {';
//		$jsArray[] = 'if (typeof RTEarea === "undefined" || typeof SG === "undefined") {';
//		$jsArray[] = '	window.setTimeout("configureEditorInstance[' . GeneralUtility::quoteJSvalue($this->domIdentifier) . ']();", 40);';
//		$jsArray[] = '} else {';
674
		$jsArray[] = 'editornumber = "' . $this->domIdentifier . '";';
675
		$jsArray[] = 'var RTEarea = window.RTEarea || {};';
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
		$jsArray[] = 'RTEarea[editornumber] = new Object();';
		$jsArray[] = 'RTEarea[editornumber].RTEtsConfigParams = "&RTEtsConfigParams=' . rawurlencode($this->RTEtsConfigParams()) . '";';
		$jsArray[] = 'RTEarea[editornumber].number = editornumber;';
		$jsArray[] = 'RTEarea[editornumber].deleted = false;';
		$jsArray[] = 'RTEarea[editornumber].textAreaId = "' . $this->domIdentifier . '";';
		$jsArray[] = 'RTEarea[editornumber].id = "RTEarea" + editornumber;';
		$jsArray[] = 'RTEarea[editornumber].RTEWidthOverride = "'
			. (isset($backendUser->uc['rteWidth']) && trim($backendUser->uc['rteWidth'])
				? trim($backendUser->uc['rteWidth'])
				: trim($this->processedRteConfiguration['RTEWidthOverride'])) . '";';
		$jsArray[] = 'RTEarea[editornumber].RTEHeightOverride = "'
			. (isset($backendUser->uc['rteHeight']) && (int) $backendUser->uc['rteHeight']
				? (int) $backendUser->uc['rteHeight']
				: (int) $this->processedRteConfiguration['RTEHeightOverride']) . '";';
		$jsArray[] = 'RTEarea[editornumber].resizable = '
			. (isset($backendUser->uc['rteResize']) && $backendUser->uc['rteResize']
				? 'true;'
				: (trim($this->processedRteConfiguration['rteResize']) ? 'true;' : 'false;'));
		$jsArray[] = 'RTEarea[editornumber].maxHeight = "'
			. (isset($backendUser->uc['rteMaxHeight']) && (int) $backendUser->uc['rteMaxHeight']
				? trim($backendUser->uc['rteMaxHeight'])
				: ((int) $this->processedRteConfiguration['rteMaxHeight'] ?: '2000')) . '";';
		$jsArray[] = 'RTEarea[editornumber].fullScreen = ' . ($this->isInFullScreenMode() ? 'true;' : 'false;');
		$jsArray[] = 'RTEarea[editornumber].showStatusBar = ' . (trim($this->processedRteConfiguration['showStatusBar']) ? 'true;' : 'false;');
		$jsArray[] = 'RTEarea[editornumber].enableWordClean = ' . (trim($this->processedRteConfiguration['enableWordClean']) ? 'true;' : 'false;');
		$jsArray[] = 'RTEarea[editornumber].htmlRemoveComments = ' . (trim($this->processedRteConfiguration['removeComments']) ? 'true;' : 'false;');
		$jsArray[] = 'RTEarea[editornumber].disableEnterParagraphs = ' . (trim($this->processedRteConfiguration['disableEnterParagraphs']) ? 'true;' : 'false;');
		$jsArray[] = 'RTEarea[editornumber].disableObjectResizing = ' . (trim($this->processedRteConfiguration['disableObjectResizing']) ? 'true;' : 'false;');
		$jsArray[] = 'RTEarea[editornumber].removeTrailingBR = ' . (trim($this->processedRteConfiguration['removeTrailingBR']) ? 'true;' : 'false;');
		$jsArray[] = 'RTEarea[editornumber].useCSS = ' . (trim($this->processedRteConfiguration['useCSS']) ? 'true' : 'false') . ';';
		$jsArray[] = 'RTEarea[editornumber].keepButtonGroupTogether = ' . (trim($this->processedRteConfiguration['keepButtonGroupTogether']) ? 'true;' : 'false;');
		$jsArray[] = 'RTEarea[editornumber].disablePCexamples = ' . (trim($this->processedRteConfiguration['disablePCexamples']) ? 'true;' : 'false;');
		$jsArray[] = 'RTEarea[editornumber].showTagFreeClasses = ' . (trim($this->processedRteConfiguration['showTagFreeClasses']) ? 'true;' : 'false;');
		$jsArray[] = 'RTEarea[editornumber].tceformsNested = ' . (!empty($this->data) ? json_encode($this->data['tabAndInlineStack']) : '[]') . ';';
		$jsArray[] = 'RTEarea[editornumber].dialogueWindows = new Object();';
		if (isset($this->processedRteConfiguration['dialogueWindows.']['defaultPositionFromTop'])) {
			$jsArray[] = 'RTEarea[editornumber].dialogueWindows.positionFromTop = ' . (int) $this->processedRteConfiguration['dialogueWindows.']['defaultPositionFromTop'] . ';';
		}
		if (isset($this->processedRteConfiguration['dialogueWindows.']['defaultPositionFromLeft'])) {
			$jsArray[] = 'RTEarea[editornumber].dialogueWindows.positionFromLeft = ' . (int) $this->processedRteConfiguration['dialogueWindows.']['defaultPositionFromLeft'] . ';';
		}
		$jsArray[] = 'RTEarea[editornumber].sys_language_content = "' . $this->contentLanguageUid . '";';
		$jsArray[] = 'RTEarea[editornumber].typo3ContentLanguage = "' . $this->contentTypo3Language . '";';
		$jsArray[] = 'RTEarea[editornumber].userUid = "' . 'BE_' . $backendUser->user['uid'] . '";';

		// Setting the plugin flags
		$jsArray[] = 'RTEarea[editornumber].plugin = new Object();';
		foreach ($this->pluginEnabledArray as $pluginId) {
			$jsArray[] = 'RTEarea[editornumber].plugin.' . $pluginId . ' = true;';
		}

		// Setting the buttons configuration
		$jsArray[] = 'RTEarea[editornumber].buttons = new Object();';
		if (is_array($this->processedRteConfiguration['buttons.'])) {
			foreach ($this->processedRteConfiguration['buttons.'] as $buttonIndex => $conf) {
				$button = substr($buttonIndex, 0, -1);
				if (is_array($conf)) {
					$jsArray[] = 'RTEarea[editornumber].buttons.' . $button . ' = ' . $this->buildNestedJSArray($conf) . ';';
				}
			}
		}

		// Setting the list of tags to be removed if specified in the RTE config
		if (trim($this->processedRteConfiguration['removeTags'])) {
			$jsArray[] = 'RTEarea[editornumber].htmlRemoveTags = /^(' . implode('|', GeneralUtility::trimExplode(',', $this->processedRteConfiguration['removeTags'], TRUE)) . ')$/i;';
		}

		// Setting the list of tags to be removed with their contents if specified in the RTE config
		if (trim($this->processedRteConfiguration['removeTagsAndContents'])) {
			$jsArray[] = 'RTEarea[editornumber].htmlRemoveTagsAndContents = /^(' . implode('|', GeneralUtility::trimExplode(',', $this->processedRteConfiguration['removeTagsAndContents'], TRUE)) . ')$/i;';
		}

		// Setting array of custom tags if specified in the RTE config
		if (!empty($this->processedRteConfiguration['customTags'])) {
			$customTags = GeneralUtility::trimExplode(',', $this->processedRteConfiguration['customTags'], TRUE);
			if (!empty($customTags)) {
				$jsArray[] = 'RTEarea[editornumber].customTags= ' . json_encode($customTags) . ';';
			}
		}

		// Setting array of content css files if specified in the RTE config
		$versionNumberedFileNames = array();
//		$contentCssFileNames = $this->getContentCssFileNames();
//		foreach ($contentCssFileNames as $contentCssFileName) {
//			$versionNumberedFileNames[] = GeneralUtility::createVersionNumberedFilename($contentCssFileName);
//		}
		$jsArray[] = 'RTEarea[editornumber].pageStyle = ["' . implode('","', $versionNumberedFileNames) . '"];';

		$jsArray[] = 'RTEarea[editornumber].classesUrl = "' . $this->writeTemporaryFile(('classes_' . $this->language), 'js', $this->buildJSClassesArray()) . '";';

		// Add Javascript configuration for registered plugins
		foreach ($this->registeredPlugins as $pluginId => $plugin) {
			/** @var RteHtmlAreaApi $plugin */
			if ($this->isPluginEnabled($pluginId)) {
				$jsPluginString = $plugin->buildJavascriptConfiguration();
				if ($jsPluginString) {
					$jsArray[] = $plugin->buildJavascriptConfiguration();
				}
			}
		}

		// Avoid premature reference to HTMLArea when being initially loaded by IRRE Ajax call
		$jsArray[] = 'RTEarea[editornumber].toolbar = ' . $this->getJSToolbarArray() . ';';
		$jsArray[] = 'RTEarea[editornumber].convertButtonId = ' . json_encode(array_flip($this->convertToolbarForHtmlAreaArray)) . ';';
		$jsArray[] = 'RTEarea[editornumber].editor = {
			getPlugin: function(pluginId) {
				return plugin;
			}
		};';
785
		//$jsArray[] = 'RTEarea.initEditor(editornumber);';
786
787
788
//		$jsArray[] = '}';
//		$jsArray[] = '}';
//		$jsArray[] = 'configureEditorInstance["' . $this->domIdentifier . '"]();';
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937

		$this->resultArray['additionalJavaScriptPost'][] = implode(LF, $jsArray);
	}

	/**
	 * Get the name of the contentCSS files to use
	 *
	 * @return array An array of full file name of the content css files to use
	 */
	protected function getContentCssFileNames() {
		$contentCss = is_array($this->processedRteConfiguration['contentCSS.']) ? $this->processedRteConfiguration['contentCSS.'] : array();
		if (isset($this->processedRteConfiguration['contentCSS'])) {
			$contentCss[] = trim($this->processedRteConfiguration['contentCSS']);
		}
		$contentCssFiles = array();
		if (!empty($contentCss)) {
			foreach ($contentCss as $contentCssKey => $contentCssfile) {
				$fileName = trim($contentCssfile);
				$absolutePath = GeneralUtility::getFileAbsFileName($fileName);
				if (file_exists($absolutePath) && filesize($absolutePath)) {
					$contentCssFiles[$contentCssKey] = $this->getFullFileName($fileName);
				}
			}
		} else {
			// Fallback to default content css file if none of the configured files exists and is not empty
			$contentCssFiles['default'] = $this->getFullFileName('EXT:rtehtmlarea/Resources/Public/Css/ContentCss/Default.css');
		}
		return array_unique($contentCssFiles);
	}

	/**
	 * Return TRUE, if the plugin can be loaded
	 *
	 * @param string $pluginId : The identification string of the plugin
	 * @return bool TRUE if the plugin can be loaded
	 */
	protected function isPluginEnabled($pluginId) {
		return in_array($pluginId, $this->pluginEnabledArray);
	}

	/**
	 * Return JS arrays of classes configuration
	 *
	 * @return string JS classes arrays
	 */
	protected function buildJSClassesArray() {
		$RTEProperties = $this->vanillaRteTsConfig['properties'];
		// Declare sub-arrays
		$classesArray = array(
			'labels' => array(),
			'values' => array(),
			'noShow' => array(),
			'alternating' => array(),
			'counting' => array(),
			'selectable' => array(),
			'requires' => array(),
			'requiredBy' => array(),
			'XOR' => array()
		);
		$JSClassesArray = '';
		// Scanning the list of classes if specified in the RTE config
		if (is_array($RTEProperties['classes.'])) {
			foreach ($RTEProperties['classes.'] as $className => $conf) {
				$className = rtrim($className, '.');

				$label = '';
				if (!empty($conf['name'])) {
					$label = $this->getLanguageService()->sL(trim($conf['name']));
					$label = str_replace('"', '\\"', str_replace('\\\'', '\'', $label));
				}
				$classesArray['labels'][$className] = $label;
				$classesArray['values'][$className] = str_replace('\\\'', '\'', $conf['value']);
				if (isset($conf['noShow'])) {
					$classesArray['noShow'][$className] = $conf['noShow'];
				}
				if (is_array($conf['alternating.'])) {
					$classesArray['alternating'][$className] = $conf['alternating.'];
				}
				if (is_array($conf['counting.'])) {
					$classesArray['counting'][$className] = $conf['counting.'];
				}
				if (isset($conf['selectable'])) {
					$classesArray['selectable'][$className] = $conf['selectable'];
				}
				if (isset($conf['requires'])) {
					$classesArray['requires'][$className] = explode(',', GeneralUtility::rmFromList($className, $this->cleanList($conf['requires'])));
				}
			}
			// Remove circularities from classes dependencies
			$requiringClasses = array_keys($classesArray['requires']);
			foreach ($requiringClasses as $requiringClass) {
				if ($this->hasCircularDependency($classesArray, $requiringClass, $requiringClass)) {
					unset($classesArray['requires'][$requiringClass]);
				}
			}
			// Reverse relationship for the dependency checks when removing styles
			$requiringClasses = array_keys($classesArray['requires']);
			foreach ($requiringClasses as $className) {
				foreach ($classesArray['requires'][$className] as $requiredClass) {
					if (!is_array($classesArray['requiredBy'][$requiredClass])) {
						$classesArray['requiredBy'][$requiredClass] = array();
					}
					if (!in_array($className, $classesArray['requiredBy'][$requiredClass])) {
						$classesArray['requiredBy'][$requiredClass][] = $className;
					}
				}
			}
		}
		// Scanning the list of sets of mutually exclusives classes if specified in the RTE config
		if (is_array($RTEProperties['mutuallyExclusiveClasses.'])) {
			foreach ($RTEProperties['mutuallyExclusiveClasses.'] as $listName => $conf) {
				$classSet = GeneralUtility::trimExplode(',', $conf, TRUE);
				$classList = implode(',', $classSet);
				foreach ($classSet as $className) {
					$classesArray['XOR'][$className] = '/^(' . implode('|', GeneralUtility::trimExplode(',', GeneralUtility::rmFromList($className, $classList), TRUE)) . ')$/';
				}
			}
		}
		foreach ($classesArray as $key => $subArray) {
			$JSClassesArray .= 'HTMLArea.classes' . ucfirst($key) . ' = ' . $this->buildNestedJSArray($subArray) . ';' . LF;
		}
		return $JSClassesArray;
	}

	/**
	 * Check for possible circularity in classes dependencies
	 *
	 * @param array $classesArray : reference to the array of classes dependencies
	 * @param string $requiringClass : class requiring at some iteration level from the initial requiring class
	 * @param string $initialClass : initial class from which a circular relationship is being searched
	 * @param int $recursionLevel : depth of recursive call
	 * @return bool TRUE, if a circular relationship is found
	 */
	protected function hasCircularDependency(&$classesArray, $requiringClass, $initialClass, $recursionLevel = 0) {
		if (is_array($classesArray['requires'][$requiringClass])) {
			if (in_array($initialClass, $classesArray['requires'][$requiringClass])) {
				return TRUE;
			} else {
				if ($recursionLevel++ < 20) {
					foreach ($classesArray['requires'][$requiringClass] as $requiringClass2) {
						if ($this->hasCircularDependency($classesArray, $requiringClass2, $initialClass, $recursionLevel)) {
							return TRUE;
						}
					}
				}
				return FALSE;
			}
		} else {
			return FALSE;
Marco Huber's avatar
Marco Huber committed
938
		}
939
	}
Marco Huber's avatar
Marco Huber committed
940

941
942
943
944
945
946
947
948
949
950
951
952
953
954
	/**
	 * Translate Page TS Config array in JS nested array definition
	 * Replace 0 values with false
	 * Unquote regular expression values
	 * Replace empty arrays with empty objects
	 *
	 * @param array $conf : Page TSConfig configuration array
	 * @return string nested JS array definition
	 */
	protected function buildNestedJSArray($conf) {
		$convertedConf = GeneralUtility::removeDotsFromTS($conf);
		return str_replace(
			array(':"0"', ':"\\/^(', ')$\\/i"', ':"\\/^(', ')$\\/"', '[]'),
			array(':false', ':/^(', ')$/i', ':/^(', ')$/', '{}'), json_encode($convertedConf)
Marco Huber's avatar
Marco Huber committed
955
		);
956
	}
Marco Huber's avatar
Marco Huber committed
957

958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
	/**
	 * Writes contents in a file in typo3temp and returns the file name
	 *
	 * @param string $label : A label to insert at the beginning of the name of the file
	 * @param string $fileExtension : The file extension of the file, defaulting to 'js'
	 * @param string $contents : The contents to write into the file
	 * @return string The name of the file written to typo3temp
	 * @throws \RuntimeException If writing to file failed
	 */
	protected function writeTemporaryFile($label, $fileExtension = 'js', $contents = '') {
		$relativeFilename = 'typo3temp/Tinymce4Rte/' . str_replace('-', '_', $label) . '_' . GeneralUtility::shortMD5($contents, 20) . '.' . $fileExtension;
		$destination = PATH_site . $relativeFilename;
		if (!file_exists($destination)) {
			$minifiedJavaScript = '';
			if ($fileExtension === 'js' && $contents !== '') {
				$minifiedJavaScript = GeneralUtility::minifyJavaScript($contents);
			}
			$failure = GeneralUtility::writeFileToTypo3tempDir($destination, $minifiedJavaScript ? $minifiedJavaScript : $contents);
			if ($failure) {
				throw new \RuntimeException($failure, 1294585668);
			}
		}
		if (isset($GLOBALS['TSFE'])) {
			$fileName = $relativeFilename;
		} else {
			$fileName = '../' . $relativeFilename;
		}
		return GeneralUtility::resolveBackPath($fileName);
	}

	/**
	 * Both rte framework and rte plugins can have label files that are
	 * used in JS. The methods gathers those and creates a JS object from
	 * file labels.
	 *
	 * @return string
	 */
	protected function createJavaScriptLanguageLabelsFromFiles() {
		$labelArray = array();
		// Load labels of 3 base files into JS
		foreach (array('tooltips', 'msg', 'dialogs') as $identifier) {
			$fileName = 'EXT:tinymce4_rte/Resources/Private/Language/locallang_' . $identifier . '.xlf';
			$newLabels = $this->getMergedLabelsFromFile($fileName);
For faster browsing, not all history is shown. View entire blame