diff options
-rw-r--r-- | lib/plugins/styling/_test/colors.test.php | 72 | ||||
-rw-r--r-- | lib/plugins/styling/admin.php | 43 | ||||
-rw-r--r-- | lib/plugins/styling/plugin.info.txt | 2 |
3 files changed, 94 insertions, 23 deletions
diff --git a/lib/plugins/styling/_test/colors.test.php b/lib/plugins/styling/_test/colors.test.php new file mode 100644 index 000000000..5f5425437 --- /dev/null +++ b/lib/plugins/styling/_test/colors.test.php @@ -0,0 +1,72 @@ +<?php + +/** + * Color handling tests for the styling plugin + * + * @group plugin_styling + * @group plugins + */ +class colors_plugin_styling_test extends DokuWikiTest +{ + + /** + * @return array + * @see testColorType + */ + public function provideColorType() + { + return [ + ['foobar', 'text'], + ['white', 'text'], + ['#fff', 'color'], + ['#f0f0f0', 'color'], + ['#f0f0', 'text'], + ['some #f0f0f0 color', 'text'], + ]; + } + + /** + * @param string $input + * @param string $expect + * @dataProvider provideColorType + * @noinspection PhpUnhandledExceptionInspection + * @noinspection PhpDocMissingThrowsInspection + */ + public function testColorType($input, $expect) + { + $plugin = new admin_plugin_styling(); + $output = $this->callInaccessibleMethod($plugin, 'colorType', [$input]); + $this->assertEquals($expect, $output); + } + + /** + * @return array + * @see testColorValue + */ + public function provideColorValue() + { + return [ + ['foobar', 'foobar'], + ['white', 'white'], + ['#fff', '#ffffff'], + ['#123', '#112233'], + ['#f0f0f0', '#f0f0f0'], + ['#f0f0', '#f0f0'], + ['some #f0f0f0 color', 'some #f0f0f0 color'], + ]; + } + + /** + * @param string $input + * @param string $expect + * @dataProvider provideColorValue + * @noinspection PhpUnhandledExceptionInspection + * @noinspection PhpDocMissingThrowsInspection + */ + public function testColorValue($input, $expect) + { + $plugin = new admin_plugin_styling(); + $output = $this->callInaccessibleMethod($plugin, 'colorValue', [$input]); + $this->assertEquals($expect, $output); + } +} diff --git a/lib/plugins/styling/admin.php b/lib/plugins/styling/admin.php index 70c0572ea..d454422ef 100644 --- a/lib/plugins/styling/admin.php +++ b/lib/plugins/styling/admin.php @@ -60,7 +60,7 @@ class admin_plugin_styling extends DokuWiki_Admin_Plugin global $conf; global $ID; - $styleUtil = new \dokuwiki\StyleUtils($conf['template'], true); + $styleUtil = new \dokuwiki\StyleUtils($conf['template'], true, true); $styleini = $styleUtil->cssStyleini(); $replacements = $styleini['replacements']; @@ -85,8 +85,8 @@ class admin_plugin_styling extends DokuWiki_Admin_Plugin echo '<tr>'; echo '<td><label for="tpl__'.hsc($key).'">'.$name.'</label></td>'; - echo '<td><input type="'.$this->colorType($key).'" name="tpl['.hsc($key).']" id="tpl__'.hsc($key).'" - value="'.hsc($value).'" dir="ltr" /></td>'; + echo '<td><input type="'.$this->colorType($value).'" name="tpl['.hsc($key).']" id="tpl__'.hsc($key).'" + value="'.hsc($this->colorValue($value)).'" dir="ltr" /></td>'; echo '</tr>'; } echo '</tbody></table>'; @@ -114,29 +114,28 @@ class admin_plugin_styling extends DokuWiki_Admin_Plugin } /** - * Decide the input type based on the key name + * Adjust three char color codes to the 6 char one supported by browser's color input * - * @param string $key + * @param string $value + * @return string + */ + protected function colorValue($value) + { + if (preg_match('/^#([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])$/', $value, $match)) { + return '#' . $match[1] . $match[1] . $match[2] . $match[2] . $match[3] . $match[3]; + } + return $value; + } + + /** + * Decide the input type based on the value + * + * @param string $value * @return string color|text */ - protected function colorType($key) + protected function colorType($value) { - static $colors = array( - 'text', - 'background', - 'text_alt', - 'background_alt', - 'text_neu', - 'background_neu', - 'border', - 'highlight', - 'background_site', - 'link', - 'existing', - 'missing', - ); - - if (preg_match('/colou?r/', $key) || in_array(trim($key, '_'), $colors)) { + if (preg_match('/^#([0-9a-fA-F]{3}){1,2}$/', $value)) { return 'color'; } else { return 'text'; diff --git a/lib/plugins/styling/plugin.info.txt b/lib/plugins/styling/plugin.info.txt index 9f002e282..e374eaf54 100644 --- a/lib/plugins/styling/plugin.info.txt +++ b/lib/plugins/styling/plugin.info.txt @@ -1,7 +1,7 @@ base styling author Andreas Gohr email andi@splitbrain.org -date 2015-07-26 +date 2020-06-14 name styling plugin desc Allows to edit style.ini replacements url https://www.dokuwiki.org/plugin:styling |