Web Inspector: support `console.screenshot` with detached <canvas>
authordrousso@apple.com <drousso@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 15 Aug 2019 19:16:04 +0000 (19:16 +0000)
committerdrousso@apple.com <drousso@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 15 Aug 2019 19:16:04 +0000 (19:16 +0000)
https://bugs.webkit.org/show_bug.cgi?id=200723

Reviewed by Joseph Pecoraro.

Source/WebCore:

* page/PageConsoleClient.cpp:
(WebCore::snapshotCanvas): Added.
(WebCore::PageConsoleClient::screenshot):

LayoutTests:

* inspector/console/console-screenshot.html:
* inspector/console/console-screenshot-expected.txt:

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@248736 268f45cc-cd09-0410-ab3c-d52691b4dbfc

LayoutTests/ChangeLog
LayoutTests/inspector/console/console-screenshot-expected.txt
LayoutTests/inspector/console/console-screenshot.html
Source/WebCore/ChangeLog
Source/WebCore/page/PageConsoleClient.cpp

index 39586b9..21884eb 100644 (file)
@@ -1,3 +1,13 @@
+2019-08-15  Devin Rousso  <drousso@apple.com>
+
+        Web Inspector: support `console.screenshot` with detached <canvas>
+        https://bugs.webkit.org/show_bug.cgi?id=200723
+
+        Reviewed by Joseph Pecoraro.
+
+        * inspector/console/console-screenshot.html:
+        * inspector/console/console-screenshot-expected.txt:
+
 2019-08-15  Sihui Liu  <sihui_liu@apple.com>
 
         Some improvements on web storage
index af8acf7..e9da4d8 100644 (file)
@@ -2,6 +2,7 @@ CONSOLE MESSAGE: [object HTMLDivElement]
 CONSOLE MESSAGE: [object HTMLDivElement]
 CONSOLE MESSAGE: [object HTMLImageElement]
 CONSOLE MESSAGE: [object HTMLPictureElement]
+CONSOLE MESSAGE: [object HTMLCanvasElement]
 CONSOLE MESSAGE: [object HTMLDivElement]
 CONSOLE MESSAGE: [object ImageData]
 CONSOLE MESSAGE: [object ImageBitmap]
@@ -39,6 +40,12 @@ PASS: The image should not be empty.
 PASS: The image width should be 2px.
 PASS: The image height should be 2px.
 
+-- Running test case: console.screenshot.Node.DetachedScreenshotable.Canvas
+PASS: The added message should be an image.
+PASS: The image should not be empty.
+PASS: The image width should be 2px.
+PASS: The image height should be 2px.
+
 -- Running test case: console.screenshot.Node.DetachedNonScreenshotable
 PASS: Could not capture screenshot
 
index b64fcc7..c90818b 100644 (file)
@@ -31,6 +31,18 @@ function testHTMLPictureElement() {
     });
 }
 
+function testHTMLCanvasElement() {
+    let canvas = document.createElement("canvas");
+    canvas.width = 2;
+    canvas.height = 2;
+
+    let context = canvas.getContext("2d");
+    context.fillStyle = "red";
+    context.fillRect(0, 0, 2, 2);
+
+    console.screenshot(canvas);
+}
+
 function testImageBitmap() {
     // 2x2 red square
     let image = document.createElement("img");
@@ -104,6 +116,18 @@ function test()
     });
 
     addTest({
+        name: "console.screenshot.Node.DetachedScreenshotable.Canvas",
+        expression: `testHTMLCanvasElement()`,
+        async imageMessageAddedCallback(message) {
+            InspectorTest.expectNotEqual(message.messageText, "data:", "The image should not be empty.");
+
+            let img = await WI.ImageUtilities.promisifyLoad(message.messageText);
+            InspectorTest.expectEqual(img.width, 2, "The image width should be 2px.");
+            InspectorTest.expectEqual(img.height, 2, "The image height should be 2px.");
+        },
+    });
+
+    addTest({
         name: "console.screenshot.Node.DetachedNonScreenshotable",
         expression: `console.screenshot(createDetachedTest())`,
         shouldError: true,
index 3bb2646..2f0b4f4 100644 (file)
@@ -1,3 +1,14 @@
+2019-08-15  Devin Rousso  <drousso@apple.com>
+
+        Web Inspector: support `console.screenshot` with detached <canvas>
+        https://bugs.webkit.org/show_bug.cgi?id=200723
+
+        Reviewed by Joseph Pecoraro.
+
+        * page/PageConsoleClient.cpp:
+        (WebCore::snapshotCanvas): Added.
+        (WebCore::PageConsoleClient::screenshot):
+
 2019-08-15  Sihui Liu  <sihui_liu@apple.com>
 
         Some improvements on web storage
index 1891e95..8246adc 100644 (file)
@@ -281,6 +281,26 @@ void PageConsoleClient::recordEnd(JSC::ExecState* state, Ref<ScriptArguments>&&
     }
 }
 
+static Optional<String> snapshotCanvas(HTMLCanvasElement& canvasElement, CanvasRenderingContext& canvasRenderingContext)
+{
+#if ENABLE(WEBGL)
+    if (is<WebGLRenderingContextBase>(canvasRenderingContext))
+        downcast<WebGLRenderingContextBase>(canvasRenderingContext).setPreventBufferClearForInspector(true);
+#endif
+
+    auto result = canvasElement.toDataURL("image/png"_s);
+
+#if ENABLE(WEBGL)
+    if (is<WebGLRenderingContextBase>(canvasRenderingContext))
+        downcast<WebGLRenderingContextBase>(canvasRenderingContext).setPreventBufferClearForInspector(false);
+#endif
+
+    if (!result.hasException())
+        return result.releaseReturnValue().string;
+
+    return WTF::nullopt;
+}
+
 void PageConsoleClient::screenshot(JSC::ExecState* state, Ref<ScriptArguments>&& arguments)
 {
     String dataURL;
@@ -321,13 +341,22 @@ void PageConsoleClient::screenshot(JSC::ExecState* state, Ref<ScriptArguments>&&
                         videoElement.paintCurrentFrameInContext(snapshot->context(), FloatRect(0, 0, videoWidth, videoHeight));
                     }
 #endif
+                    else if (is<HTMLCanvasElement>(node)) {
+                        auto& canvasElement = downcast<HTMLCanvasElement>(*node);
+                        if (auto* canvasRenderingContext = canvasElement.renderingContext()) {
+                            if (auto result = snapshotCanvas(canvasElement, *canvasRenderingContext))
+                                dataURL = result.value();
+                        }
+                    }
                 }
 
-                if (!snapshot)
-                    snapshot = WebCore::snapshotNode(m_page.mainFrame(), *node);
+                if (dataURL.isEmpty()) {
+                    if (!snapshot)
+                        snapshot = WebCore::snapshotNode(m_page.mainFrame(), *node);
 
-                if (snapshot)
-                    dataURL = snapshot->toDataURL("image/png"_s, WTF::nullopt, PreserveResolution::Yes);
+                    if (snapshot)
+                        dataURL = snapshot->toDataURL("image/png"_s, WTF::nullopt, PreserveResolution::Yes);
+                }
             }
         } else if (auto* imageData = JSImageData::toWrapped(state->vm(), possibleTarget)) {
             target = possibleTarget;
@@ -350,20 +379,8 @@ void PageConsoleClient::screenshot(JSC::ExecState* state, Ref<ScriptArguments>&&
             if (is<HTMLCanvasElement>(canvas)) {
                 target = possibleTarget;
                 if (UNLIKELY(InspectorInstrumentation::hasFrontends())) {
-#if ENABLE(WEBGL)
-                    if (is<WebGLRenderingContextBase>(context))
-                        downcast<WebGLRenderingContextBase>(context)->setPreventBufferClearForInspector(true);
-#endif
-
-                    auto result = downcast<HTMLCanvasElement>(canvas).toDataURL("image/png"_s);
-
-#if ENABLE(WEBGL)
-                    if (is<WebGLRenderingContextBase>(context))
-                        downcast<WebGLRenderingContextBase>(context)->setPreventBufferClearForInspector(false);
-#endif
-
-                    if (!result.hasException())
-                        dataURL = result.releaseReturnValue().string;
+                    if (auto result = snapshotCanvas(downcast<HTMLCanvasElement>(canvas), *context))
+                        dataURL = result.value();
                 }
             }