From a4a5938dc3b6fc09836b338cf82b1ab353ee0698 Mon Sep 17 00:00:00 2001 From: Matteo Ferretti Date: Sun, 15 Nov 2015 14:48:32 +0100 Subject: [PATCH] Bug 1212477 - Needs a way to access to 's context (2d, webgl) from Anonymous Content API; r=roc;r=smaug --- dom/base/AnonymousContent.cpp | 25 ++++++++ dom/base/AnonymousContent.h | 4 ++ dom/base/test/mochitest.ini | 1 + dom/base/test/test_anonymousContent_api.html | 2 +- .../test/test_anonymousContent_canvas.html | 57 +++++++++++++++++++ dom/canvas/CanvasRenderingContext2D.h | 4 ++ dom/canvas/WebGLContext.cpp | 7 ++- dom/webidl/AnonymousContent.webidl | 8 +++ 8 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 dom/base/test/test_anonymousContent_canvas.html diff --git a/dom/base/AnonymousContent.cpp b/dom/base/AnonymousContent.cpp index 6ef54a809624..90aeab6bcaff 100644 --- a/dom/base/AnonymousContent.cpp +++ b/dom/base/AnonymousContent.cpp @@ -11,6 +11,7 @@ #include "nsIDocument.h" #include "nsIDOMHTMLCollection.h" #include "nsStyledElement.h" +#include "HTMLCanvasElement.h" namespace mozilla { namespace dom { @@ -112,6 +113,30 @@ AnonymousContent::RemoveAttributeForElement(const nsAString& aElementId, element->RemoveAttribute(aName, aRv); } +already_AddRefed +AnonymousContent::GetCanvasContext(const nsAString& aElementId, + const nsAString& aContextId, + ErrorResult& aRv) +{ + Element* element = GetElementById(aElementId); + + if (!element) { + aRv.Throw(NS_ERROR_NOT_AVAILABLE); + return nullptr; + } + + if (!element->IsHTMLElement(nsGkAtoms::canvas)) { + return nullptr; + } + + nsCOMPtr context; + + HTMLCanvasElement* canvas = static_cast(element); + canvas->GetContext(aContextId, getter_AddRefs(context)); + + return context.forget(); +} + Element* AnonymousContent::GetElementById(const nsAString& aElementId) { diff --git a/dom/base/AnonymousContent.h b/dom/base/AnonymousContent.h index 38b50f042437..6d6b1e2571ce 100644 --- a/dom/base/AnonymousContent.h +++ b/dom/base/AnonymousContent.h @@ -52,6 +52,10 @@ public: const nsAString& aName, ErrorResult& aRv); + already_AddRefed GetCanvasContext(const nsAString& aElementId, + const nsAString& aContextId, + ErrorResult& aRv); + private: ~AnonymousContent(); Element* GetElementById(const nsAString& aElementId); diff --git a/dom/base/test/mochitest.ini b/dom/base/test/mochitest.ini index caff335fb397..eec191536f8c 100644 --- a/dom/base/test/mochitest.ini +++ b/dom/base/test/mochitest.ini @@ -259,6 +259,7 @@ support-files = [test_anonymousContent_api.html] [test_anonymousContent_append_after_reflow.html] +[test_anonymousContent_canvas.html] [test_anonymousContent_insert.html] [test_anonymousContent_manipulate_content.html] [test_anonymousContent_style_csp.html] diff --git a/dom/base/test/test_anonymousContent_api.html b/dom/base/test/test_anonymousContent_api.html index 7f5423c275c0..bcb2b2877005 100644 --- a/dom/base/test/test_anonymousContent_api.html +++ b/dom/base/test/test_anonymousContent_api.html @@ -45,7 +45,7 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=1020244 let members = ["getTextContentForElement", "setTextContentForElement", "getAttributeForElement", "setAttributeForElement", - "removeAttributeForElement"]; + "removeAttributeForElement", "getCanvasContext"]; for (let member of members) { ok(member in anonymousContent, "AnonymousContent object defines " + member); } diff --git a/dom/base/test/test_anonymousContent_canvas.html b/dom/base/test/test_anonymousContent_canvas.html new file mode 100644 index 000000000000..9ee1eca200c0 --- /dev/null +++ b/dom/base/test/test_anonymousContent_canvas.html @@ -0,0 +1,57 @@ + + + + + + Test for Bug 1212477 - Needs a way to access to <canvas>'s context (2d, webgl) from Anonymous Content API + + + + + Mozilla Bug 1212477 +
+
text content
+ + + +
+ + + diff --git a/dom/canvas/CanvasRenderingContext2D.h b/dom/canvas/CanvasRenderingContext2D.h index 9264e356a8e4..d2782b594d34 100644 --- a/dom/canvas/CanvasRenderingContext2D.h +++ b/dom/canvas/CanvasRenderingContext2D.h @@ -71,6 +71,10 @@ public: HTMLCanvasElement* GetCanvas() const { + if (mCanvasElement->IsInNativeAnonymousSubtree()) { + return nullptr; + } + // corresponds to changes to the old bindings made in bug 745025 return mCanvasElement->GetOriginalCanvas(); } diff --git a/dom/canvas/WebGLContext.cpp b/dom/canvas/WebGLContext.cpp index 7a712f623926..958a7bce24ee 100644 --- a/dom/canvas/WebGLContext.cpp +++ b/dom/canvas/WebGLContext.cpp @@ -1235,7 +1235,12 @@ WebGLContext::GetCanvas(Nullable& { if (mCanvasElement) { MOZ_RELEASE_ASSERT(!mOffscreenCanvas); - retval.SetValue().SetAsHTMLCanvasElement() = mCanvasElement; + + if (mCanvasElement->IsInNativeAnonymousSubtree()) { + retval.SetNull(); + } else { + retval.SetValue().SetAsHTMLCanvasElement() = mCanvasElement; + } } else if (mOffscreenCanvas) { retval.SetValue().SetAsOffscreenCanvas() = mOffscreenCanvas; } else { diff --git a/dom/webidl/AnonymousContent.webidl b/dom/webidl/AnonymousContent.webidl index 8aa44e19df89..65b4d79f1f8f 100644 --- a/dom/webidl/AnonymousContent.webidl +++ b/dom/webidl/AnonymousContent.webidl @@ -53,4 +53,12 @@ interface AnonymousContent { [Throws] void removeAttributeForElement(DOMString elementId, DOMString attributeName); + + /** + * Get the canvas' context for the element specified if it's a + * node, `null` otherwise. + */ + [Throws] + nsISupports? getCanvasContext(DOMString elementId, + DOMString contextId); };