From 688a8b41ebb8c80cc988a7b3697bb3d19fe54ce6 Mon Sep 17 00:00:00 2001 From: Boris Zbarsky Date: Thu, 23 Aug 2012 21:08:08 -0700 Subject: [PATCH] Bug 753517 part 3. Expose the API needed for Paris bindings on nsDOMCSSDeclaration and nsICSSDeclaration. r=dbaron,peterv --- dom/bindings/ErrorResult.h | 1 + layout/style/Declaration.cpp | 4 +- layout/style/Declaration.h | 4 +- layout/style/nsCSSRules.cpp | 21 ++++-- layout/style/nsCSSRules.h | 1 + layout/style/nsComputedDOMStyle.cpp | 23 +++--- layout/style/nsComputedDOMStyle.h | 3 +- layout/style/nsDOMCSSAttrDeclaration.h | 2 +- layout/style/nsDOMCSSDeclaration.cpp | 12 +--- layout/style/nsDOMCSSDeclaration.h | 41 ++++++++++- layout/style/nsICSSDeclaration.h | 97 +++++++++++++++++++++++--- 11 files changed, 172 insertions(+), 37 deletions(-) diff --git a/dom/bindings/ErrorResult.h b/dom/bindings/ErrorResult.h index 792de9ea927b..bbd9404a865f 100644 --- a/dom/bindings/ErrorResult.h +++ b/dom/bindings/ErrorResult.h @@ -12,6 +12,7 @@ #define mozilla_ErrorResult_h #include "nscore.h" +#include "mozilla/Assertions.h" namespace mozilla { diff --git a/layout/style/Declaration.cpp b/layout/style/Declaration.cpp index bf0ba2f79ade..aeeb224a6133 100644 --- a/layout/style/Declaration.cpp +++ b/layout/style/Declaration.cpp @@ -987,7 +987,7 @@ Declaration::List(FILE* out, int32_t aIndent) const } #endif -void +bool Declaration::GetNthProperty(uint32_t aIndex, nsAString& aReturn) const { aReturn.Truncate(); @@ -995,8 +995,10 @@ Declaration::GetNthProperty(uint32_t aIndex, nsAString& aReturn) const nsCSSProperty property = OrderValueAt(aIndex); if (0 <= property) { AppendASCIItoUTF16(nsCSSProps::GetStringValue(property), aReturn); + return true; } } + return false; } void diff --git a/layout/style/Declaration.h b/layout/style/Declaration.h index eff2787b5613..e1214e35d089 100644 --- a/layout/style/Declaration.h +++ b/layout/style/Declaration.h @@ -70,7 +70,9 @@ public: uint32_t Count() const { return mOrder.Length(); } - void GetNthProperty(uint32_t aIndex, nsAString& aReturn) const; + + // Returns whether we actually had a property at aIndex + bool GetNthProperty(uint32_t aIndex, nsAString& aReturn) const; void ToString(nsAString& aString) const; diff --git a/layout/style/nsCSSRules.cpp b/layout/style/nsCSSRules.cpp index f026ff260d5e..ddf5f96e7040 100644 --- a/layout/style/nsCSSRules.cpp +++ b/layout/style/nsCSSRules.cpp @@ -1558,8 +1558,19 @@ nsCSSFontFaceStyleDecl::GetLength(uint32_t *aLength) // DOMString item (in unsigned long index); NS_IMETHODIMP -nsCSSFontFaceStyleDecl::Item(uint32_t index, nsAString & aResult) - { +nsCSSFontFaceStyleDecl::Item(uint32_t aIndex, nsAString& aReturn) +{ + bool found; + IndexedGetter(aIndex, found, aReturn); + if (!found) { + aReturn.Truncate(); + } + return NS_OK; +} + +void +nsCSSFontFaceStyleDecl::IndexedGetter(uint32_t index, bool& aFound, nsAString & aResult) +{ int32_t nset = -1; for (nsCSSFontDesc id = nsCSSFontDesc(eCSSFontDesc_UNKNOWN + 1); id < eCSSFontDesc_COUNT; @@ -1568,13 +1579,13 @@ nsCSSFontFaceStyleDecl::Item(uint32_t index, nsAString & aResult) != eCSSUnit_Null) { nset++; if (nset == int32_t(index)) { + aFound = true; aResult.AssignASCII(nsCSSProps::GetStringValue(id).get()); - return NS_OK; + return; } } } - aResult.Truncate(); - return NS_OK; + aFound = false; } // readonly attribute nsIDOMCSSRule parentRule; diff --git a/layout/style/nsCSSRules.h b/layout/style/nsCSSRules.h index 8b4c3dd379c1..7c4dc73c5289 100644 --- a/layout/style/nsCSSRules.h +++ b/layout/style/nsCSSRules.h @@ -165,6 +165,7 @@ public: NS_DECL_NSICSSDECLARATION virtual nsINode *GetParentObject(); + virtual void IndexedGetter(uint32_t aIndex, bool& aFound, nsAString& aPropName); nsresult GetPropertyValue(nsCSSFontDesc aFontDescID, nsAString & aResult) const; diff --git a/layout/style/nsComputedDOMStyle.cpp b/layout/style/nsComputedDOMStyle.cpp index 30d0b45a4ef8..3b10bb8dd215 100644 --- a/layout/style/nsComputedDOMStyle.cpp +++ b/layout/style/nsComputedDOMStyle.cpp @@ -545,18 +545,21 @@ nsComputedDOMStyle::SetProperty(const nsAString& aPropertyName, NS_IMETHODIMP nsComputedDOMStyle::Item(uint32_t aIndex, nsAString& aReturn) { - aReturn.Truncate(); - - uint32_t length = 0; - const ComputedStyleMapEntry* propMap = GetQueryablePropertyMap(&length); - if (aIndex < length) { - CopyASCIItoUTF16(nsCSSProps::GetStringValue(propMap[aIndex].mProperty), - aReturn); - } - - return NS_OK; + return nsDOMCSSDeclaration::Item(aIndex, aReturn); } +void +nsComputedDOMStyle::IndexedGetter(uint32_t aIndex, bool& aFound, + nsAString& aPropName) +{ + uint32_t length = 0; + const ComputedStyleMapEntry* propMap = GetQueryablePropertyMap(&length); + aFound = aIndex < length; + if (aFound) { + CopyASCIItoUTF16(nsCSSProps::GetStringValue(propMap[aIndex].mProperty), + aPropName); + } +} // Property getters... diff --git a/layout/style/nsComputedDOMStyle.h b/layout/style/nsComputedDOMStyle.h index 406e38a91b21..60b8ba4b3bed 100644 --- a/layout/style/nsComputedDOMStyle.h +++ b/layout/style/nsComputedDOMStyle.h @@ -25,7 +25,7 @@ class nsIFrame; class nsIPresShell; -class nsComputedDOMStyle : public nsDOMCSSDeclaration +class nsComputedDOMStyle MOZ_FINAL : public nsDOMCSSDeclaration { public: NS_DECL_CYCLE_COLLECTING_ISUPPORTS @@ -35,6 +35,7 @@ public: NS_DECL_NSICSSDECLARATION NS_DECL_NSIDOMCSSSTYLEDECLARATION + virtual void IndexedGetter(uint32_t aIndex, bool& aFound, nsAString& aPropName); nsComputedDOMStyle(mozilla::dom::Element* aElement, const nsAString& aPseudoElt, diff --git a/layout/style/nsDOMCSSAttrDeclaration.h b/layout/style/nsDOMCSSAttrDeclaration.h index dc3c98d48c1c..56a42bdaefa1 100644 --- a/layout/style/nsDOMCSSAttrDeclaration.h +++ b/layout/style/nsDOMCSSAttrDeclaration.h @@ -23,7 +23,7 @@ class Element; } } -class nsDOMCSSAttributeDeclaration : public nsDOMCSSDeclaration +class nsDOMCSSAttributeDeclaration MOZ_FINAL : public nsDOMCSSDeclaration { public: typedef mozilla::dom::Element Element; diff --git a/layout/style/nsDOMCSSDeclaration.cpp b/layout/style/nsDOMCSSDeclaration.cpp index f02218b818a8..11b0d0cbb658 100644 --- a/layout/style/nsDOMCSSDeclaration.cpp +++ b/layout/style/nsDOMCSSDeclaration.cpp @@ -144,17 +144,11 @@ nsDOMCSSDeclaration::GetPropertyCSSValue(const nsAString& aPropertyName, return NS_OK; } -NS_IMETHODIMP -nsDOMCSSDeclaration::Item(uint32_t aIndex, nsAString& aReturn) +void +nsDOMCSSDeclaration::IndexedGetter(uint32_t aIndex, bool& aFound, nsAString& aPropName) { css::Declaration* decl = GetCSSDeclaration(false); - - aReturn.SetLength(0); - if (decl) { - decl->GetNthProperty(aIndex, aReturn); - } - - return NS_OK; + aFound = decl && decl->GetNthProperty(aIndex, aPropName); } NS_IMETHODIMP diff --git a/layout/style/nsDOMCSSDeclaration.h b/layout/style/nsDOMCSSDeclaration.h index 0ff610952e14..c9d89443635a 100644 --- a/layout/style/nsDOMCSSDeclaration.h +++ b/layout/style/nsDOMCSSDeclaration.h @@ -33,7 +33,14 @@ public: // of implementing AddRef/Release. NS_IMETHOD QueryInterface(REFNSIID aIID, void** aInstancePtr); + // Declare addref and release so they can be called on us, but don't + // implement them. Our subclasses must handle their own + // refcounting. + NS_IMETHOD_(nsrefcnt) AddRef() = 0; + NS_IMETHOD_(nsrefcnt) Release() = 0; + NS_DECL_NSICSSDECLARATION + using nsICSSDeclaration::GetLength; // Require subclasses to implement |GetParentRule|. //NS_DECL_NSIDOMCSSSTYLEDECLARATION @@ -50,13 +57,45 @@ public: NS_IMETHOD SetProperty(const nsAString & propertyName, const nsAString & value, const nsAString & priority); NS_IMETHOD GetLength(uint32_t *aLength); - NS_IMETHOD Item(uint32_t index, nsAString & _retval); NS_IMETHOD GetParentRule(nsIDOMCSSRule * *aParentRule) = 0; // We implement this as a shim which forwards to GetPropertyValue // and SetPropertyValue; subclasses need not. NS_DECL_NSIDOMCSS2PROPERTIES + // WebIDL interface for CSS2Properties +#define CSS_PROP_DOMPROP_PREFIXED(prop_) Moz ## prop_ +#define CSS_PROP(name_, id_, method_, flags_, pref_, parsevariant_, \ + kwtable_, stylestruct_, stylestructoffset_, animtype_) \ + void \ + Get##method_(nsAString& aValue, mozilla::ErrorResult& rv) \ + { \ + rv = GetPropertyValue(eCSSProperty_##id_, aValue); \ + } \ + \ + void \ + Set##method_(const nsAString& aValue, mozilla::ErrorResult& rv) \ + { \ + rv = SetPropertyValue(eCSSProperty_##id_, aValue); \ + } + +#define CSS_PROP_LIST_EXCLUDE_INTERNAL +#define CSS_PROP_SHORTHAND(name_, id_, method_, flags_, pref_) \ + CSS_PROP(name_, id_, method_, flags_, pref_, X, X, X, X, X) +#include "nsCSSPropList.h" + +#define CSS_PROP_ALIAS(aliasname_, propid_, aliasmethod_, pref_) \ + CSS_PROP(X, propid_, aliasmethod_, X, pref_, X, X, X, X, X) +#include "nsCSSPropAliasList.h" +#undef CSS_PROP_ALIAS + +#undef CSS_PROP_SHORTHAND +#undef CSS_PROP_LIST_EXCLUDE_INTERNAL +#undef CSS_PROP +#undef CSS_PROP_DOMPROP_PREFIXED + + virtual void IndexedGetter(uint32_t aIndex, bool& aFound, nsAString& aPropName); + protected: // This method can return null regardless of the value of aAllocate; // however, a null return should only be considered a failure diff --git a/layout/style/nsICSSDeclaration.h b/layout/style/nsICSSDeclaration.h index bfb04a3cd6f6..1d87f3deb6c2 100644 --- a/layout/style/nsICSSDeclaration.h +++ b/layout/style/nsICSSDeclaration.h @@ -22,11 +22,16 @@ #include "nsIDOMCSSStyleDeclaration.h" #include "nsCSSProperty.h" #include "nsWrapperCache.h" +#include "mozilla/dom/BindingUtils.h" +#include "nsString.h" +#include "nsIDOMCSSRule.h" +#include "nsIDOMCSSValue.h" +#include "mozilla/ErrorResult.h" -// 57eb81d1-a607-4429-926b-802519d43aad +// dbeabbfa-6cb3-4f5c-aec2-dd558d9d681f #define NS_ICSSDECLARATION_IID \ - { 0x57eb81d1, 0xa607, 0x4429, \ - {0x92, 0x6b, 0x80, 0x25, 0x19, 0xd4, 0x3a, 0xad } } +{ 0xdbeabbfa, 0x6cb3, 0x4f5c, \ + { 0xae, 0xc2, 0xdd, 0x55, 0x8d, 0x9d, 0x68, 0x1f } } class nsINode; @@ -43,11 +48,6 @@ public: NS_IMETHOD GetPropertyValue(const nsCSSProperty aPropID, nsAString& aValue) = 0; - // Also have to declare the nsIDOMCSSStyleDeclaration method, so we - // don't hide it... very sad, but it stole the good method name - NS_IMETHOD GetPropertyValue(const nsAString& aPropName, - nsAString& aValue) = 0; - /** * Method analogous to nsIDOMCSSStyleDeclaration::SetProperty. This * method does NOT allow setting a priority (the priority will @@ -57,6 +57,87 @@ public: const nsAString& aValue) = 0; virtual nsINode *GetParentObject() = 0; + + // Also have to declare all the nsIDOMCSSStyleDeclaration methods, + // since we want to be able to call them from the WebIDL versions. + NS_IMETHOD GetCssText(nsAString& aCssText) = 0; + NS_IMETHOD SetCssText(const nsAString& aCssText) = 0; + NS_IMETHOD GetPropertyValue(const nsAString& aPropName, + nsAString& aValue) = 0; + NS_IMETHOD GetPropertyCSSValue(const nsAString& aPropertyName, + nsIDOMCSSValue** aReturn) = 0; + NS_IMETHOD RemoveProperty(const nsAString& aPropertyName, + nsAString& aReturn) = 0; + NS_IMETHOD GetPropertyPriority(const nsAString& aPropertyName, + nsAString& aReturn) = 0; + NS_IMETHOD SetProperty(const nsAString& aPropertyName, + const nsAString& aValue, + const nsAString& aPriority) = 0; + NS_IMETHOD GetLength(uint32_t* aLength) = 0; + NS_IMETHOD Item(uint32_t aIndex, nsAString& aReturn) + { + bool found; + IndexedGetter(aIndex, found, aReturn); + if (!found) { + aReturn.Truncate(); + } + return NS_OK; + } + NS_IMETHOD GetParentRule(nsIDOMCSSRule * *aParentRule) = 0; + + // WebIDL interface for CSSStyleDeclaration + void SetCssText(const nsAString& aString, mozilla::ErrorResult& rv) { + rv = SetCssText(aString); + } + void GetCssText(nsString& aString) { + // Cast to nsAString& so we end up calling our virtual + // |GetCssText(nsAString& aCssText)| overload, which does the real work. + GetCssText(static_cast(aString)); + } + uint32_t GetLength() { + uint32_t length; + GetLength(&length); + return length; + } + void Item(uint32_t aIndex, nsString& aPropName) { + Item(aIndex, static_cast(aPropName)); + } + + // The actual implementation of the Item method and the WebIDL indexed getter + virtual void IndexedGetter(uint32_t aIndex, bool& aFound, nsAString& aPropName) = 0; + + void GetPropertyValue(const nsAString& aPropName, nsString& aValue, + mozilla::ErrorResult& rv) { + rv = GetPropertyValue(aPropName, aValue); + } + already_AddRefed + GetPropertyCSSValue(const nsAString& aPropName, mozilla::ErrorResult& rv) { + nsCOMPtr val; + rv = GetPropertyCSSValue(aPropName, getter_AddRefs(val)); + return val.forget(); + } + void GetPropertyPriority(const nsAString& aPropName, nsString& aPriority) { + GetPropertyPriority(aPropName, static_cast(aPriority)); + } + // XXXbz we should nix the Optional thing once bug 759622 is fixed. + void SetProperty(const nsAString& aPropName, const nsAString& aValue, + const mozilla::dom::Optional& aPriority, + mozilla::ErrorResult& rv) { + if (aPriority.WasPassed()) { + rv = SetProperty(aPropName, aValue, aPriority.Value()); + } else { + rv = SetProperty(aPropName, aValue, EmptyString()); + } + } + void RemoveProperty(const nsAString& aPropName, nsString& aRetval, + mozilla::ErrorResult& rv) { + rv = RemoveProperty(aPropName, aRetval); + } + already_AddRefed GetParentRule() { + nsCOMPtr rule; + GetParentRule(getter_AddRefs(rule)); + return rule.forget(); + } }; NS_DEFINE_STATIC_IID_ACCESSOR(nsICSSDeclaration, NS_ICSSDECLARATION_IID)