diff options
author | Andreas Kling <kling@serenityos.org> | 2020-06-21 11:39:32 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-06-21 11:39:32 +0200 |
commit | a64033e581bd82816051456cb1e3b6b21762c543 (patch) | |
tree | b6e4721d2f0e2bf57e2e0754c9d38bf28fa54051 /Libraries/LibWeb/Bindings | |
parent | 5eb39a5f61a2617e795b26ba95bc73d83d43a195 (diff) | |
download | serenity-a64033e581bd82816051456cb1e3b6b21762c543.zip |
LibWeb: Generate Element bindings from IDL :^)
Had to do a bunch more hacking on WrapperGenerator to support this.
We now support attribute setters as well.
Diffstat (limited to 'Libraries/LibWeb/Bindings')
-rw-r--r-- | Libraries/LibWeb/Bindings/ElementWrapper.cpp | 157 | ||||
-rw-r--r-- | Libraries/LibWeb/Bindings/ElementWrapper.h | 57 | ||||
-rw-r--r-- | Libraries/LibWeb/Bindings/Wrapper.h | 1 |
3 files changed, 1 insertions, 214 deletions
diff --git a/Libraries/LibWeb/Bindings/ElementWrapper.cpp b/Libraries/LibWeb/Bindings/ElementWrapper.cpp deleted file mode 100644 index 1fca778af3..0000000000 --- a/Libraries/LibWeb/Bindings/ElementWrapper.cpp +++ /dev/null @@ -1,157 +0,0 @@ -/* - * Copyright (c) 2020, Andreas Kling <kling@serenityos.org> - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include <AK/FlyString.h> -#include <AK/Function.h> -#include <LibJS/Interpreter.h> -#include <LibJS/Runtime/Error.h> -#include <LibJS/Runtime/GlobalObject.h> -#include <LibJS/Runtime/PrimitiveString.h> -#include <LibJS/Runtime/Value.h> -#include <LibWeb/Bindings/ElementWrapper.h> -#include <LibWeb/DOM/AttributeNames.h> -#include <LibWeb/DOM/Element.h> - -namespace Web { -namespace Bindings { - -ElementWrapper::ElementWrapper(JS::GlobalObject& global_object, Element& element) - : NodeWrapper(global_object, element) -{ -} - -void ElementWrapper::initialize(JS::Interpreter& interpreter, JS::GlobalObject& global_object) -{ - NodeWrapper::initialize(interpreter, global_object); - - define_native_property("innerHTML", inner_html_getter, inner_html_setter); - define_native_property("id", id_getter, id_setter); - - u8 attributes = JS::Attribute::Configurable | JS::Attribute::Enumerable | JS::Attribute::Writable; - define_native_function("getAttribute", get_attribute, 1, attributes); - define_native_function("setAttribute", set_attribute, 2, attributes); -} - -ElementWrapper::~ElementWrapper() -{ -} - -Element& ElementWrapper::node() -{ - return static_cast<Element&>(NodeWrapper::impl()); -} - -const Element& ElementWrapper::node() const -{ - return static_cast<const Element&>(NodeWrapper::impl()); -} - -static Element* impl_from(JS::Interpreter& interpreter, JS::GlobalObject& global_object) -{ - auto* this_object = interpreter.this_value(global_object).to_object(interpreter, global_object); - if (!this_object) - return nullptr; - // FIXME: Verify that it's an ElementWrapper somehow! - return &static_cast<ElementWrapper*>(this_object)->node(); -} - -JS_DEFINE_NATIVE_FUNCTION(ElementWrapper::get_attribute) -{ - auto* impl = impl_from(interpreter, global_object); - if (!impl) - return {}; - - if (interpreter.argument_count() < 1) - return interpreter.throw_exception<JS::TypeError>(JS::ErrorType::BadArgCountOne, "getAttribute"); - - auto attribute_name = interpreter.argument(0).to_string(interpreter); - if (interpreter.exception()) - return {}; - - auto attribute_value = impl->attribute(attribute_name); - if (attribute_value.is_null()) - return JS::js_null(); - - return JS::js_string(interpreter, attribute_value); -} - -JS_DEFINE_NATIVE_FUNCTION(ElementWrapper::set_attribute) -{ - auto* impl = impl_from(interpreter, global_object); - if (!impl) - return {}; - - if (interpreter.argument_count() < 2) - return interpreter.throw_exception<JS::TypeError>(JS::ErrorType::BadArgCountMany, "setAttribute", "two"); - - auto attribute_name = interpreter.argument(0).to_string(interpreter); - if (interpreter.exception()) - return {}; - - auto attribute_value = interpreter.argument(1).to_string(interpreter); - if (interpreter.exception()) - return {}; - - impl->set_attribute(attribute_name, attribute_value); - return JS::js_undefined(); -} - -JS_DEFINE_NATIVE_GETTER(ElementWrapper::inner_html_getter) -{ - if (auto* impl = impl_from(interpreter, global_object)) - return JS::js_string(interpreter, impl->inner_html()); - return {}; -} - -JS_DEFINE_NATIVE_SETTER(ElementWrapper::inner_html_setter) -{ - if (auto* impl = impl_from(interpreter, global_object)) { - auto string = value.to_string(interpreter); - if (interpreter.exception()) - return; - impl->set_inner_html(string); - } -} - -JS_DEFINE_NATIVE_GETTER(ElementWrapper::id_getter) -{ - if (auto* impl = impl_from(interpreter, global_object)) - return JS::js_string(interpreter, impl->attribute(HTML::AttributeNames::id)); - return {}; -} - -JS_DEFINE_NATIVE_SETTER(ElementWrapper::id_setter) -{ - if (auto* impl = impl_from(interpreter, global_object)) { - auto string = value.to_string(interpreter); - if (interpreter.exception()) - return; - impl->set_attribute(HTML::AttributeNames::id, string); - } -} - -} -} diff --git a/Libraries/LibWeb/Bindings/ElementWrapper.h b/Libraries/LibWeb/Bindings/ElementWrapper.h deleted file mode 100644 index b7e53d1247..0000000000 --- a/Libraries/LibWeb/Bindings/ElementWrapper.h +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright (c) 2020, Andreas Kling <kling@serenityos.org> - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#pragma once - -#include <LibWeb/Bindings/NodeWrapper.h> - -namespace Web { -namespace Bindings { - -class ElementWrapper : public NodeWrapper { -public: - ElementWrapper(JS::GlobalObject&, Element&); - virtual void initialize(JS::Interpreter&, JS::GlobalObject&) override; - virtual ~ElementWrapper() override; - - Element& node(); - const Element& node() const; - -private: - virtual const char* class_name() const override { return "ElementWrapper"; } - - JS_DECLARE_NATIVE_GETTER(inner_html_getter); - JS_DECLARE_NATIVE_SETTER(inner_html_setter); - - JS_DECLARE_NATIVE_GETTER(id_getter); - JS_DECLARE_NATIVE_SETTER(id_setter); - - JS_DECLARE_NATIVE_FUNCTION(get_attribute); - JS_DECLARE_NATIVE_FUNCTION(set_attribute); -}; - -} -} diff --git a/Libraries/LibWeb/Bindings/Wrapper.h b/Libraries/LibWeb/Bindings/Wrapper.h index 24c288b01c..09d9a6b5cb 100644 --- a/Libraries/LibWeb/Bindings/Wrapper.h +++ b/Libraries/LibWeb/Bindings/Wrapper.h @@ -40,6 +40,7 @@ class Wrapper public: virtual bool is_node_wrapper() const { return false; } virtual bool is_document_wrapper() const { return false; } + virtual bool is_element_wrapper() const { return false; } protected: explicit Wrapper(Object& prototype) |