PRBool nsAttrValue::ParseColor(const nsAString& aString, nsIDocument* aDocument) { nsAutoString colorStr(aString); colorStr.CompressWhitespace(PR_TRUE, PR_TRUE); if (colorStr.IsEmpty()) { Reset(); return PR_FALSE; } nscolor color; // No color names begin with a '#', but numerical colors do so // it is a very common first char if ((colorStr.CharAt(0) != '#') && NS_ColorNameToRGB(colorStr, &color)) { SetTo(colorStr); return PR_TRUE; } // Check if we are in compatibility mode if (aDocument->GetCompatibilityMode() == eCompatibility_NavQuirks) { NS_LooseHexToRGB(colorStr, &color); } else { if (colorStr.First() != '#') { Reset(); return PR_FALSE; } colorStr.Cut(0, 1); if (!NS_HexToRGB(colorStr, &color)) { Reset(); return PR_FALSE; } } PRInt32 colAsInt = static_cast<PRInt32>(color); PRInt32 tmp = colAsInt * NS_ATTRVALUE_INTEGERTYPE_MULTIPLIER; if (tmp / NS_ATTRVALUE_INTEGERTYPE_MULTIPLIER == colAsInt) { ResetIfSet(); SetIntValueAndType(colAsInt, eColor); } else if (EnsureEmptyMiscContainer()) { MiscContainer* cont = GetMiscContainer(); cont->mColor = color; cont->mType = eColor; } return PR_TRUE; }
PRBool nsAttrValue::ParseColor(const nsAString& aString) { ResetIfSet(); // FIXME (partially, at least): HTML5's algorithm says we shouldn't do // the whitespace compression, trimming, or the test for emptiness. // (I'm a little skeptical that we shouldn't do the whitespace // trimming; WebKit also does it.) nsAutoString colorStr(aString); colorStr.CompressWhitespace(PR_TRUE, PR_TRUE); if (colorStr.IsEmpty()) { return PR_FALSE; } nscolor color; // No color names begin with a '#'; in standards mode, all acceptable // numeric colors do. if (colorStr.First() == '#') { nsDependentString withoutHash(colorStr.get() + 1, colorStr.Length() - 1); if (NS_HexToRGB(withoutHash, &color)) { SetColorValue(color, aString); return PR_TRUE; } } else { if (NS_ColorNameToRGB(colorStr, &color)) { SetColorValue(color, aString); return PR_TRUE; } } // FIXME (maybe): HTML5 says we should handle system colors. This // means we probably need another storage type, since we'd need to // handle dynamic changes. However, I think this is a bad idea: // http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2010-May/026449.html // Use NS_LooseHexToRGB as a fallback if nothing above worked. if (NS_LooseHexToRGB(colorStr, &color)) { SetColorValue(color, aString); return PR_TRUE; } return PR_FALSE; }