From b3aefe3cc85bc9c941449c1f10ff9fce02b57a5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20S=CC=8Ckoda?= Date: Tue, 21 Aug 2012 10:07:03 +0200 Subject: [PATCH] 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. --- lib/editor/tinymce/lib.php | 35 ++++++++++++---- lib/editor/tinymce/tests/editor_test.php | 53 ++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 7 deletions(-) create mode 100644 lib/editor/tinymce/tests/editor_test.php diff --git a/lib/editor/tinymce/lib.php b/lib/editor/tinymce/lib.php index 6ffef3927ad..73f01f774b1 100644 --- a/lib/editor/tinymce/lib.php +++ b/lib/editor/tinymce/lib.php @@ -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. diff --git a/lib/editor/tinymce/tests/editor_test.php b/lib/editor/tinymce/tests/editor_test.php new file mode 100644 index 00000000000..27db61bb075 --- /dev/null +++ b/lib/editor/tinymce/tests/editor_test.php @@ -0,0 +1,53 @@ +. + +/** + * 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); + } +}