Skip to content

Commit 2fa84f1

Browse files
tete17Lubrsi
authored andcommitted
LibWeb: Properly propagate errors for Node set_text_content
This function was supposed to throw errors even before the TrustedTypes spec thanks to the CharacterData replaceData call but had a MUST. This changes this to ensure this function can throw an error
1 parent 887537b commit 2fa84f1

File tree

7 files changed

+19
-19
lines changed

7 files changed

+19
-19
lines changed

Libraries/LibWeb/DOM/DocumentLoading.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ static WebIDL::ExceptionOr<GC::Ref<DOM::Document>> load_media_document(HTML::Nav
296296
};
297297

298298
auto style_element = TRY(DOM::create_element(document, HTML::TagNames::style, Namespace::HTML));
299-
style_element->set_text_content(R"~~~(
299+
MUST(style_element->set_text_content(R"~~~(
300300
:root {
301301
background-color: #222;
302302
}
@@ -310,7 +310,7 @@ static WebIDL::ExceptionOr<GC::Ref<DOM::Document>> load_media_document(HTML::Nav
310310
img {
311311
background-color: #fff;
312312
}
313-
)~~~"_utf16);
313+
)~~~"_utf16));
314314
TRY(document->head()->append_child(style_element));
315315

316316
auto url_string = document->url_string();

Libraries/LibWeb/DOM/Node.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ Optional<Utf16String> Node::text_content() const
199199
}
200200

201201
// https://dom.spec.whatwg.org/#ref-for-dom-node-textcontent%E2%91%A0
202-
void Node::set_text_content(Optional<Utf16String> const& maybe_content)
202+
WebIDL::ExceptionOr<void> Node::set_text_content(Optional<Utf16String> const& maybe_content)
203203
{
204204
// The textContent setter steps are to, if the given value is null, act as if it was the empty string instead,
205205
// and then do as described below, switching on the interface this implements:
@@ -209,20 +209,19 @@ void Node::set_text_content(Optional<Utf16String> const& maybe_content)
209209
if (is<DocumentFragment>(this) || is<Element>(this)) {
210210
// OPTIMIZATION: Replacing nothing with nothing is a no-op. Avoid all invalidation in this case.
211211
if (!first_child() && content.is_empty()) {
212-
return;
212+
return {};
213213
}
214214
string_replace_all(content);
215215
}
216216

217217
// If CharacterData, replace data with node this, offset 0, count this’s length, and data the given value.
218218
else if (auto* character_data = as_if<CharacterData>(*this)) {
219-
MUST(character_data->replace_data(0, character_data->length_in_utf16_code_units(), content));
219+
TRY(character_data->replace_data(0, character_data->length_in_utf16_code_units(), content));
220220
}
221221

222222
// If Attr, set an existing attribute value with this and the given value.
223223
else if (auto* attribute = as_if<Attr>(*this)) {
224-
// FIXME: This should propagate
225-
MUST(attribute->set_value(content.to_utf8_but_should_be_ported_to_utf16()));
224+
TRY(attribute->set_value(content.to_utf8_but_should_be_ported_to_utf16()));
226225
}
227226

228227
// Otherwise, do nothing.
@@ -233,6 +232,7 @@ void Node::set_text_content(Optional<Utf16String> const& maybe_content)
233232
}
234233

235234
document().bump_dom_tree_version();
235+
return {};
236236
}
237237

238238
// https://dom.spec.whatwg.org/#dom-node-normalize

Libraries/LibWeb/DOM/Node.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ class WEB_API Node : public EventTarget
280280

281281
Utf16String descendant_text_content() const;
282282
Optional<Utf16String> text_content() const;
283-
void set_text_content(Optional<Utf16String> const&);
283+
WebIDL::ExceptionOr<void> set_text_content(Optional<Utf16String> const&);
284284

285285
WebIDL::ExceptionOr<void> normalize();
286286

Libraries/LibWeb/HTML/HTMLDetailsElement.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ WebIDL::ExceptionOr<void> HTMLDetailsElement::create_shadow_tree_if_needed()
243243

244244
// The third child element is either a link or style element with the following styles for the default summary:
245245
auto style = TRY(DOM::create_element(document(), HTML::TagNames::style, Namespace::HTML));
246-
style->set_text_content(R"~~~(
246+
MUST(style->set_text_content(R"~~~(
247247
:host summary {
248248
display: list-item;
249249
counter-increment: list-item 0;
@@ -252,7 +252,7 @@ WebIDL::ExceptionOr<void> HTMLDetailsElement::create_shadow_tree_if_needed()
252252
:host([open]) summary {
253253
list-style-type: disclosure-open;
254254
}
255-
)~~~"_utf16);
255+
)~~~"_utf16));
256256
MUST(shadow_root->append_child(style));
257257

258258
m_summary_slot = static_cast<HTML::HTMLSlotElement&>(*summary_slot);

Libraries/LibWeb/HTML/HTMLInputElement.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,7 +1082,7 @@ void HTMLInputElement::create_text_input_shadow_tree()
10821082
m_text_node = realm().create<DOM::Text>(document(), Utf16String {});
10831083
if (type_state() == TypeAttributeState::Password)
10841084
m_text_node->set_is_password_input({}, true);
1085-
m_text_node->set_text_content(m_value);
1085+
MUST(m_text_node->set_text_content(m_value));
10861086
handle_maxlength_attribute();
10871087
MUST(m_inner_text_element->append_child(*m_text_node));
10881088

@@ -1225,15 +1225,15 @@ void HTMLInputElement::update_file_input_shadow_tree()
12251225
return;
12261226

12271227
auto files_label = has_attribute(HTML::AttributeNames::multiple) ? "files"sv : "file"sv;
1228-
m_file_button->set_text_content(Utf16String::formatted("Select {}...", files_label));
1228+
MUST(m_file_button->set_text_content(Utf16String::formatted("Select {}...", files_label)));
12291229

12301230
if (m_selected_files && m_selected_files->length() > 0) {
12311231
if (m_selected_files->length() == 1)
1232-
m_file_label->set_text_content(Utf16String::from_utf8(m_selected_files->item(0)->name()));
1232+
MUST(m_file_label->set_text_content(Utf16String::from_utf8(m_selected_files->item(0)->name())));
12331233
else
1234-
m_file_label->set_text_content(Utf16String::formatted("{} files selected.", m_selected_files->length()));
1234+
MUST(m_file_label->set_text_content(Utf16String::formatted("{} files selected.", m_selected_files->length())));
12351235
} else {
1236-
m_file_label->set_text_content(Utf16String::formatted("No {} selected.", files_label));
1236+
MUST(m_file_label->set_text_content(Utf16String::formatted("No {} selected.", files_label)));
12371237
}
12381238
}
12391239

Libraries/LibWeb/HTML/HTMLSelectElement.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ void HTMLSelectElement::update_inner_text_element()
633633
// Update inner text element to the label of the selected option
634634
for (auto const& option_element : m_cached_list_of_options) {
635635
if (option_element->selected()) {
636-
m_inner_text_element->set_text_content(Infra::strip_and_collapse_whitespace(Utf16String::from_utf8(option_element->label())));
636+
MUST(m_inner_text_element->set_text_content(Infra::strip_and_collapse_whitespace(Utf16String::from_utf8(option_element->label()))));
637637
return;
638638
}
639639
}

Libraries/LibWeb/HTML/HTMLTextAreaElement.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ void HTMLTextAreaElement::reset_algorithm()
117117
set_raw_value(child_text_content());
118118

119119
if (m_text_node) {
120-
m_text_node->set_text_content(m_raw_value);
120+
MUST(m_text_node->set_text_content(m_raw_value));
121121
update_placeholder_visibility();
122122
}
123123
}
@@ -350,7 +350,7 @@ void HTMLTextAreaElement::create_shadow_tree_if_needed()
350350
m_text_node = realm().create<DOM::Text>(document(), Utf16String {});
351351
// NOTE: If `children_changed()` was called before now, `m_raw_value` will hold the text content.
352352
// Otherwise, it will get filled in whenever that does get called.
353-
m_text_node->set_text_content(m_raw_value);
353+
MUST(m_text_node->set_text_content(m_raw_value));
354354
handle_maxlength_attribute();
355355
MUST(m_inner_text_element->append_child(*m_text_node));
356356

@@ -403,7 +403,7 @@ void HTMLTextAreaElement::children_changed(ChildrenChangedMetadata const* metada
403403
if (!m_dirty_value) {
404404
set_raw_value(child_text_content());
405405
if (m_text_node)
406-
m_text_node->set_text_content(m_raw_value);
406+
MUST(m_text_node->set_text_content(m_raw_value));
407407
update_placeholder_visibility();
408408
}
409409
}

0 commit comments

Comments
 (0)