From b337b4370a7417e40014928d2ab69e13911c5ab9 Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Sun, 5 Nov 2023 12:37:49 +1300 Subject: [PATCH] LibWeb: Port validate_and_extract from DeprecatedFlyString --- .../Libraries/LibWeb/DOM/DOMImplementation.cpp | 2 +- Userland/Libraries/LibWeb/DOM/Document.cpp | 16 +++++++--------- Userland/Libraries/LibWeb/DOM/Document.h | 8 ++++---- Userland/Libraries/LibWeb/DOM/Element.cpp | 12 ++++++------ Userland/Libraries/LibWeb/DOM/Element.h | 2 +- 5 files changed, 19 insertions(+), 21 deletions(-) diff --git a/Userland/Libraries/LibWeb/DOM/DOMImplementation.cpp b/Userland/Libraries/LibWeb/DOM/DOMImplementation.cpp index a5e70a8aa1..6089b9bbef 100644 --- a/Userland/Libraries/LibWeb/DOM/DOMImplementation.cpp +++ b/Userland/Libraries/LibWeb/DOM/DOMImplementation.cpp @@ -140,7 +140,7 @@ JS::NonnullGCPtr DOMImplementation::create_html_document(Optional> DOMImplementation::create_document_type(String const& qualified_name, String const& public_id, String const& system_id) { // 1. Validate qualifiedName. - TRY(Document::validate_qualified_name(realm(), qualified_name.to_deprecated_string())); + TRY(Document::validate_qualified_name(realm(), qualified_name)); // 2. Return a new doctype, with qualifiedName as its name, publicId as its public ID, and systemId as its system ID, and with its node document set to the associated document of this. auto document_type = DocumentType::create(document()); diff --git a/Userland/Libraries/LibWeb/DOM/Document.cpp b/Userland/Libraries/LibWeb/DOM/Document.cpp index 3155b4f8c1..0a7abe12b0 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.cpp +++ b/Userland/Libraries/LibWeb/DOM/Document.cpp @@ -1374,7 +1374,7 @@ WebIDL::ExceptionOr> Document::create_element_ns(Optio namespace_to_use = namespace_.value(); // 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_to_use, qualified_name.to_deprecated_string())); + auto extracted_qualified_name = TRY(validate_and_extract(realm(), namespace_to_use, qualified_name)); // 2. Let is be null. Optional is_value; @@ -2270,14 +2270,12 @@ bool Document::is_valid_name(String const& name) } // https://dom.spec.whatwg.org/#validate -WebIDL::ExceptionOr Document::validate_qualified_name(JS::Realm& realm, DeprecatedString const& qualified_name) +WebIDL::ExceptionOr Document::validate_qualified_name(JS::Realm& realm, FlyString const& qualified_name) { if (qualified_name.is_empty()) return WebIDL::InvalidCharacterError::create(realm, "Empty string is not a valid qualified name."_fly_string); - Utf8View utf8view { qualified_name }; - if (!utf8view.validate()) - return WebIDL::InvalidCharacterError::create(realm, "Invalid qualified name."_fly_string); + auto utf8view = qualified_name.code_points(); Optional colon_offset; @@ -2311,12 +2309,12 @@ WebIDL::ExceptionOr Document::validate_qualified_nam if (*colon_offset == 0) return WebIDL::InvalidCharacterError::create(realm, "Qualified name can't start with colon (:)."_fly_string); - if (*colon_offset >= (qualified_name.length() - 1)) + if (*colon_offset >= (qualified_name.bytes_as_string_view().length() - 1)) return WebIDL::InvalidCharacterError::create(realm, "Qualified name can't end with colon (:)."_fly_string); return Document::PrefixAndTagName { - .prefix = qualified_name.substring_view(0, *colon_offset), - .tag_name = qualified_name.substring_view(*colon_offset + 1), + .prefix = MUST(FlyString::from_utf8(qualified_name.bytes_as_string_view().substring_view(0, *colon_offset))), + .tag_name = MUST(FlyString::from_utf8(qualified_name.bytes_as_string_view().substring_view(*colon_offset + 1))), }; } @@ -3010,7 +3008,7 @@ WebIDL::ExceptionOr> Document::create_attribute_ns(Option namespace_to_use = namespace_.value(); // 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_to_use, qualified_name.to_deprecated_string())); + auto extracted_qualified_name = TRY(validate_and_extract(realm(), namespace_to_use, qualified_name)); // 2. Return a new attribute whose namespace is namespace, namespace prefix is prefix, local name is localName, and node document is this. diff --git a/Userland/Libraries/LibWeb/DOM/Document.h b/Userland/Libraries/LibWeb/DOM/Document.h index 78930cec41..311514f5ca 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.h +++ b/Userland/Libraries/LibWeb/DOM/Document.h @@ -1,13 +1,13 @@ /* * Copyright (c) 2018-2023, Andreas Kling * Copyright (c) 2021-2023, Linus Groh + * Copyright (c) 2023, Shannon Booth * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once -#include #include #include #include @@ -408,10 +408,10 @@ public: static bool is_valid_name(String const&); struct PrefixAndTagName { - DeprecatedFlyString prefix; - DeprecatedFlyString tag_name; + FlyString prefix; + FlyString tag_name; }; - static WebIDL::ExceptionOr validate_qualified_name(JS::Realm&, DeprecatedString const& qualified_name); + static WebIDL::ExceptionOr validate_qualified_name(JS::Realm&, FlyString const& qualified_name); JS::NonnullGCPtr create_node_iterator(Node& root, unsigned what_to_show, JS::GCPtr); JS::NonnullGCPtr create_tree_walker(Node& root, unsigned what_to_show, JS::GCPtr); diff --git a/Userland/Libraries/LibWeb/DOM/Element.cpp b/Userland/Libraries/LibWeb/DOM/Element.cpp index 013f8e50c1..a75f052cc8 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.cpp +++ b/Userland/Libraries/LibWeb/DOM/Element.cpp @@ -206,7 +206,7 @@ WebIDL::ExceptionOr Element::set_attribute(FlyString const& name, String c } // https://dom.spec.whatwg.org/#validate-and-extract -WebIDL::ExceptionOr validate_and_extract(JS::Realm& realm, Optional namespace_, DeprecatedFlyString qualified_name) +WebIDL::ExceptionOr validate_and_extract(JS::Realm& realm, Optional namespace_, FlyString const& qualified_name) { // 1. If namespace is the empty string, then set it to null. if (namespace_.has_value() && namespace_.value().is_empty()) @@ -222,10 +222,10 @@ WebIDL::ExceptionOr validate_and_extract(JS::Realm& realm, Option auto local_name = qualified_name; // 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(':'); + if (qualified_name.bytes_as_string_view().contains(':')) { + auto parts = qualified_name.bytes_as_string_view().split_view(':'); prefix = MUST(FlyString::from_utf8(parts[0])); - local_name = parts[1]; + local_name = MUST(FlyString::from_utf8(parts[1])); } // 6. If prefix is non-null and namespace is null, then throw a "NamespaceError" DOMException. @@ -245,7 +245,7 @@ WebIDL::ExceptionOr validate_and_extract(JS::Realm& realm, Option return WebIDL::NamespaceError::create(realm, "Namespace is the XMLNS namespace and neither qualifiedName nor prefix is 'xmlns'."_fly_string); // 10. Return namespace, prefix, and localName. - return QualifiedName { MUST(FlyString::from_deprecated_fly_string(local_name)), prefix, namespace_ }; + return QualifiedName { local_name, prefix, namespace_ }; } // https://dom.spec.whatwg.org/#dom-element-setattributens @@ -257,7 +257,7 @@ WebIDL::ExceptionOr Element::set_attribute_ns(Optional const& name namespace_to_use = namespace_.value(); // 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_to_use, qualified_name.to_deprecated_fly_string())); + auto extracted_qualified_name = TRY(validate_and_extract(realm(), namespace_to_use, qualified_name)); // 2. Set an attribute value for this using localName, value, and also prefix and namespace. set_attribute_value(extracted_qualified_name.local_name(), value.to_deprecated_fly_string(), extracted_qualified_name.prefix(), extracted_qualified_name.namespace_()); diff --git a/Userland/Libraries/LibWeb/DOM/Element.h b/Userland/Libraries/LibWeb/DOM/Element.h index e6ff684af8..48242d0221 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.h +++ b/Userland/Libraries/LibWeb/DOM/Element.h @@ -434,6 +434,6 @@ private: template<> inline bool Node::fast_is() const { return is_element(); } -WebIDL::ExceptionOr validate_and_extract(JS::Realm&, Optional namespace_, DeprecatedFlyString qualified_name); +WebIDL::ExceptionOr validate_and_extract(JS::Realm&, Optional namespace_, FlyString const& qualified_name); }