From b5984db716301e7d685554c0aeb6024a129cc444 Mon Sep 17 00:00:00 2001 From: MatmaRex Date: Thu, 6 Jun 2013 17:33:37 +0200 Subject: [PATCH] CSSJanus: Fix handling of CSS3 color syntaxes The 'color' rule, and by extension 'four_notation_color' too, only understood #rrggbb and named colors. Extend it to match rgb[a](...) and hsl[a](...) syntaxes as well. This makes usage of rgb(a)/hsl(a) syntax in declarations like 'border-color: a b c d;' be interpreted and flipped correctly. Change-Id: I218a9aa55a86b3d955b92375c1a209fdde312138 --- RELEASE-NOTES-1.22 | 1 + includes/libs/CSSJanus.php | 2 +- tests/phpunit/includes/libs/CSSJanusTest.php | 15 +++++++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/RELEASE-NOTES-1.22 b/RELEASE-NOTES-1.22 index 8bf680b276..63453c5d9e 100644 --- a/RELEASE-NOTES-1.22 +++ b/RELEASE-NOTES-1.22 @@ -169,6 +169,7 @@ production. backwards-compatible in all reasonable cases (uses of the __TOC__ magic word, the #toc CSS id and the .toc CSS class). However, particularly bad abuse of the id or the class can possibly break. +* CSSJanus now supports rgb, hsl, rgba, and hsla color syntaxes. === API changes in 1.22 === * (bug 25553) The JSON output formatter now leaves forward slashes unescaped diff --git a/includes/libs/CSSJanus.php b/includes/libs/CSSJanus.php index ac33448322..46236cd73b 100644 --- a/includes/libs/CSSJanus.php +++ b/includes/libs/CSSJanus.php @@ -96,7 +96,7 @@ class CSSJanus { $patterns['ident'] = "-?{$patterns['nmstart']}{$patterns['nmchar']}*"; $patterns['quantity'] = "{$patterns['num']}(?:\s*{$patterns['unit']}|{$patterns['ident']})?"; $patterns['possibly_negative_quantity'] = "((?:-?{$patterns['quantity']})|(?:inherit|auto))"; - $patterns['color'] = "(#?{$patterns['nmchar']}+)"; + $patterns['color'] = "(#?{$patterns['nmchar']}+|(?:rgba?|hsla?)\([ \d.,%-]+\))"; $patterns['url_chars'] = "(?:{$patterns['url_special_chars']}|{$patterns['nonAscii']}|{$patterns['escape']})*"; $patterns['lookahead_not_open_brace'] = "(?!({$patterns['nmchar']}|\r?\n|\s|#|\:|\.|\,|\+|>)*?{)"; $patterns['lookahead_not_closing_paren'] = "(?!{$patterns['url_chars']}?{$patterns['valid_after_uri_chars']}\))"; diff --git a/tests/phpunit/includes/libs/CSSJanusTest.php b/tests/phpunit/includes/libs/CSSJanusTest.php index a1b87f63ae..e6432a935e 100644 --- a/tests/phpunit/includes/libs/CSSJanusTest.php +++ b/tests/phpunit/includes/libs/CSSJanusTest.php @@ -150,6 +150,21 @@ class CSSJanusTest extends MediaWikiTestCase { array( '#settings td p strong' ), + array( + // Color names + '.foo { border-color: red green blue white }', + '.foo { border-color: red white blue green }', + ), + array( + // Color name, hexdecimal, RGB & RGBA + '.foo { border-color: red #f00 rgb(255, 0, 0) rgba(255, 0, 0, 0.5) }', + '.foo { border-color: red rgba(255, 0, 0, 0.5) rgb(255, 0, 0) #f00 }', + ), + array( + // Color name, hexdecimal, HSL & HSLA + '.foo { border-color: red #f00 hsl(0, 100%, 50%) hsla(0, 100%, 50%, 0.5) }', + '.foo { border-color: red hsla(0, 100%, 50%, 0.5) hsl(0, 100%, 50%) #f00 }', + ), array( // Do not mangle 5 or more values '.foo { -x-unknown: 1 2 3 4 5; }' -- 2.20.1