Bug 1346623 - Part 2: Add an API to get computed style values through an AnonymousContent object. r=bholley
MozReview-Commit-ID: Dbvuk16CjFT
--- a/dom/base/AnonymousContent.cpp
+++ b/dom/base/AnonymousContent.cpp
@@ -2,16 +2,17 @@
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */
#include "AnonymousContent.h"
#include "mozilla/dom/Element.h"
#include "mozilla/dom/AnonymousContentBinding.h"
+#include "nsComputedDOMStyle.h"
#include "nsCycleCollectionParticipant.h"
#include "nsIDocument.h"
#include "nsIDOMHTMLCollection.h"
#include "nsIFrame.h"
#include "nsStyledElement.h"
#include "HTMLCanvasElement.h"
namespace mozilla {
@@ -203,10 +204,34 @@ AnonymousContent::GetElementById(const n
bool
AnonymousContent::WrapObject(JSContext* aCx,
JS::Handle<JSObject*> aGivenProto,
JS::MutableHandle<JSObject*> aReflector)
{
return AnonymousContentBinding::Wrap(aCx, this, aGivenProto, aReflector);
}
+void
+AnonymousContent::GetComputedStylePropertyValue(const nsAString& aElementId,
+ const nsAString& aPropertyName,
+ DOMString& aResult,
+ ErrorResult& aRv)
+{
+ Element* element = GetElementById(aElementId);
+ if (!element) {
+ aRv.Throw(NS_ERROR_NOT_AVAILABLE);
+ return;
+ }
+
+ nsIPresShell* shell = element->OwnerDoc()->GetShell();
+ if (!shell) {
+ aRv.Throw(NS_ERROR_NOT_AVAILABLE);
+ return;
+ }
+
+ RefPtr<nsComputedDOMStyle> cs =
+ new nsComputedDOMStyle(element, NS_LITERAL_STRING(""), shell,
+ nsComputedDOMStyle::eAll);
+ aRv = cs->GetPropertyValue(aPropertyName, aResult);
+}
+
} // namespace dom
} // namespace mozilla
--- a/dom/base/AnonymousContent.h
+++ b/dom/base/AnonymousContent.h
@@ -63,16 +63,21 @@ public:
JS::Handle<JSObject*> aKeyframes,
const UnrestrictedDoubleOrKeyframeAnimationOptions& aOptions,
ErrorResult& aError);
void SetCutoutRectsForElement(const nsAString& aElementId,
const Sequence<OwningNonNull<DOMRect>>& aRects,
ErrorResult& aError);
+ void GetComputedStylePropertyValue(const nsAString& aElementId,
+ const nsAString& aPropertyName,
+ DOMString& aResult,
+ ErrorResult& aRv);
+
private:
~AnonymousContent();
nsCOMPtr<Element> mContentNode;
};
} // namespace dom
} // namespace mozilla
--- a/dom/webidl/AnonymousContent.webidl
+++ b/dom/webidl/AnonymousContent.webidl
@@ -72,9 +72,17 @@ interface AnonymousContent {
* Accepts a list of (possibly overlapping) DOMRects which describe a shape
* in CSS pixels relative to the element's border box. This shape will be
* excluded from the element's background color rendering. The element will
* not render any background images once this method has been called.
*/
[Throws]
void setCutoutRectsForElement(DOMString elementId,
sequence<DOMRect> rects);
+
+ /**
+ * Get the computed value of a property on an element inside this custom
+ * anonymous content.
+ */
+ [Throws]
+ DOMString? getComputedStylePropertyValue(DOMString elementId,
+ DOMString propertyName);
};