Skip to content

Commit

Permalink
Bug 1846745 - Remove nsColorNames.h. r=tlouw
Browse files Browse the repository at this point in the history
Use the CSS parser to parse named colors.

Differential Revision: https://phabricator.services.mozilla.com/D185156
  • Loading branch information
emilio committed Aug 3, 2023
1 parent ed2e1b4 commit 595328d
Show file tree
Hide file tree
Showing 14 changed files with 40 additions and 381 deletions.
6 changes: 5 additions & 1 deletion dom/base/nsAttrValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1693,8 +1693,12 @@ bool nsAttrValue::ParseColor(const nsAString& aString) {
SetColorValue(color, aString);
return true;
}
} else if (colorStr.LowerCaseEqualsLiteral("transparent")) {
SetColorValue(NS_RGBA(0, 0, 0, 0), aString);
return true;
} else {
if (NS_ColorNameToRGB(colorStr, &color)) {
const NS_ConvertUTF16toUTF8 colorNameU8(colorStr);
if (Servo_ColorNameToRgb(&colorNameU8, &color)) {
SetColorValue(color, aString);
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion dom/chrome-webidl/InspectorUtils.webidl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace InspectorUtils {
sequence<DOMString> getCSSPropertyNames(optional PropertyNamesOptions options = {});
sequence<PropertyPref> getCSSPropertyPrefs();
[Throws] sequence<DOMString> getCSSValuesForProperty(UTF8String property);
DOMString rgbToColorName(octet r, octet g, octet b);
UTF8String rgbToColorName(octet r, octet g, octet b);
InspectorRGBATuple? colorToRGBA(UTF8String colorString, optional Document? doc = null);
boolean isValidCSSColor(UTF8String colorString);
[Throws] sequence<DOMString> getSubpropertiesForCSSProperty(UTF8String property);
Expand Down
9 changes: 6 additions & 3 deletions editor/libeditor/HTMLEditUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2380,8 +2380,11 @@ bool HTMLEditUtils::MaybeCSSSpecificColorValue(const nsAString& aColorValue) {
return true;
}
nscolor color = NS_RGB(0, 0, 0);
if (colorValue.IsEmpty() || colorValue.First() == '#' ||
NS_ColorNameToRGB(colorValue, &color)) {
if (colorValue.IsEmpty() || colorValue.First() == '#') {
return false;
}
const NS_ConvertUTF16toUTF8 colorU8(colorValue);
if (Servo_ColorNameToRgb(&colorU8, &color)) {
return false;
}
if (colorValue.LowerCaseEqualsASCII("initial") ||
Expand All @@ -2391,7 +2394,7 @@ bool HTMLEditUtils::MaybeCSSSpecificColorValue(const nsAString& aColorValue) {
colorValue.LowerCaseEqualsASCII("currentcolor")) {
return true;
}
return ServoCSSParser::IsValidCSSColor(NS_ConvertUTF16toUTF8(colorValue));
return ServoCSSParser::IsValidCSSColor(colorU8);
}

static bool ComputeColor(const nsAString& aColorValue, nscolor* aColor,
Expand Down
2 changes: 0 additions & 2 deletions gfx/src/moz.build
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ EXPORTS += [
"gfxTelemetry.h",
"nsBoundingMetrics.h",
"nsColor.h",
"nsColorNameList.h",
"nsColorNames.h",
"nsCoord.h",
"nsDeviceContext.h",
"nsFont.h",
Expand Down
64 changes: 2 additions & 62 deletions gfx/src/nsColor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,50 +9,15 @@
#include "mozilla/MathAlgorithms.h"

#include "nsColor.h"
#include <sys/types.h> // for int32_t
#include "nsColorNames.h" // for nsColorNames
#include "nsDebug.h" // for NS_ASSERTION, etc
#include <sys/types.h> // for int32_t
#include "nsDebug.h" // for NS_ASSERTION, etc
#include "nsStaticNameTable.h"
#include "nsString.h" // for nsAutoCString, nsString, etc
#include "nscore.h" // for nsAString, etc
#include "prtypes.h" // for PR_BEGIN_MACRO, etc

using namespace mozilla;

// define an array of all color names
#define GFX_COLOR(_name, _value) #_name,
static const char* const kColorNames[] = {
#include "nsColorNameList.h"
};
#undef GFX_COLOR

// define an array of all color name values
#define GFX_COLOR(_name, _value) _value,
static const nscolor kColors[] = {
#include "nsColorNameList.h"
};
#undef GFX_COLOR

#define eColorName_COUNT (ArrayLength(kColorNames))
#define eColorName_UNKNOWN (-1)

static nsStaticCaseInsensitiveNameTable* gColorTable = nullptr;

void nsColorNames::AddRefTable(void) {
NS_ASSERTION(!gColorTable, "pre existing array!");
if (!gColorTable) {
gColorTable =
new nsStaticCaseInsensitiveNameTable(kColorNames, eColorName_COUNT);
}
}

void nsColorNames::ReleaseTable(void) {
if (gColorTable) {
delete gColorTable;
gColorTable = nullptr;
}
}

static int ComponentValue(const char16_t* aColorSpec, int aLen, int color,
int dpc) {
int component = 0;
Expand Down Expand Up @@ -192,21 +157,6 @@ bool NS_LooseHexToRGB(const nsString& aColorSpec, nscolor* aResult) {
return true;
}

bool NS_ColorNameToRGB(const nsAString& aColorName, nscolor* aResult) {
if (!gColorTable) return false;

int32_t id = gColorTable->Lookup(aColorName);
if (eColorName_UNKNOWN < id) {
NS_ASSERTION(uint32_t(id) < eColorName_COUNT,
"gColorTable->Lookup messed up");
if (aResult) {
*aResult = kColors[id];
}
return true;
}
return false;
}

// Fast approximate division by 255. It has the property that
// for all 0 <= n <= 255*255, FAST_DIVIDE_BY_255(n) == n/255.
// But it only uses two adds and two shifts instead of an
Expand Down Expand Up @@ -250,13 +200,3 @@ nscolor NS_ComposeColors(nscolor aBG, nscolor aFG) {

return NS_RGBA(r, g, b, a);
}

const char* NS_RGBToColorName(nscolor aColor) {
for (size_t idx = 0; idx < ArrayLength(kColors); ++idx) {
if (kColors[idx] == aColor) {
return kColorNames[idx];
}
}

return nullptr;
}
11 changes: 0 additions & 11 deletions gfx/src/nsColor.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,4 @@ bool NS_LooseHexToRGB(const nsString& aBuf, nscolor* aResult);
// There is no function to translate a color to a hex string, because
// the hex-string syntax does not support transparency.

// Translate a color name to a color. Return true if it parses ok,
// otherwise return false.
bool NS_ColorNameToRGB(const nsAString& aBuf, nscolor* aResult);

// Return a color name for the given nscolor. If there is no color
// name for it, returns null. If there are multiple possible color
// names for the given color, the first one in nsColorNameList.h
// (which is generally the first one in alphabetical order) will be
// returned.
const char* NS_RGBToColorName(nscolor aColor);

#endif /* nsColor_h___ */
180 changes: 0 additions & 180 deletions gfx/src/nsColorNameList.h

This file was deleted.

16 changes: 0 additions & 16 deletions gfx/src/nsColorNames.h

This file was deleted.

Loading

0 comments on commit 595328d

Please sign in to comment.