summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2023-03-10 14:56:03 +0100
committerAndreas Kling <kling@serenityos.org>2023-03-10 14:56:03 +0100
commit8c5c78f1f11c43f5c763ba74e3a1ea9db057b6f4 (patch)
treeec2299a051e0faa86faf7a93e7fdc927ab32fdb8
parent109ed27423959cce1a0b50b1b6f883f06a19736d (diff)
downloadserenity-8c5c78f1f11c43f5c763ba74e3a1ea9db057b6f4.zip
LibWeb: Implement Document.createAttribute{,NS}()
-rw-r--r--Userland/Libraries/LibWeb/DOM/Document.cpp25
-rw-r--r--Userland/Libraries/LibWeb/DOM/Document.h5
-rw-r--r--Userland/Libraries/LibWeb/DOM/Document.idl3
3 files changed, 31 insertions, 2 deletions
diff --git a/Userland/Libraries/LibWeb/DOM/Document.cpp b/Userland/Libraries/LibWeb/DOM/Document.cpp
index 7ccf729904..48cf783e79 100644
--- a/Userland/Libraries/LibWeb/DOM/Document.cpp
+++ b/Userland/Libraries/LibWeb/DOM/Document.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
+ * Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
* Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
@@ -2374,4 +2374,27 @@ DeprecatedString Document::dump_accessibility_tree_as_json()
return builder.to_deprecated_string();
}
+// https://dom.spec.whatwg.org/#dom-document-createattribute
+WebIDL::ExceptionOr<JS::NonnullGCPtr<Attr>> Document::create_attribute(DeprecatedString const& local_name)
+{
+ // 1. If localName does not match the Name production in XML, then throw an "InvalidCharacterError" DOMException.
+ if (!is_valid_name(local_name))
+ return WebIDL::InvalidCharacterError::create(realm(), "Invalid character in attribute name.");
+
+ // 2. If this is an HTML document, then set localName to localName in ASCII lowercase.
+ // 3. Return a new attribute whose local name is localName and node document is this.
+ return Attr::create(*this, is_html_document() ? local_name.to_lowercase() : local_name);
+}
+
+// https://dom.spec.whatwg.org/#dom-document-createattributens
+WebIDL::ExceptionOr<JS::NonnullGCPtr<Attr>> Document::create_attribute_ns(DeprecatedString const& namespace_, DeprecatedString const& qualified_name)
+{
+ // 1. Let namespace, prefix, and localName be the result of passing namespace and qualifiedName to validate and extract.
+ auto extracted_qualified_name = TRY(validate_and_extract(realm(), namespace_, qualified_name));
+
+ // 2. Return a new attribute whose namespace is namespace, namespace prefix is prefix, local name is localName, and node document is this.
+
+ return Attr::create(*this, extracted_qualified_name);
+}
+
}
diff --git a/Userland/Libraries/LibWeb/DOM/Document.h b/Userland/Libraries/LibWeb/DOM/Document.h
index a79b577853..44f4512e2c 100644
--- a/Userland/Libraries/LibWeb/DOM/Document.h
+++ b/Userland/Libraries/LibWeb/DOM/Document.h
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
+ * Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
@@ -228,6 +228,9 @@ public:
JS::NonnullGCPtr<Comment> create_comment(DeprecatedString const& data);
WebIDL::ExceptionOr<JS::NonnullGCPtr<ProcessingInstruction>> create_processing_instruction(DeprecatedString const& target, DeprecatedString const& data);
+ WebIDL::ExceptionOr<JS::NonnullGCPtr<Attr>> create_attribute(DeprecatedString const& local_name);
+ WebIDL::ExceptionOr<JS::NonnullGCPtr<Attr>> create_attribute_ns(DeprecatedString const& namespace_, DeprecatedString const& qualified_name);
+
WebIDL::ExceptionOr<JS::NonnullGCPtr<Event>> create_event(DeprecatedString const& interface);
JS::NonnullGCPtr<Range> create_range();
diff --git a/Userland/Libraries/LibWeb/DOM/Document.idl b/Userland/Libraries/LibWeb/DOM/Document.idl
index 4a610f011c..144340746a 100644
--- a/Userland/Libraries/LibWeb/DOM/Document.idl
+++ b/Userland/Libraries/LibWeb/DOM/Document.idl
@@ -81,6 +81,9 @@ interface Document : Node {
Comment createComment(DOMString data);
[NewObject] ProcessingInstruction createProcessingInstruction(DOMString target, DOMString data);
+ [NewObject] Attr createAttribute(DOMString localName);
+ [NewObject] Attr createAttributeNS(DOMString? namespace, DOMString qualifiedName);
+
Range createRange();
Event createEvent(DOMString interface);