Bug 1212477 - Needs a way to access to <canvas>'s context (2d, webgl) from Anonymous Content API; r=roc;r=smaug

This commit is contained in:
Matteo Ferretti 2015-11-15 14:48:32 +01:00
parent daec939f23
commit a4a5938dc3
8 changed files with 106 additions and 2 deletions

View File

@ -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<nsISupports>
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<nsISupports> context;
HTMLCanvasElement* canvas = static_cast<HTMLCanvasElement*>(element);
canvas->GetContext(aContextId, getter_AddRefs(context));
return context.forget();
}
Element*
AnonymousContent::GetElementById(const nsAString& aElementId)
{

View File

@ -52,6 +52,10 @@ public:
const nsAString& aName,
ErrorResult& aRv);
already_AddRefed<nsISupports> GetCanvasContext(const nsAString& aElementId,
const nsAString& aContextId,
ErrorResult& aRv);
private:
~AnonymousContent();
Element* GetElementById(const nsAString& aElementId);

View File

@ -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]

View File

@ -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);
}

View File

@ -0,0 +1,57 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1212477
-->
<head>
<meta charset="utf-8">
<title>Test for Bug 1212477 - Needs a way to access to &lt;canvas&gt;'s context (2d, webgl) from Anonymous Content API</title>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1212477">Mozilla Bug 1212477</a>
<div>
<div id="id" class="test">text content</div>
<canvas id="canvas2d"></canvas>
<canvas id="canvas-webgl"></canvas>
<canvas id="canvas-foo"></canvas>
</div>
<script type="application/javascript;version=1.8">
let chromeDocument = SpecialPowers.wrap(document);
let testElement = document.querySelector("div");
let anonymousContent = chromeDocument.insertAnonymousContent(testElement);
is(anonymousContent.getCanvasContext("id", "2d"), null,
"Context is null for non-canvas elements");
let context2d = anonymousContent.getCanvasContext("canvas2d", "2d");
is(context2d.toString(), "[object CanvasRenderingContext2D]",
"2D Context is returned properly");
is(context2d.canvas, null,
"context's canvas property is null in anonymous content");
is (anonymousContent.getCanvasContext("canvas-foo", "foo"), null,
"Context is null for unknown context type");
SimpleTest.doesThrow(
() => anonymousContent.getCanvasContext("foo", "2d"),
"NS_ERROR_NOT_AVAILABLE",
"Get a context using unexisting id should throw"
);
let webgl = anonymousContent.getCanvasContext("canvas-webgl", "webgl");
is(webgl.toString(), "[object WebGLRenderingContext]",
"WebGL Context is returned properly");
is(webgl.canvas, null,
"WebGL context's canvas property is null in anonymous content");
chromeDocument.removeAnonymousContent(anonymousContent);
</script>
</body>
</html>

View File

@ -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();
}

View File

@ -1235,7 +1235,12 @@ WebGLContext::GetCanvas(Nullable<dom::OwningHTMLCanvasElementOrOffscreenCanvas>&
{
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 {

View File

@ -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 <canvas>
* node, `null` otherwise.
*/
[Throws]
nsISupports? getCanvasContext(DOMString elementId,
DOMString contextId);
};