⚠ Archived content — this site is no longer maintained.   Current WebKit documentation is at docs.webkit.org.

Changeset 248736 in webkit


Ignore:
Timestamp:
Aug 15, 2019, 12:16:04 PM (7 years ago)
Author:
Devin Rousso
Message:

Web Inspector: support console.screenshot with detached <canvas>
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:
Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/ChangeLog

    r248734 r248736  
     12019-08-15  Devin Rousso  <drousso@apple.com>
     2
     3        Web Inspector: support `console.screenshot` with detached <canvas>
     4        https://bugs.webkit.org/show_bug.cgi?id=200723
     5
     6        Reviewed by Joseph Pecoraro.
     7
     8        * inspector/console/console-screenshot.html:
     9        * inspector/console/console-screenshot-expected.txt:
     10
    1112019-08-15  Sihui Liu  <sihui_liu@apple.com>
    212
  • trunk/LayoutTests/inspector/console/console-screenshot-expected.txt

    r248688 r248736  
    33CONSOLE MESSAGE: [object HTMLImageElement]
    44CONSOLE MESSAGE: [object HTMLPictureElement]
     5CONSOLE MESSAGE: [object HTMLCanvasElement]
    56CONSOLE MESSAGE: [object HTMLDivElement]
    67CONSOLE MESSAGE: [object ImageData]
     
    3536
    3637-- Running test case: console.screenshot.Node.DetachedScreenshotable.Picture
     38PASS: The added message should be an image.
     39PASS: The image should not be empty.
     40PASS: The image width should be 2px.
     41PASS: The image height should be 2px.
     42
     43-- Running test case: console.screenshot.Node.DetachedScreenshotable.Canvas
    3744PASS: The added message should be an image.
    3845PASS: The image should not be empty.
  • trunk/LayoutTests/inspector/console/console-screenshot.html

    r248688 r248736  
    3030        console.screenshot(picture);
    3131    });
     32}
     33
     34function testHTMLCanvasElement() {
     35    let canvas = document.createElement("canvas");
     36    canvas.width = 2;
     37    canvas.height = 2;
     38
     39    let context = canvas.getContext("2d");
     40    context.fillStyle = "red";
     41    context.fillRect(0, 0, 2, 2);
     42
     43    console.screenshot(canvas);
    3244}
    3345
     
    102114        name: "console.screenshot.Node.DetachedScreenshotable.Picture",
    103115        expression: `testHTMLPictureElement()`,
     116    });
     117
     118    addTest({
     119        name: "console.screenshot.Node.DetachedScreenshotable.Canvas",
     120        expression: `testHTMLCanvasElement()`,
     121        async imageMessageAddedCallback(message) {
     122            InspectorTest.expectNotEqual(message.messageText, "data:", "The image should not be empty.");
     123
     124            let img = await WI.ImageUtilities.promisifyLoad(message.messageText);
     125            InspectorTest.expectEqual(img.width, 2, "The image width should be 2px.");
     126            InspectorTest.expectEqual(img.height, 2, "The image height should be 2px.");
     127        },
    104128    });
    105129
  • trunk/Source/WebCore/ChangeLog

    r248734 r248736  
     12019-08-15  Devin Rousso  <drousso@apple.com>
     2
     3        Web Inspector: support `console.screenshot` with detached <canvas>
     4        https://bugs.webkit.org/show_bug.cgi?id=200723
     5
     6        Reviewed by Joseph Pecoraro.
     7
     8        * page/PageConsoleClient.cpp:
     9        (WebCore::snapshotCanvas): Added.
     10        (WebCore::PageConsoleClient::screenshot):
     11
    1122019-08-15  Sihui Liu  <sihui_liu@apple.com>
    213
  • trunk/Source/WebCore/page/PageConsoleClient.cpp

    r248688 r248736  
    282282}
    283283
     284static Optional<String> snapshotCanvas(HTMLCanvasElement& canvasElement, CanvasRenderingContext& canvasRenderingContext)
     285{
     286#if ENABLE(WEBGL)
     287    if (is<WebGLRenderingContextBase>(canvasRenderingContext))
     288        downcast<WebGLRenderingContextBase>(canvasRenderingContext).setPreventBufferClearForInspector(true);
     289#endif
     290
     291    auto result = canvasElement.toDataURL("image/png"_s);
     292
     293#if ENABLE(WEBGL)
     294    if (is<WebGLRenderingContextBase>(canvasRenderingContext))
     295        downcast<WebGLRenderingContextBase>(canvasRenderingContext).setPreventBufferClearForInspector(false);
     296#endif
     297
     298    if (!result.hasException())
     299        return result.releaseReturnValue().string;
     300
     301    return WTF::nullopt;
     302}
     303
    284304void PageConsoleClient::screenshot(JSC::ExecState* state, Ref<ScriptArguments>&& arguments)
    285305{
     
    322342                    }
    323343#endif
     344                    else if (is<HTMLCanvasElement>(node)) {
     345                        auto& canvasElement = downcast<HTMLCanvasElement>(*node);
     346                        if (auto* canvasRenderingContext = canvasElement.renderingContext()) {
     347                            if (auto result = snapshotCanvas(canvasElement, *canvasRenderingContext))
     348                                dataURL = result.value();
     349                        }
     350                    }
    324351                }
    325352
    326                 if (!snapshot)
    327                     snapshot = WebCore::snapshotNode(m_page.mainFrame(), *node);
    328 
    329                 if (snapshot)
    330                     dataURL = snapshot->toDataURL("image/png"_s, WTF::nullopt, PreserveResolution::Yes);
     353                if (dataURL.isEmpty()) {
     354                    if (!snapshot)
     355                        snapshot = WebCore::snapshotNode(m_page.mainFrame(), *node);
     356
     357                    if (snapshot)
     358                        dataURL = snapshot->toDataURL("image/png"_s, WTF::nullopt, PreserveResolution::Yes);
     359                }
    331360            }
    332361        } else if (auto* imageData = JSImageData::toWrapped(state->vm(), possibleTarget)) {
     
    351380                target = possibleTarget;
    352381                if (UNLIKELY(InspectorInstrumentation::hasFrontends())) {
    353 #if ENABLE(WEBGL)
    354                     if (is<WebGLRenderingContextBase>(context))
    355                         downcast<WebGLRenderingContextBase>(context)->setPreventBufferClearForInspector(true);
    356 #endif
    357 
    358                     auto result = downcast<HTMLCanvasElement>(canvas).toDataURL("image/png"_s);
    359 
    360 #if ENABLE(WEBGL)
    361                     if (is<WebGLRenderingContextBase>(context))
    362                         downcast<WebGLRenderingContextBase>(context)->setPreventBufferClearForInspector(false);
    363 #endif
    364 
    365                     if (!result.hasException())
    366                         dataURL = result.releaseReturnValue().string;
     382                    if (auto result = snapshotCanvas(downcast<HTMLCanvasElement>(canvas), *context))
     383                        dataURL = result.value();
    367384                }
    368385            }
Note: See TracChangeset for help on using the changeset viewer.