From 23f4621c551dd23e1544c17e330340b2915568a2 Mon Sep 17 00:00:00 2001 From: Boris Zbarsky Date: Thu, 14 Mar 2013 15:43:00 -0400 Subject: [PATCH] Bug 848745 part 1. Add a scriptable API for converting CSS color names to RGB triples. r=dbaron,miker --- dom/webidl/DummyBinding.webidl | 1 + dom/webidl/InspectorUtils.webidl | 16 ++++++++++++++++ dom/webidl/WebIDL.mk | 1 + layout/inspector/public/inIDOMUtils.idl | 6 +++++- layout/inspector/src/inDOMUtils.cpp | 22 ++++++++++++++++++++++ 5 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 dom/webidl/InspectorUtils.webidl diff --git a/dom/webidl/DummyBinding.webidl b/dom/webidl/DummyBinding.webidl index 27f6b5431bf..edbc0b2f1e7 100644 --- a/dom/webidl/DummyBinding.webidl +++ b/dom/webidl/DummyBinding.webidl @@ -13,6 +13,7 @@ interface DummyInterface { RTCConfiguration rtcConfiguration(); CFStateChangeEventDict cfstateChangeEvent(); USSDReceivedEventDict ussdReceivedEvent(); + InspectorRGBTriple rgbTriple(); }; interface DummyInterfaceWorkers { diff --git a/dom/webidl/InspectorUtils.webidl b/dom/webidl/InspectorUtils.webidl new file mode 100644 index 00000000000..2f73a839d29 --- /dev/null +++ b/dom/webidl/InspectorUtils.webidl @@ -0,0 +1,16 @@ +/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. + */ +dictionary InspectorRGBTriple { + /* + * NOTE: Using octet for RGB components is not generally OK, because + * they can be outside the 0-255 range, but for backwards-compatible + * named colors (which is what we use this dictionary for) the 0-255 + * assumption is fine. + */ + octet r = 0; + octet g = 0; + octet b = 0; +}; diff --git a/dom/webidl/WebIDL.mk b/dom/webidl/WebIDL.mk index 51759d8737b..75e3e0e07d0 100644 --- a/dom/webidl/WebIDL.mk +++ b/dom/webidl/WebIDL.mk @@ -119,6 +119,7 @@ webidl_files = \ HTMLUListElement.webidl \ IDBVersionChangeEvent.webidl \ ImageData.webidl \ + InspectorUtils.webidl \ LinkStyle.webidl \ LocalMediaStream.webidl \ Location.webidl \ diff --git a/layout/inspector/public/inIDOMUtils.idl b/layout/inspector/public/inIDOMUtils.idl index c24495476c6..6c2854a160d 100644 --- a/layout/inspector/public/inIDOMUtils.idl +++ b/layout/inspector/public/inIDOMUtils.idl @@ -16,7 +16,7 @@ interface nsIDOMFontFaceList; interface nsIDOMRange; interface nsIDOMCSSStyleSheet; -[scriptable, uuid(ea7e58ad-da38-4d1e-b0fc-fd7fb5c560c9)] +[scriptable, uuid(1d9c29dc-230a-441e-bba9-49104ffa185e)] interface inIDOMUtils : nsISupports { // CSS utilities @@ -54,6 +54,10 @@ interface inIDOMUtils : nsISupports [optional] out unsigned long aCount, [retval, array, size_is(aCount)] out wstring aProps); + // Utilities for working with CSS colors + [implicit_jscontext] + jsval colorNameToRGB(in DOMString aColorName); + // DOM Node utilities boolean isIgnorableWhitespace(in nsIDOMCharacterData aDataNode); // Returns the "parent" of a node. The parent of a document node is the diff --git a/layout/inspector/src/inDOMUtils.cpp b/layout/inspector/src/inDOMUtils.cpp index ccc0c40ccb6..4dd1597ceaa 100644 --- a/layout/inspector/src/inDOMUtils.cpp +++ b/layout/inspector/src/inDOMUtils.cpp @@ -31,6 +31,7 @@ #include "nsRuleWalker.h" #include "nsRuleProcessorData.h" #include "nsCSSRuleProcessor.h" +#include "mozilla/dom/InspectorUtilsBinding.h" using namespace mozilla; using namespace mozilla::css; @@ -414,6 +415,27 @@ inDOMUtils::GetCSSPropertyNames(uint32_t aFlags, uint32_t* aCount, return NS_OK; } +NS_IMETHODIMP +inDOMUtils::ColorNameToRGB(const nsAString& aColorName, JSContext* aCx, + JS::Value* aValue) +{ + nscolor color; + if (!NS_ColorNameToRGB(aColorName, &color)) { + return NS_ERROR_INVALID_ARG; + } + + InspectorRGBTriple triple; + triple.mR = NS_GET_R(color); + triple.mG = NS_GET_G(color); + triple.mB = NS_GET_B(color); + + if (!triple.ToObject(aCx, nullptr, aValue)) { + return NS_ERROR_FAILURE; + } + + return NS_OK; +} + NS_IMETHODIMP inDOMUtils::GetBindingURLs(nsIDOMElement *aElement, nsIArray **_retval) {