MDL-34990 improve custom toolbar setting parsing
It is probably better to parse the setting every time because somebody may put unsupported values directly into config.php, performance should not be an issue because we do not have editors on every page.
This commit is contained in:
@@ -186,19 +186,14 @@ class tinymce_texteditor extends texteditor {
|
||||
editor_tinymce_plugin::all_update_init_params($params, $context, $options);
|
||||
|
||||
// Should we override the default toolbar layout unconditionally?
|
||||
$customtoolbar = trim($config->customtoolbar);
|
||||
$customtoolbar = self::parse_toolbar_setting($config->customtoolbar);
|
||||
if ($customtoolbar) {
|
||||
unset($params['theme_advanced_buttons1']);
|
||||
unset($params['theme_advanced_buttons2']);
|
||||
unset($params['theme_advanced_buttons3']);
|
||||
unset($params['theme_advanced_buttons4']);
|
||||
$customtoolbar = str_replace("\r", "\n", $customtoolbar);
|
||||
$i = 1;
|
||||
foreach (explode("\n", $customtoolbar) as $line) {
|
||||
$line = preg_replace('/\s/', '', $line);
|
||||
if ($line === '') {
|
||||
continue;
|
||||
}
|
||||
foreach ($customtoolbar as $line) {
|
||||
$params['theme_advanced_buttons'.$i] = $line;
|
||||
$i++;
|
||||
}
|
||||
@@ -210,6 +205,32 @@ class tinymce_texteditor extends texteditor {
|
||||
return $params;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the custom toolbar setting.
|
||||
* @param string $customtoolbar
|
||||
* @return array csv toolbar lines
|
||||
*/
|
||||
public static function parse_toolbar_setting($customtoolbar) {
|
||||
$result = array();
|
||||
$customtoolbar = trim($customtoolbar);
|
||||
if ($customtoolbar === '') {
|
||||
return $result;
|
||||
}
|
||||
$customtoolbar = str_replace("\r", "\n", $customtoolbar);
|
||||
$customtoolbar = strtolower($customtoolbar);
|
||||
foreach (explode("\n", $customtoolbar) as $line) {
|
||||
$line = preg_replace('/[^a-z0-9_,\|\-]/', ',', $line);
|
||||
$line = str_replace('|', ',|,', $line);
|
||||
$line = preg_replace('/,,+/', ',', $line);
|
||||
$line = trim($line, ',|');
|
||||
if ($line === '') {
|
||||
continue;
|
||||
}
|
||||
$result[] = $line;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a named plugin object. Will cause fatal error if plugin doesn't
|
||||
* exist. This is intended for use by plugin files themselves.
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* TinyMCE tests.
|
||||
*
|
||||
* @package editor_tinymce
|
||||
* @category phpunit
|
||||
* @copyright 2012 Petr Skoda {@link http://skodak.org}
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
|
||||
/**
|
||||
* TinyMCE tests.
|
||||
*
|
||||
* @package editor_tinymce
|
||||
* @category phpunit
|
||||
* @copyright 2012 Petr Skoda {@link http://skodak.org}
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class editor_tinymce_testcase extends advanced_testcase {
|
||||
|
||||
public function test_toolbar_parsing() {
|
||||
global $CFG;
|
||||
require_once("$CFG->dirroot/lib/editorlib.php");
|
||||
require_once("$CFG->dirroot/lib/editor/tinymce/lib.php");
|
||||
|
||||
$result = tinymce_texteditor::parse_toolbar_setting("bold,italic\npreview");
|
||||
$this->assertSame(array('bold,italic', 'preview'), $result);
|
||||
|
||||
$result = tinymce_texteditor::parse_toolbar_setting("| bold,|italic*blink\rpreview\n\n| \n paste STYLE | ");
|
||||
$this->assertSame(array('bold,|,italic,blink', 'preview', 'paste,style'), $result);
|
||||
|
||||
$result = tinymce_texteditor::parse_toolbar_setting("| \n\n| \n \r");
|
||||
$this->assertSame(array(), $result);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user