Serenity Operating System
at hosted 100 lines 3.5 kB view raw
1/* 2 * Copyright (c) 2020, Andreas Kling <kling@serenityos.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include <AK/FlyString.h> 28#include <AK/Function.h> 29#include <LibJS/Interpreter.h> 30#include <LibJS/Runtime/PrimitiveString.h> 31#include <LibJS/Runtime/Value.h> 32#include <LibWeb/Bindings/CanvasRenderingContext2DWrapper.h> 33#include <LibWeb/Bindings/HTMLCanvasElementWrapper.h> 34#include <LibWeb/DOM/CanvasRenderingContext2D.h> 35#include <LibWeb/DOM/HTMLCanvasElement.h> 36 37namespace Web { 38namespace Bindings { 39 40HTMLCanvasElementWrapper::HTMLCanvasElementWrapper(HTMLCanvasElement& element) 41 : ElementWrapper(element) 42{ 43 put_native_function("getContext", get_context, 1); 44 45 put_native_property("width", width_getter, nullptr); 46 put_native_property("height", height_getter, nullptr); 47} 48 49HTMLCanvasElementWrapper::~HTMLCanvasElementWrapper() 50{ 51} 52 53HTMLCanvasElement& HTMLCanvasElementWrapper::node() 54{ 55 return static_cast<HTMLCanvasElement&>(NodeWrapper::node()); 56} 57 58const HTMLCanvasElement& HTMLCanvasElementWrapper::node() const 59{ 60 return static_cast<const HTMLCanvasElement&>(NodeWrapper::node()); 61} 62 63static HTMLCanvasElement* impl_from(JS::Interpreter& interpreter) 64{ 65 auto* this_object = interpreter.this_value().to_object(interpreter.heap()); 66 if (!this_object) 67 return nullptr; 68 // FIXME: Verify that it's a HTMLCanvasElementWrapper somehow! 69 return &static_cast<HTMLCanvasElementWrapper*>(this_object)->node(); 70} 71 72JS::Value HTMLCanvasElementWrapper::get_context(JS::Interpreter& interpreter) 73{ 74 auto* impl = impl_from(interpreter); 75 if (!impl) 76 return {}; 77 auto& arguments = interpreter.call_frame().arguments; 78 if (arguments.size() >= 1) { 79 auto* context = impl->get_context(arguments[0].to_string()); 80 return wrap(interpreter.heap(), *context); 81 } 82 return JS::js_undefined(); 83} 84 85JS::Value HTMLCanvasElementWrapper::width_getter(JS::Interpreter& interpreter) 86{ 87 if (auto* impl = impl_from(interpreter)) 88 return JS::Value(impl->preferred_width()); 89 return {}; 90} 91 92JS::Value HTMLCanvasElementWrapper::height_getter(JS::Interpreter& interpreter) 93{ 94 if (auto* impl = impl_from(interpreter)) 95 return JS::Value(impl->preferred_height()); 96 return {}; 97} 98 99} 100}