BUG fix missing language on non-global configs

BUG Prevent external plugins from being passed to the minifier
API Change TinyMCEConfig::requireJS to getScriptURL() for testabilitiy
This commit is contained in:
Damian Mooyman 2016-04-04 09:59:31 +12:00
parent 6926e85fff
commit 640691f544
2 changed files with 56 additions and 21 deletions

View File

@ -34,6 +34,7 @@ class TinyMCEConfig extends HtmlEditorConfig {
'remove_script_host' => true, 'remove_script_host' => true,
'convert_urls' => false, // Prevent site-root images being rewritten to base relative 'convert_urls' => false, // Prevent site-root images being rewritten to base relative
'menubar' => false, 'menubar' => false,
'language' => 'en',
); );
/** /**
@ -198,6 +199,24 @@ class TinyMCEConfig extends HtmlEditorConfig {
return $this->plugins; return $this->plugins;
} }
/**
* Get list of plugins without custom locations, which is the set of
* plugins which can be loaded via the standard plugin path, and could
* potentially be minified
*
* @return array
*/
public function getInternalPlugins() {
// Return only plugins with no custom url
$plugins = [];
foreach($this->getPlugins() as $name => $url) {
if(empty($url)) {
$plugins[] = $name;
}
}
return $plugins;
}
/** /**
* Get all button rows, skipping empty rows * Get all button rows, skipping empty rows
* *
@ -416,31 +435,26 @@ class TinyMCEConfig extends HtmlEditorConfig {
* Generate gzipped TinyMCE configuration including plugins and languages. * Generate gzipped TinyMCE configuration including plugins and languages.
* This ends up "pre-loading" TinyMCE bundled with the required plugins * This ends up "pre-loading" TinyMCE bundled with the required plugins
* so that multiple HTTP requests on the client don't need to be made. * so that multiple HTTP requests on the client don't need to be made.
*
* @return string
*/ */
public function requireJS() { public function getScriptURL() {
require_once THIRDPARTY_PATH . '/tinymce/tiny_mce_gzip.php'; // If gzip is disabled just return core script url
$useGzip = Config::inst()->get('HtmlEditorField', 'use_gzip'); $useGzip = Config::inst()->get('HtmlEditorField', 'use_gzip');
$languages = array(); if(!$useGzip) {
return THIRDPARTY_DIR . '/tinymce/tinymce.min.js';
foreach(self::$configs as $configID => $config) {
$languages[] = $config->getOption('language');
} }
// tinyMCE JS requirement // tinyMCE JS requirement
if($useGzip) { require_once THIRDPARTY_PATH . '/tinymce/tiny_mce_gzip.php';
$tag = TinyMCE_Compressor::renderTag(array( $tag = TinyMCE_Compressor::renderTag(array(
'url' => THIRDPARTY_DIR . '/tinymce/tiny_mce_gzip.php', 'url' => THIRDPARTY_DIR . '/tinymce/tiny_mce_gzip.php',
'plugins' => implode(',', array_keys($this->getPlugins())), 'plugins' => implode(',', $this->getInternalPlugins()),
'themes' => $this->getTheme(), 'themes' => $this->getTheme(),
'languages' => implode(',', array_filter($languages)) 'languages' => $this->getOption('language')
), true); ), true);
preg_match('/src="([^"]*)"/', $tag, $matches); preg_match('/src="([^"]*)"/', $tag, $matches);
return html_entity_decode($matches[1]);
Requirements::javascript(html_entity_decode($matches[1]));
} else {
Requirements::javascript(THIRDPARTY_DIR . '/tinymce/tinymce.min.js');
}
} }
public function init() { public function init() {
@ -451,7 +465,7 @@ class TinyMCEConfig extends HtmlEditorConfig {
Requirements::javascript(FRAMEWORK_ADMIN_DIR . '/javascript/dist/ssui.core.js'); Requirements::javascript(FRAMEWORK_ADMIN_DIR . '/javascript/dist/ssui.core.js');
// include TinyMCE Javascript // include TinyMCE Javascript
$this->requireJS(); Requirements::javascript($this->getScriptURL());
Requirements::javascript(FRAMEWORK_DIR ."/javascript/dist/HtmlEditorField.js"); Requirements::javascript(FRAMEWORK_DIR ."/javascript/dist/HtmlEditorField.js");
Requirements::css(THIRDPARTY_DIR . '/jquery-ui-themes/smoothness/jquery-ui.css'); Requirements::css(THIRDPARTY_DIR . '/jquery-ui-themes/smoothness/jquery-ui.css');

View File

@ -28,11 +28,15 @@ class HtmlEditorConfigTest extends SapphireTest {
public function testEnablePluginsByArrayWithPaths() { public function testEnablePluginsByArrayWithPaths() {
Config::inst()->update('Director', 'alternate_base_url', 'http://mysite.com/subdir'); Config::inst()->update('Director', 'alternate_base_url', 'http://mysite.com/subdir');
$c = new TinyMCEConfig(); $c = new TinyMCEConfig();
$c->setTheme('modern');
$c->setOption('language', 'es');
$c->disablePlugins('table', 'emoticons', 'paste', 'code', 'link', 'importcss');
$c->enablePlugins(array( $c->enablePlugins(array(
'plugin1' => 'mypath/plugin1.js', 'plugin1' => 'mypath/plugin1.js',
'plugin2' => '/anotherbase/mypath/plugin2.js', 'plugin2' => '/anotherbase/mypath/plugin2.js',
'plugin3' => 'https://www.google.com/plugin.js', 'plugin3' => 'https://www.google.com/plugin.js',
'plugin4' => null, 'plugin4' => null,
'plugin5' => null,
)); ));
$attributes = $c->getAttributes(); $attributes = $c->getAttributes();
$config = Convert::json2array($attributes['data-config']); $config = Convert::json2array($attributes['data-config']);
@ -66,6 +70,23 @@ class HtmlEditorConfigTest extends SapphireTest {
'http://mysite.com/subdir/framework/thirdparty/tinymce/plugins/plugin4/plugin.min.js', 'http://mysite.com/subdir/framework/thirdparty/tinymce/plugins/plugin4/plugin.min.js',
$plugins['plugin4'] $plugins['plugin4']
); );
// Check that internal plugins are extractable separately
$this->assertEquals(['plugin4', 'plugin5'], $c->getInternalPlugins());
// Test plugins included via gzip compresser
Config::inst()->update('HtmlEditorField', 'use_gzip', true);
$this->assertEquals(
'framework/thirdparty/tinymce/tiny_mce_gzip.php?js=1&plugins=plugin4,plugin5&themes=modern&languages=es&diskcache=true&src=true',
$c->getScriptURL()
);
// If gzip is disabled only the core plugin is loaded
Config::inst()->remove('HtmlEditorField', 'use_gzip');
$this->assertEquals(
'framework/thirdparty/tinymce/tinymce.min.js',
$c->getScriptURL()
);
} }
public function testDisablePluginsByString() { public function testDisablePluginsByString() {