diff options
author | Andreas Kling <kling@serenityos.org> | 2021-01-12 12:17:30 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-01-12 12:17:46 +0100 |
commit | 13d7c09125f8eec703d0a43a9a87fc8aa08f7319 (patch) | |
tree | 70fd643c429cea5c1f9362c2674511d17a53f3b5 /Userland/Libraries/LibWeb/Bindings | |
parent | dc28c07fa526841e05e16161c74a6c23984f1dd5 (diff) | |
download | serenity-13d7c09125f8eec703d0a43a9a87fc8aa08f7319.zip |
Libraries: Move to Userland/Libraries/
Diffstat (limited to 'Userland/Libraries/LibWeb/Bindings')
31 files changed, 2437 insertions, 0 deletions
diff --git a/Userland/Libraries/LibWeb/Bindings/EventListenerWrapper.cpp b/Userland/Libraries/LibWeb/Bindings/EventListenerWrapper.cpp new file mode 100644 index 0000000000..2f9cbac2c2 --- /dev/null +++ b/Userland/Libraries/LibWeb/Bindings/EventListenerWrapper.cpp @@ -0,0 +1,46 @@ +/* + * 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 <LibJS/Runtime/Function.h> +#include <LibJS/Runtime/GlobalObject.h> +#include <LibWeb/Bindings/EventListenerWrapper.h> +#include <LibWeb/DOM/EventListener.h> + +namespace Web { +namespace Bindings { + +EventListenerWrapper::EventListenerWrapper(JS::GlobalObject& global_object, DOM::EventListener& impl) + : Wrapper(*global_object.object_prototype()) + , m_impl(impl) +{ +} + +EventListenerWrapper::~EventListenerWrapper() +{ +} + +} +} diff --git a/Userland/Libraries/LibWeb/Bindings/EventListenerWrapper.h b/Userland/Libraries/LibWeb/Bindings/EventListenerWrapper.h new file mode 100644 index 0000000000..fdd74108ea --- /dev/null +++ b/Userland/Libraries/LibWeb/Bindings/EventListenerWrapper.h @@ -0,0 +1,49 @@ +/* + * 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/Wrapper.h> + +namespace Web { +namespace Bindings { + +class EventListenerWrapper final : public Wrapper { + JS_OBJECT(EventListenerWrapper, Wrapper); + +public: + EventListenerWrapper(JS::GlobalObject&, DOM::EventListener&); + virtual ~EventListenerWrapper() override; + + DOM::EventListener& impl() { return *m_impl; } + const DOM::EventListener& impl() const { return *m_impl; } + +private: + NonnullRefPtr<DOM::EventListener> m_impl; +}; + +} +} diff --git a/Userland/Libraries/LibWeb/Bindings/EventTargetWrapperFactory.cpp b/Userland/Libraries/LibWeb/Bindings/EventTargetWrapperFactory.cpp new file mode 100644 index 0000000000..4a6e6344e9 --- /dev/null +++ b/Userland/Libraries/LibWeb/Bindings/EventTargetWrapperFactory.cpp @@ -0,0 +1,37 @@ +/* + * 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 <LibWeb/Bindings/EventTargetWrapperFactory.h> +#include <LibWeb/DOM/EventTarget.h> + +namespace Web::Bindings { + +EventTargetWrapper* wrap(JS::GlobalObject& global_object, DOM::EventTarget& target) +{ + return target.create_wrapper(global_object); +} + +} diff --git a/Userland/Libraries/LibWeb/Bindings/EventTargetWrapperFactory.h b/Userland/Libraries/LibWeb/Bindings/EventTargetWrapperFactory.h new file mode 100644 index 0000000000..c28c7724ca --- /dev/null +++ b/Userland/Libraries/LibWeb/Bindings/EventTargetWrapperFactory.h @@ -0,0 +1,36 @@ +/* + * 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 <LibJS/Forward.h> +#include <LibWeb/Forward.h> + +namespace Web::Bindings { + +EventTargetWrapper* wrap(JS::GlobalObject&, DOM::EventTarget&); + +} diff --git a/Userland/Libraries/LibWeb/Bindings/EventWrapperFactory.cpp b/Userland/Libraries/LibWeb/Bindings/EventWrapperFactory.cpp new file mode 100644 index 0000000000..3064799cf0 --- /dev/null +++ b/Userland/Libraries/LibWeb/Bindings/EventWrapperFactory.cpp @@ -0,0 +1,42 @@ +/* + * 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 <LibWeb/Bindings/EventWrapper.h> +#include <LibWeb/Bindings/EventWrapperFactory.h> +#include <LibWeb/Bindings/MouseEventWrapper.h> + +namespace Web { +namespace Bindings { + +EventWrapper* wrap(JS::GlobalObject& global_object, DOM::Event& event) +{ + if (is<UIEvents::MouseEvent>(event)) + return static_cast<MouseEventWrapper*>(wrap_impl(global_object, static_cast<UIEvents::MouseEvent&>(event))); + return static_cast<EventWrapper*>(wrap_impl(global_object, event)); +} + +} +} diff --git a/Userland/Libraries/LibWeb/Bindings/EventWrapperFactory.h b/Userland/Libraries/LibWeb/Bindings/EventWrapperFactory.h new file mode 100644 index 0000000000..ebe82c5e88 --- /dev/null +++ b/Userland/Libraries/LibWeb/Bindings/EventWrapperFactory.h @@ -0,0 +1,36 @@ +/* + * 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 <LibJS/Forward.h> +#include <LibWeb/Forward.h> + +namespace Web::Bindings { + +EventWrapper* wrap(JS::GlobalObject&, DOM::Event&); + +} diff --git a/Userland/Libraries/LibWeb/Bindings/LocationObject.cpp b/Userland/Libraries/LibWeb/Bindings/LocationObject.cpp new file mode 100644 index 0000000000..93d00d8af7 --- /dev/null +++ b/Userland/Libraries/LibWeb/Bindings/LocationObject.cpp @@ -0,0 +1,146 @@ +/* + * 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/StringBuilder.h> +#include <LibWeb/Bindings/LocationObject.h> +#include <LibWeb/Bindings/WindowObject.h> +#include <LibWeb/DOM/Document.h> +#include <LibWeb/DOM/Window.h> + +namespace Web { +namespace Bindings { + +LocationObject::LocationObject(JS::GlobalObject& global_object) + : Object(*global_object.object_prototype()) +{ +} + +void LocationObject::initialize(JS::GlobalObject& global_object) +{ + Object::initialize(global_object); + u8 attr = JS::Attribute::Writable | JS::Attribute::Enumerable; + define_native_property("href", href_getter, href_setter, attr); + define_native_property("host", host_getter, nullptr, attr); + define_native_property("hostname", hostname_getter, nullptr, attr); + define_native_property("pathname", pathname_getter, nullptr, attr); + define_native_property("hash", hash_getter, nullptr, attr); + define_native_property("search", search_getter, nullptr, attr); + define_native_property("protocol", protocol_getter, nullptr, attr); + + define_native_function("reload", reload, 0, JS::Attribute::Enumerable); +} + +LocationObject::~LocationObject() +{ +} + +JS_DEFINE_NATIVE_GETTER(LocationObject::href_getter) +{ + auto& window = static_cast<WindowObject&>(global_object); + return JS::js_string(vm, window.impl().document().url().to_string()); +} + +JS_DEFINE_NATIVE_SETTER(LocationObject::href_setter) +{ + auto& window = static_cast<WindowObject&>(global_object); + auto new_href = value.to_string(global_object); + if (vm.exception()) + return; + auto href_url = window.impl().document().complete_url(new_href); + if (!href_url.is_valid()) { + vm.throw_exception<JS::URIError>(global_object, String::formatted("Invalid URL '{}'", new_href)); + return; + } + window.impl().did_set_location_href({}, href_url); +} + +JS_DEFINE_NATIVE_GETTER(LocationObject::pathname_getter) +{ + auto& window = static_cast<WindowObject&>(global_object); + return JS::js_string(vm, window.impl().document().url().path()); +} + +JS_DEFINE_NATIVE_GETTER(LocationObject::hostname_getter) +{ + auto& window = static_cast<WindowObject&>(global_object); + return JS::js_string(vm, window.impl().document().url().host()); +} + +JS_DEFINE_NATIVE_GETTER(LocationObject::host_getter) +{ + auto& window = static_cast<WindowObject&>(global_object); + auto url = window.impl().document().url(); + StringBuilder builder; + builder.append(url.host()); + builder.append(':'); + builder.appendf("%u", url.port()); + return JS::js_string(vm, builder.to_string()); +} + +JS_DEFINE_NATIVE_GETTER(LocationObject::hash_getter) +{ + auto& window = static_cast<WindowObject&>(global_object); + auto fragment = window.impl().document().url().fragment(); + if (!fragment.length()) + return JS::js_string(vm, ""); + StringBuilder builder; + builder.append('#'); + builder.append(fragment); + return JS::js_string(vm, builder.to_string()); +} + +JS_DEFINE_NATIVE_GETTER(LocationObject::search_getter) +{ + auto& window = static_cast<WindowObject&>(global_object); + auto query = window.impl().document().url().query(); + if (!query.length()) + return JS::js_string(vm, ""); + StringBuilder builder; + builder.append('?'); + builder.append(query); + return JS::js_string(vm, builder.to_string()); +} + +JS_DEFINE_NATIVE_GETTER(LocationObject::protocol_getter) +{ + auto& window = static_cast<WindowObject&>(global_object); + StringBuilder builder; + builder.append(window.impl().document().url().protocol()); + builder.append(':'); + return JS::js_string(vm, builder.to_string()); +} + +JS_DEFINE_NATIVE_FUNCTION(LocationObject::reload) +{ + auto& window = static_cast<WindowObject&>(global_object); + window.impl().did_call_location_reload({}); + return JS::js_undefined(); +} + +} + +} diff --git a/Userland/Libraries/LibWeb/Bindings/LocationObject.h b/Userland/Libraries/LibWeb/Bindings/LocationObject.h new file mode 100644 index 0000000000..76a38946c1 --- /dev/null +++ b/Userland/Libraries/LibWeb/Bindings/LocationObject.h @@ -0,0 +1,58 @@ +/* + * 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 <LibJS/Runtime/Object.h> +#include <LibWeb/Forward.h> + +namespace Web { +namespace Bindings { + +class LocationObject final : public JS::Object { + JS_OBJECT(LocationObject, JS::Object); + +public: + explicit LocationObject(JS::GlobalObject&); + virtual void initialize(JS::GlobalObject&) override; + virtual ~LocationObject() override; + +private: + JS_DECLARE_NATIVE_FUNCTION(reload); + + JS_DECLARE_NATIVE_GETTER(href_getter); + JS_DECLARE_NATIVE_SETTER(href_setter); + + JS_DECLARE_NATIVE_GETTER(host_getter); + JS_DECLARE_NATIVE_GETTER(hostname_getter); + JS_DECLARE_NATIVE_GETTER(pathname_getter); + JS_DECLARE_NATIVE_GETTER(hash_getter); + JS_DECLARE_NATIVE_GETTER(search_getter); + JS_DECLARE_NATIVE_GETTER(protocol_getter); +}; + +} +} diff --git a/Userland/Libraries/LibWeb/Bindings/NavigatorObject.cpp b/Userland/Libraries/LibWeb/Bindings/NavigatorObject.cpp new file mode 100644 index 0000000000..248097d8e3 --- /dev/null +++ b/Userland/Libraries/LibWeb/Bindings/NavigatorObject.cpp @@ -0,0 +1,69 @@ +/* + * 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 <LibJS/Runtime/Array.h> +#include <LibJS/Runtime/GlobalObject.h> +#include <LibWeb/Bindings/NavigatorObject.h> +#include <LibWeb/Loader/ResourceLoader.h> + +namespace Web { +namespace Bindings { + +NavigatorObject::NavigatorObject(JS::GlobalObject& global_object) + : Object(*global_object.object_prototype()) +{ +} + +void NavigatorObject::initialize(JS::GlobalObject& global_object) +{ + auto& heap = this->heap(); + auto* languages = JS::Array::create(global_object); + languages->indexed_properties().append(js_string(heap, "en-US")); + + define_property("appCodeName", js_string(heap, "Mozilla")); + define_property("appName", js_string(heap, "Netscape")); + define_property("appVersion", js_string(heap, "4.0")); + define_property("language", languages->get(0)); + define_property("languages", languages); + define_property("platform", js_string(heap, "SerenityOS")); + define_property("product", js_string(heap, "Gecko")); + + define_native_property("userAgent", user_agent_getter, nullptr); +} + +NavigatorObject::~NavigatorObject() +{ +} + +JS_DEFINE_NATIVE_GETTER(NavigatorObject::user_agent_getter) +{ + return JS::js_string(vm, ResourceLoader::the().user_agent()); +} + +} + +} diff --git a/Userland/Libraries/LibWeb/Bindings/NavigatorObject.h b/Userland/Libraries/LibWeb/Bindings/NavigatorObject.h new file mode 100644 index 0000000000..fe10271e96 --- /dev/null +++ b/Userland/Libraries/LibWeb/Bindings/NavigatorObject.h @@ -0,0 +1,48 @@ +/* + * 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 <LibJS/Runtime/Object.h> +#include <LibWeb/Forward.h> + +namespace Web { +namespace Bindings { + +class NavigatorObject final : public JS::Object { + JS_OBJECT(NavigatorObject, JS::Object); + +public: + NavigatorObject(JS::GlobalObject&); + virtual void initialize(JS::GlobalObject&) override; + virtual ~NavigatorObject() override; + +private: + JS_DECLARE_NATIVE_GETTER(user_agent_getter); +}; + +} +} diff --git a/Userland/Libraries/LibWeb/Bindings/NodeWrapperFactory.cpp b/Userland/Libraries/LibWeb/Bindings/NodeWrapperFactory.cpp new file mode 100644 index 0000000000..f78491d33a --- /dev/null +++ b/Userland/Libraries/LibWeb/Bindings/NodeWrapperFactory.cpp @@ -0,0 +1,347 @@ +/* + * Copyright (c) 2020, Andreas Kling <kling@serenityos.org> + * Copyright (c) 2020, Luke Wilde <luke.wilde@live.co.uk> + * 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 <LibWeb/Bindings/CharacterDataWrapper.h> +#include <LibWeb/Bindings/CommentWrapper.h> +#include <LibWeb/Bindings/DocumentFragmentWrapper.h> +#include <LibWeb/Bindings/DocumentTypeWrapper.h> +#include <LibWeb/Bindings/DocumentWrapper.h> +#include <LibWeb/Bindings/HTMLAnchorElementWrapper.h> +#include <LibWeb/Bindings/HTMLAreaElementWrapper.h> +#include <LibWeb/Bindings/HTMLAudioElementWrapper.h> +#include <LibWeb/Bindings/HTMLBRElementWrapper.h> +#include <LibWeb/Bindings/HTMLBaseElementWrapper.h> +#include <LibWeb/Bindings/HTMLBodyElementWrapper.h> +#include <LibWeb/Bindings/HTMLButtonElementWrapper.h> +#include <LibWeb/Bindings/HTMLCanvasElementWrapper.h> +#include <LibWeb/Bindings/HTMLDListElementWrapper.h> +#include <LibWeb/Bindings/HTMLDataElementWrapper.h> +#include <LibWeb/Bindings/HTMLDataListElementWrapper.h> +#include <LibWeb/Bindings/HTMLDetailsElementWrapper.h> +#include <LibWeb/Bindings/HTMLDialogElementWrapper.h> +#include <LibWeb/Bindings/HTMLDirectoryElementWrapper.h> +#include <LibWeb/Bindings/HTMLDivElementWrapper.h> +#include <LibWeb/Bindings/HTMLElementWrapper.h> +#include <LibWeb/Bindings/HTMLEmbedElementWrapper.h> +#include <LibWeb/Bindings/HTMLFieldSetElementWrapper.h> +#include <LibWeb/Bindings/HTMLFontElementWrapper.h> +#include <LibWeb/Bindings/HTMLFormElementWrapper.h> +#include <LibWeb/Bindings/HTMLFrameElementWrapper.h> +#include <LibWeb/Bindings/HTMLFrameSetElementWrapper.h> +#include <LibWeb/Bindings/HTMLHRElementWrapper.h> +#include <LibWeb/Bindings/HTMLHeadElementWrapper.h> +#include <LibWeb/Bindings/HTMLHeadingElementWrapper.h> +#include <LibWeb/Bindings/HTMLHtmlElementWrapper.h> +#include <LibWeb/Bindings/HTMLIFrameElementWrapper.h> +#include <LibWeb/Bindings/HTMLImageElementWrapper.h> +#include <LibWeb/Bindings/HTMLInputElementWrapper.h> +#include <LibWeb/Bindings/HTMLLIElementWrapper.h> +#include <LibWeb/Bindings/HTMLLabelElementWrapper.h> +#include <LibWeb/Bindings/HTMLLegendElementWrapper.h> +#include <LibWeb/Bindings/HTMLLinkElementWrapper.h> +#include <LibWeb/Bindings/HTMLMapElementWrapper.h> +#include <LibWeb/Bindings/HTMLMarqueeElementWrapper.h> +#include <LibWeb/Bindings/HTMLMenuElementWrapper.h> +#include <LibWeb/Bindings/HTMLMetaElementWrapper.h> +#include <LibWeb/Bindings/HTMLMeterElementWrapper.h> +#include <LibWeb/Bindings/HTMLModElementWrapper.h> +#include <LibWeb/Bindings/HTMLOListElementWrapper.h> +#include <LibWeb/Bindings/HTMLObjectElementWrapper.h> +#include <LibWeb/Bindings/HTMLOptGroupElementWrapper.h> +#include <LibWeb/Bindings/HTMLOptionElementWrapper.h> +#include <LibWeb/Bindings/HTMLOutputElementWrapper.h> +#include <LibWeb/Bindings/HTMLParagraphElementWrapper.h> +#include <LibWeb/Bindings/HTMLParamElementWrapper.h> +#include <LibWeb/Bindings/HTMLPictureElementWrapper.h> +#include <LibWeb/Bindings/HTMLPreElementWrapper.h> +#include <LibWeb/Bindings/HTMLProgressElementWrapper.h> +#include <LibWeb/Bindings/HTMLQuoteElementWrapper.h> +#include <LibWeb/Bindings/HTMLScriptElementWrapper.h> +#include <LibWeb/Bindings/HTMLSelectElementWrapper.h> +#include <LibWeb/Bindings/HTMLSlotElementWrapper.h> +#include <LibWeb/Bindings/HTMLSourceElementWrapper.h> +#include <LibWeb/Bindings/HTMLSpanElementWrapper.h> +#include <LibWeb/Bindings/HTMLStyleElementWrapper.h> +#include <LibWeb/Bindings/HTMLTableCaptionElementWrapper.h> +#include <LibWeb/Bindings/HTMLTableCellElementWrapper.h> +#include <LibWeb/Bindings/HTMLTableColElementWrapper.h> +#include <LibWeb/Bindings/HTMLTableElementWrapper.h> +#include <LibWeb/Bindings/HTMLTableRowElementWrapper.h> +#include <LibWeb/Bindings/HTMLTableSectionElementWrapper.h> +#include <LibWeb/Bindings/HTMLTemplateElementWrapper.h> +#include <LibWeb/Bindings/HTMLTextAreaElementWrapper.h> +#include <LibWeb/Bindings/HTMLTimeElementWrapper.h> +#include <LibWeb/Bindings/HTMLTitleElementWrapper.h> +#include <LibWeb/Bindings/HTMLTrackElementWrapper.h> +#include <LibWeb/Bindings/HTMLUListElementWrapper.h> +#include <LibWeb/Bindings/HTMLUnknownElementWrapper.h> +#include <LibWeb/Bindings/HTMLVideoElementWrapper.h> +#include <LibWeb/Bindings/NodeWrapper.h> +#include <LibWeb/Bindings/NodeWrapperFactory.h> +#include <LibWeb/Bindings/SVGPathElementWrapper.h> +#include <LibWeb/Bindings/SVGSVGElementWrapper.h> +#include <LibWeb/Bindings/TextWrapper.h> +#include <LibWeb/DOM/Document.h> +#include <LibWeb/DOM/Node.h> +#include <LibWeb/HTML/HTMLAnchorElement.h> +#include <LibWeb/HTML/HTMLAreaElement.h> +#include <LibWeb/HTML/HTMLAudioElement.h> +#include <LibWeb/HTML/HTMLBRElement.h> +#include <LibWeb/HTML/HTMLBaseElement.h> +#include <LibWeb/HTML/HTMLBodyElement.h> +#include <LibWeb/HTML/HTMLButtonElement.h> +#include <LibWeb/HTML/HTMLCanvasElement.h> +#include <LibWeb/HTML/HTMLDListElement.h> +#include <LibWeb/HTML/HTMLDataElement.h> +#include <LibWeb/HTML/HTMLDataListElement.h> +#include <LibWeb/HTML/HTMLDetailsElement.h> +#include <LibWeb/HTML/HTMLDialogElement.h> +#include <LibWeb/HTML/HTMLDirectoryElement.h> +#include <LibWeb/HTML/HTMLDivElement.h> +#include <LibWeb/HTML/HTMLEmbedElement.h> +#include <LibWeb/HTML/HTMLFieldSetElement.h> +#include <LibWeb/HTML/HTMLFontElement.h> +#include <LibWeb/HTML/HTMLFormElement.h> +#include <LibWeb/HTML/HTMLFrameElement.h> +#include <LibWeb/HTML/HTMLFrameSetElement.h> +#include <LibWeb/HTML/HTMLHRElement.h> +#include <LibWeb/HTML/HTMLHeadElement.h> +#include <LibWeb/HTML/HTMLHeadingElement.h> +#include <LibWeb/HTML/HTMLHtmlElement.h> +#include <LibWeb/HTML/HTMLIFrameElement.h> +#include <LibWeb/HTML/HTMLImageElement.h> +#include <LibWeb/HTML/HTMLInputElement.h> +#include <LibWeb/HTML/HTMLLIElement.h> +#include <LibWeb/HTML/HTMLLabelElement.h> +#include <LibWeb/HTML/HTMLLegendElement.h> +#include <LibWeb/HTML/HTMLLinkElement.h> +#include <LibWeb/HTML/HTMLMapElement.h> +#include <LibWeb/HTML/HTMLMarqueeElement.h> +#include <LibWeb/HTML/HTMLMenuElement.h> +#include <LibWeb/HTML/HTMLMetaElement.h> +#include <LibWeb/HTML/HTMLMeterElement.h> +#include <LibWeb/HTML/HTMLModElement.h> +#include <LibWeb/HTML/HTMLOListElement.h> +#include <LibWeb/HTML/HTMLObjectElement.h> +#include <LibWeb/HTML/HTMLOptGroupElement.h> +#include <LibWeb/HTML/HTMLOptionElement.h> +#include <LibWeb/HTML/HTMLOutputElement.h> +#include <LibWeb/HTML/HTMLParagraphElement.h> +#include <LibWeb/HTML/HTMLParamElement.h> +#include <LibWeb/HTML/HTMLPictureElement.h> +#include <LibWeb/HTML/HTMLPreElement.h> +#include <LibWeb/HTML/HTMLProgressElement.h> +#include <LibWeb/HTML/HTMLQuoteElement.h> +#include <LibWeb/HTML/HTMLScriptElement.h> +#include <LibWeb/HTML/HTMLSelectElement.h> +#include <LibWeb/HTML/HTMLSlotElement.h> +#include <LibWeb/HTML/HTMLSourceElement.h> +#include <LibWeb/HTML/HTMLSpanElement.h> +#include <LibWeb/HTML/HTMLStyleElement.h> +#include <LibWeb/HTML/HTMLTableCaptionElement.h> +#include <LibWeb/HTML/HTMLTableCellElement.h> +#include <LibWeb/HTML/HTMLTableColElement.h> +#include <LibWeb/HTML/HTMLTableElement.h> +#include <LibWeb/HTML/HTMLTableRowElement.h> +#include <LibWeb/HTML/HTMLTableSectionElement.h> +#include <LibWeb/HTML/HTMLTemplateElement.h> +#include <LibWeb/HTML/HTMLTextAreaElement.h> +#include <LibWeb/HTML/HTMLTimeElement.h> +#include <LibWeb/HTML/HTMLTitleElement.h> +#include <LibWeb/HTML/HTMLTrackElement.h> +#include <LibWeb/HTML/HTMLUListElement.h> +#include <LibWeb/HTML/HTMLUnknownElement.h> +#include <LibWeb/HTML/HTMLVideoElement.h> +#include <LibWeb/SVG/SVGPathElement.h> +#include <LibWeb/SVG/SVGSVGElement.h> + +namespace Web::Bindings { + +NodeWrapper* wrap(JS::GlobalObject& global_object, DOM::Node& node) +{ + if (is<DOM::Document>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<DOM::Document>(node))); + if (is<DOM::DocumentType>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<DOM::DocumentType>(node))); + if (is<HTML::HTMLAnchorElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLAnchorElement>(node))); + if (is<HTML::HTMLAreaElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLAreaElement>(node))); + if (is<HTML::HTMLAudioElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLAudioElement>(node))); + if (is<HTML::HTMLBaseElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLBaseElement>(node))); + if (is<HTML::HTMLBodyElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLBodyElement>(node))); + if (is<HTML::HTMLBRElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLBRElement>(node))); + if (is<HTML::HTMLButtonElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLButtonElement>(node))); + if (is<HTML::HTMLCanvasElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLCanvasElement>(node))); + if (is<HTML::HTMLDataElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLDataElement>(node))); + if (is<HTML::HTMLDataListElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLDataListElement>(node))); + if (is<HTML::HTMLDetailsElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLDetailsElement>(node))); + if (is<HTML::HTMLDialogElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLDialogElement>(node))); + if (is<HTML::HTMLDirectoryElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLDirectoryElement>(node))); + if (is<HTML::HTMLDivElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLDivElement>(node))); + if (is<HTML::HTMLDListElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLDListElement>(node))); + if (is<HTML::HTMLEmbedElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLEmbedElement>(node))); + if (is<HTML::HTMLFieldSetElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLFieldSetElement>(node))); + if (is<HTML::HTMLFontElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLFontElement>(node))); + if (is<HTML::HTMLFormElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLFormElement>(node))); + if (is<HTML::HTMLFrameElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLFrameElement>(node))); + if (is<HTML::HTMLFrameSetElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLFrameSetElement>(node))); + if (is<HTML::HTMLHeadElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLHeadElement>(node))); + if (is<HTML::HTMLHeadingElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLHeadingElement>(node))); + if (is<HTML::HTMLHRElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLHRElement>(node))); + if (is<HTML::HTMLHtmlElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLHtmlElement>(node))); + if (is<HTML::HTMLIFrameElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLIFrameElement>(node))); + if (is<HTML::HTMLImageElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLImageElement>(node))); + if (is<HTML::HTMLInputElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLInputElement>(node))); + if (is<HTML::HTMLLabelElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLLabelElement>(node))); + if (is<HTML::HTMLLegendElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLLegendElement>(node))); + if (is<HTML::HTMLLIElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLLIElement>(node))); + if (is<HTML::HTMLLinkElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLLinkElement>(node))); + if (is<HTML::HTMLMapElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLMapElement>(node))); + if (is<HTML::HTMLMarqueeElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLMarqueeElement>(node))); + if (is<HTML::HTMLMenuElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLMenuElement>(node))); + if (is<HTML::HTMLMetaElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLMetaElement>(node))); + if (is<HTML::HTMLMeterElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLMeterElement>(node))); + if (is<HTML::HTMLModElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLModElement>(node))); + if (is<HTML::HTMLObjectElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLObjectElement>(node))); + if (is<HTML::HTMLOListElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLOListElement>(node))); + if (is<HTML::HTMLOptGroupElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLOptGroupElement>(node))); + if (is<HTML::HTMLOptionElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLOptionElement>(node))); + if (is<HTML::HTMLOutputElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLOutputElement>(node))); + if (is<HTML::HTMLParagraphElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLParagraphElement>(node))); + if (is<HTML::HTMLParamElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLParamElement>(node))); + if (is<HTML::HTMLPictureElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLPictureElement>(node))); + if (is<HTML::HTMLPreElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLPreElement>(node))); + if (is<HTML::HTMLProgressElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLProgressElement>(node))); + if (is<HTML::HTMLQuoteElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLQuoteElement>(node))); + if (is<HTML::HTMLScriptElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLScriptElement>(node))); + if (is<HTML::HTMLSelectElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLSelectElement>(node))); + if (is<HTML::HTMLSlotElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLSlotElement>(node))); + if (is<HTML::HTMLSourceElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLSourceElement>(node))); + if (is<HTML::HTMLSpanElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLSpanElement>(node))); + if (is<HTML::HTMLStyleElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLStyleElement>(node))); + if (is<HTML::HTMLTableCaptionElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLTableCaptionElement>(node))); + if (is<HTML::HTMLTableCellElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLTableCellElement>(node))); + if (is<HTML::HTMLTableColElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLTableColElement>(node))); + if (is<HTML::HTMLTableElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLTableElement>(node))); + if (is<HTML::HTMLTableRowElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLTableRowElement>(node))); + if (is<HTML::HTMLTableSectionElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLTableSectionElement>(node))); + if (is<HTML::HTMLTemplateElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLTemplateElement>(node))); + if (is<HTML::HTMLTextAreaElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLTextAreaElement>(node))); + if (is<HTML::HTMLTimeElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLTimeElement>(node))); + if (is<HTML::HTMLTitleElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLTitleElement>(node))); + if (is<HTML::HTMLTrackElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLTrackElement>(node))); + if (is<HTML::HTMLUListElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLUListElement>(node))); + if (is<HTML::HTMLUnknownElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLUnknownElement>(node))); + if (is<HTML::HTMLVideoElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLVideoElement>(node))); + if (is<HTML::HTMLElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<HTML::HTMLElement>(node))); + if (is<SVG::SVGSVGElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<SVG::SVGSVGElement>(node))); + if (is<SVG::SVGPathElement>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<SVG::SVGPathElement>(node))); + if (is<DOM::Element>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<DOM::Element>(node))); + if (is<DOM::DocumentFragment>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<DOM::DocumentFragment>(node))); + if (is<DOM::Comment>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<DOM::Comment>(node))); + if (is<DOM::Text>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<DOM::Text>(node))); + if (is<DOM::CharacterData>(node)) + return static_cast<NodeWrapper*>(wrap_impl(global_object, downcast<DOM::CharacterData>(node))); + return static_cast<NodeWrapper*>(wrap_impl(global_object, node)); +} + +} diff --git a/Userland/Libraries/LibWeb/Bindings/NodeWrapperFactory.h b/Userland/Libraries/LibWeb/Bindings/NodeWrapperFactory.h new file mode 100644 index 0000000000..153e78cb32 --- /dev/null +++ b/Userland/Libraries/LibWeb/Bindings/NodeWrapperFactory.h @@ -0,0 +1,38 @@ +/* + * 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 <LibJS/Forward.h> +#include <LibWeb/Forward.h> + +namespace Web { +namespace Bindings { + +NodeWrapper* wrap(JS::GlobalObject&, DOM::Node&); + +} +} diff --git a/Userland/Libraries/LibWeb/Bindings/RangeConstructor.cpp b/Userland/Libraries/LibWeb/Bindings/RangeConstructor.cpp new file mode 100644 index 0000000000..27311c4420 --- /dev/null +++ b/Userland/Libraries/LibWeb/Bindings/RangeConstructor.cpp @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2020, the SerenityOS developers. + * 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 <LibJS/Heap/Heap.h> +#include <LibWeb/Bindings/RangeConstructor.h> +#include <LibWeb/Bindings/RangePrototype.h> +#include <LibWeb/Bindings/RangeWrapper.h> +#include <LibWeb/Bindings/WindowObject.h> +#include <LibWeb/DOM/Range.h> + +namespace Web::Bindings { + +RangeConstructor::RangeConstructor(JS::GlobalObject& global_object) + : NativeFunction(*global_object.function_prototype()) +{ +} + +void RangeConstructor::initialize(JS::GlobalObject& global_object) +{ + auto& vm = this->vm(); + NativeFunction::initialize(global_object); + auto& window = static_cast<WindowObject&>(global_object); + define_property(vm.names.prototype, window.range_prototype(), 0); + define_property(vm.names.length, JS::Value(0), JS::Attribute::Configurable); +} + +JS::Value RangeConstructor::call() +{ + vm().throw_exception<JS::TypeError>(global_object(), JS::ErrorType::ConstructorWithoutNew, "Range"); + return {}; +} + +JS::Value RangeConstructor::construct(Function&) +{ + auto& window = static_cast<WindowObject&>(global_object()); + return heap().allocate<RangeWrapper>(window, window, DOM::Range::create(window.impl())); +} + +} diff --git a/Userland/Libraries/LibWeb/Bindings/RangeConstructor.h b/Userland/Libraries/LibWeb/Bindings/RangeConstructor.h new file mode 100644 index 0000000000..3189c67bc6 --- /dev/null +++ b/Userland/Libraries/LibWeb/Bindings/RangeConstructor.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2020, the SerenityOS developers. + * 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 <LibJS/Runtime/NativeFunction.h> + +namespace Web::Bindings { + +class RangeConstructor final : public JS::NativeFunction { +public: + explicit RangeConstructor(JS::GlobalObject&); + + void initialize(JS::GlobalObject&) override; + + JS::Value call() override; + JS::Value construct(JS::Function& new_target) override; + +private: + bool has_constructor() const override { return true; } + const char* class_name() const override { return "RangeConstructor"; } +}; + +} diff --git a/Userland/Libraries/LibWeb/Bindings/RangePrototype.cpp b/Userland/Libraries/LibWeb/Bindings/RangePrototype.cpp new file mode 100644 index 0000000000..ed317cb1c6 --- /dev/null +++ b/Userland/Libraries/LibWeb/Bindings/RangePrototype.cpp @@ -0,0 +1,161 @@ +/* + * Copyright (c) 2020, the SerenityOS developers. + * 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/Function.h> +#include <LibJS/Runtime/GlobalObject.h> +#include <LibWeb/Bindings/NodeWrapper.h> +#include <LibWeb/Bindings/NodeWrapperFactory.h> +#include <LibWeb/Bindings/RangePrototype.h> +#include <LibWeb/Bindings/RangeWrapper.h> +#include <LibWeb/DOM/Range.h> + +namespace Web::Bindings { + +RangePrototype::RangePrototype(JS::GlobalObject& global_object) + : Object(*global_object.object_prototype()) +{ +} + +void RangePrototype::initialize(JS::GlobalObject& global_object) +{ + auto default_attributes = JS::Attribute::Enumerable | JS::Attribute::Configurable; + + Object::initialize(global_object); + + define_native_function("setStart", set_start, 2); + define_native_function("setEnd", set_end, 2); + define_native_function("cloneRange", clone_range, 0); + + define_native_property("startContainer", start_container_getter, nullptr, default_attributes); + define_native_property("endContainer", end_container_getter, nullptr, default_attributes); + define_native_property("startOffset", start_offset_getter, nullptr, default_attributes); + define_native_property("endOffset", end_offset_getter, nullptr, default_attributes); +} + +static DOM::Range* impl_from(JS::VM& vm, JS::GlobalObject& global_object) +{ + auto* this_object = vm.this_value(global_object).to_object(global_object); + if (!this_object) + return nullptr; + if (StringView("RangeWrapper") != this_object->class_name()) { + vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotA, "Range"); + return nullptr; + } + return &static_cast<RangeWrapper*>(this_object)->impl(); +} + +JS_DEFINE_NATIVE_FUNCTION(RangePrototype::set_start) +{ + auto* impl = impl_from(vm, global_object); + if (!impl) + return {}; + + auto arg0 = vm.argument(0).to_object(global_object); + if (vm.exception()) + return {}; + auto arg1 = vm.argument(1).to_number(global_object); + if (vm.exception()) + return {}; + + if (!is<NodeWrapper>(arg0)) { + vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotA, "Node"); + return {}; + } + + impl->set_start(static_cast<NodeWrapper*>(arg0)->impl(), arg1.as_i32()); + + return JS::js_undefined(); +} + +JS_DEFINE_NATIVE_FUNCTION(RangePrototype::set_end) +{ + auto* impl = impl_from(vm, global_object); + if (!impl) + return {}; + + auto arg0 = vm.argument(0).to_object(global_object); + if (vm.exception()) + return {}; + auto arg1 = vm.argument(1).to_number(global_object); + if (vm.exception()) + return {}; + + if (!is<NodeWrapper>(arg0)) { + vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotA, "Node"); + return {}; + } + + impl->set_end(static_cast<NodeWrapper*>(arg0)->impl(), arg1.as_i32()); + + return JS::js_undefined(); +} + +JS_DEFINE_NATIVE_FUNCTION(RangePrototype::clone_range) +{ + auto* impl = impl_from(vm, global_object); + if (!impl) + return {}; + + return wrap(global_object, *impl->clone_range()); +} + +JS_DEFINE_NATIVE_GETTER(RangePrototype::start_container_getter) +{ + auto* impl = impl_from(vm, global_object); + if (!impl) + return {}; + + return wrap(global_object, *impl->start_container()); +} + +JS_DEFINE_NATIVE_GETTER(RangePrototype::end_container_getter) +{ + auto* impl = impl_from(vm, global_object); + if (!impl) + return {}; + + return wrap(global_object, *impl->end_container()); +} + +JS_DEFINE_NATIVE_GETTER(RangePrototype::start_offset_getter) +{ + auto* impl = impl_from(vm, global_object); + if (!impl) + return {}; + + return JS::Value(impl->start_offset()); +} + +JS_DEFINE_NATIVE_GETTER(RangePrototype::end_offset_getter) +{ + auto* impl = impl_from(vm, global_object); + if (!impl) + return {}; + + return JS::Value(impl->end_offset()); +} + +} diff --git a/Userland/Libraries/LibWeb/Bindings/RangePrototype.h b/Userland/Libraries/LibWeb/Bindings/RangePrototype.h new file mode 100644 index 0000000000..560eaa4a7c --- /dev/null +++ b/Userland/Libraries/LibWeb/Bindings/RangePrototype.h @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2020, the SerenityOS developers. + * 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 <LibJS/Runtime/Object.h> + +namespace Web::Bindings { + +class RangePrototype final : public JS::Object { + JS_OBJECT(RangePrototype, JS::Object); + +public: + explicit RangePrototype(JS::GlobalObject&); + + void initialize(JS::GlobalObject&) override; + +private: + JS_DECLARE_NATIVE_FUNCTION(set_start); + JS_DECLARE_NATIVE_FUNCTION(set_end); + JS_DECLARE_NATIVE_FUNCTION(clone_range); + + JS_DECLARE_NATIVE_GETTER(start_container_getter); + JS_DECLARE_NATIVE_GETTER(end_container_getter); + JS_DECLARE_NATIVE_GETTER(start_offset_getter); + JS_DECLARE_NATIVE_GETTER(end_offset_getter); + JS_DECLARE_NATIVE_GETTER(collapsed_getter); +}; + +} diff --git a/Userland/Libraries/LibWeb/Bindings/RangeWrapper.cpp b/Userland/Libraries/LibWeb/Bindings/RangeWrapper.cpp new file mode 100644 index 0000000000..dc58a4358d --- /dev/null +++ b/Userland/Libraries/LibWeb/Bindings/RangeWrapper.cpp @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2020, the SerenityOS developers. + * 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 <LibWeb/Bindings/RangePrototype.h> +#include <LibWeb/Bindings/RangeWrapper.h> +#include <LibWeb/Bindings/WindowObject.h> +#include <LibWeb/Bindings/Wrappable.h> +#include <LibWeb/DOM/Range.h> + +namespace Web::Bindings { + +RangeWrapper::RangeWrapper(JS::GlobalObject& global_object, DOM::Range& impl) + : Wrapper(global_object) + , m_impl(impl) +{ + set_prototype(static_cast<WindowObject&>(global_object).range_prototype()); +} + +RangeWrapper* wrap(JS::GlobalObject& global_object, DOM::Range& impl) +{ + return static_cast<RangeWrapper*>(wrap_impl(global_object, impl)); +} + +} diff --git a/Userland/Libraries/LibWeb/Bindings/RangeWrapper.h b/Userland/Libraries/LibWeb/Bindings/RangeWrapper.h new file mode 100644 index 0000000000..5528d81911 --- /dev/null +++ b/Userland/Libraries/LibWeb/Bindings/RangeWrapper.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2020, the SerenityOS developers. + * 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/Wrapper.h> + +namespace Web::Bindings { + +class RangeWrapper final : public Wrapper { +public: + RangeWrapper(JS::GlobalObject&, DOM::Range&); + + DOM::Range& impl() { return m_impl; } + const DOM::Range& impl() const { return m_impl; } + +private: + virtual const char* class_name() const override { return "RangeWrapper"; } + + NonnullRefPtr<DOM::Range> m_impl; +}; + +RangeWrapper* wrap(JS::GlobalObject&, DOM::Range&); + +} diff --git a/Userland/Libraries/LibWeb/Bindings/ScriptExecutionContext.cpp b/Userland/Libraries/LibWeb/Bindings/ScriptExecutionContext.cpp new file mode 100644 index 0000000000..ea7e58864e --- /dev/null +++ b/Userland/Libraries/LibWeb/Bindings/ScriptExecutionContext.cpp @@ -0,0 +1,35 @@ +/* + * 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 <LibWeb/Bindings/ScriptExecutionContext.h> + +namespace Web::Bindings { + +ScriptExecutionContext::~ScriptExecutionContext() +{ +} + +} diff --git a/Userland/Libraries/LibWeb/Bindings/ScriptExecutionContext.h b/Userland/Libraries/LibWeb/Bindings/ScriptExecutionContext.h new file mode 100644 index 0000000000..1c21910741 --- /dev/null +++ b/Userland/Libraries/LibWeb/Bindings/ScriptExecutionContext.h @@ -0,0 +1,43 @@ +/* + * 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 <AK/Weakable.h> +#include <LibJS/Forward.h> +#include <LibWeb/Forward.h> + +namespace Web::Bindings { + +class ScriptExecutionContext { +public: + virtual ~ScriptExecutionContext(); + + // FIXME: This should not work this way long-term, interpreters should be on the stack. + virtual JS::Interpreter& interpreter() = 0; +}; + +} diff --git a/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp b/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp new file mode 100644 index 0000000000..f147a86bf0 --- /dev/null +++ b/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp @@ -0,0 +1,361 @@ +/* + * 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/Base64.h> +#include <AK/ByteBuffer.h> +#include <AK/FlyString.h> +#include <AK/Function.h> +#include <AK/String.h> +#include <AK/Utf8View.h> +#include <LibJS/Runtime/Error.h> +#include <LibJS/Runtime/Function.h> +#include <LibJS/Runtime/Shape.h> +#include <LibTextCodec/Decoder.h> +#include <LibWeb/Bindings/DocumentWrapper.h> +#include <LibWeb/Bindings/EventWrapper.h> +#include <LibWeb/Bindings/EventWrapperFactory.h> +#include <LibWeb/Bindings/LocationObject.h> +#include <LibWeb/Bindings/NavigatorObject.h> +#include <LibWeb/Bindings/NodeWrapperFactory.h> +#include <LibWeb/Bindings/PerformanceWrapper.h> +#include <LibWeb/Bindings/RangeConstructor.h> +#include <LibWeb/Bindings/RangePrototype.h> +#include <LibWeb/Bindings/WindowObject.h> +#include <LibWeb/Bindings/XMLHttpRequestConstructor.h> +#include <LibWeb/Bindings/XMLHttpRequestPrototype.h> +#include <LibWeb/DOM/Document.h> +#include <LibWeb/DOM/Event.h> +#include <LibWeb/DOM/Window.h> +#include <LibWeb/Origin.h> + +namespace Web { +namespace Bindings { + +WindowObject::WindowObject(DOM::Window& impl) + : m_impl(impl) +{ + impl.set_wrapper({}, *this); +} + +void WindowObject::initialize() +{ + GlobalObject::initialize(); + + define_property("window", this, JS::Attribute::Enumerable); + define_property("frames", this, JS::Attribute::Enumerable); + define_property("self", this, JS::Attribute::Enumerable); + define_native_property("document", document_getter, document_setter, JS::Attribute::Enumerable); + define_native_property("performance", performance_getter, nullptr, JS::Attribute::Enumerable); + define_native_function("alert", alert); + define_native_function("confirm", confirm); + define_native_function("setInterval", set_interval, 1); + define_native_function("setTimeout", set_timeout, 1); + define_native_function("clearInterval", clear_interval, 1); + define_native_function("clearTimeout", clear_timeout, 1); + define_native_function("requestAnimationFrame", request_animation_frame, 1); + define_native_function("cancelAnimationFrame", cancel_animation_frame, 1); + define_native_function("atob", atob, 1); + define_native_function("btoa", btoa, 1); + + // Legacy + define_native_property("event", event_getter, nullptr, JS::Attribute::Enumerable); + + define_property("navigator", heap().allocate<NavigatorObject>(*this, *this), JS::Attribute::Enumerable | JS::Attribute::Configurable); + define_property("location", heap().allocate<LocationObject>(*this, *this), JS::Attribute::Enumerable | JS::Attribute::Configurable); + + m_xhr_prototype = heap().allocate<XMLHttpRequestPrototype>(*this, *this); + add_constructor("XMLHttpRequest", m_xhr_constructor, m_xhr_prototype); + + m_range_prototype = heap().allocate<RangePrototype>(*this, *this); + add_constructor("Range", m_range_constructor, m_range_prototype); +} + +WindowObject::~WindowObject() +{ +} + +void WindowObject::visit_edges(Visitor& visitor) +{ + GlobalObject::visit_edges(visitor); + visitor.visit(m_xhr_constructor); + visitor.visit(m_xhr_prototype); + visitor.visit(m_range_constructor); + visitor.visit(m_range_prototype); +} + +Origin WindowObject::origin() const +{ + return impl().document().origin(); +} + +static DOM::Window* impl_from(JS::VM& vm, JS::GlobalObject& global_object) +{ + auto* this_object = vm.this_value(global_object).to_object(global_object); + if (!this_object) { + ASSERT_NOT_REACHED(); + return nullptr; + } + if (StringView("WindowObject") != this_object->class_name()) { + vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotA, "WindowObject"); + return nullptr; + } + return &static_cast<WindowObject*>(this_object)->impl(); +} + +JS_DEFINE_NATIVE_FUNCTION(WindowObject::alert) +{ + auto* impl = impl_from(vm, global_object); + if (!impl) + return {}; + String message = ""; + if (vm.argument_count()) { + message = vm.argument(0).to_string(global_object); + if (vm.exception()) + return {}; + } + impl->alert(message); + return JS::js_undefined(); +} + +JS_DEFINE_NATIVE_FUNCTION(WindowObject::confirm) +{ + auto* impl = impl_from(vm, global_object); + if (!impl) + return {}; + String message = ""; + if (vm.argument_count()) { + message = vm.argument(0).to_string(global_object); + if (vm.exception()) + return {}; + } + return JS::Value(impl->confirm(message)); +} + +JS_DEFINE_NATIVE_FUNCTION(WindowObject::set_interval) +{ + auto* impl = impl_from(vm, global_object); + if (!impl) + return {}; + if (!vm.argument_count()) { + vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountAtLeastOne, "setInterval"); + return {}; + } + auto* callback_object = vm.argument(0).to_object(global_object); + if (!callback_object) + return {}; + if (!callback_object->is_function()) { + vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAFunctionNoParam); + return {}; + } + i32 interval = 0; + if (vm.argument_count() >= 2) { + interval = vm.argument(1).to_i32(global_object); + if (vm.exception()) + return {}; + if (interval < 0) + interval = 0; + } + + auto timer_id = impl->set_interval(*static_cast<JS::Function*>(callback_object), interval); + return JS::Value(timer_id); +} + +JS_DEFINE_NATIVE_FUNCTION(WindowObject::set_timeout) +{ + auto* impl = impl_from(vm, global_object); + if (!impl) + return {}; + if (!vm.argument_count()) { + vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountAtLeastOne, "setTimeout"); + return {}; + } + auto* callback_object = vm.argument(0).to_object(global_object); + if (!callback_object) + return {}; + if (!callback_object->is_function()) { + vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAFunctionNoParam); + return {}; + } + i32 interval = 0; + if (vm.argument_count() >= 2) { + interval = vm.argument(1).to_i32(global_object); + if (vm.exception()) + return {}; + if (interval < 0) + interval = 0; + } + + auto timer_id = impl->set_timeout(*static_cast<JS::Function*>(callback_object), interval); + return JS::Value(timer_id); +} + +JS_DEFINE_NATIVE_FUNCTION(WindowObject::clear_timeout) +{ + auto* impl = impl_from(vm, global_object); + if (!impl) + return {}; + if (!vm.argument_count()) { + vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountAtLeastOne, "clearTimeout"); + return {}; + } + i32 timer_id = vm.argument(0).to_i32(global_object); + if (vm.exception()) + return {}; + impl->clear_timeout(timer_id); + return JS::js_undefined(); +} + +JS_DEFINE_NATIVE_FUNCTION(WindowObject::clear_interval) +{ + auto* impl = impl_from(vm, global_object); + if (!impl) + return {}; + if (!vm.argument_count()) { + vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountAtLeastOne, "clearInterval"); + return {}; + } + i32 timer_id = vm.argument(0).to_i32(global_object); + if (vm.exception()) + return {}; + impl->clear_timeout(timer_id); + return JS::js_undefined(); +} + +JS_DEFINE_NATIVE_FUNCTION(WindowObject::request_animation_frame) +{ + auto* impl = impl_from(vm, global_object); + if (!impl) + return {}; + if (!vm.argument_count()) { + vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountOne, "requestAnimationFrame"); + return {}; + } + auto* callback_object = vm.argument(0).to_object(global_object); + if (!callback_object) + return {}; + if (!callback_object->is_function()) { + vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAFunctionNoParam); + return {}; + } + return JS::Value(impl->request_animation_frame(*static_cast<JS::Function*>(callback_object))); +} + +JS_DEFINE_NATIVE_FUNCTION(WindowObject::cancel_animation_frame) +{ + auto* impl = impl_from(vm, global_object); + if (!impl) + return {}; + if (!vm.argument_count()) { + vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountOne, "cancelAnimationFrame"); + return {}; + } + auto id = vm.argument(0).to_i32(global_object); + if (vm.exception()) + return {}; + impl->cancel_animation_frame(id); + return JS::js_undefined(); +} + +JS_DEFINE_NATIVE_FUNCTION(WindowObject::atob) +{ + auto* impl = impl_from(vm, global_object); + if (!impl) + return {}; + if (!vm.argument_count()) { + vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountOne, "atob"); + return {}; + } + auto string = vm.argument(0).to_string(global_object); + if (vm.exception()) + return {}; + auto decoded = decode_base64(StringView(string)); + + // decode_base64() returns a byte string. LibJS uses UTF-8 for strings. Use Latin1Decoder to convert bytes 128-255 to UTF-8. + auto decoder = TextCodec::decoder_for("windows-1252"); + ASSERT(decoder); + return JS::js_string(vm, decoder->to_utf8(decoded)); +} + +JS_DEFINE_NATIVE_FUNCTION(WindowObject::btoa) +{ + auto* impl = impl_from(vm, global_object); + if (!impl) + return {}; + if (!vm.argument_count()) { + vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountOne, "btoa"); + return {}; + } + auto string = vm.argument(0).to_string(global_object); + if (vm.exception()) + return {}; + + Vector<u8> byte_string; + byte_string.ensure_capacity(string.length()); + for (u32 code_point : Utf8View(string)) { + if (code_point > 0xff) { + vm.throw_exception<JS::InvalidCharacterError>(global_object, JS::ErrorType::NotAByteString, "btoa"); + return {}; + } + byte_string.append(code_point); + } + + auto encoded = encode_base64(byte_string.span()); + return JS::js_string(vm, move(encoded)); +} + +JS_DEFINE_NATIVE_GETTER(WindowObject::document_getter) +{ + auto* impl = impl_from(vm, global_object); + if (!impl) + return {}; + return wrap(global_object, impl->document()); +} + +JS_DEFINE_NATIVE_SETTER(WindowObject::document_setter) +{ + // FIXME: Figure out what we should do here. Just ignore attempts to set window.document for now. +} + +JS_DEFINE_NATIVE_GETTER(WindowObject::performance_getter) +{ + auto* impl = impl_from(vm, global_object); + if (!impl) + return {}; + return wrap(global_object, impl->performance()); +} + +JS_DEFINE_NATIVE_GETTER(WindowObject::event_getter) +{ + auto* impl = impl_from(vm, global_object); + if (!impl) + return {}; + if (!impl->current_event()) + return JS::js_undefined(); + return wrap(global_object, const_cast<DOM::Event&>(*impl->current_event())); +} + +} +} diff --git a/Userland/Libraries/LibWeb/Bindings/WindowObject.h b/Userland/Libraries/LibWeb/Bindings/WindowObject.h new file mode 100644 index 0000000000..d68a3364c9 --- /dev/null +++ b/Userland/Libraries/LibWeb/Bindings/WindowObject.h @@ -0,0 +1,88 @@ +/* + * 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 <AK/TypeCasts.h> +#include <AK/Weakable.h> +#include <LibJS/Runtime/GlobalObject.h> +#include <LibWeb/Forward.h> + +namespace Web { +namespace Bindings { + +class WindowObject final + : public JS::GlobalObject + , public Weakable<WindowObject> { +public: + explicit WindowObject(DOM::Window&); + virtual void initialize() override; + virtual ~WindowObject() override; + + DOM::Window& impl() { return *m_impl; } + const DOM::Window& impl() const { return *m_impl; } + + Origin origin() const; + + XMLHttpRequestPrototype* xhr_prototype() { return m_xhr_prototype; } + XMLHttpRequestConstructor* xhr_constructor() { return m_xhr_constructor; } + + RangePrototype* range_prototype() { return m_range_prototype; } + RangeConstructor* range_constructor() { return m_range_constructor; } + +private: + virtual const char* class_name() const override { return "WindowObject"; } + virtual void visit_edges(Visitor&) override; + + JS_DECLARE_NATIVE_GETTER(document_getter); + JS_DECLARE_NATIVE_SETTER(document_setter); + + JS_DECLARE_NATIVE_GETTER(performance_getter); + + JS_DECLARE_NATIVE_GETTER(event_getter); + + JS_DECLARE_NATIVE_FUNCTION(alert); + JS_DECLARE_NATIVE_FUNCTION(confirm); + JS_DECLARE_NATIVE_FUNCTION(set_interval); + JS_DECLARE_NATIVE_FUNCTION(set_timeout); + JS_DECLARE_NATIVE_FUNCTION(clear_interval); + JS_DECLARE_NATIVE_FUNCTION(clear_timeout); + JS_DECLARE_NATIVE_FUNCTION(request_animation_frame); + JS_DECLARE_NATIVE_FUNCTION(cancel_animation_frame); + JS_DECLARE_NATIVE_FUNCTION(atob); + JS_DECLARE_NATIVE_FUNCTION(btoa); + + NonnullRefPtr<DOM::Window> m_impl; + + XMLHttpRequestConstructor* m_xhr_constructor { nullptr }; + XMLHttpRequestPrototype* m_xhr_prototype { nullptr }; + + RangePrototype* m_range_prototype { nullptr }; + RangeConstructor* m_range_constructor { nullptr }; +}; + +} +} diff --git a/Userland/Libraries/LibWeb/Bindings/Wrappable.cpp b/Userland/Libraries/LibWeb/Bindings/Wrappable.cpp new file mode 100644 index 0000000000..b257a378a6 --- /dev/null +++ b/Userland/Libraries/LibWeb/Bindings/Wrappable.cpp @@ -0,0 +1,44 @@ +/* + * 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 <LibWeb/Bindings/Wrappable.h> +#include <LibWeb/Bindings/Wrapper.h> + +namespace Web { +namespace Bindings { + +Wrappable::~Wrappable() +{ +} + +void Wrappable::set_wrapper(Wrapper& wrapper) +{ + ASSERT(!m_wrapper); + m_wrapper = wrapper.make_weak_ptr(); +} + +} +} diff --git a/Userland/Libraries/LibWeb/Bindings/Wrappable.h b/Userland/Libraries/LibWeb/Bindings/Wrappable.h new file mode 100644 index 0000000000..211fc4755a --- /dev/null +++ b/Userland/Libraries/LibWeb/Bindings/Wrappable.h @@ -0,0 +1,57 @@ +/* + * 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 <AK/WeakPtr.h> +#include <LibJS/Heap/Heap.h> +#include <LibJS/Runtime/GlobalObject.h> +#include <LibWeb/Forward.h> + +namespace Web::Bindings { + +class Wrappable { +public: + virtual ~Wrappable(); + + void set_wrapper(Wrapper&); + Wrapper* wrapper() { return m_wrapper; } + const Wrapper* wrapper() const { return m_wrapper; } + +private: + WeakPtr<Wrapper> m_wrapper; +}; + +template<class NativeObject> +inline Wrapper* wrap_impl(JS::GlobalObject& global_object, NativeObject& native_object) +{ + if (!native_object.wrapper()) { + native_object.set_wrapper(*global_object.heap().allocate<typename NativeObject::WrapperType>(global_object, global_object, native_object)); + } + return native_object.wrapper(); +} + +} diff --git a/Userland/Libraries/LibWeb/Bindings/Wrapper.h b/Userland/Libraries/LibWeb/Bindings/Wrapper.h new file mode 100644 index 0000000000..c83556605a --- /dev/null +++ b/Userland/Libraries/LibWeb/Bindings/Wrapper.h @@ -0,0 +1,49 @@ +/* + * 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 <AK/NonnullRefPtr.h> +#include <AK/Weakable.h> +#include <LibJS/Runtime/Object.h> +#include <LibWeb/Forward.h> + +namespace Web::Bindings { + +class Wrapper + : public JS::Object + , public Weakable<Wrapper> { + JS_OBJECT(Wrapper, JS::Object); + +public: +protected: + explicit Wrapper(Object& prototype) + : Object(prototype) + { + } +}; + +} diff --git a/Userland/Libraries/LibWeb/Bindings/XMLHttpRequestConstructor.cpp b/Userland/Libraries/LibWeb/Bindings/XMLHttpRequestConstructor.cpp new file mode 100644 index 0000000000..16bb2a4b8b --- /dev/null +++ b/Userland/Libraries/LibWeb/Bindings/XMLHttpRequestConstructor.cpp @@ -0,0 +1,73 @@ +/* + * 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 <LibJS/Heap/Heap.h> +#include <LibJS/Runtime/GlobalObject.h> +#include <LibWeb/Bindings/WindowObject.h> +#include <LibWeb/Bindings/XMLHttpRequestConstructor.h> +#include <LibWeb/Bindings/XMLHttpRequestPrototype.h> +#include <LibWeb/Bindings/XMLHttpRequestWrapper.h> +#include <LibWeb/DOM/XMLHttpRequest.h> + +namespace Web::Bindings { + +XMLHttpRequestConstructor::XMLHttpRequestConstructor(JS::GlobalObject& global_object) + : NativeFunction(*global_object.function_prototype()) +{ +} + +void XMLHttpRequestConstructor::initialize(JS::GlobalObject& global_object) +{ + auto& vm = this->vm(); + NativeFunction::initialize(global_object); + auto& window = static_cast<WindowObject&>(global_object); + define_property(vm.names.prototype, window.xhr_prototype(), 0); + define_property(vm.names.length, JS::Value(1), JS::Attribute::Configurable); + + define_property("UNSENT", JS::Value((i32)XMLHttpRequest::ReadyState::Unsent), JS::Attribute::Enumerable); + define_property("OPENED", JS::Value((i32)XMLHttpRequest::ReadyState::Opened), JS::Attribute::Enumerable); + define_property("HEADERS_RECEIVED", JS::Value((i32)XMLHttpRequest::ReadyState::HeadersReceived), JS::Attribute::Enumerable); + define_property("LOADING", JS::Value((i32)XMLHttpRequest::ReadyState::Loading), JS::Attribute::Enumerable); + define_property("DONE", JS::Value((i32)XMLHttpRequest::ReadyState::Done), JS::Attribute::Enumerable); +} + +XMLHttpRequestConstructor::~XMLHttpRequestConstructor() +{ +} + +JS::Value XMLHttpRequestConstructor::call() +{ + vm().throw_exception<JS::TypeError>(global_object(), JS::ErrorType::ConstructorWithoutNew, "XMLHttpRequest"); + return {}; +} + +JS::Value XMLHttpRequestConstructor::construct(Function&) +{ + auto& window = static_cast<WindowObject&>(global_object()); + return heap().allocate<XMLHttpRequestWrapper>(window, window, XMLHttpRequest::create(window.impl())); +} + +} diff --git a/Userland/Libraries/LibWeb/Bindings/XMLHttpRequestConstructor.h b/Userland/Libraries/LibWeb/Bindings/XMLHttpRequestConstructor.h new file mode 100644 index 0000000000..0ca6f5063f --- /dev/null +++ b/Userland/Libraries/LibWeb/Bindings/XMLHttpRequestConstructor.h @@ -0,0 +1,47 @@ +/* + * 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 <LibJS/Runtime/NativeFunction.h> + +namespace Web::Bindings { + +class XMLHttpRequestConstructor final : public JS::NativeFunction { +public: + explicit XMLHttpRequestConstructor(JS::GlobalObject&); + virtual void initialize(JS::GlobalObject&) override; + virtual ~XMLHttpRequestConstructor() override; + + virtual JS::Value call() override; + virtual JS::Value construct(JS::Function& new_target) override; + +private: + virtual bool has_constructor() const override { return true; } + virtual const char* class_name() const override { return "XMLHttpRequestConstructor"; } +}; + +} diff --git a/Userland/Libraries/LibWeb/Bindings/XMLHttpRequestPrototype.cpp b/Userland/Libraries/LibWeb/Bindings/XMLHttpRequestPrototype.cpp new file mode 100644 index 0000000000..2db588bc2c --- /dev/null +++ b/Userland/Libraries/LibWeb/Bindings/XMLHttpRequestPrototype.cpp @@ -0,0 +1,112 @@ +/* + * 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/Function.h> +#include <LibJS/Runtime/Error.h> +#include <LibJS/Runtime/GlobalObject.h> +#include <LibWeb/Bindings/XMLHttpRequestPrototype.h> +#include <LibWeb/Bindings/XMLHttpRequestWrapper.h> +#include <LibWeb/DOM/XMLHttpRequest.h> + +namespace Web::Bindings { + +XMLHttpRequestPrototype::XMLHttpRequestPrototype(JS::GlobalObject& global_object) + : Object(*global_object.object_prototype()) +{ +} + +void XMLHttpRequestPrototype::initialize(JS::GlobalObject& global_object) +{ + Object::initialize(global_object); + define_native_function("open", open, 2); + define_native_function("send", send, 0); + define_native_property("readyState", ready_state_getter, nullptr, JS::Attribute::Enumerable | JS::Attribute::Configurable); + define_native_property("responseText", response_text_getter, nullptr, JS::Attribute::Enumerable | JS::Attribute::Configurable); + + define_property("UNSENT", JS::Value((i32)XMLHttpRequest::ReadyState::Unsent), JS::Attribute::Enumerable); + define_property("OPENED", JS::Value((i32)XMLHttpRequest::ReadyState::Opened), JS::Attribute::Enumerable); + define_property("HEADERS_RECEIVED", JS::Value((i32)XMLHttpRequest::ReadyState::HeadersReceived), JS::Attribute::Enumerable); + define_property("LOADING", JS::Value((i32)XMLHttpRequest::ReadyState::Loading), JS::Attribute::Enumerable); + define_property("DONE", JS::Value((i32)XMLHttpRequest::ReadyState::Done), JS::Attribute::Enumerable); +} + +XMLHttpRequestPrototype::~XMLHttpRequestPrototype() +{ +} + +static XMLHttpRequest* impl_from(JS::VM& vm, JS::GlobalObject& global_object) +{ + auto* this_object = vm.this_value(global_object).to_object(global_object); + if (!this_object) + return nullptr; + if (!is<XMLHttpRequestWrapper>(this_object)) { + vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotA, "XMLHttpRequest"); + return nullptr; + } + return &static_cast<XMLHttpRequestWrapper*>(this_object)->impl(); +} + +JS_DEFINE_NATIVE_FUNCTION(XMLHttpRequestPrototype::open) +{ + auto* impl = impl_from(vm, global_object); + if (!impl) + return {}; + auto arg0 = vm.argument(0).to_string(global_object); + if (vm.exception()) + return {}; + auto arg1 = vm.argument(1).to_string(global_object); + if (vm.exception()) + return {}; + impl->open(arg0, arg1); + return JS::js_undefined(); +} + +JS_DEFINE_NATIVE_FUNCTION(XMLHttpRequestPrototype::send) +{ + auto* impl = impl_from(vm, global_object); + if (!impl) + return {}; + impl->send(); + return JS::js_undefined(); +} + +JS_DEFINE_NATIVE_GETTER(XMLHttpRequestPrototype::ready_state_getter) +{ + auto* impl = impl_from(vm, global_object); + if (!impl) + return {}; + return JS::Value((i32)impl->ready_state()); +} + +JS_DEFINE_NATIVE_GETTER(XMLHttpRequestPrototype::response_text_getter) +{ + auto* impl = impl_from(vm, global_object); + if (!impl) + return {}; + return JS::js_string(vm, impl->response_text()); +} + +} diff --git a/Userland/Libraries/LibWeb/Bindings/XMLHttpRequestPrototype.h b/Userland/Libraries/LibWeb/Bindings/XMLHttpRequestPrototype.h new file mode 100644 index 0000000000..7b534b3b00 --- /dev/null +++ b/Userland/Libraries/LibWeb/Bindings/XMLHttpRequestPrototype.h @@ -0,0 +1,49 @@ +/* + * 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 <LibJS/Runtime/Object.h> + +namespace Web::Bindings { + +class XMLHttpRequestPrototype final : public JS::Object { + JS_OBJECT(XMLHttpRequestPrototype, JS::Object); + +public: + explicit XMLHttpRequestPrototype(JS::GlobalObject&); + virtual void initialize(JS::GlobalObject&) override; + virtual ~XMLHttpRequestPrototype() override; + +private: + JS_DECLARE_NATIVE_FUNCTION(open); + JS_DECLARE_NATIVE_FUNCTION(send); + + JS_DECLARE_NATIVE_GETTER(ready_state_getter); + JS_DECLARE_NATIVE_GETTER(response_text_getter); +}; + +} diff --git a/Userland/Libraries/LibWeb/Bindings/XMLHttpRequestWrapper.cpp b/Userland/Libraries/LibWeb/Bindings/XMLHttpRequestWrapper.cpp new file mode 100644 index 0000000000..dedd97313a --- /dev/null +++ b/Userland/Libraries/LibWeb/Bindings/XMLHttpRequestWrapper.cpp @@ -0,0 +1,62 @@ +/* + * 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 <LibJS/Runtime/GlobalObject.h> +#include <LibJS/Runtime/Value.h> +#include <LibWeb/Bindings/WindowObject.h> +#include <LibWeb/Bindings/XMLHttpRequestPrototype.h> +#include <LibWeb/Bindings/XMLHttpRequestWrapper.h> +#include <LibWeb/DOM/XMLHttpRequest.h> + +namespace Web::Bindings { + +XMLHttpRequestWrapper* wrap(JS::GlobalObject& global_object, XMLHttpRequest& impl) +{ + return static_cast<XMLHttpRequestWrapper*>(wrap_impl(global_object, impl)); +} + +XMLHttpRequestWrapper::XMLHttpRequestWrapper(JS::GlobalObject& global_object, XMLHttpRequest& impl) + : EventTargetWrapper(global_object, impl) +{ + set_prototype(static_cast<WindowObject&>(global_object).xhr_prototype()); +} + +XMLHttpRequestWrapper::~XMLHttpRequestWrapper() +{ +} + +XMLHttpRequest& XMLHttpRequestWrapper::impl() +{ + return static_cast<XMLHttpRequest&>(EventTargetWrapper::impl()); +} + +const XMLHttpRequest& XMLHttpRequestWrapper::impl() const +{ + return static_cast<const XMLHttpRequest&>(EventTargetWrapper::impl()); +} + +} diff --git a/Userland/Libraries/LibWeb/Bindings/XMLHttpRequestWrapper.h b/Userland/Libraries/LibWeb/Bindings/XMLHttpRequestWrapper.h new file mode 100644 index 0000000000..a961564b60 --- /dev/null +++ b/Userland/Libraries/LibWeb/Bindings/XMLHttpRequestWrapper.h @@ -0,0 +1,47 @@ +/* + * 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/EventTargetWrapper.h> + +namespace Web::Bindings { + +class XMLHttpRequestWrapper final : public EventTargetWrapper { +public: + XMLHttpRequestWrapper(JS::GlobalObject&, XMLHttpRequest&); + virtual ~XMLHttpRequestWrapper() override; + + XMLHttpRequest& impl(); + const XMLHttpRequest& impl() const; + +private: + virtual const char* class_name() const override { return "XMLHttpRequestWrapper"; } +}; + +XMLHttpRequestWrapper* wrap(JS::GlobalObject&, XMLHttpRequest&); + +} |