made color parsing more strict in strict mode, just like nav in quirk mode

This commit is contained in:
peterl%netscape.com 1999-07-07 01:18:11 +00:00
Родитель 39583487e8
Коммит c93821d97e
1 изменённых файлов: 54 добавлений и 40 удалений

Просмотреть файл

@ -23,50 +23,41 @@
static int ComponentValue(const char* aColorSpec, int aLen, int color, int dpc)
{
int component = 0;
if (((1 + color) * dpc) <= aLen) {
aColorSpec += (color * dpc);
while (--dpc >= 0) {
char ch = *aColorSpec++;
if ((ch == 'o') || (ch == 'O')) { // letter O == zero
component = component*16;
}
else if ((ch >= '0') && (ch <= '9')) {
component = component*16 + (ch - '0');
} else {
// "ch&7" handles lower and uppercase hex alphabetics
component = component*16 + (ch & 7) + 9;
}
int index = (color * dpc);
if (2 < dpc) {
dpc = 2;
}
while (--dpc >= 0) {
char ch = ((index < aLen) ? aColorSpec[index++] : '0');
if (('0' <= ch) && (ch <= '9')) {
component = (component * 16) + (ch - '0');
} else if ((('a' <= ch) && (ch <= 'f')) ||
(('A' <= ch) && (ch <= 'F'))) {
// "ch&7" handles lower and uppercase hex alphabetics
component = (component * 16) + (ch & 7) + 9;
}
else { // not a hex digit, treat it like 0
component = (component * 16);
}
}
return component;
}
// Note: This handles 9 digits of hex to be compatible with eric
// bina's original code. However, it is pickyer with respect to what a
// legal color is and will only return true for perfectly legal color
// values.
static PRBool HexToRGB(const char* aColorSpec, PRBool aStrict, nscolor* aResult)
extern "C" NS_GFX_(PRBool) NS_HexToRGB(const char* aColorSpec, nscolor* aResult)
{
NS_PRECONDITION(nsnull != aColorSpec, "null ptr");
if (nsnull == aColorSpec) {
return PR_FALSE;
}
if (aColorSpec[0] == '#') {
aColorSpec++;
}
int nameLen = PL_strlen(aColorSpec);
if (((PR_TRUE == aStrict) && ((nameLen == 3) || (nameLen == 6) || (nameLen == 9))) ||
((0 < nameLen) && (nameLen < 10))) {
if ((nameLen == 3) || (nameLen == 6)) {
// Make sure the digits are legal
for (int i = 0; i < nameLen; i++) {
char ch = aColorSpec[i];
if (((ch >= '0') && (ch <= '9')) ||
((ch >= 'a') && (ch <= 'f')) ||
((ch >= 'A') && (ch <= 'F')) ||
((! aStrict) &&
((ch == 'o') || (ch == 'O')))) { // Nav let Oh's == zeros, egads
((ch >= 'A') && (ch <= 'F'))) {
// Legal character
continue;
}
@ -75,8 +66,7 @@ static PRBool HexToRGB(const char* aColorSpec, PRBool aStrict, nscolor* aResult)
}
// Convert the ascii to binary
int dpc = (nameLen / 3) + (((nameLen % 3) != 0) ? 1 : 0);
int dpc = ((3 == nameLen) ? 1 : 2);
// Translate components from hex to binary
int r = ComponentValue(aColorSpec, nameLen, 0, dpc);
int g = ComponentValue(aColorSpec, nameLen, 1, dpc);
@ -87,11 +77,6 @@ static PRBool HexToRGB(const char* aColorSpec, PRBool aStrict, nscolor* aResult)
r = (r << 4) | r;
g = (g << 4) | g;
b = (b << 4) | b;
} else if (dpc == 3) {
// Drop off the low digit from 12 bit values.
r = r >> 4;
g = g >> 4;
b = b >> 4;
}
NS_ASSERTION((r >= 0) && (r <= 255), "bad r");
NS_ASSERTION((g >= 0) && (g <= 255), "bad g");
@ -106,14 +91,43 @@ static PRBool HexToRGB(const char* aColorSpec, PRBool aStrict, nscolor* aResult)
return PR_FALSE;
}
extern "C" NS_GFX_(PRBool) NS_HexToRGB(const char* aColorSpec, nscolor* aResult)
{
return HexToRGB(aColorSpec, PR_TRUE, aResult);
}
// compatible with legacy Nav behavior
extern "C" NS_GFX_(PRBool) NS_LooseHexToRGB(const char* aColorSpec, nscolor* aResult)
{
return HexToRGB(aColorSpec, PR_FALSE, aResult);
NS_PRECONDITION(nsnull != aColorSpec, "null ptr");
if (nsnull == aColorSpec) {
return PR_FALSE;
}
if ('#' == aColorSpec[0]) {
aColorSpec++;
}
int nameLen = PL_strlen(aColorSpec);
if (3 < nameLen) {
// Convert the ascii to binary
int dpc = (nameLen / 3) + (((nameLen % 3) != 0) ? 1 : 0);
if (4 < dpc) {
dpc = 4;
}
// Translate components from hex to binary
int r = ComponentValue(aColorSpec, nameLen, 0, dpc);
int g = ComponentValue(aColorSpec, nameLen, 1, dpc);
int b = ComponentValue(aColorSpec, nameLen, 2, dpc);
NS_ASSERTION((r >= 0) && (r <= 255), "bad r");
NS_ASSERTION((g >= 0) && (g <= 255), "bad g");
NS_ASSERTION((b >= 0) && (b <= 255), "bad b");
if (nsnull != aResult) {
*aResult = NS_RGB(r, g, b);
}
}
else {
if (nsnull != aResult) {
*aResult = NS_RGB(0, 0, 0);
}
}
return PR_TRUE;
}
extern "C" NS_GFX_(PRBool) NS_ColorNameToRGB(const char* aColorName, nscolor* aResult)