Kaynağa Gözat

LibWeb: Use FlyString for create_element() prefix strings

Andreas Kling 1 yıl önce
ebeveyn
işleme
8f82bd044b

+ 1 - 1
Userland/Libraries/LibWeb/DOM/Document.cpp

@@ -1387,7 +1387,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Element>> Document::create_element_ns(Optio
     }
 
     // 4. Return the result of creating an element given document, localName, namespace, prefix, is, and with the synchronous custom elements flag set.
-    return TRY(DOM::create_element(*this, extracted_qualified_name.local_name(), extracted_qualified_name.deprecated_namespace_(), extracted_qualified_name.deprecated_prefix(), move(is_value), true));
+    return TRY(DOM::create_element(*this, extracted_qualified_name.local_name(), extracted_qualified_name.deprecated_namespace_(), extracted_qualified_name.prefix(), move(is_value), true));
 }
 
 JS::NonnullGCPtr<DocumentFragment> Document::create_document_fragment()

+ 6 - 6
Userland/Libraries/LibWeb/DOM/Element.cpp

@@ -221,7 +221,7 @@ WebIDL::ExceptionOr<QualifiedName> validate_and_extract(JS::Realm& realm, Deprec
     TRY(Document::validate_qualified_name(realm, qualified_name));
 
     // 3. Let prefix be null.
-    DeprecatedFlyString prefix = {};
+    Optional<FlyString> prefix = {};
 
     // 4. Let localName be qualifiedName.
     auto local_name = qualified_name;
@@ -229,12 +229,12 @@ WebIDL::ExceptionOr<QualifiedName> validate_and_extract(JS::Realm& realm, Deprec
     // 5. If qualifiedName contains a U+003A (:), then strictly split the string on it and set prefix to the part before and localName to the part after.
     if (qualified_name.view().contains(':')) {
         auto parts = qualified_name.view().split_view(':');
-        prefix = parts[0];
+        prefix = MUST(FlyString::from_utf8(parts[0]));
         local_name = parts[1];
     }
 
     // 6. If prefix is non-null and namespace is null, then throw a "NamespaceError" DOMException.
-    if (!prefix.is_null() && namespace_.is_null())
+    if (prefix.has_value() && namespace_.is_null())
         return WebIDL::NamespaceError::create(realm, "Prefix is non-null and namespace is null."_fly_string);
 
     // 7. If prefix is "xml" and namespace is not the XML namespace, then throw a "NamespaceError" DOMException.
@@ -264,13 +264,13 @@ WebIDL::ExceptionOr<void> Element::set_attribute_ns(Optional<String> const& name
     auto extracted_qualified_name = TRY(validate_and_extract(realm(), deprecated_namespace, qualified_name.to_deprecated_fly_string()));
 
     // 2. Set an attribute value for this using localName, value, and also prefix and namespace.
-    set_attribute_value(extracted_qualified_name.local_name().to_deprecated_fly_string(), value.to_deprecated_fly_string(), extracted_qualified_name.deprecated_prefix(), extracted_qualified_name.deprecated_namespace_());
+    set_attribute_value(extracted_qualified_name.local_name(), value.to_deprecated_fly_string(), extracted_qualified_name.prefix(), extracted_qualified_name.deprecated_namespace_());
 
     return {};
 }
 
 // https://dom.spec.whatwg.org/#concept-element-attributes-set-value
-void Element::set_attribute_value(DeprecatedFlyString const& local_name, DeprecatedString const& value, DeprecatedFlyString const& prefix, DeprecatedFlyString const& namespace_)
+void Element::set_attribute_value(FlyString const& local_name, DeprecatedString const& value, Optional<FlyString> const& prefix, DeprecatedFlyString const& namespace_)
 {
     // 1. Let attribute be the result of getting an attribute given namespace, localName, and element.
     auto* attribute = m_attributes->get_attribute_ns(namespace_, local_name);
@@ -279,7 +279,7 @@ void Element::set_attribute_value(DeprecatedFlyString const& local_name, Depreca
     //    is localName, value is value, and node document is element’s node document, then append this attribute to element,
     //    and then return.
     if (!attribute) {
-        QualifiedName name { MUST(FlyString::from_deprecated_fly_string(local_name)), prefix, namespace_ };
+        QualifiedName name { local_name, prefix, namespace_ };
 
         auto new_attribute = Attr::create(document(), move(name), MUST(String::from_deprecated_string(value)));
         m_attributes->append_attribute(new_attribute);

+ 1 - 1
Userland/Libraries/LibWeb/DOM/Element.h

@@ -118,7 +118,7 @@ public:
 
     // FIXME: This should be taking an Optional<FlyString>
     WebIDL::ExceptionOr<void> set_attribute_ns(Optional<String> const& namespace_, FlyString const& qualified_name, FlyString const& value);
-    void set_attribute_value(DeprecatedFlyString const& local_name, DeprecatedString const& value, DeprecatedFlyString const& prefix = {}, DeprecatedFlyString const& namespace_ = {});
+    void set_attribute_value(FlyString const& local_name, DeprecatedString const& value, Optional<FlyString> const& prefix = {}, DeprecatedFlyString const& namespace_ = {});
     WebIDL::ExceptionOr<JS::GCPtr<Attr>> set_attribute_node(Attr&);
     WebIDL::ExceptionOr<JS::GCPtr<Attr>> set_attribute_node_ns(Attr&);
 

+ 2 - 5
Userland/Libraries/LibWeb/DOM/ElementFactory.cpp

@@ -490,7 +490,7 @@ static JS::GCPtr<MathML::MathMLElement> create_mathml_element(JS::Realm& realm,
     return nullptr;
 }
 // https://dom.spec.whatwg.org/#concept-create-element
-WebIDL::ExceptionOr<JS::NonnullGCPtr<Element>> create_element(Document& document, FlyString local_name, DeprecatedFlyString namespace_, DeprecatedFlyString prefix, Optional<String> is_value, bool synchronous_custom_elements_flag)
+WebIDL::ExceptionOr<JS::NonnullGCPtr<Element>> create_element(Document& document, FlyString local_name, DeprecatedFlyString namespace_, Optional<FlyString> prefix, Optional<String> is_value, bool synchronous_custom_elements_flag)
 {
     auto& realm = document.realm();
 
@@ -581,10 +581,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Element>> create_element(Document& document
                     return JS::throw_completion(WebIDL::NotSupportedError::create(realm, "Synchronously created custom element must have the same local name that element creation was invoked with"_fly_string));
 
                 // 10. Set result’s namespace prefix to prefix.
-                if (prefix.is_null())
-                    element->set_prefix({});
-                else
-                    element->set_prefix(MUST(FlyString::from_deprecated_fly_string(prefix)));
+                element->set_prefix(prefix);
 
                 // 11. Set result’s is value to null.
                 element->set_is_value(Optional<String> {});

+ 1 - 1
Userland/Libraries/LibWeb/DOM/ElementFactory.h

@@ -15,6 +15,6 @@ ErrorOr<FixedArray<FlyString>> valid_local_names_for_given_html_element_interfac
 bool is_unknown_html_element(FlyString const& tag_name);
 
 // FIXME: The spec doesn't say what the default value of synchronous_custom_elements_flag should be.
-WebIDL::ExceptionOr<JS::NonnullGCPtr<Element>> create_element(Document&, FlyString local_name, DeprecatedFlyString namespace_, DeprecatedFlyString prefix = {}, Optional<String> is = Optional<String> {}, bool synchronous_custom_elements_flag = false);
+WebIDL::ExceptionOr<JS::NonnullGCPtr<Element>> create_element(Document&, FlyString local_name, DeprecatedFlyString namespace_, Optional<FlyString> prefix = {}, Optional<String> is = Optional<String> {}, bool synchronous_custom_elements_flag = false);
 
 }

+ 1 - 1
Userland/Libraries/LibWeb/DOM/Node.cpp

@@ -809,7 +809,7 @@ JS::NonnullGCPtr<Node> Node::clone_node(Document* document, bool clone_children)
     if (is<Element>(this)) {
         // 1. Let copy be the result of creating an element, given document, node’s local name, node’s namespace, node’s namespace prefix, and node’s is value, with the synchronous custom elements flag unset.
         auto& element = *verify_cast<Element>(this);
-        auto element_copy = DOM::create_element(*document, element.local_name(), element.namespace_(), element.deprecated_prefix(), element.is_value(), false).release_value_but_fixme_should_propagate_errors();
+        auto element_copy = DOM::create_element(*document, element.local_name(), element.namespace_(), element.prefix(), element.is_value(), false).release_value_but_fixme_should_propagate_errors();
 
         // 2. For each attribute in node’s attribute list:
         element.for_each_attribute([&](auto& name, auto& value) {

+ 2 - 2
Userland/Libraries/LibWeb/DOM/QualifiedName.cpp

@@ -54,8 +54,8 @@ QualifiedName::QualifiedName(FlyString const& local_name, Optional<FlyString> co
 {
 }
 
-QualifiedName::QualifiedName(FlyString const& local_name, DeprecatedFlyString const& prefix, DeprecatedFlyString const& namespace_)
-    : QualifiedName(local_name, prefix.is_null() ? Optional<FlyString> {} : MUST(FlyString::from_deprecated_fly_string(prefix)), namespace_.is_null() ? Optional<FlyString> {} : MUST(FlyString::from_deprecated_fly_string(namespace_)))
+QualifiedName::QualifiedName(FlyString const& local_name, Optional<FlyString> const& prefix, DeprecatedFlyString const& namespace_)
+    : QualifiedName(local_name, prefix, namespace_.is_null() ? Optional<FlyString> {} : MUST(FlyString::from_deprecated_fly_string(namespace_)))
 {
 }
 

+ 1 - 1
Userland/Libraries/LibWeb/DOM/QualifiedName.h

@@ -16,7 +16,7 @@ namespace Web::DOM {
 class QualifiedName {
 public:
     QualifiedName(FlyString const& local_name, Optional<FlyString> const& prefix, Optional<FlyString> const& namespace_);
-    QualifiedName(FlyString const& local_name, DeprecatedFlyString const& prefix, DeprecatedFlyString const& namespace_);
+    QualifiedName(FlyString const& local_name, Optional<FlyString> const& prefix, DeprecatedFlyString const& namespace_);
 
     FlyString const& local_name() const { return m_impl->local_name; }
     Optional<FlyString> const& prefix() const { return m_impl->prefix; }

+ 1 - 1
Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp

@@ -502,7 +502,7 @@ class PlaceholderElement final : public HTMLDivElement {
 
 public:
     PlaceholderElement(DOM::Document& document)
-        : HTMLDivElement(document, DOM::QualifiedName { HTML::TagNames::div, ""sv, Namespace::HTML })
+        : HTMLDivElement(document, DOM::QualifiedName { HTML::TagNames::div, ""_fly_string, Namespace::HTML })
     {
     }
     virtual Optional<CSS::Selector::PseudoElement> pseudo_element() const override { return CSS::Selector::PseudoElement::Placeholder; }