Support extended color names in HTML 5 validation (#914)

Fixes #908
This commit is contained in:
Caleb Callaway 2020-11-30 07:24:12 -08:00 committed by GitHub
parent 4cf169360a
commit ba69b9e7cf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 444 additions and 33 deletions

View file

@ -64,22 +64,154 @@ static const ctmbstr frameExtensions[] =
/* List of possible colour values */
static const int colorValues[][3] =
{
{ 0, 0, 0},
{128,128,128},
{192,192,192},
{255,255,255},
{192, 0, 0},
{255, 0, 0},
{128, 0,128},
{255, 0,255},
{ 0,128, 0},
{ 0,255, 0},
{128,128, 0},
{255,255, 0},
{ 0, 0,128},
{ 0, 0,255},
{ 0,128,128},
{ 0,255,255}
{240, 248, 255 },
{250, 235, 215 },
{0, 255, 255 },
{127, 255, 212 },
{240, 255, 255 },
{245, 245, 220 },
{255, 228, 196 },
{0, 0, 0 },
{255, 235, 205 },
{0, 0, 255 },
{138, 43, 226 },
{165, 42, 42 },
{222, 184, 135 },
{95, 158, 160 },
{127, 255, 0 },
{210, 105, 30 },
{255, 127, 80 },
{100, 149, 237 },
{255, 248, 220 },
{220, 20, 60 },
{0, 255, 255 },
{0, 0, 139 },
{0, 139, 139 },
{184, 134, 11 },
{169, 169, 169 },
{0, 100, 0 },
{169, 169, 169 },
{189, 183, 107 },
{139, 0, 139 },
{85, 107, 47 },
{255, 140, 0 },
{153, 50, 204 },
{139, 0, 0 },
{233, 150, 122 },
{143, 188, 143 },
{72, 61, 139 },
{47, 79, 79 },
{47, 79, 79 },
{0, 206, 209 },
{148, 0, 211 },
{255, 20, 147 },
{0, 191, 255 },
{105, 105, 105 },
{105, 105, 105 },
{30, 144, 255 },
{178, 34, 34 },
{255, 250, 240 },
{34, 139, 34 },
{255, 0, 255 },
{220, 220, 220 },
{248, 248, 255 },
{255, 215, 0 },
{218, 165, 32 },
{128, 128, 128 },
{0, 128, 0 },
{173, 255, 47 },
{128, 128, 128 },
{240, 255, 240 },
{255, 105, 180 },
{205, 92, 92 },
{75, 0, 130 },
{255, 255, 240 },
{240, 230, 140 },
{230, 230, 250 },
{255, 240, 245 },
{124, 252, 0 },
{255, 250, 205 },
{173, 216, 230 },
{240, 128, 128 },
{224, 255, 255 },
{250, 250, 210 },
{211, 211, 211 },
{144, 238, 144 },
{211, 211, 211 },
{255, 182, 193 },
{255, 160, 122 },
{32, 178, 170 },
{135, 206, 250 },
{119, 136, 153 },
{119, 136, 153 },
{176, 196, 222 },
{255, 255, 224 },
{0, 255, 0 },
{50, 205, 50 },
{250, 240, 230 },
{255, 0, 255 },
{128, 0, 0 },
{102, 205, 170 },
{0, 0, 205 },
{186, 85, 211 },
{147, 112, 219 },
{60, 179, 113 },
{123, 104, 238 },
{0, 250, 154 },
{72, 209, 204 },
{199, 21, 133 },
{25, 25, 112 },
{245, 255, 250 },
{255, 228, 225 },
{255, 228, 181 },
{255, 222, 173 },
{0, 0, 128 },
{253, 245, 230 },
{128, 128, 0 },
{107, 142, 35 },
{255, 165, 0 },
{255, 69, 0 },
{218, 112, 214 },
{238, 232, 170 },
{152, 251, 152 },
{175, 238, 238 },
{219, 112, 147 },
{255, 239, 213 },
{255, 218, 185 },
{205, 133, 63 },
{255, 192, 203 },
{221, 160, 221 },
{176, 224, 230 },
{128, 0, 128 },
{102, 51, 153 },
{255, 0, 0 },
{188, 143, 143 },
{65, 105, 225 },
{139, 69, 19 },
{250, 128, 114 },
{244, 164, 96 },
{46, 139, 87 },
{255, 245, 238 },
{160, 82, 45 },
{192, 192, 192 },
{135, 206, 235 },
{106, 90, 205 },
{112, 128, 144 },
{112, 128, 144 },
{255, 250, 250 },
{0, 255, 127 },
{70, 130, 180 },
{210, 180, 140 },
{0, 128, 128 },
{216, 191, 216 },
{255, 99, 71 },
{64, 224, 208 },
{238, 130, 238 },
{245, 222, 179 },
{255, 255, 255 },
{245, 245, 245 },
{255, 255, 0 },
{154, 205, 50 }
};
#define N_COLOR_VALS (sizeof(colorValues)/(sizeof(int[3]))
@ -87,22 +219,154 @@ static const int colorValues[][3] =
/* These arrays are used to convert color names to their RGB values */
static const ctmbstr colorNames[] =
{
"aliceblue",
"antiquewhite",
"aqua",
"aquamarine",
"azure",
"beige",
"bisque",
"black",
"silver",
"grey",
"white",
"maroon",
"red",
"purple",
"fuchsia",
"green",
"lime",
"olive",
"yellow",
"navy",
"blanchedalmond",
"blue",
"blueviolet",
"brown",
"burlywood",
"cadetblue",
"chartreuse",
"chocolate",
"coral",
"cornflowerblue",
"cornsilk",
"crimson",
"cyan",
"darkblue",
"darkcyan",
"darkgoldenrod",
"darkgray",
"darkgreen",
"darkgrey",
"darkkhaki",
"darkmagenta",
"darkolivegreen",
"darkorange",
"darkorchid",
"darkred",
"darksalmon",
"darkseagreen",
"darkslateblue",
"darkslategray",
"darkslategrey",
"darkturquoise",
"darkviolet",
"deeppink",
"deepskyblue",
"dimgray",
"dimgrey",
"dodgerblue",
"firebrick",
"floralwhite",
"forestgreen",
"fuchsia",
"gainsboro",
"ghostwhite",
"gold",
"goldenrod",
"gray",
"green",
"greenyellow",
"grey",
"honeydew",
"hotpink",
"indianred",
"indigo",
"ivory",
"khaki",
"lavender",
"lavenderblush",
"lawngreen",
"lemonchiffon",
"lightblue",
"lightcoral",
"lightcyan",
"lightgoldenrodyellow",
"lightgray",
"lightgreen",
"lightgrey",
"lightpink",
"lightsalmon",
"lightseagreen",
"lightskyblue",
"lightslategray",
"lightslategrey",
"lightsteelblue",
"lightyellow",
"lime",
"limegreen",
"linen",
"magenta",
"maroon",
"mediumaquamarine",
"mediumblue",
"mediumorchid",
"mediumpurple",
"mediumseagreen",
"mediumslateblue",
"mediumspringgreen",
"mediumturquoise",
"mediumvioletred",
"midnightblue",
"mintcream",
"mistyrose",
"moccasin",
"navajowhite",
"navy",
"oldlace",
"olive",
"olivedrab",
"orange",
"orangered",
"orchid",
"palegoldenrod",
"palegreen",
"paleturquoise",
"palevioletred",
"papayawhip",
"peachpuff",
"peru",
"pink",
"plum",
"powderblue",
"purple",
"rebeccapurple",
"red",
"rosybrown",
"royalblue",
"saddlebrown",
"salmon",
"sandybrown",
"seagreen",
"seashell",
"sienna",
"silver",
"skyblue",
"slateblue",
"slategray",
"slategrey",
"snow",
"springgreen",
"steelblue",
"tan",
"teal",
"aqua"
"thistle",
"tomato",
"turquoise",
"violet",
"wheat",
"white",
"whitesmoke",
"yellow",
"yellowgreen",
};
#define N_COLOR_NAMES (sizeof(colorNames)/sizeof(ctmbstr))

View file

@ -589,7 +589,144 @@ static const struct _colors colors[] =
{ NULL, NULL }
};
static ctmbstr GetColorCode(ctmbstr name)
static const struct _colors extended_colors[] =
{
{ "aliceblue", "#f0f8ff" },
{ "antiquewhite", "#faebd7" },
{ "aquamarine", "#7fffd4" },
{ "azure", "#f0ffff" },
{ "beige", "#f5f5dc" },
{ "bisque", "#ffe4c4" },
{ "blanchedalmond", "#ffebcd" },
{ "blueviolet", "#8a2be2" },
{ "brown", "#a52a2a" },
{ "burlywood", "#deb887" },
{ "cadetblue", "#5f9ea0" },
{ "chartreuse", "#7fff00" },
{ "chocolate", "#d2691e" },
{ "coral", "#ff7f50" },
{ "cornflowerblue", "#6495ed" },
{ "cornsilk", "#fff8dc" },
{ "crimson", "#dc143c" },
{ "cyan", "#00ffff" },
{ "darkblue", "#00008b" },
{ "darkcyan", "#008b8b" },
{ "darkgoldenrod", "#b8860b" },
{ "darkgray", "#a9a9a9" },
{ "darkgreen", "#006400" },
{ "darkgrey", "#a9a9a9" },
{ "darkkhaki", "#bdb76b" },
{ "darkmagenta", "#8b008b" },
{ "darkolivegreen", "#556b2f" },
{ "darkorange", "#ff8c00" },
{ "darkorchid", "#9932cc" },
{ "darkred", "#8b0000" },
{ "darksalmon", "#e9967a" },
{ "darkseagreen", "#8fbc8f" },
{ "darkslateblue", "#483d8b" },
{ "darkslategray", "#2f4f4f" },
{ "darkslategrey", "#2f4f4f" },
{ "darkturquoise", "#00ced1" },
{ "darkviolet", "#9400d3" },
{ "deeppink", "#ff1493" },
{ "deepskyblue", "#00bfff" },
{ "dimgray", "#696969" },
{ "dimgrey", "#696969" },
{ "dodgerblue", "#1e90ff" },
{ "firebrick", "#b22222" },
{ "floralwhite", "#fffaf0" },
{ "forestgreen", "#228b22" },
{ "gainsboro", "#dcdcdc" },
{ "ghostwhite", "#f8f8ff" },
{ "gold", "#ffd700" },
{ "goldenrod", "#daa520" },
{ "greenyellow", "#adff2f" },
{ "grey", "#808080" },
{ "honeydew", "#f0fff0" },
{ "hotpink", "#ff69b4" },
{ "indianred", "#cd5c5c" },
{ "indigo", "#4b0082" },
{ "ivory", "#fffff0" },
{ "khaki", "#f0e68c" },
{ "lavender", "#e6e6fa" },
{ "lavenderblush", "#fff0f5" },
{ "lawngreen", "#7cfc00" },
{ "lemonchiffon", "#fffacd" },
{ "lightblue", "#add8e6" },
{ "lightcoral", "#f08080" },
{ "lightcyan", "#e0ffff" },
{ "lightgoldenrodyellow", "#fafad2" },
{ "lightgray", "#d3d3d3" },
{ "lightgreen", "#90ee90" },
{ "lightgrey", "#d3d3d3" },
{ "lightpink", "#ffb6c1" },
{ "lightsalmon", "#ffa07a" },
{ "lightseagreen", "#20b2aa" },
{ "lightskyblue", "#87cefa" },
{ "lightslategray", "#778899" },
{ "lightslategrey", "#778899" },
{ "lightsteelblue", "#b0c4de" },
{ "lightyellow", "#ffffe0" },
{ "limegreen", "#32cd32" },
{ "linen", "#faf0e6" },
{ "magenta", "#ff00ff" },
{ "mediumaquamarine", "#66cdaa" },
{ "mediumblue", "#0000cd" },
{ "mediumorchid", "#ba55d3" },
{ "mediumpurple", "#9370db" },
{ "mediumseagreen", "#3cb371" },
{ "mediumslateblue", "#7b68ee" },
{ "mediumspringgreen", "#00fa9a" },
{ "mediumturquoise", "#48d1cc" },
{ "mediumvioletred", "#c71585" },
{ "midnightblue", "#191970" },
{ "mintcream", "#f5fffa" },
{ "mistyrose", "#ffe4e1" },
{ "moccasin", "#ffe4b5" },
{ "navajowhite", "#ffdead" },
{ "oldlace", "#fdf5e6" },
{ "olivedrab", "#6b8e23" },
{ "orange", "#ffa500" },
{ "orangered", "#ff4500" },
{ "orchid", "#da70d6" },
{ "palegoldenrod", "#eee8aa" },
{ "palegreen", "#98fb98" },
{ "paleturquoise", "#afeeee" },
{ "palevioletred", "#db7093" },
{ "papayawhip", "#ffefd5" },
{ "peachpuff", "#ffdab9" },
{ "peru", "#cd853f" },
{ "pink", "#ffc0cb" },
{ "plum", "#dda0dd" },
{ "powderblue", "#b0e0e6" },
{ "rebeccapurple", "#663399" },
{ "rosybrown", "#bc8f8f" },
{ "royalblue", "#4169e1" },
{ "saddlebrown", "#8b4513" },
{ "salmon", "#fa8072" },
{ "sandybrown", "#f4a460" },
{ "seagreen", "#2e8b57" },
{ "seashell", "#fff5ee" },
{ "sienna", "#a0522d" },
{ "skyblue", "#87ceeb" },
{ "slateblue", "#6a5acd" },
{ "slategray", "#708090" },
{ "slategrey", "#708090" },
{ "snow", "#fffafa" },
{ "springgreen", "#00ff7f" },
{ "steelblue", "#4682b4" },
{ "tan", "#d2b48c" },
{ "thistle", "#d8bfd8" },
{ "tomato", "#ff6347" },
{ "turquoise", "#40e0d0" },
{ "violet", "#ee82ee" },
{ "wheat", "#f5deb3" },
{ "whitesmoke", "#f5f5f5" },
{ "yellowgreen", "#9acd32" },
{ NULL, NULL }
};
static ctmbstr GetColorCode(ctmbstr name, Bool use_css_colors)
{
uint i;
@ -597,10 +734,15 @@ static ctmbstr GetColorCode(ctmbstr name)
if (TY_(tmbstrcasecmp)(name, colors[i].name) == 0)
return colors[i].hex;
if (use_css_colors)
for (i = 0; extended_colors[i].name; ++i)
if (TY_(tmbstrcasecmp)(name, extended_colors[i].name) == 0)
return extended_colors[i].hex;
return NULL;
}
static ctmbstr GetColorName(ctmbstr code)
static ctmbstr GetColorName(ctmbstr code, Bool use_css_colors)
{
uint i;
@ -608,6 +750,11 @@ static ctmbstr GetColorName(ctmbstr code)
if (TY_(tmbstrcasecmp)(code, colors[i].hex) == 0)
return colors[i].name;
if (use_css_colors)
for (i = 0; extended_colors[i].name; ++i)
if (TY_(tmbstrcasecmp)(code, extended_colors[i].hex) == 0)
return extended_colors[i].name;
return NULL;
}
@ -2020,7 +2167,7 @@ void CheckColor( TidyDocImpl* doc, Node *node, AttVal *attval)
if (valid && given[0] == '#' && cfgBool(doc, TidyReplaceColor))
{
ctmbstr newName = GetColorName(given);
ctmbstr newName = GetColorName(given, TY_(IsHTML5Mode)(doc));
if (newName)
{
@ -2031,7 +2178,7 @@ void CheckColor( TidyDocImpl* doc, Node *node, AttVal *attval)
/* if it is not a valid color code, it is a color name */
if (!valid)
valid = GetColorCode(given) != NULL;
valid = GetColorCode(given, TY_(IsHTML5Mode)(doc)) != NULL;
if (valid && given[0] == '#')
attval->value = TY_(tmbstrtoupper)(attval->value);