From 449c6c560407bf64906b7ce7806d537f7f3068c5 Mon Sep 17 00:00:00 2001 From: Luke Date: Thu, 28 Jan 2021 20:15:04 +0000 Subject: [PATCH] LibWeb: Add simple implementation of Document.createElementNS --- .../Libraries/LibWeb/CodeGenerators/WrapperGenerator.cpp | 6 +++--- Userland/Libraries/LibWeb/DOM/Document.cpp | 9 +++++++++ Userland/Libraries/LibWeb/DOM/Document.h | 1 + Userland/Libraries/LibWeb/DOM/Document.idl | 1 + 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/Userland/Libraries/LibWeb/CodeGenerators/WrapperGenerator.cpp b/Userland/Libraries/LibWeb/CodeGenerators/WrapperGenerator.cpp index 7588af66f0b..39e8ac2bdd1 100644 --- a/Userland/Libraries/LibWeb/CodeGenerators/WrapperGenerator.cpp +++ b/Userland/Libraries/LibWeb/CodeGenerators/WrapperGenerator.cpp @@ -56,7 +56,7 @@ static String snake_name(const StringView& title_name) static String make_input_acceptable_cpp(const String& input) { - if (input.is_one_of("class", "template", "for", "default", "char")) { + if (input.is_one_of("class", "template", "for", "default", "char", "namespace")) { StringBuilder builder; builder.append(input); builder.append('_'); @@ -1031,7 +1031,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, bool optional = false) { auto scoped_generator = generator.fork(); - scoped_generator.set("cpp_name", cpp_name); + scoped_generator.set("cpp_name", make_input_acceptable_cpp(cpp_name)); scoped_generator.set("js_name", js_name); scoped_generator.set("js_suffix", js_suffix); scoped_generator.set("legacy_null_to_empty_string", legacy_null_to_empty_string ? "true" : "false"); @@ -1109,7 +1109,7 @@ static @fully_qualified_name@* impl_from(JS::VM& vm, JS::GlobalObject& global_ob Vector parameter_names; size_t argument_index = 0; for (auto& parameter : parameters) { - parameter_names.append(snake_name(parameter.name)); + parameter_names.append(make_input_acceptable_cpp(snake_name(parameter.name))); arguments_generator.set("argument.index", String::number(argument_index)); arguments_generator.append(R"~~~( diff --git a/Userland/Libraries/LibWeb/DOM/Document.cpp b/Userland/Libraries/LibWeb/DOM/Document.cpp index f61e81fb703..347e81dcae3 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.cpp +++ b/Userland/Libraries/LibWeb/DOM/Document.cpp @@ -566,12 +566,21 @@ JS::Value Document::run_javascript(const StringView& source) return result; } +// https://dom.spec.whatwg.org/#dom-document-createelement +// FIXME: This only implements step 6 of the algorithm and does not take in options. NonnullRefPtr Document::create_element(const String& tag_name) { // FIXME: Let namespace be the HTML namespace, if this is an HTML document or this’s content type is "application/xhtml+xml", and null otherwise. return DOM::create_element(*this, tag_name, Namespace::HTML); } +// https://dom.spec.whatwg.org/#internal-createelementns-steps +// FIXME: This only implements step 4 of the algorithm and does not take in options. +NonnullRefPtr Document::create_element_ns(const String& namespace_, const String& qualified_name) +{ + return DOM::create_element(*this, qualified_name, namespace_); +} + NonnullRefPtr Document::create_document_fragment() { return adopt(*new DocumentFragment(*this)); diff --git a/Userland/Libraries/LibWeb/DOM/Document.h b/Userland/Libraries/LibWeb/DOM/Document.h index a46413758dd..c7367f68387 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.h +++ b/Userland/Libraries/LibWeb/DOM/Document.h @@ -146,6 +146,7 @@ public: JS::Value run_javascript(const StringView&); NonnullRefPtr create_element(const String& tag_name); + NonnullRefPtr create_element_ns(const String& namespace_, const String& qualifed_name); NonnullRefPtr create_document_fragment(); NonnullRefPtr create_text_node(const String& data); NonnullRefPtr create_comment(const String& data); diff --git a/Userland/Libraries/LibWeb/DOM/Document.idl b/Userland/Libraries/LibWeb/DOM/Document.idl index 3b34314e8d0..a691c583a2f 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.idl +++ b/Userland/Libraries/LibWeb/DOM/Document.idl @@ -19,6 +19,7 @@ interface Document : Node { ArrayFromVector querySelectorAll(DOMString selectors); Element createElement(DOMString tagName); + Element createElementNS(DOMString? namespace, DOMString qualifiedName); DocumentFragment createDocumentFragment(); Text createTextNode(DOMString data); Comment createComment(DOMString data);