Bug 1427419 - Part 11: Move inIDOMUtils.rgbToColorName to InspectorUtils. r=bz draft
authorCameron McCormack <cam@mcc.id.au>
Sat, 06 Jan 2018 15:08:14 +0800
changeset 716756 7d5820e22808742b38700fb6b483627ac4c98580
parent 716755 657f7f8ffc0aaaa7c3988431c575a5e54f444ea8
child 716757 6961c8930b63c1846abe557d30282690674e5d5a
push id94496
push userbmo:cam@mcc.id.au
push dateSat, 06 Jan 2018 07:08:40 +0000
reviewersbz
bugs1427419
milestone59.0a1
Bug 1427419 - Part 11: Move inIDOMUtils.rgbToColorName to InspectorUtils. r=bz MozReview-Commit-ID: LZcGUEO4Ois
devtools/client/shared/test/unit/test_cssColorDatabase.js
dom/webidl/InspectorUtils.webidl
layout/inspector/InspectorUtils.h
layout/inspector/inDOMUtils.cpp
layout/inspector/inIDOMUtils.idl
--- a/devtools/client/shared/test/unit/test_cssColorDatabase.js
+++ b/devtools/client/shared/test/unit/test_cssColorDatabase.js
@@ -33,17 +33,17 @@ function checkOne(colorName, checkName) 
 
   if (checkName) {
     let {r, g, b} = ours;
 
     // The color we got might not map back to the same name; but our
     // implementation should agree with DOMUtils about which name is
     // canonical.
     let ourName = colorUtils.rgbToColorName(r, g, b);
-    let domName = DOMUtils.rgbToColorName(r, g, b);
+    let domName = InspectorUtils.rgbToColorName(r, g, b);
 
     equal(ourName, domName,
           colorName + " canonical name agrees with DOMUtils");
   }
 }
 
 function run_test() {
   for (let name in cssColors) {
--- a/dom/webidl/InspectorUtils.webidl
+++ b/dom/webidl/InspectorUtils.webidl
@@ -27,16 +27,17 @@ namespace InspectorUtils {
   [Throws] boolean selectorMatchesElement(
       Element element,
       CSSStyleRule rule,
       unsigned long selectorIndex,
       [TreatNullAs=EmptyString] optional DOMString pseudo = "");
   boolean isInheritedProperty(DOMString property);
   sequence<DOMString> getCSSPropertyNames(optional PropertyNamesOptions options);
   [Throws] sequence<DOMString> getCSSValuesForProperty(DOMString property);
+  [Throws] DOMString rgbToColorName(octet r, octet g, octet b);
 };
 
 dictionary PropertyNamesOptions {
   boolean includeAliases = false;
 };
 
 dictionary InspectorRGBATuple {
   /*
--- a/layout/inspector/InspectorUtils.h
+++ b/layout/inspector/InspectorUtils.h
@@ -110,16 +110,22 @@ public:
                                   nsTArray<nsString>& aResult);
 
   // Get a list of all valid keywords and colors for aProperty.
   static void GetCSSValuesForProperty(GlobalObject& aGlobal,
                                       const nsAString& aPropertyName,
                                       nsTArray<nsString>& aResult,
                                       ErrorResult& aRv);
 
+  // Utilities for working with CSS colors
+  static void RgbToColorName(GlobalObject& aGlobal,
+                             uint8_t aR, uint8_t aG, uint8_t aB,
+                             nsAString& aResult,
+                             ErrorResult& aRv);
+
 private:
   static already_AddRefed<nsStyleContext>
     GetCleanStyleContextForElement(Element* aElement, nsAtom* aPseudo);
 };
 
 } // namespace dom
 } // namespace mozilla
 
--- a/layout/inspector/inDOMUtils.cpp
+++ b/layout/inspector/inDOMUtils.cpp
@@ -850,33 +850,35 @@ InspectorUtils::GetCSSValuesForProperty(
     }
   }
   // All CSS properties take initial, inherit and unset.
   InsertNoDuplicates(aResult, NS_LITERAL_STRING("initial"));
   InsertNoDuplicates(aResult, NS_LITERAL_STRING("inherit"));
   InsertNoDuplicates(aResult, NS_LITERAL_STRING("unset"));
 }
 
-} // namespace dom
-} // namespace mozilla
-
-NS_IMETHODIMP
-inDOMUtils::RgbToColorName(uint8_t aR, uint8_t aG, uint8_t aB,
-                           nsAString& aColorName)
+/* static */ void
+InspectorUtils::RgbToColorName(GlobalObject& aGlobalObject,
+                               uint8_t aR, uint8_t aG, uint8_t aB,
+                               nsAString& aColorName,
+                               ErrorResult& aRv)
 {
   const char* color = NS_RGBToColorName(NS_RGB(aR, aG, aB));
   if (!color) {
     aColorName.Truncate();
-    return NS_ERROR_INVALID_ARG;
+    aRv.Throw(NS_ERROR_INVALID_ARG);
+    return;
   }
 
   aColorName.AssignASCII(color);
-  return NS_OK;
 }
 
+} // namespace dom
+} // namespace mozilla
+
 NS_IMETHODIMP
 inDOMUtils::ColorToRGBA(const nsAString& aColorString, JSContext* aCx,
                         JS::MutableHandle<JS::Value> aValue)
 {
   nscolor color = NS_RGB(0, 0, 0);
 
 #ifdef MOZ_STYLO
   if (!ServoCSSParser::ComputeColor(nullptr, NS_RGB(0, 0, 0), aColorString,
--- a/layout/inspector/inIDOMUtils.idl
+++ b/layout/inspector/inIDOMUtils.idl
@@ -15,19 +15,16 @@ interface nsIDOMNode;
 interface nsIDOMNodeList;
 interface nsIDOMFontFaceList;
 interface nsIDOMRange;
 interface nsIDOMCSSStyleSheet;
 
 [scriptable, uuid(362e98c3-82c2-4ad8-8dcb-00e8e4eab497)]
 interface inIDOMUtils : nsISupports
 {
-  // Utilities for working with CSS colors
-  AString rgbToColorName(in octet aR, in octet aG, in octet aB);
-
   // Convert a given CSS color string to rgba. Returns null on failure or an
   // InspectorRGBATuple on success.
   //
   // NOTE: Converting a color to RGBA may be lossy when converting from some
   // formats e.g. CMYK.
   [implicit_jscontext]
   jsval colorToRGBA(in DOMString aColorString);