Forráskód Böngészése

LibWeb: Add initial implementation of document.implementation

Luke 4 éve
szülő
commit
dcb21b0c3a

+ 2 - 0
Libraries/LibWeb/CMakeLists.txt

@@ -32,6 +32,7 @@ set(SOURCES
     DOM/Document.cpp
     DOM/DocumentFragment.cpp
     DOM/DocumentType.cpp
+    DOM/DOMImplementation.cpp
     DOM/Element.cpp
     DOM/ElementFactory.cpp
     DOM/EventDispatcher.cpp
@@ -228,6 +229,7 @@ libweb_js_wrapper(DOM/Comment)
 libweb_js_wrapper(DOM/Document)
 libweb_js_wrapper(DOM/DocumentFragment)
 libweb_js_wrapper(DOM/DocumentType)
+libweb_js_wrapper(DOM/DOMImplementation)
 libweb_js_wrapper(DOM/Element)
 libweb_js_wrapper(DOM/Event)
 libweb_js_wrapper(DOM/EventTarget)

+ 30 - 6
Libraries/LibWeb/CodeGenerators/WrapperGenerator.cpp

@@ -78,6 +78,7 @@ struct Type {
 struct Parameter {
     Type type;
     String name;
+    bool optional { false };
 };
 
 struct Function {
@@ -88,8 +89,14 @@ struct Function {
 
     size_t length() const
     {
-        // FIXME: Take optional arguments into account
-        return parameters.size();
+        // FIXME: This seems to produce a length that is way over what it's supposed to be.
+        //        For example, getElementsByTagName has its length set to 20 when it should be 1.
+        size_t length = 0;
+        for (auto& parameter : parameters) {
+            if (!parameter.optional)
+                length++;
+        }
+        return length;
     }
 };
 
@@ -198,10 +205,13 @@ static OwnPtr<Interface> parse_interface(const StringView& input)
         for (;;) {
             if (lexer.consume_specific(')'))
                 break;
+            bool optional = lexer.consume_specific("optional");
+            if (optional)
+                consume_whitespace();
             auto type = parse_type();
             consume_whitespace();
             auto name = lexer.consume_until([](auto ch) { return isspace(ch) || ch == ',' || ch == ')'; });
-            parameters.append({ move(type), move(name) });
+            parameters.append({ move(type), move(name), optional });
             if (lexer.consume_specific(')'))
                 break;
             assert_specific(',');
@@ -496,6 +506,7 @@ void generate_implementation(const IDL::Interface& interface)
 #include <LibWeb/Bindings/@wrapper_class@.h>
 #include <LibWeb/Bindings/CanvasRenderingContext2DWrapper.h>
 #include <LibWeb/Bindings/CommentWrapper.h>
+#include <LibWeb/Bindings/DOMImplementationWrapper.h>
 #include <LibWeb/Bindings/DocumentFragmentWrapper.h>
 #include <LibWeb/Bindings/DocumentTypeWrapper.h>
 #include <LibWeb/Bindings/DocumentWrapper.h>
@@ -595,7 +606,7 @@ static @fully_qualified_name@* impl_from(JS::VM& vm, JS::GlobalObject& global_ob
 )~~~");
     }
 
-    auto generate_to_cpp = [&](auto& parameter, auto& js_name, const auto& js_suffix, auto cpp_name, bool return_void = false, bool legacy_null_to_empty_string = false) {
+    auto generate_to_cpp = [&](auto& parameter, auto& js_name, const auto& js_suffix, auto cpp_name, bool return_void = false, bool legacy_null_to_empty_string = false, bool optional = false) {
         auto scoped_generator = generator.fork();
         scoped_generator.set("cpp_name", cpp_name);
         scoped_generator.set("js_name", js_name);
@@ -608,12 +619,24 @@ static @fully_qualified_name@* impl_from(JS::VM& vm, JS::GlobalObject& global_ob
         else
             scoped_generator.set("return_statement", "return {};");
 
+        // FIXME: Add support for optional to all types
         if (parameter.type.name == "DOMString") {
-            scoped_generator.append(R"~~~(
+            if (!optional) {
+                scoped_generator.append(R"~~~(
     auto @cpp_name@ = @js_name@@js_suffix@.to_string(global_object, @legacy_null_to_empty_string@);
     if (vm.exception())
         @return_statement@
 )~~~");
+            } else {
+                scoped_generator.append(R"~~~(
+    String @cpp_name@;
+    if (!@js_name@@js_suffix@.is_undefined()) {
+        @cpp_name@ = @js_name@@js_suffix@.to_string(global_object, @legacy_null_to_empty_string@);
+        if (vm.exception())
+            @return_statement@
+    }
+)~~~");
+            }
         } else if (parameter.type.name == "EventListener") {
             scoped_generator.append(R"~~~(
     if (!@js_name@@js_suffix@.is_function()) {
@@ -663,7 +686,8 @@ static @fully_qualified_name@* impl_from(JS::VM& vm, JS::GlobalObject& global_ob
             arguments_generator.append(R"~~~(
     auto arg@argument.index@ = vm.argument(@argument.index@);
 )~~~");
-            generate_to_cpp(parameter, "arg", String::number(argument_index), snake_name(parameter.name), return_void);
+            // FIXME: Parameters can have [LegacyNullToEmptyString] attached.
+            generate_to_cpp(parameter, "arg", String::number(argument_index), snake_name(parameter.name), return_void, false, parameter.optional);
             ++argument_index;
         }
 

+ 74 - 0
Libraries/LibWeb/DOM/DOMImplementation.cpp

@@ -0,0 +1,74 @@
+/*
+ * 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/DOM/DOMImplementation.h>
+#include <LibWeb/DOM/Document.h>
+#include <LibWeb/DOM/DocumentType.h>
+#include <LibWeb/DOM/ElementFactory.h>
+#include <LibWeb/DOM/Text.h>
+#include <LibWeb/Namespace.h>
+#include <LibWeb/Origin.h>
+
+namespace Web::DOM {
+
+DOMImplementation::DOMImplementation(Document& document)
+    : m_document(document)
+{
+}
+
+const NonnullRefPtr<Document> DOMImplementation::create_htmldocument(const String& title) const
+{
+    auto html_document = Document::create();
+
+    html_document->set_content_type("text/html");
+
+    auto doctype = adopt(*new DocumentType(html_document));
+    doctype->set_name("html");
+    html_document->append_child(doctype);
+
+    auto html_element = create_element(html_document, HTML::TagNames::html, Namespace::HTML);
+    html_document->append_child(html_element);
+
+    auto head_element = create_element(html_document, HTML::TagNames::head, Namespace::HTML);
+    html_element->append_child(head_element);
+
+    if (!title.is_null()) {
+        auto title_element = create_element(html_document, HTML::TagNames::title, Namespace::HTML);
+        head_element->append_child(title_element);
+
+        auto text_node = adopt(*new Text(html_document, title));
+        title_element->append_child(text_node);
+    }
+
+    auto body_element = create_element(html_document, HTML::TagNames::body, Namespace::HTML);
+    html_element->append_child(body_element);
+
+    html_document->set_origin(m_document.origin());
+
+    return html_document;
+}
+
+}

+ 60 - 0
Libraries/LibWeb/DOM/DOMImplementation.h

@@ -0,0 +1,60 @@
+/*
+ * 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 <AK/NonnullRefPtr.h>
+#include <AK/RefCounted.h>
+#include <AK/Weakable.h>
+#include <LibWeb/Bindings/Wrappable.h>
+
+namespace Web::DOM {
+
+class DOMImplementation final
+    : public RefCounted<DOMImplementation>
+    , public Weakable<DOMImplementation>
+    , public Bindings::Wrappable {
+public:
+    using WrapperType = Bindings::DOMImplementationWrapper;
+
+    static NonnullRefPtr<DOMImplementation> create(Document& document)
+    {
+        return adopt(*new DOMImplementation(document));
+    }
+
+    // FIXME: snake_case in WrapperGenerator turns "createHTMLDocument" into "create_htmldocument"
+    const NonnullRefPtr<Document> create_htmldocument(const String& title) const;
+
+    // https://dom.spec.whatwg.org/#dom-domimplementation-hasfeature
+    bool has_feature() const { return true; }
+
+private:
+    explicit DOMImplementation(Document&);
+
+    Document& m_document;
+};
+
+}

+ 7 - 0
Libraries/LibWeb/DOM/DOMImplementation.idl

@@ -0,0 +1,7 @@
+interface DOMImplementation {
+
+    Document createHTMLDocument(optional DOMString title);
+
+    boolean hasFeature();
+
+}

+ 8 - 0
Libraries/LibWeb/DOM/Document.cpp

@@ -68,6 +68,7 @@ Document::Document(const URL& url)
     , m_style_sheets(CSS::StyleSheetList::create(*this))
     , m_url(url)
     , m_window(Window::create_with_document(*this))
+    , m_implementation(DOMImplementation::create(*this))
 {
     m_style_update_timer = Core::Timer::create_single_shot(0, [this] {
         update_style();
@@ -136,6 +137,13 @@ Origin Document::origin() const
     return { m_url.protocol(), m_url.host(), m_url.port() };
 }
 
+void Document::set_origin(const Origin& origin)
+{
+    m_url.set_protocol(origin.protocol());
+    m_url.set_host(origin.host());
+    m_url.set_port(origin.port());
+}
+
 void Document::schedule_style_update()
 {
     if (m_style_update_timer->is_active())

+ 6 - 0
Libraries/LibWeb/DOM/Document.h

@@ -39,6 +39,7 @@
 #include <LibWeb/CSS/StyleResolver.h>
 #include <LibWeb/CSS/StyleSheet.h>
 #include <LibWeb/CSS/StyleSheetList.h>
+#include <LibWeb/DOM/DOMImplementation.h>
 #include <LibWeb/DOM/NonElementParentNode.h>
 #include <LibWeb/DOM/ParentNode.h>
 
@@ -64,6 +65,7 @@ public:
     URL url() const { return m_url; }
 
     Origin origin() const;
+    void set_origin(const Origin& origin);
 
     bool is_scripting_enabled() const { return true; }
 
@@ -197,6 +199,8 @@ public:
     const String& content_type() const { return m_content_type; }
     void set_content_type(const String& content_type) { m_content_type = content_type; }
 
+    const NonnullRefPtr<DOMImplementation> implementation() { return m_implementation; }
+
 private:
     explicit Document(const URL&);
 
@@ -258,6 +262,8 @@ private:
 
     String m_ready_state { "loading" };
     String m_content_type;
+
+    NonnullRefPtr<DOMImplementation> m_implementation;
 };
 
 }

+ 2 - 0
Libraries/LibWeb/DOM/Document.idl

@@ -1,5 +1,7 @@
 interface Document : Node {
 
+    readonly attribute DOMImplementation implementation;
+
     readonly attribute DOMString contentType;
 
     Element? getElementById(DOMString id);

+ 2 - 0
Libraries/LibWeb/Forward.h

@@ -40,6 +40,7 @@ class Comment;
 class Document;
 class DocumentFragment;
 class DocumentType;
+class DOMImplementation;
 class Element;
 class Event;
 class EventHandler;
@@ -179,6 +180,7 @@ class CommentWrapper;
 class DocumentFragmentWrapper;
 class DocumentTypeWrapper;
 class DocumentWrapper;
+class DOMImplementationWrapper;
 class ElementWrapper;
 class EventListenerWrapper;
 class EventTargetWrapper;