Bug 1427419 - Part 16: Move inIDOMUtils.cssPropertySupportsType to InspectorUtils. r=bz draft
authorCameron McCormack <cam@mcc.id.au>
Sat, 06 Jan 2018 15:08:14 +0800
changeset 716761 c485735c338e1c372090f8180da33234122bcd4b
parent 716760 94d94f3cfc6f865616327df838bbe0ae4e54cb20
child 716762 33edc022a0add1b722ec60650c5bd63604c08ed6
push id94496
push userbmo:cam@mcc.id.au
push dateSat, 06 Jan 2018 07:08:40 +0000
reviewersbz
bugs1427419
milestone59.0a1
Bug 1427419 - Part 16: Move inIDOMUtils.cssPropertySupportsType to InspectorUtils. r=bz MozReview-Commit-ID: AfNA6g5lTec
devtools/server/actors/css-properties.js
dom/webidl/InspectorUtils.webidl
layout/inspector/InspectorUtils.h
layout/inspector/inDOMUtils.cpp
layout/inspector/inIDOMUtils.idl
layout/inspector/tests/test_bug1006595.html
layout/style/test/test_bug1112014.html
--- a/devtools/server/actors/css-properties.js
+++ b/devtools/server/actors/css-properties.js
@@ -48,17 +48,17 @@ function generateCssProperties() {
   const properties = {};
   const propertyNames = InspectorUtils.getCSSPropertyNames({ includeAliases: true });
   const colors = Object.keys(cssColors);
 
   propertyNames.forEach(name => {
     // Get the list of CSS types this property supports.
     let supports = [];
     for (let type in CSS_TYPES) {
-      if (safeCssPropertySupportsType(name, DOMUtils["TYPE_" + type])) {
+      if (safeCssPropertySupportsType(name, InspectorUtils["TYPE_" + type])) {
         supports.push(CSS_TYPES[type]);
       }
     }
 
     // Don't send colors over RDP, these will be re-attached by the front.
     let values = InspectorUtils.getCSSValuesForProperty(name);
     if (values.includes("aliceblue")) {
       values = values.filter(x => !colors.includes(x));
@@ -95,23 +95,23 @@ function isCssPropertyKnown(name) {
   } catch (e) {
     return false;
   }
 }
 
 exports.isCssPropertyKnown = isCssPropertyKnown;
 
 /**
- * A wrapper for DOMUtils.cssPropertySupportsType that ignores invalid
+ * A wrapper for InspectorUtils.cssPropertySupportsType that ignores invalid
  * properties.
  *
  * @param {String} name The property name.
  * @param {number} type The type tested for support.
  * @return {Boolean} Whether the property supports the type.
  *        If the property is unknown, false is returned.
  */
 function safeCssPropertySupportsType(name, type) {
   try {
-    return DOMUtils.cssPropertySupportsType(name, type);
+    return InspectorUtils.cssPropertySupportsType(name, type);
   } catch (e) {
     return false;
   }
 }
--- a/dom/webidl/InspectorUtils.webidl
+++ b/dom/webidl/InspectorUtils.webidl
@@ -32,16 +32,30 @@ namespace InspectorUtils {
   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);
   InspectorRGBATuple? colorToRGBA(DOMString colorString);
   boolean isValidCSSColor(DOMString colorString);
   [Throws] sequence<DOMString> getSubpropertiesForCSSProperty(DOMString property);
   [Throws] boolean cssPropertyIsShorthand(DOMString property);
+
+  // TODO: Change this to use an enum.
+  const unsigned long TYPE_LENGTH = 0;
+  const unsigned long TYPE_PERCENTAGE = 1;
+  const unsigned long TYPE_COLOR = 2;
+  const unsigned long TYPE_URL = 3;
+  const unsigned long TYPE_ANGLE = 4;
+  const unsigned long TYPE_FREQUENCY = 5;
+  const unsigned long TYPE_TIME = 6;
+  const unsigned long TYPE_GRADIENT = 7;
+  const unsigned long TYPE_TIMING_FUNCTION = 8;
+  const unsigned long TYPE_IMAGE_RECT = 9;
+  const unsigned long TYPE_NUMBER = 10;
+  [Throws] boolean cssPropertySupportsType(DOMString property, unsigned long type);
 };
 
 dictionary PropertyNamesOptions {
   boolean includeAliases = false;
 };
 
 dictionary InspectorRGBATuple {
   /*
--- a/layout/inspector/InspectorUtils.h
+++ b/layout/inspector/InspectorUtils.h
@@ -145,16 +145,25 @@ public:
                                              ErrorResult& aRv);
 
   // Check whether a given CSS property is a shorthand.  Throws on unsupported
   // property names.
   static bool CssPropertyIsShorthand(GlobalObject& aGlobal,
                                      const nsAString& aProperty,
                                      ErrorResult& aRv);
 
+  // Check whether values of the given type are valid values for the property.
+  // For shorthands, checks whether there's a corresponding longhand property
+  // that accepts values of this type.  Throws on unsupported properties or
+  // unknown types.
+  static bool CssPropertySupportsType(GlobalObject& aGlobal,
+                                      const nsAString& aProperty,
+                                      uint32_t aType,
+                                      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
@@ -601,19 +601,16 @@ InspectorUtils::CssPropertyIsShorthand(G
 
   if (propertyID == eCSSPropertyExtra_variable) {
     return false;
   }
 
   return nsCSSProps::IsShorthand(propertyID);
 }
 
-} // namespace dom
-} // namespace mozilla
-
 // A helper function that determines whether the given property
 // supports the given type.
 static bool
 PropertySupportsVariant(nsCSSPropertyID aPropertyID, uint32_t aVariant)
 {
   if (nsCSSProps::IsShorthand(aPropertyID)) {
     // We need a special case for border here, because while it resets
     // border-image, it can't actually parse an image.
@@ -735,79 +732,78 @@ PropertySupportsVariant(nsCSSPropertyID 
     }
 
     return (supported & aVariant) != 0;
   }
 
   return (nsCSSProps::ParserVariant(aPropertyID) & aVariant) != 0;
 }
 
-NS_IMETHODIMP
-inDOMUtils::CssPropertySupportsType(const nsAString& aProperty, uint32_t aType,
-                                    bool *_retval)
+bool
+InspectorUtils::CssPropertySupportsType(GlobalObject& aGlobalObject,
+                                        const nsAString& aProperty,
+                                        uint32_t aType,
+                                        ErrorResult& aRv)
 {
   nsCSSPropertyID propertyID =
     nsCSSProps::LookupProperty(aProperty, CSSEnabledState::eForAllContent);
   if (propertyID == eCSSProperty_UNKNOWN) {
-    return NS_ERROR_FAILURE;
+    aRv.Throw(NS_ERROR_FAILURE);
+    return false;
   }
 
   if (propertyID >= eCSSProperty_COUNT) {
-    *_retval = false;
-    return NS_OK;
+    return false;
   }
 
   uint32_t variant;
   switch (aType) {
-  case TYPE_LENGTH:
+  case InspectorUtilsBinding::TYPE_LENGTH:
     variant = VARIANT_LENGTH;
     break;
-  case TYPE_PERCENTAGE:
+  case InspectorUtilsBinding::TYPE_PERCENTAGE:
     variant = VARIANT_PERCENT;
     break;
-  case TYPE_COLOR:
+  case InspectorUtilsBinding::TYPE_COLOR:
     variant = VARIANT_COLOR;
     break;
-  case TYPE_URL:
+  case InspectorUtilsBinding::TYPE_URL:
     variant = VARIANT_URL;
     break;
-  case TYPE_ANGLE:
+  case InspectorUtilsBinding::TYPE_ANGLE:
     variant = VARIANT_ANGLE;
     break;
-  case TYPE_FREQUENCY:
+  case InspectorUtilsBinding::TYPE_FREQUENCY:
     variant = VARIANT_FREQUENCY;
     break;
-  case TYPE_TIME:
+  case InspectorUtilsBinding::TYPE_TIME:
     variant = VARIANT_TIME;
     break;
-  case TYPE_GRADIENT:
+  case InspectorUtilsBinding::TYPE_GRADIENT:
     variant = VARIANT_GRADIENT;
     break;
-  case TYPE_TIMING_FUNCTION:
+  case InspectorUtilsBinding::TYPE_TIMING_FUNCTION:
     variant = VARIANT_TIMING_FUNCTION;
     break;
-  case TYPE_IMAGE_RECT:
+  case InspectorUtilsBinding::TYPE_IMAGE_RECT:
     variant = VARIANT_IMAGE_RECT;
     break;
-  case TYPE_NUMBER:
+  case InspectorUtilsBinding::TYPE_NUMBER:
     // Include integers under "number"?
     variant = VARIANT_NUMBER | VARIANT_INTEGER;
     break;
   default:
     // Unknown type
-    return NS_ERROR_NOT_AVAILABLE;
+    aRv.Throw(NS_ERROR_NOT_AVAILABLE);
+    return false;
   }
 
-  *_retval = PropertySupportsVariant(propertyID, variant);
-  return NS_OK;
+  return PropertySupportsVariant(propertyID, variant);
 }
 
-namespace mozilla {
-namespace dom {
-
 /* static */ void
 InspectorUtils::GetCSSValuesForProperty(GlobalObject& aGlobalObject,
                                         const nsAString& aProperty,
                                         nsTArray<nsString>& aResult,
                                         ErrorResult& aRv)
 {
   nsCSSPropertyID propertyID = nsCSSProps::
     LookupProperty(aProperty, CSSEnabledState::eForAllContent);
--- a/layout/inspector/inIDOMUtils.idl
+++ b/layout/inspector/inIDOMUtils.idl
@@ -15,33 +15,16 @@ interface nsIDOMNode;
 interface nsIDOMNodeList;
 interface nsIDOMFontFaceList;
 interface nsIDOMRange;
 interface nsIDOMCSSStyleSheet;
 
 [scriptable, uuid(362e98c3-82c2-4ad8-8dcb-00e8e4eab497)]
 interface inIDOMUtils : nsISupports
 {
-  // Check whether values of the given type are valid values for the property.
-  // For shorthands, checks whether there's a corresponding longhand property
-  // that accepts values of this type.  Throws on unsupported properties or
-  // unknown types.
-  const unsigned long TYPE_LENGTH = 0;
-  const unsigned long TYPE_PERCENTAGE = 1;
-  const unsigned long TYPE_COLOR = 2;
-  const unsigned long TYPE_URL = 3;
-  const unsigned long TYPE_ANGLE = 4;
-  const unsigned long TYPE_FREQUENCY = 5;
-  const unsigned long TYPE_TIME = 6;
-  const unsigned long TYPE_GRADIENT = 7;
-  const unsigned long TYPE_TIMING_FUNCTION = 8;
-  const unsigned long TYPE_IMAGE_RECT = 9;
-  const unsigned long TYPE_NUMBER = 10;
-  bool cssPropertySupportsType(in AString aProperty, in unsigned long type);
-
   // DOM Node utilities
   boolean isIgnorableWhitespace(in nsIDOMCharacterData aDataNode);
   // Returns the "parent" of a node.  The parent of a document node is the
   // frame/iframe containing that document.  aShowingAnonymousContent says
   // whether we are showing anonymous content.
   nsIDOMNode getParentForNode(in nsIDOMNode aNode,
                               in boolean aShowingAnonymousContent);
   nsIDOMNodeList getChildrenForNode(in nsIDOMNode aNode,
--- a/layout/inspector/tests/test_bug1006595.html
+++ b/layout/inspector/tests/test_bug1006595.html
@@ -15,18 +15,16 @@ https://bugzilla.mozilla.org/show_bug.cg
   const InspectorUtils = SpecialPowers.InspectorUtils;
 
   function arraysEqual(arr1, arr2, message) {
     is(arr1.length, arr2.length, message + " length");
     for (var i = 0; i < arr1.length; ++i) {
       is(arr1[i], arr2[i], message + " element at index " + i);
     }
   }
-  var utils = SpecialPowers.Cc["@mozilla.org/inspector/dom-utils;1"]
-    .getService(SpecialPowers.Ci.inIDOMUtils);
 
   var paddingSubProps = InspectorUtils.getSubpropertiesForCSSProperty("padding");
   arraysEqual(paddingSubProps,
               [ "padding-top",
                 "padding-right",
                 "padding-bottom",
                 "padding-left" ],
               "'padding' subproperties");
@@ -36,80 +34,80 @@ https://bugzilla.mozilla.org/show_bug.cg
               "'color' subproperties");
 
   var varProps = InspectorUtils.getSubpropertiesForCSSProperty("--foo");
   arraysEqual(varProps, ["--foo"], "'--foo' subproperties");
 
   ok(InspectorUtils.cssPropertyIsShorthand("padding"), "'padding' is a shorthand")
   ok(!InspectorUtils.cssPropertyIsShorthand("color"), "'color' is not a shorthand")
 
-  ok(utils.cssPropertySupportsType("padding", utils.TYPE_LENGTH),
+  ok(InspectorUtils.cssPropertySupportsType("padding", InspectorUtils.TYPE_LENGTH),
      "'padding' can be a length");
-  ok(!utils.cssPropertySupportsType("padding", utils.TYPE_COLOR),
+  ok(!InspectorUtils.cssPropertySupportsType("padding", InspectorUtils.TYPE_COLOR),
      "'padding' can't be a color");
 
-  ok(utils.cssPropertySupportsType("padding", utils.TYPE_PERCENTAGE),
+  ok(InspectorUtils.cssPropertySupportsType("padding", InspectorUtils.TYPE_PERCENTAGE),
      "'padding' can be a percentage");
-  ok(!utils.cssPropertySupportsType("color", utils.TYPE_PERCENTAGE),
+  ok(!InspectorUtils.cssPropertySupportsType("color", InspectorUtils.TYPE_PERCENTAGE),
      "'color' can't be a percentage");
 
-  ok(utils.cssPropertySupportsType("color", utils.TYPE_COLOR),
+  ok(InspectorUtils.cssPropertySupportsType("color", InspectorUtils.TYPE_COLOR),
      "'color' can be a color");
-  ok(utils.cssPropertySupportsType("background", utils.TYPE_COLOR),
+  ok(InspectorUtils.cssPropertySupportsType("background", InspectorUtils.TYPE_COLOR),
      "'background' can be a color");
-  ok(!utils.cssPropertySupportsType("background-image", utils.TYPE_COLOR),
+  ok(!InspectorUtils.cssPropertySupportsType("background-image", InspectorUtils.TYPE_COLOR),
      "'background-image' can't be a color");
 
-  ok(utils.cssPropertySupportsType("background-image", utils.TYPE_URL),
+  ok(InspectorUtils.cssPropertySupportsType("background-image", InspectorUtils.TYPE_URL),
      "'background-image' can be a URL");
-  ok(utils.cssPropertySupportsType("background", utils.TYPE_URL),
+  ok(InspectorUtils.cssPropertySupportsType("background", InspectorUtils.TYPE_URL),
      "'background' can be a URL");
-  ok(!utils.cssPropertySupportsType("background-color", utils.TYPE_URL),
+  ok(!InspectorUtils.cssPropertySupportsType("background-color", InspectorUtils.TYPE_URL),
      "'background-color' can't be a URL");
 
   // There are no properties claiming to be of TYPE_ANGLE.  image-orientation
   // would be, but it doesn't use table-driven parsing.
 
   // There are no properties claiming to be of TYPE_FREQUENCY
 
-  ok(utils.cssPropertySupportsType("transition", utils.TYPE_TIME),
+  ok(InspectorUtils.cssPropertySupportsType("transition", InspectorUtils.TYPE_TIME),
      "'transition' can be a time");
-  ok(utils.cssPropertySupportsType("transition-duration", utils.TYPE_TIME),
+  ok(InspectorUtils.cssPropertySupportsType("transition-duration", InspectorUtils.TYPE_TIME),
      "'transition-duration' can be a time");
-  ok(!utils.cssPropertySupportsType("background-color", utils.TYPE_TIME),
+  ok(!InspectorUtils.cssPropertySupportsType("background-color", InspectorUtils.TYPE_TIME),
      "'background-color' can't be a time");
 
-  ok(utils.cssPropertySupportsType("background-image", utils.TYPE_GRADIENT),
+  ok(InspectorUtils.cssPropertySupportsType("background-image", InspectorUtils.TYPE_GRADIENT),
      "'background-image' can be a gradient");
-  ok(utils.cssPropertySupportsType("background", utils.TYPE_GRADIENT),
+  ok(InspectorUtils.cssPropertySupportsType("background", InspectorUtils.TYPE_GRADIENT),
      "'background' can be a gradient");
-  ok(!utils.cssPropertySupportsType("background-color", utils.TYPE_GRADIENT),
+  ok(!InspectorUtils.cssPropertySupportsType("background-color", InspectorUtils.TYPE_GRADIENT),
      "'background-color' can't be a gradient");
 
-  ok(utils.cssPropertySupportsType("transition", utils.TYPE_TIMING_FUNCTION),
+  ok(InspectorUtils.cssPropertySupportsType("transition", InspectorUtils.TYPE_TIMING_FUNCTION),
      "'transition' can be a timing function");
-  ok(utils.cssPropertySupportsType("transition-timing-function",
-                                   utils.TYPE_TIMING_FUNCTION),
+  ok(InspectorUtils.cssPropertySupportsType("transition-timing-function",
+                                   InspectorUtils.TYPE_TIMING_FUNCTION),
      "'transition-duration' can be a timing function");
-  ok(!utils.cssPropertySupportsType("background-color",
-                                    utils.TYPE_TIMING_FUNCTION),
+  ok(!InspectorUtils.cssPropertySupportsType("background-color",
+                                    InspectorUtils.TYPE_TIMING_FUNCTION),
      "'background-color' can't be a timing function");
 
-  ok(utils.cssPropertySupportsType("background-image", utils.TYPE_IMAGE_RECT),
+  ok(InspectorUtils.cssPropertySupportsType("background-image", InspectorUtils.TYPE_IMAGE_RECT),
      "'background-image' can be an image rect");
-  ok(utils.cssPropertySupportsType("background", utils.TYPE_IMAGE_RECT),
+  ok(InspectorUtils.cssPropertySupportsType("background", InspectorUtils.TYPE_IMAGE_RECT),
      "'background' can be an image rect");
-  ok(!utils.cssPropertySupportsType("background-color", utils.TYPE_IMAGE_RECT),
+  ok(!InspectorUtils.cssPropertySupportsType("background-color", InspectorUtils.TYPE_IMAGE_RECT),
      "'background-color' can't be an image rect");
 
-  ok(utils.cssPropertySupportsType("z-index", utils.TYPE_NUMBER),
+  ok(InspectorUtils.cssPropertySupportsType("z-index", InspectorUtils.TYPE_NUMBER),
      "'z-index' can be a number");
-  ok(utils.cssPropertySupportsType("line-height", utils.TYPE_NUMBER),
+  ok(InspectorUtils.cssPropertySupportsType("line-height", InspectorUtils.TYPE_NUMBER),
      "'line-height' can be a number");
-  ok(!utils.cssPropertySupportsType("background-color", utils.TYPE_NUMBER),
+  ok(!InspectorUtils.cssPropertySupportsType("background-color", InspectorUtils.TYPE_NUMBER),
      "'background-color' can't be a number");
 
   </script>
 </head>
 <body>
 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1006595">Mozilla Bug 1006595</a>
 <p id="display"></p>
 <div id="content" style="display: none">
--- a/layout/style/test/test_bug1112014.html
+++ b/layout/style/test/test_bug1112014.html
@@ -5,18 +5,17 @@ https://bugzilla.mozilla.org/show_bug.cg
 -->
 <head>
   <meta charset="utf-8">
   <title>Test for Bug 1112014</title>
   <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
   <script src="property_database.js"></script>
   <script type="application/javascript">
 
-  let utils = SpecialPowers.Cc["@mozilla.org/inspector/dom-utils;1"]
-    .getService(SpecialPowers.Ci.inIDOMUtils);
+  const InspectorUtils = SpecialPowers.InspectorUtils;
 
   SimpleTest.requestLongerTimeout(2);
 
   // This holds a canonical test value for each TYPE_ constant.
   let testValues = {
     TYPE_LENGTH: "10px",
     TYPE_PERCENTAGE: "50%",
     TYPE_COLOR: "rgb(3,3,3)",
@@ -82,17 +81,17 @@ https://bugzilla.mozilla.org/show_bug.cg
     }
   };
 
 
   // Ensure that all the TYPE_ constants have a representative
   // test value, to try to ensure that this test is updated
   // whenever a new type is added.
   let reps = [];
-  for (let tc in utils) {
+  for (let tc in InspectorUtils) {
     if (/TYPE_/.test(tc)) {
       if (!(tc in testValues)) {
 	reps.push(tc);
       }
     }
   }
   is(reps.join(","), "", "all types have representative test value");
 
@@ -102,26 +101,26 @@ https://bugzilla.mozilla.org/show_bug.cg
     for (let iter in testValues) {
       let testValue = testValues[iter];
       if (propertyName in overrideValues &&
 	  iter in overrideValues[propertyName]) {
 	testValue = overrideValues[propertyName][iter];
       }
 
       let supported =
-        utils.cssPropertySupportsType(propertyName, utils[iter]);
+        InspectorUtils.cssPropertySupportsType(propertyName, InspectorUtils[iter]);
       let parsed = CSS.supports(propertyName, testValue);
       is(supported, parsed, propertyName + " supports " + iter);
     }
   }
 
   // Regression test for an assertion failure in an earlier version of
   // the code.  Note that cssPropertySupportsType returns false for
   // all types for a variable.
-  ok(!utils.cssPropertySupportsType("--variable", utils.TYPE_COLOR),
+  ok(!InspectorUtils.cssPropertySupportsType("--variable", InspectorUtils.TYPE_COLOR),
      "cssPropertySupportsType returns false for variable");
 
   </script>
 </head>
 <body>
 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1112014">Mozilla Bug 1112014</a>
 <p id="display"></p>
 <div id="content" style="display: none">