Skip to content

Commit 9da09e4

Browse files
kennethmyhralinusg
authored andcommitted
LibWeb: Port URL and URLSearchParams to new String
1 parent 843c9d6 commit 9da09e4

File tree

12 files changed

+229
-157
lines changed

12 files changed

+229
-157
lines changed

Userland/Libraries/LibWeb/Fetch/BodyInit.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ WebIDL::ExceptionOr<Infrastructure::BodyWithType> extract_body(JS::Realm& realm,
9090
},
9191
[&](JS::Handle<URL::URLSearchParams> const& url_search_params) -> WebIDL::ExceptionOr<void> {
9292
// Set source to the result of running the application/x-www-form-urlencoded serializer with object’s list.
93-
source = url_search_params->to_deprecated_string().to_byte_buffer();
93+
auto search_params_bytes = TRY(url_search_params->to_string()).bytes();
94+
source = TRY_OR_THROW_OOM(vm, ByteBuffer::copy(search_params_bytes));
9495
// Set type to `application/x-www-form-urlencoded;charset=UTF-8`.
9596
type = TRY_OR_THROW_OOM(vm, ByteBuffer::copy("application/x-www-form-urlencoded;charset=UTF-8"sv.bytes()));
9697
return {};

Userland/Libraries/LibWeb/HTML/HTMLButtonElement.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ HTMLButtonElement::HTMLButtonElement(DOM::Document& document, DOM::QualifiedName
3232
case TypeAttributeState::Submit:
3333
// Submit Button
3434
// Submit element's form owner from element.
35-
form()->submit_form(this);
35+
form()->submit_form(this).release_value_but_fixme_should_propagate_errors();
3636
break;
3737
case TypeAttributeState::Reset:
3838
// Reset Button

Userland/Libraries/LibWeb/HTML/HTMLFormElement.cpp

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,21 @@ void HTMLFormElement::visit_edges(Cell::Visitor& visitor)
4747
visitor.visit(element.ptr());
4848
}
4949

50-
void HTMLFormElement::submit_form(JS::GCPtr<HTMLElement> submitter, bool from_submit_binding)
50+
ErrorOr<void> HTMLFormElement::submit_form(JS::GCPtr<HTMLElement> submitter, bool from_submit_binding)
5151
{
5252
if (cannot_navigate())
53-
return;
53+
return {};
5454

5555
if (action().is_null()) {
5656
dbgln("Unsupported form action ''");
57-
return;
57+
return {};
5858
}
5959

6060
auto effective_method = method().to_lowercase();
6161

6262
if (effective_method == "dialog") {
6363
dbgln("Failed to submit form: Unsupported form method '{}'", method());
64-
return;
64+
return {};
6565
}
6666

6767
if (effective_method != "get" && effective_method != "post") {
@@ -70,7 +70,7 @@ void HTMLFormElement::submit_form(JS::GCPtr<HTMLElement> submitter, bool from_su
7070

7171
if (!from_submit_binding) {
7272
if (m_firing_submission_events)
73-
return;
73+
return {};
7474

7575
m_firing_submission_events = true;
7676

@@ -91,58 +91,65 @@ void HTMLFormElement::submit_form(JS::GCPtr<HTMLElement> submitter, bool from_su
9191
m_firing_submission_events = false;
9292

9393
if (!continue_)
94-
return;
94+
return {};
9595

9696
// This is checked again because arbitrary JS may have run when handling submit,
9797
// which may have changed the result.
9898
if (cannot_navigate())
99-
return;
99+
return {};
100100
}
101101

102102
AK::URL url(document().parse_url(action()));
103103

104104
if (!url.is_valid()) {
105105
dbgln("Failed to submit form: Invalid URL: {}", action());
106-
return;
106+
return {};
107107
}
108108

109109
if (url.scheme() == "file") {
110110
if (document().url().scheme() != "file") {
111111
dbgln("Failed to submit form: Security violation: {} may not submit to {}", document().url(), url);
112-
return;
112+
return {};
113113
}
114114
if (effective_method != "get") {
115115
dbgln("Failed to submit form: Unsupported form method '{}' for URL: {}", method(), url);
116-
return;
116+
return {};
117117
}
118118
} else if (url.scheme() != "http" && url.scheme() != "https") {
119119
dbgln("Failed to submit form: Unsupported protocol for URL: {}", url);
120-
return;
120+
return {};
121121
}
122122

123123
Vector<URL::QueryParam> parameters;
124124

125125
for_each_in_inclusive_subtree_of_type<HTMLInputElement>([&](auto& input) {
126-
if (!input.name().is_null() && (input.type() != "submit" || &input == submitter))
127-
parameters.append({ input.name(), input.value() });
126+
if (!input.name().is_null() && (input.type() != "submit" || &input == submitter)) {
127+
auto name = String::from_deprecated_string(input.name()).release_value_but_fixme_should_propagate_errors();
128+
auto value = String::from_deprecated_string(input.value()).release_value_but_fixme_should_propagate_errors();
129+
parameters.append({ move(name), move(value) });
130+
}
128131
return IterationDecision::Continue;
129132
});
130133

131134
if (effective_method == "get") {
132-
url.set_query(url_encode(parameters, AK::URL::PercentEncodeSet::ApplicationXWWWFormUrlencoded));
135+
auto url_encoded_parameters = TRY(url_encode(parameters, AK::URL::PercentEncodeSet::ApplicationXWWWFormUrlencoded)).to_deprecated_string();
136+
url.set_query(move(url_encoded_parameters));
133137
}
134138

135139
LoadRequest request = LoadRequest::create_for_url_on_page(url, document().page());
136140

137141
if (effective_method == "post") {
138-
auto body = url_encode(parameters, AK::URL::PercentEncodeSet::ApplicationXWWWFormUrlencoded).to_byte_buffer();
142+
auto url_encoded_parameters_as_bytes = TRY(url_encode(parameters, AK::URL::PercentEncodeSet::ApplicationXWWWFormUrlencoded)).bytes();
143+
auto body = TRY(ByteBuffer::copy(url_encoded_parameters_as_bytes));
139144
request.set_method("POST");
140145
request.set_header("Content-Type", "application/x-www-form-urlencoded");
141146
request.set_body(move(body));
142147
}
143148

144149
if (auto* page = document().page())
145150
page->load(request);
151+
152+
return {};
146153
}
147154

148155
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#resetting-a-form
@@ -166,9 +173,12 @@ void HTMLFormElement::reset_form()
166173
}
167174
}
168175

169-
void HTMLFormElement::submit()
176+
WebIDL::ExceptionOr<void> HTMLFormElement::submit()
170177
{
171-
submit_form(this, true);
178+
auto& vm = realm().vm();
179+
180+
TRY_OR_THROW_OOM(vm, submit_form(this, true));
181+
return {};
172182
}
173183

174184
// https://html.spec.whatwg.org/multipage/forms.html#dom-form-reset

Userland/Libraries/LibWeb/HTML/HTMLFormElement.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ class HTMLFormElement final : public HTMLElement {
2222
DeprecatedString action() const;
2323
DeprecatedString method() const { return attribute(HTML::AttributeNames::method); }
2424

25-
void submit_form(JS::GCPtr<HTMLElement> submitter, bool from_submit_binding = false);
25+
ErrorOr<void> submit_form(JS::GCPtr<HTMLElement> submitter, bool from_submit_binding = false);
2626

2727
void reset_form();
2828

2929
// NOTE: This is for the JS bindings. Use submit_form instead.
30-
void submit();
30+
WebIDL::ExceptionOr<void> submit();
3131

3232
// NOTE: This is for the JS bindings. Use submit_form instead.
3333
void reset();

Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ HTMLInputElement::HTMLInputElement(DOM::Document& document, DOM::QualifiedName q
3535
// FIXME: 1. If this element is not mutable and is not in the Checkbox state and is not in the Radio state, then return.
3636

3737
// 2. Run this element's input activation behavior, if any, and do nothing otherwise.
38-
run_input_activation_behavior();
38+
run_input_activation_behavior().release_value_but_fixme_should_propagate_errors();
3939
};
4040
}
4141

@@ -217,12 +217,12 @@ WebIDL::ExceptionOr<void> HTMLInputElement::show_picker()
217217
}
218218

219219
// https://html.spec.whatwg.org/multipage/input.html#input-activation-behavior
220-
void HTMLInputElement::run_input_activation_behavior()
220+
ErrorOr<void> HTMLInputElement::run_input_activation_behavior()
221221
{
222222
if (type_state() == TypeAttributeState::Checkbox || type_state() == TypeAttributeState::RadioButton) {
223223
// 1. If the element is not connected, then return.
224224
if (!is_connected())
225-
return;
225+
return {};
226226

227227
// 2. Fire an event named input at the element with the bubbles and composed attributes initialized to true.
228228
auto input_event = DOM::Event::create(realm(), HTML::EventNames::input).release_value_but_fixme_should_propagate_errors();
@@ -238,19 +238,21 @@ void HTMLInputElement::run_input_activation_behavior()
238238
JS::GCPtr<HTMLFormElement> form;
239239
// 1. If the element does not have a form owner, then return.
240240
if (!(form = this->form()))
241-
return;
241+
return {};
242242

243243
// 2. If the element's node document is not fully active, then return.
244244
if (!document().is_fully_active())
245-
return;
245+
return {};
246246

247247
// 3. Submit the form owner from the element.
248-
form->submit_form(this);
248+
TRY(form->submit_form(this));
249249
} else if (type_state() == TypeAttributeState::FileUpload) {
250250
show_the_picker_if_applicable(*this);
251251
} else {
252252
dispatch_event(DOM::Event::create(realm(), EventNames::change).release_value_but_fixme_should_propagate_errors());
253253
}
254+
255+
return {};
254256
}
255257

256258
void HTMLInputElement::did_edit_text_node(Badge<BrowsingContext>)

Userland/Libraries/LibWeb/HTML/HTMLInputElement.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ class HTMLInputElement final
139139

140140
static TypeAttributeState parse_type_attribute(StringView);
141141
void create_shadow_tree_if_needed();
142-
void run_input_activation_behavior();
142+
ErrorOr<void> run_input_activation_behavior();
143143
void set_checked_within_group();
144144

145145
// https://html.spec.whatwg.org/multipage/input.html#value-sanitization-algorithm

0 commit comments

Comments
 (0)