From ffe39d7f3015ded9f31b5f0fac1d9bd0159c5388 Mon Sep 17 00:00:00 2001 From: "Edward Z. Yang" Date: Sun, 27 Aug 2006 00:35:57 +0000 Subject: [PATCH] Basic color keywords translated into hexadecimal values. git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@323 48356398-32a2-884e-a903-53898d9a118a --- NEWS | 1 + library/HTMLPurifier/AttrDef/Color.php | 27 ++++++++++++++++++++++++ tests/HTMLPurifier/AttrDef/ColorTest.php | 3 +++ 3 files changed, 31 insertions(+) diff --git a/NEWS b/NEWS index db0ba917..76930cd7 100644 --- a/NEWS +++ b/NEWS @@ -6,6 +6,7 @@ NEWS ( CHANGELOG and HISTORY ) HTMLPurifier - Malformed UTF-8 and non-SGML character detection and cleaning implemented - API documentation completed - Shorthand CSS properties implemented: font +- Basic color keywords translated into hexadecimal values 1.0.0beta, released 2006-08-16 - First public release, most functionality implemented. Notable omissions are: diff --git a/library/HTMLPurifier/AttrDef/Color.php b/library/HTMLPurifier/AttrDef/Color.php index 506be5b8..3948a19c 100644 --- a/library/HTMLPurifier/AttrDef/Color.php +++ b/library/HTMLPurifier/AttrDef/Color.php @@ -8,11 +8,38 @@ require_once 'HTMLPurifier/AttrDef.php'; class HTMLPurifier_AttrDef_Color extends HTMLPurifier_AttrDef { + /** + * Color keyword lookup table. + * @todo Extend it to include all usually allowed colors. + */ + var $colors = array( + 'maroon' => '#800000', + 'red' => '#F00', + 'orange' => '#FFA500', + 'yellow' => '#FF0', + 'olive' => '#808000', + 'purple' => '#800080', + 'fuchsia' => '#F0F', + 'white' => '#FFF', + 'lime' => '#0F0', + 'green' => '#008000', + 'navy' => '#000080', + 'blue' => '#00F', + 'aqua' => '#0FF', + 'teal' => '#008080', + 'black' => '#000', + 'silver' => '#C0C0C0', + 'gray' => '#808080' + ); + function validate($color, $config, &$context) { $color = trim($color); if (!$color) return false; + $lower = strtolower($color); + if (isset($this->colors[$lower])) return $this->colors[$lower]; + if ($color[0] === '#') { // hexadecimal handling $hex = substr($color, 1); diff --git a/tests/HTMLPurifier/AttrDef/ColorTest.php b/tests/HTMLPurifier/AttrDef/ColorTest.php index 84ddff17..a38c551b 100644 --- a/tests/HTMLPurifier/AttrDef/ColorTest.php +++ b/tests/HTMLPurifier/AttrDef/ColorTest.php @@ -23,6 +23,9 @@ class HTMLPurifier_AttrDef_ColorTest extends HTMLPurifier_AttrDefHarness $this->assertDef('rgb(200%, -10%, 0%)', 'rgb(100%,0%,0%)'); $this->assertDef('rgb(256,-23,34)', 'rgb(255,0,34)'); + // color keywords, of course + $this->assertDef('red', '#F00'); + // maybe hex transformations would be another nice feature // at the very least transform rgb percent to rgb integer