Skip to content

Commit 184e767

Browse files
authored
SR-307 Color name resolving fix
1 parent f3ee0a6 commit 184e767

File tree

1 file changed

+177
-0
lines changed

1 file changed

+177
-0
lines changed

lib/Sabberworm/CSS/Parser.php

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,9 +499,17 @@ private function parseColorValue() {
499499
if ($this->comes('#')) {
500500
$this->consume('#');
501501
$sValue = $this->parseIdentifier(false);
502+
503+
502504
if ($this->strlen($sValue) === 3) {
503505
$sValue = $sValue[0] . $sValue[0] . $sValue[1] . $sValue[1] . $sValue[2] . $sValue[2];
504506
}
507+
508+
if ($this->isColorName($sValue)) {
509+
$sValue = $this->resolveColor($sValue);
510+
}
511+
512+
505513
$aColor = array('r' => new Size(intval($sValue[0] . $sValue[1], 16), null, true, $this->iLineNo), 'g' => new Size(intval($sValue[2] . $sValue[3], 16), null, true, $this->iLineNo), 'b' => new Size(intval($sValue[4] . $sValue[5], 16), null, true, $this->iLineNo));
506514
} else {
507515
$sColorMode = $this->parseIdentifier(false);
@@ -747,4 +755,173 @@ private function strpos($sString, $sNeedle, $iOffset) {
747755
}
748756
}
749757

758+
private function getColorNameList() {
759+
760+
return [
761+
'black' => '000000',
762+
'navy' => '000080',
763+
'darkblue' => '00008b',
764+
'mediumblue' => '0000cd',
765+
'blue' => '0000ff',
766+
'darkgreen' => '006400',
767+
'green' => '008000',
768+
'teal' => '008080',
769+
'darkcyan' => '008b8b',
770+
'deepskyblue' => '00bfff',
771+
'darkturquoise' => '00ced1',
772+
'mediumspringgreen' => '00fa9a',
773+
'lime' => '00ff00',
774+
'springgreen' => '00ff7f',
775+
'aqua' => '00ffff',
776+
'cyan' => '00ffff',
777+
'midnightblue' => '191970',
778+
'dodgerblue' => '1e90ff',
779+
'lightseagreen' => '20b2aa',
780+
'forestgreen' => '228b22',
781+
'seagreen' => '2e8b57',
782+
'darkslategray' => '2f4f4f',
783+
'darkslategrey' => '2f4f4f',
784+
'limegreen' => '32cd32',
785+
'mediumseagreen' => '3cb371',
786+
'turquoise' => '40e0d0',
787+
'royalblue' => '4169e1',
788+
'steelblue' => '4682b4',
789+
'darkslateblue' => '483d8b',
790+
'mediumturquoise' => '48d1cc',
791+
'indigo ' => '4b0082',
792+
'darkolivegreen' => '556b2f',
793+
'cadetblue' => '5f9ea0',
794+
'cornflowerblue' => '6495ed',
795+
'rebeccapurple' => '663399',
796+
'mediumaquamarine' => '66cdaa',
797+
'dimgray' => '696969',
798+
'dimgrey' => '696969',
799+
'slateblue' => '6a5acd',
800+
'olivedrab' => '6b8e23',
801+
'slategray' => '708090',
802+
'slategrey' => '708090',
803+
'lightslategray' => '778899',
804+
'lightslategrey' => '778899',
805+
'mediumslateblue' => '7b68ee',
806+
'lawngreen' => '7cfc00',
807+
'chartreuse' => '7fff00',
808+
'aquamarine' => '7fffd4',
809+
'maroon' => '800000',
810+
'purple' => '800080',
811+
'olive' => '808000',
812+
'gray' => '808080',
813+
'grey' => '808080',
814+
'skyblue' => '87ceeb',
815+
'lightskyblue' => '87cefa',
816+
'blueviolet' => '8a2be2',
817+
'darkred' => '8b0000',
818+
'darkmagenta' => '8b008b',
819+
'saddlebrown' => '8b4513',
820+
'darkseagreen' => '8fbc8f',
821+
'lightgreen' => '90ee90',
822+
'mediumpurple' => '9370db',
823+
'darkviolet' => '9400d3',
824+
'palegreen' => '98fb98',
825+
'darkorchid' => '9932cc',
826+
'yellowgreen' => '9acd32',
827+
'sienna' => 'a0522d',
828+
'brown' => 'a52a2a',
829+
'darkgray' => 'a9a9a9',
830+
'darkgrey' => 'a9a9a9',
831+
'lightblue' => 'add8e6',
832+
'greenyellow' => 'adff2f',
833+
'paleturquoise' => 'afeeee',
834+
'lightsteelblue' => 'b0c4de',
835+
'powderblue' => 'b0e0e6',
836+
'firebrick' => 'b22222',
837+
'darkgoldenrod' => 'b8860b',
838+
'mediumorchid' => 'ba55d3',
839+
'rosybrown' => 'bc8f8f',
840+
'darkkhaki' => 'bdb76b',
841+
'silver' => 'c0c0c0',
842+
'mediumvioletred' => 'c71585',
843+
'indianred ' => 'cd5c5c',
844+
'peru' => 'cd853f',
845+
'chocolate' => 'd2691e',
846+
'tan' => 'd2b48c',
847+
'lightgray' => 'd3d3d3',
848+
'lightgrey' => 'd3d3d3',
849+
'thistle' => 'd8bfd8',
850+
'orchid' => 'da70d6',
851+
'goldenrod' => 'daa520',
852+
'palevioletred' => 'db7093',
853+
'crimson' => 'dc143c',
854+
'gainsboro' => 'dcdcdc',
855+
'plum' => 'dda0dd',
856+
'burlywood' => 'deb887',
857+
'lightcyan' => 'e0ffff',
858+
'lavender' => 'e6e6fa',
859+
'darksalmon' => 'e9967a',
860+
'violet' => 'ee82ee',
861+
'palegoldenrod' => 'eee8aa',
862+
'lightcoral' => 'f08080',
863+
'khaki' => 'f0e68c',
864+
'aliceblue' => 'f0f8ff',
865+
'honeydew' => 'f0fff0',
866+
'azure' => 'f0ffff',
867+
'sandybrown' => 'f4a460',
868+
'wheat' => 'f5deb3',
869+
'beige' => 'f5f5dc',
870+
'whitesmoke' => 'f5f5f5',
871+
'mintcream' => 'f5fffa',
872+
'ghostwhite' => 'f8f8ff',
873+
'salmon' => 'fa8072',
874+
'antiquewhite' => 'faebd7',
875+
'linen' => 'faf0e6',
876+
'lightgoldenrodyellow' => 'fafad2',
877+
'oldlace' => 'fdf5e6',
878+
'red' => 'ff0000',
879+
'fuchsia' => 'ff00ff',
880+
'magenta' => 'ff00ff',
881+
'deeppink' => 'ff1493',
882+
'orangered' => 'ff4500',
883+
'tomato' => 'ff6347',
884+
'hotpink' => 'ff69b4',
885+
'coral' => 'ff7f50',
886+
'darkorange' => 'ff8c00',
887+
'lightsalmon' => 'ffa07a',
888+
'orange' => 'ffa500',
889+
'lightpink' => 'ffb6c1',
890+
'pink' => 'ffc0cb',
891+
'gold' => 'ffd700',
892+
'peachpuff' => 'ffdab9',
893+
'navajowhite' => 'ffdead',
894+
'moccasin' => 'ffe4b5',
895+
'bisque' => 'ffe4c4',
896+
'mistyrose' => 'ffe4e1',
897+
'blanchedalmond' => 'ffebcd',
898+
'papayawhip' => 'ffefd5',
899+
'lavenderblush' => 'fff0f5',
900+
'seashell' => 'fff5ee',
901+
'cornsilk' => 'fff8dc',
902+
'lemonchiffon' => 'fffacd',
903+
'floralwhite' => 'fffaf0',
904+
'snow' => 'fffafa',
905+
'yellow' => 'ffff00',
906+
'lightyellow' => 'ffffe0',
907+
'ivory' => 'fffff0',
908+
'white' => 'ffffff'
909+
];
910+
911+
}
912+
913+
private function isColorName($color) {
914+
915+
$colors = $this->getColorNameList();
916+
return array_key_exists(strtolower($color), $colors);
917+
918+
}
919+
920+
private function resolveColor($color) {
921+
922+
$colors = $this->getColorNameList();
923+
return $colors[strtolower($color)];
924+
925+
}
926+
750927
}

0 commit comments

Comments
 (0)