1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 10:37:45 +00:00

LibWeb: Use FlyString for create_element() namespace strings

This commit is contained in:
Andreas Kling 2023-11-04 09:46:23 +01:00
parent 8f82bd044b
commit f052823f5f
21 changed files with 65 additions and 64 deletions

View file

@ -104,17 +104,17 @@ JS::NonnullGCPtr<Document> DOMImplementation::create_html_document(Optional<Stri
MUST(html_document->append_child(*doctype));
// 4. Append the result of creating an element given doc, html, and the HTML namespace, to doc.
auto html_element = create_element(html_document, HTML::TagNames::html, Namespace::HTML).release_value_but_fixme_should_propagate_errors();
auto html_element = create_element(html_document, HTML::TagNames::html, MUST(FlyString::from_deprecated_fly_string(Namespace::HTML))).release_value_but_fixme_should_propagate_errors();
MUST(html_document->append_child(html_element));
// 5. Append the result of creating an element given doc, head, and the HTML namespace, to the html element created earlier.
auto head_element = create_element(html_document, HTML::TagNames::head, Namespace::HTML).release_value_but_fixme_should_propagate_errors();
auto head_element = create_element(html_document, HTML::TagNames::head, MUST(FlyString::from_deprecated_fly_string(Namespace::HTML))).release_value_but_fixme_should_propagate_errors();
MUST(html_element->append_child(head_element));
// 6. If title is given:
if (title.has_value()) {
// 1. Append the result of creating an element given doc, title, and the HTML namespace, to the head element created earlier.
auto title_element = create_element(html_document, HTML::TagNames::title, Namespace::HTML).release_value_but_fixme_should_propagate_errors();
auto title_element = create_element(html_document, HTML::TagNames::title, MUST(FlyString::from_deprecated_fly_string(Namespace::HTML))).release_value_but_fixme_should_propagate_errors();
MUST(head_element->append_child(title_element));
// 2. Append a new Text node, with its data set to title (which could be the empty string) and its node document set to doc, to the title element created earlier.
@ -123,7 +123,7 @@ JS::NonnullGCPtr<Document> DOMImplementation::create_html_document(Optional<Stri
}
// 7. Append the result of creating an element given doc, body, and the HTML namespace, to the html element created earlier.
auto body_element = create_element(html_document, HTML::TagNames::body, Namespace::HTML).release_value_but_fixme_should_propagate_errors();
auto body_element = create_element(html_document, HTML::TagNames::body, MUST(FlyString::from_deprecated_fly_string(Namespace::HTML))).release_value_but_fixme_should_propagate_errors();
MUST(html_element->append_child(body_element));
// 8. docs origin is thiss associated documents origin.

View file

@ -741,7 +741,7 @@ WebIDL::ExceptionOr<void> Document::set_title(String const& title)
else {
// 1. Let element be the result of creating an element given the document element's node document, title,
// and the SVG namespace.
element = TRY(DOM::create_element(*this, HTML::TagNames::title, Namespace::SVG));
element = TRY(DOM::create_element(*this, HTML::TagNames::title, MUST(FlyString::from_deprecated_fly_string(Namespace::SVG))));
// 2. Insert element as the first child of the document element.
document_element->insert_before(*element, nullptr);
@ -770,7 +770,7 @@ WebIDL::ExceptionOr<void> Document::set_title(String const& title)
else {
// 1. Let element be the result of creating an element given the document element's node document, title,
// and the HTML namespace.
element = TRY(DOM::create_element(*this, HTML::TagNames::title, Namespace::HTML));
element = TRY(DOM::create_element(*this, HTML::TagNames::title, MUST(FlyString::from_deprecated_fly_string(Namespace::HTML))));
// 2. Append element to the head element.
TRY(head_element->append_child(*element));
@ -1356,12 +1356,12 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Element>> Document::create_element(String c
}
// 5. Let namespace be the HTML namespace, if this is an HTML document or thiss content type is "application/xhtml+xml"; otherwise null.
DeprecatedFlyString namespace_;
Optional<FlyString> namespace_;
if (document_type() == Type::HTML || content_type() == "application/xhtml+xml"sv)
namespace_ = Namespace::HTML;
namespace_ = MUST(FlyString::from_deprecated_fly_string(Namespace::HTML));
// 6. Return the result of creating an element given this, localName, namespace, null, is, and with the synchronous custom elements flag set.
return TRY(DOM::create_element(*this, MUST(FlyString::from_deprecated_fly_string(local_name)), namespace_, {}, move(is_value), true));
return TRY(DOM::create_element(*this, MUST(FlyString::from_deprecated_fly_string(local_name)), move(namespace_), {}, move(is_value), true));
}
// https://dom.spec.whatwg.org/#dom-document-createelementns
@ -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.prefix(), move(is_value), true));
return TRY(DOM::create_element(*this, extracted_qualified_name.local_name(), extracted_qualified_name.namespace_(), extracted_qualified_name.prefix(), move(is_value), true));
}
JS::NonnullGCPtr<DocumentFragment> Document::create_document_fragment()

View file

@ -79,21 +79,21 @@ static bool build_markdown_document(DOM::Document& document, ByteBuffer const& d
static bool build_text_document(DOM::Document& document, ByteBuffer const& data)
{
auto html_element = DOM::create_element(document, HTML::TagNames::html, Namespace::HTML).release_value_but_fixme_should_propagate_errors();
auto html_element = DOM::create_element(document, HTML::TagNames::html, MUST(FlyString::from_deprecated_fly_string(Namespace::HTML))).release_value_but_fixme_should_propagate_errors();
MUST(document.append_child(html_element));
auto head_element = DOM::create_element(document, HTML::TagNames::head, Namespace::HTML).release_value_but_fixme_should_propagate_errors();
auto head_element = DOM::create_element(document, HTML::TagNames::head, MUST(FlyString::from_deprecated_fly_string(Namespace::HTML))).release_value_but_fixme_should_propagate_errors();
MUST(html_element->append_child(head_element));
auto title_element = DOM::create_element(document, HTML::TagNames::title, Namespace::HTML).release_value_but_fixme_should_propagate_errors();
auto title_element = DOM::create_element(document, HTML::TagNames::title, MUST(FlyString::from_deprecated_fly_string(Namespace::HTML))).release_value_but_fixme_should_propagate_errors();
MUST(head_element->append_child(title_element));
auto title_text = document.create_text_node(MUST(String::from_deprecated_string(document.url().basename())));
MUST(title_element->append_child(title_text));
auto body_element = DOM::create_element(document, HTML::TagNames::body, Namespace::HTML).release_value_but_fixme_should_propagate_errors();
auto body_element = DOM::create_element(document, HTML::TagNames::body, MUST(FlyString::from_deprecated_fly_string(Namespace::HTML))).release_value_but_fixme_should_propagate_errors();
MUST(html_element->append_child(body_element));
auto pre_element = DOM::create_element(document, HTML::TagNames::pre, Namespace::HTML).release_value_but_fixme_should_propagate_errors();
auto pre_element = DOM::create_element(document, HTML::TagNames::pre, MUST(FlyString::from_deprecated_fly_string(Namespace::HTML))).release_value_but_fixme_should_propagate_errors();
MUST(body_element->append_child(pre_element));
MUST(pre_element->append_child(document.create_text_node(String::from_utf8(StringView { data }).release_value_but_fixme_should_propagate_errors())));
@ -110,22 +110,22 @@ static bool build_image_document(DOM::Document& document, ByteBuffer const& data
if (!bitmap)
return false;
auto html_element = DOM::create_element(document, HTML::TagNames::html, Namespace::HTML).release_value_but_fixme_should_propagate_errors();
auto html_element = DOM::create_element(document, HTML::TagNames::html, MUST(FlyString::from_deprecated_fly_string(Namespace::HTML))).release_value_but_fixme_should_propagate_errors();
MUST(document.append_child(html_element));
auto head_element = DOM::create_element(document, HTML::TagNames::head, Namespace::HTML).release_value_but_fixme_should_propagate_errors();
auto head_element = DOM::create_element(document, HTML::TagNames::head, MUST(FlyString::from_deprecated_fly_string(Namespace::HTML))).release_value_but_fixme_should_propagate_errors();
MUST(html_element->append_child(head_element));
auto title_element = DOM::create_element(document, HTML::TagNames::title, Namespace::HTML).release_value_but_fixme_should_propagate_errors();
auto title_element = DOM::create_element(document, HTML::TagNames::title, MUST(FlyString::from_deprecated_fly_string(Namespace::HTML))).release_value_but_fixme_should_propagate_errors();
MUST(head_element->append_child(title_element));
auto basename = LexicalPath::basename(document.url().serialize_path());
auto title_text = document.heap().allocate<DOM::Text>(document.realm(), document, MUST(String::formatted("{} [{}x{}]", basename, bitmap->width(), bitmap->height())));
MUST(title_element->append_child(*title_text));
auto body_element = DOM::create_element(document, HTML::TagNames::body, Namespace::HTML).release_value_but_fixme_should_propagate_errors();
auto body_element = DOM::create_element(document, HTML::TagNames::body, MUST(FlyString::from_deprecated_fly_string(Namespace::HTML))).release_value_but_fixme_should_propagate_errors();
MUST(html_element->append_child(body_element));
auto image_element = DOM::create_element(document, HTML::TagNames::img, Namespace::HTML).release_value_but_fixme_should_propagate_errors();
auto image_element = DOM::create_element(document, HTML::TagNames::img, MUST(FlyString::from_deprecated_fly_string(Namespace::HTML))).release_value_but_fixme_should_propagate_errors();
MUST(image_element->set_attribute(HTML::AttributeNames::src, MUST(document.url().to_string())));
MUST(body_element->append_child(image_element));
@ -160,16 +160,16 @@ bool build_xml_document(DOM::Document& document, ByteBuffer const& data)
static bool build_video_document(DOM::Document& document)
{
auto html_element = DOM::create_element(document, HTML::TagNames::html, Namespace::HTML).release_value_but_fixme_should_propagate_errors();
auto html_element = DOM::create_element(document, HTML::TagNames::html, MUST(FlyString::from_deprecated_fly_string(Namespace::HTML))).release_value_but_fixme_should_propagate_errors();
MUST(document.append_child(html_element));
auto head_element = DOM::create_element(document, HTML::TagNames::head, Namespace::HTML).release_value_but_fixme_should_propagate_errors();
auto head_element = DOM::create_element(document, HTML::TagNames::head, MUST(FlyString::from_deprecated_fly_string(Namespace::HTML))).release_value_but_fixme_should_propagate_errors();
MUST(html_element->append_child(head_element));
auto body_element = DOM::create_element(document, HTML::TagNames::body, Namespace::HTML).release_value_but_fixme_should_propagate_errors();
auto body_element = DOM::create_element(document, HTML::TagNames::body, MUST(FlyString::from_deprecated_fly_string(Namespace::HTML))).release_value_but_fixme_should_propagate_errors();
MUST(html_element->append_child(body_element));
auto video_element = DOM::create_element(document, HTML::TagNames::video, Namespace::HTML).release_value_but_fixme_should_propagate_errors();
auto video_element = DOM::create_element(document, HTML::TagNames::video, MUST(FlyString::from_deprecated_fly_string(Namespace::HTML))).release_value_but_fixme_should_propagate_errors();
MUST(video_element->set_attribute(HTML::AttributeNames::src, MUST(document.url().to_string())));
MUST(video_element->set_attribute(HTML::AttributeNames::autoplay, String {}));
MUST(video_element->set_attribute(HTML::AttributeNames::controls, String {}));
@ -180,16 +180,16 @@ static bool build_video_document(DOM::Document& document)
static bool build_audio_document(DOM::Document& document)
{
auto html_element = DOM::create_element(document, HTML::TagNames::html, Namespace::HTML).release_value_but_fixme_should_propagate_errors();
auto html_element = DOM::create_element(document, HTML::TagNames::html, MUST(FlyString::from_deprecated_fly_string(Namespace::HTML))).release_value_but_fixme_should_propagate_errors();
MUST(document.append_child(html_element));
auto head_element = DOM::create_element(document, HTML::TagNames::head, Namespace::HTML).release_value_but_fixme_should_propagate_errors();
auto head_element = DOM::create_element(document, HTML::TagNames::head, MUST(FlyString::from_deprecated_fly_string(Namespace::HTML))).release_value_but_fixme_should_propagate_errors();
MUST(html_element->append_child(head_element));
auto body_element = DOM::create_element(document, HTML::TagNames::body, Namespace::HTML).release_value_but_fixme_should_propagate_errors();
auto body_element = DOM::create_element(document, HTML::TagNames::body, MUST(FlyString::from_deprecated_fly_string(Namespace::HTML))).release_value_but_fixme_should_propagate_errors();
MUST(html_element->append_child(body_element));
auto video_element = DOM::create_element(document, HTML::TagNames::audio, Namespace::HTML).release_value_but_fixme_should_propagate_errors();
auto video_element = DOM::create_element(document, HTML::TagNames::audio, MUST(FlyString::from_deprecated_fly_string(Namespace::HTML))).release_value_but_fixme_should_propagate_errors();
MUST(video_element->set_attribute(HTML::AttributeNames::src, MUST(document.url().to_string())));
MUST(video_element->set_attribute(HTML::AttributeNames::autoplay, String {}));
MUST(video_element->set_attribute(HTML::AttributeNames::controls, String {}));

View file

@ -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_, Optional<FlyString> prefix, Optional<String> is_value, bool synchronous_custom_elements_flag)
WebIDL::ExceptionOr<JS::NonnullGCPtr<Element>> create_element(Document& document, FlyString local_name, Optional<FlyString> namespace_, Optional<FlyString> prefix, Optional<String> is_value, bool synchronous_custom_elements_flag)
{
auto& realm = document.realm();
@ -504,7 +504,8 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Element>> create_element(Document& document
// NOTE: We collapse this into just returning an element where necessary.
// 4. Let definition be the result of looking up a custom element definition given document, namespace, localName, and is.
auto definition = document.lookup_custom_element_definition(namespace_, local_name.to_deprecated_fly_string(), is_value);
DeprecatedFlyString deprecated_namespace = namespace_.has_value() ? namespace_.value().to_deprecated_fly_string() : DeprecatedFlyString {};
auto definition = document.lookup_custom_element_definition(deprecated_namespace, local_name.to_deprecated_fly_string(), is_value);
// 5. If definition is non-null, and definitions name is not equal to its local name (i.e., definition represents a customized built-in element), then:
if (definition && definition->name() != definition->local_name()) {
@ -624,7 +625,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Element>> create_element(Document& document
auto qualified_name = QualifiedName { local_name, prefix, namespace_ };
if (namespace_ == Namespace::HTML) {
if (namespace_ == MUST(FlyString::from_deprecated_fly_string(Namespace::HTML))) {
auto element = create_html_element(realm, document, move(qualified_name));
element->set_is_value(move(is_value));
element->set_custom_element_state(CustomElementState::Uncustomized);
@ -637,7 +638,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Element>> create_element(Document& document
return element;
}
if (namespace_ == Namespace::SVG) {
if (namespace_ == MUST(FlyString::from_deprecated_fly_string(Namespace::SVG))) {
auto element = create_svg_element(realm, document, qualified_name);
if (element) {
element->set_is_value(move(is_value));
@ -646,7 +647,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Element>> create_element(Document& document
}
}
if (namespace_ == Namespace::MathML) {
if (namespace_ == MUST(FlyString::from_deprecated_fly_string(Namespace::MathML))) {
auto element = create_mathml_element(realm, document, qualified_name);
if (element) {
element->set_is_value(move(is_value));

View file

@ -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_, Optional<FlyString> prefix = {}, Optional<String> is = Optional<String> {}, bool synchronous_custom_elements_flag = false);
WebIDL::ExceptionOr<JS::NonnullGCPtr<Element>> create_element(Document&, FlyString local_name, Optional<FlyString> namespace_, Optional<FlyString> prefix = {}, Optional<String> is = Optional<String> {}, bool synchronous_custom_elements_flag = false);
}

View file

@ -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, nodes local name, nodes namespace, nodes namespace prefix, and nodes 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.prefix(), element.is_value(), false).release_value_but_fixme_should_propagate_errors();
auto element_copy = DOM::create_element(*document, element.local_name(), element.namespace_uri(), element.prefix(), element.is_value(), false).release_value_but_fixme_should_propagate_errors();
// 2. For each attribute in nodes attribute list:
element.for_each_attribute([&](auto& name, auto& value) {

View file

@ -1181,7 +1181,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<DocumentFragment>> Range::create_contextual
// - "body" as its local name,
// - The HTML namespace as its namespace, and
// - The context object's node document as its node document.
element = TRY(DOM::create_element(node->document(), HTML::TagNames::body, Namespace::HTML));
element = TRY(DOM::create_element(node->document(), HTML::TagNames::body, MUST(FlyString::from_deprecated_fly_string(Namespace::HTML))));
}
// 3. Let fragment node be the result of invoking the fragment parsing algorithm with fragment as markup, and element as the context element.