瀏覽代碼

LibWeb: Implement Document.createAttribute{,NS}()

Andreas Kling 2 年之前
父節點
當前提交
8c5c78f1f1

+ 24 - 1
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-2023, Linus Groh <linusg@serenityos.org>
  * Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
  * Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
  * Copyright (c) 2021, Sam Atkins <atkinssj@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();
     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);
+}
+
 }
 }

+ 4 - 1
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>
  * Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
  *
  *
  * SPDX-License-Identifier: BSD-2-Clause
  * SPDX-License-Identifier: BSD-2-Clause
@@ -228,6 +228,9 @@ public:
     JS::NonnullGCPtr<Comment> create_comment(DeprecatedString const& data);
     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<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);
     WebIDL::ExceptionOr<JS::NonnullGCPtr<Event>> create_event(DeprecatedString const& interface);
     JS::NonnullGCPtr<Range> create_range();
     JS::NonnullGCPtr<Range> create_range();
 
 

+ 3 - 0
Userland/Libraries/LibWeb/DOM/Document.idl

@@ -81,6 +81,9 @@ interface Document : Node {
     Comment createComment(DOMString data);
     Comment createComment(DOMString data);
     [NewObject] ProcessingInstruction createProcessingInstruction(DOMString target, DOMString data);
     [NewObject] ProcessingInstruction createProcessingInstruction(DOMString target, DOMString data);
 
 
+    [NewObject] Attr createAttribute(DOMString localName);
+    [NewObject] Attr createAttributeNS(DOMString? namespace, DOMString qualifiedName);
+
     Range createRange();
     Range createRange();
     Event createEvent(DOMString interface);
     Event createEvent(DOMString interface);