Skip to content

Commit e74031a

Browse files
shannonboothawesomekling
authored andcommitted
LibWeb: Port Document interface from DeprecatedString to String
1 parent 9d88e14 commit e74031a

15 files changed

+108
-96
lines changed

Userland/Libraries/LibWeb/DOM/DOMImplementation.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Document>> DOMImplementation::create_docume
5656

5757
// 3. If qualifiedName is not the empty string, then set element to the result of running the internal createElementNS steps, given document, namespace, qualifiedName, and an empty dictionary.
5858
if (!qualified_name.is_empty())
59-
element = TRY(xml_document->create_element_ns(namespace_.value().to_deprecated_string(), qualified_name.to_deprecated_string(), ElementCreationOptions {}));
59+
element = TRY(xml_document->create_element_ns(namespace_.value(), qualified_name, ElementCreationOptions {}));
6060

6161
// 4. If doctype is non-null, append doctype to document.
6262
if (doctype)
@@ -74,13 +74,13 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Document>> DOMImplementation::create_docume
7474

7575
if (deprecated_namespace == Namespace::HTML) {
7676
// -> HTML namespace
77-
xml_document->set_content_type("application/xhtml+xml");
77+
xml_document->set_content_type("application/xhtml+xml"_string);
7878
} else if (deprecated_namespace == Namespace::SVG) {
7979
// -> SVG namespace
80-
xml_document->set_content_type("image/svg+xml");
80+
xml_document->set_content_type("image/svg+xml"_string);
8181
} else {
8282
// -> Any other namespace
83-
xml_document->set_content_type("application/xml");
83+
xml_document->set_content_type("application/xml"_string);
8484
}
8585

8686
// 8. Return document.
@@ -94,7 +94,7 @@ JS::NonnullGCPtr<Document> DOMImplementation::create_html_document(Optional<Stri
9494
auto html_document = Document::create(realm());
9595

9696
// 2. Set doc’s content type to "text/html".
97-
html_document->set_content_type("text/html");
97+
html_document->set_content_type("text/html"_string);
9898

9999
html_document->set_ready_for_post_load_tasks(true);
100100

Userland/Libraries/LibWeb/DOM/Document.cpp

Lines changed: 45 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Document>> Document::create_and_initialize(
257257
// and navigation id is navigationParams's id.
258258
auto document = HTML::HTMLDocument::create(window->realm());
259259
document->m_type = type;
260-
document->m_content_type = move(content_type);
260+
document->m_content_type = MUST(String::from_deprecated_string(content_type));
261261
document->set_origin(navigation_params.origin);
262262
document->m_policy_container = navigation_params.policy_container;
263263
document->m_active_sandboxing_flag_set = navigation_params.final_sandboxing_flag_set;
@@ -410,7 +410,7 @@ JS::GCPtr<Selection::Selection> Document::get_selection() const
410410
}
411411

412412
// https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-document-write
413-
WebIDL::ExceptionOr<void> Document::write(Vector<DeprecatedString> const& strings)
413+
WebIDL::ExceptionOr<void> Document::write(Vector<String> const& strings)
414414
{
415415
StringBuilder builder;
416416
builder.join(""sv, strings);
@@ -419,7 +419,7 @@ WebIDL::ExceptionOr<void> Document::write(Vector<DeprecatedString> const& string
419419
}
420420

421421
// https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-document-writeln
422-
WebIDL::ExceptionOr<void> Document::writeln(Vector<DeprecatedString> const& strings)
422+
WebIDL::ExceptionOr<void> Document::writeln(Vector<String> const& strings)
423423
{
424424
StringBuilder builder;
425425
builder.join(""sv, strings);
@@ -464,7 +464,7 @@ WebIDL::ExceptionOr<void> Document::run_the_document_write_steps(StringView inpu
464464
}
465465

466466
// https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-document-open
467-
WebIDL::ExceptionOr<Document*> Document::open(StringView, StringView)
467+
WebIDL::ExceptionOr<Document*> Document::open(Optional<String> const&, Optional<String> const&)
468468
{
469469
// 1. If document is an XML document, then throw an "InvalidStateError" DOMException exception.
470470
if (m_type == Type::XML)
@@ -720,7 +720,7 @@ DeprecatedString Document::title() const
720720
}
721721

722722
// https://html.spec.whatwg.org/multipage/dom.html#document.title
723-
WebIDL::ExceptionOr<void> Document::set_title(DeprecatedString const& title)
723+
WebIDL::ExceptionOr<void> Document::set_title(String const& title)
724724
{
725725
auto* document_element = this->document_element();
726726

@@ -744,7 +744,7 @@ WebIDL::ExceptionOr<void> Document::set_title(DeprecatedString const& title)
744744
}
745745

746746
// 3. String replace all with the given value within element.
747-
element->string_replace_all(title);
747+
element->string_replace_all(title.to_deprecated_string());
748748
}
749749

750750
// -> If the document element is in the HTML namespace
@@ -773,7 +773,7 @@ WebIDL::ExceptionOr<void> Document::set_title(DeprecatedString const& title)
773773
}
774774

775775
// 4. String replace all with the given value within element.
776-
element->string_replace_all(title);
776+
element->string_replace_all(title.to_deprecated_string());
777777
}
778778

779779
// -> Otherwise
@@ -784,7 +784,7 @@ WebIDL::ExceptionOr<void> Document::set_title(DeprecatedString const& title)
784784

785785
if (auto* page = this->page()) {
786786
if (browsing_context() == &page->top_level_browsing_context())
787-
page->client().page_did_change_title(title);
787+
page->client().page_did_change_title(title.to_deprecated_string());
788788
}
789789

790790
return {};
@@ -1182,10 +1182,11 @@ void Document::set_hovered_node(Node* node)
11821182
}
11831183
}
11841184

1185-
JS::NonnullGCPtr<HTMLCollection> Document::get_elements_by_name(DeprecatedString const& name)
1185+
JS::NonnullGCPtr<HTMLCollection> Document::get_elements_by_name(String const& name)
11861186
{
1187-
return HTMLCollection::create(*this, HTMLCollection::Scope::Descendants, [name](Element const& element) {
1188-
return element.name() == name;
1187+
auto deprecated_name = name.to_deprecated_string();
1188+
return HTMLCollection::create(*this, HTMLCollection::Scope::Descendants, [deprecated_name](Element const& element) {
1189+
return element.name() == deprecated_name;
11891190
});
11901191
}
11911192

@@ -1359,14 +1360,12 @@ void Document::evaluate_javascript_url(StringView url)
13591360
}
13601361

13611362
// https://dom.spec.whatwg.org/#dom-document-createelement
1362-
WebIDL::ExceptionOr<JS::NonnullGCPtr<Element>> Document::create_element(DeprecatedString const& a_local_name, Variant<DeprecatedString, ElementCreationOptions> const& options)
1363+
WebIDL::ExceptionOr<JS::NonnullGCPtr<Element>> Document::create_element(String const& a_local_name, Variant<String, ElementCreationOptions> const& options)
13631364
{
1364-
auto& vm = this->vm();
1365-
1366-
auto local_name = a_local_name;
1365+
auto local_name = a_local_name.to_deprecated_string();
13671366

13681367
// 1. If localName does not match the Name production, then throw an "InvalidCharacterError" DOMException.
1369-
if (!is_valid_name(local_name))
1368+
if (!is_valid_name(a_local_name))
13701369
return WebIDL::InvalidCharacterError::create(realm(), "Invalid character in tag name."_fly_string);
13711370

13721371
// 2. If this is an HTML document, then set localName to localName in ASCII lowercase.
@@ -1379,8 +1378,8 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Element>> Document::create_element(Deprecat
13791378
// 4. If options is a dictionary and options["is"] exists, then set is to it.
13801379
if (options.has<ElementCreationOptions>()) {
13811380
auto const& element_creation_options = options.get<ElementCreationOptions>();
1382-
if (!element_creation_options.is.is_null())
1383-
is_value = TRY_OR_THROW_OOM(vm, String::from_deprecated_string(element_creation_options.is));
1381+
if (element_creation_options.is.has_value())
1382+
is_value = element_creation_options.is.value();
13841383
}
13851384

13861385
// 5. Let namespace be the HTML namespace, if this is an HTML document or this’s content type is "application/xhtml+xml"; otherwise null.
@@ -1394,21 +1393,24 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Element>> Document::create_element(Deprecat
13941393

13951394
// https://dom.spec.whatwg.org/#dom-document-createelementns
13961395
// https://dom.spec.whatwg.org/#internal-createelementns-steps
1397-
WebIDL::ExceptionOr<JS::NonnullGCPtr<Element>> Document::create_element_ns(DeprecatedString const& namespace_, DeprecatedString const& qualified_name, Variant<DeprecatedString, ElementCreationOptions> const& options)
1396+
WebIDL::ExceptionOr<JS::NonnullGCPtr<Element>> Document::create_element_ns(Optional<String> const& namespace_, String const& qualified_name, Variant<String, ElementCreationOptions> const& options)
13981397
{
1399-
auto& vm = this->vm();
1398+
// FIXME: This conversion is ugly
1399+
StringView namespace_view;
1400+
if (namespace_.has_value())
1401+
namespace_view = namespace_->bytes_as_string_view();
14001402

14011403
// 1. Let namespace, prefix, and localName be the result of passing namespace and qualifiedName to validate and extract.
1402-
auto extracted_qualified_name = TRY(validate_and_extract(realm(), namespace_, qualified_name));
1404+
auto extracted_qualified_name = TRY(validate_and_extract(realm(), namespace_view, qualified_name.to_deprecated_string()));
14031405

14041406
// 2. Let is be null.
14051407
Optional<String> is_value;
14061408

14071409
// 3. If options is a dictionary and options["is"] exists, then set is to it.
14081410
if (options.has<ElementCreationOptions>()) {
14091411
auto const& element_creation_options = options.get<ElementCreationOptions>();
1410-
if (!element_creation_options.is.is_null())
1411-
is_value = TRY_OR_THROW_OOM(vm, String::from_deprecated_string(element_creation_options.is));
1412+
if (element_creation_options.is.has_value())
1413+
is_value = element_creation_options.is.value();
14121414
}
14131415

14141416
// 4. Return the result of creating an element given document, localName, namespace, prefix, is, and with the synchronous custom elements flag set.
@@ -1420,25 +1422,25 @@ JS::NonnullGCPtr<DocumentFragment> Document::create_document_fragment()
14201422
return heap().allocate<DocumentFragment>(realm(), *this);
14211423
}
14221424

1423-
JS::NonnullGCPtr<Text> Document::create_text_node(DeprecatedString const& data)
1425+
JS::NonnullGCPtr<Text> Document::create_text_node(String const& data)
14241426
{
1425-
return heap().allocate<Text>(realm(), *this, MUST(String::from_deprecated_string(data)));
1427+
return heap().allocate<Text>(realm(), *this, data);
14261428
}
14271429

1428-
JS::NonnullGCPtr<Comment> Document::create_comment(DeprecatedString const& data)
1430+
JS::NonnullGCPtr<Comment> Document::create_comment(String const& data)
14291431
{
1430-
return heap().allocate<Comment>(realm(), *this, MUST(String::from_deprecated_string(data)));
1432+
return heap().allocate<Comment>(realm(), *this, data);
14311433
}
14321434

14331435
// https://dom.spec.whatwg.org/#dom-document-createprocessinginstruction
1434-
WebIDL::ExceptionOr<JS::NonnullGCPtr<ProcessingInstruction>> Document::create_processing_instruction(DeprecatedString const& target, DeprecatedString const& data)
1436+
WebIDL::ExceptionOr<JS::NonnullGCPtr<ProcessingInstruction>> Document::create_processing_instruction(String const& target, String const& data)
14351437
{
14361438
// FIXME: 1. If target does not match the Name production, then throw an "InvalidCharacterError" DOMException.
14371439

14381440
// FIXME: 2. If data contains the string "?>", then throw an "InvalidCharacterError" DOMException.
14391441

14401442
// 3. Return a new ProcessingInstruction node, with target set to target, data set to data, and node document set to this.
1441-
return heap().allocate<ProcessingInstruction>(realm(), *this, data, target);
1443+
return heap().allocate<ProcessingInstruction>(realm(), *this, data.to_deprecated_string(), target.to_deprecated_string());
14421444
}
14431445

14441446
JS::NonnullGCPtr<Range> Document::create_range()
@@ -1447,7 +1449,7 @@ JS::NonnullGCPtr<Range> Document::create_range()
14471449
}
14481450

14491451
// https://dom.spec.whatwg.org/#dom-document-createevent
1450-
WebIDL::ExceptionOr<JS::NonnullGCPtr<Event>> Document::create_event(DeprecatedString const& interface)
1452+
WebIDL::ExceptionOr<JS::NonnullGCPtr<Event>> Document::create_event(StringView interface)
14511453
{
14521454
auto& realm = this->realm();
14531455

@@ -2242,7 +2244,7 @@ static inline bool is_valid_name_character(u32 code_point)
22422244
|| (code_point >= 0x203f && code_point <= 0x2040);
22432245
}
22442246

2245-
bool Document::is_valid_name(DeprecatedString const& name)
2247+
bool Document::is_valid_name(String const& name)
22462248
{
22472249
auto code_points = Utf8View { name };
22482250
auto it = code_points.begin();
@@ -2491,7 +2493,7 @@ DeprecatedString Document::domain() const
24912493
return URLParser::serialize_host(effective_domain.release_value()).release_value_but_fixme_should_propagate_errors().to_deprecated_string();
24922494
}
24932495

2494-
void Document::set_domain(DeprecatedString const& domain)
2496+
void Document::set_domain(String const& domain)
24952497
{
24962498
dbgln("(STUBBED) Document::set_domain(domain='{}')", domain);
24972499
}
@@ -2919,7 +2921,7 @@ void Document::did_stop_being_active_document_in_browsing_context(Badge<HTML::Br
29192921
}
29202922

29212923
// https://w3c.github.io/editing/docs/execCommand/#querycommandsupported()
2922-
bool Document::query_command_supported(DeprecatedString const& command) const
2924+
bool Document::query_command_supported(String const& command) const
29232925
{
29242926
dbgln("(STUBBED) Document::query_command_supported(command='{}')", command);
29252927
return false;
@@ -2981,22 +2983,28 @@ DeprecatedString Document::dump_accessibility_tree_as_json()
29812983
}
29822984

29832985
// https://dom.spec.whatwg.org/#dom-document-createattribute
2984-
WebIDL::ExceptionOr<JS::NonnullGCPtr<Attr>> Document::create_attribute(DeprecatedString const& local_name)
2986+
WebIDL::ExceptionOr<JS::NonnullGCPtr<Attr>> Document::create_attribute(String const& local_name)
29852987
{
29862988
// 1. If localName does not match the Name production in XML, then throw an "InvalidCharacterError" DOMException.
29872989
if (!is_valid_name(local_name))
29882990
return WebIDL::InvalidCharacterError::create(realm(), "Invalid character in attribute name."_fly_string);
29892991

29902992
// 2. If this is an HTML document, then set localName to localName in ASCII lowercase.
29912993
// 3. Return a new attribute whose local name is localName and node document is this.
2992-
return Attr::create(*this, is_html_document() ? local_name.to_lowercase() : local_name);
2994+
auto deprecated_local_name = local_name.to_deprecated_string();
2995+
return Attr::create(*this, is_html_document() ? deprecated_local_name.to_lowercase() : deprecated_local_name);
29932996
}
29942997

29952998
// https://dom.spec.whatwg.org/#dom-document-createattributens
2996-
WebIDL::ExceptionOr<JS::NonnullGCPtr<Attr>> Document::create_attribute_ns(DeprecatedString const& namespace_, DeprecatedString const& qualified_name)
2999+
WebIDL::ExceptionOr<JS::NonnullGCPtr<Attr>> Document::create_attribute_ns(Optional<String> const& namespace_, String const& qualified_name)
29973000
{
3001+
// FIXME: This conversion is ugly
3002+
StringView namespace_view;
3003+
if (namespace_.has_value())
3004+
namespace_view = namespace_->bytes_as_string_view();
3005+
29983006
// 1. Let namespace, prefix, and localName be the result of passing namespace and qualifiedName to validate and extract.
2999-
auto extracted_qualified_name = TRY(validate_and_extract(realm(), namespace_, qualified_name));
3007+
auto extracted_qualified_name = TRY(validate_and_extract(realm(), namespace_view, qualified_name.to_deprecated_string()));
30003008

30013009
// 2. Return a new attribute whose namespace is namespace, namespace prefix is prefix, local name is localName, and node document is this.
30023010

0 commit comments

Comments
 (0)