LibWeb: Replace is<T>() with as_if<T>() where possible
Some checks are pending
CI / macOS, arm64, Sanitizer, Clang (push) Waiting to run
CI / Linux, x86_64, Fuzzers, Clang (push) Waiting to run
CI / Linux, x86_64, Sanitizer, GNU (push) Waiting to run
CI / Linux, x86_64, Sanitizer, Clang (push) Waiting to run
Package the js repl as a binary artifact / Linux, arm64 (push) Waiting to run
Package the js repl as a binary artifact / macOS, arm64 (push) Waiting to run
Package the js repl as a binary artifact / Linux, x86_64 (push) Waiting to run
Run test262 and test-wasm / run_and_update_results (push) Waiting to run
Lint Code / lint (push) Waiting to run
Label PRs with merge conflicts / auto-labeler (push) Waiting to run
Push notes / build (push) Waiting to run

This commit is contained in:
Tim Ledbetter 2025-08-25 07:22:57 +01:00 committed by Jelle Raaijmakers
commit cb1a1a5cb5
Notes: github-actions[bot] 2025-08-25 16:46:19 +00:00
26 changed files with 92 additions and 109 deletions

View file

@ -526,10 +526,8 @@ TaskID queue_global_task(HTML::Task::Source source, JS::Object& global_object, G
// 2. Let document be global's associated Document, if global is a Window object; otherwise null.
DOM::Document* document { nullptr };
if (is<HTML::Window>(global_object)) {
auto& window_object = as<HTML::Window>(global_object);
document = &window_object.associated_document();
}
if (auto* window_object = as_if<HTML::Window>(global_object))
document = &window_object->associated_document();
// 3. Queue a task given source, event loop, document, and steps.
return queue_a_task(source, *event_loop, document, steps);

View file

@ -215,11 +215,12 @@ static void merge_with_the_next_text_node(DOM::Text& node)
auto next = node.next_sibling();
// 2. If next is not a Text node, then return.
if (!is<DOM::Text>(next))
auto* text = as_if<DOM::Text>(next);
if (!text)
return;
// 3. Replace data with node, node's data's length, 0, and next's data.
MUST(node.replace_data(node.length_in_utf16_code_units(), 0, static_cast<DOM::Text const&>(*next).data()));
MUST(node.replace_data(node.length_in_utf16_code_units(), 0, text->data()));
// 4. Remove next.
next->remove();
@ -253,8 +254,8 @@ WebIDL::ExceptionOr<void> HTMLElement::set_outer_text(Utf16View const& value)
merge_with_the_next_text_node(static_cast<DOM::Text&>(*next->previous_sibling()));
// 8. If previous is a Text node, then merge with the next text node given previous.
if (is<DOM::Text>(previous))
merge_with_the_next_text_node(static_cast<DOM::Text&>(*previous));
if (auto* previous_text = as_if<DOM::Text>(previous))
merge_with_the_next_text_node(*previous_text);
set_needs_style_update(true);
return {};
@ -842,7 +843,8 @@ GC::Ptr<DOM::NodeList> HTMLElement::labels()
if (!m_labels) {
m_labels = DOM::LiveNodeList::create(realm(), root(), DOM::LiveNodeList::Scope::Descendants, [&](auto& node) {
return is<HTMLLabelElement>(node) && as<HTMLLabelElement>(node).control() == this;
auto* label_element = as_if<HTMLLabelElement>(node);
return label_element && label_element->control() == this;
});
}

View file

@ -1087,8 +1087,8 @@ static void update_the_source_set(DOM::Element& element)
VERIFY(is<HTMLImageElement>(element) || is<HTMLLinkElement>(element));
// 1. Set el's source set to an empty source set.
if (is<HTMLImageElement>(element))
static_cast<HTMLImageElement&>(element).set_source_set(SourceSet {});
if (auto* image_element = as_if<HTMLImageElement>(element))
image_element->set_source_set(SourceSet {});
else if (is<HTMLLinkElement>(element))
TODO();

View file

@ -494,11 +494,9 @@ WebIDL::ExceptionOr<void> HTMLInputElement::run_input_activation_behavior(DOM::E
// 3. If the user activated the control while explicitly selecting a coordinate, then set the element's selected
// coordinate to that coordinate.
if (event.is_trusted() && is<UIEvents::MouseEvent>(event)) {
auto const& mouse_event = static_cast<UIEvents::MouseEvent const&>(event);
CSSPixels x { mouse_event.offset_x() };
CSSPixels y { mouse_event.offset_y() };
if (auto* mouse_event = as_if<UIEvents::MouseEvent>(event); mouse_event && event.is_trusted()) {
CSSPixels x { mouse_event->offset_x() };
CSSPixels y { mouse_event->offset_y() };
m_selected_coordinate = { x.to_int(), y.to_int() };
}

View file

@ -715,8 +715,8 @@ private:
}
// 14. ⌛ If the node after pointer is a source element, let candidate be that element.
if (is<HTMLSourceElement>(next_sibling))
candidate = static_cast<HTMLSourceElement*>(next_sibling);
if (auto* source_element = as_if<HTMLSourceElement>(next_sibling))
candidate = source_element;
// 15. ⌛ Advance pointer so that the node before pointer is now the node that was after pointer, and the node
// after pointer is the node after the node that used to be after pointer, if any.

View file

@ -213,8 +213,7 @@ GC::Ptr<HTMLTableSectionElement> HTMLTableElement::t_head()
// The tHead IDL attribute must return, on getting, the first thead element child of the table element,
// if any, or null otherwise.
for (auto* child = first_child(); child; child = child->next_sibling()) {
if (is<HTMLTableSectionElement>(*child)) {
auto table_section_element = &as<HTMLTableSectionElement>(*child);
if (auto* table_section_element = as_if<HTMLTableSectionElement>(*child)) {
if (table_section_element->local_name() == TagNames::thead)
return table_section_element;
}
@ -248,8 +247,7 @@ WebIDL::ExceptionOr<void> HTMLTableElement::set_t_head(HTMLTableSectionElement*
continue;
if (is<HTMLTableCaptionElement>(*child))
continue;
if (is<HTMLTableColElement>(*child)) {
auto table_col_element = &as<HTMLTableColElement>(*child);
if (auto* table_col_element = as_if<HTMLTableColElement>(*child)) {
if (table_col_element->local_name() == TagNames::colgroup)
continue;
}
@ -280,8 +278,7 @@ GC::Ref<HTMLTableSectionElement> HTMLTableElement::create_t_head()
continue;
if (is<HTMLTableCaptionElement>(*child))
continue;
if (is<HTMLTableColElement>(*child)) {
auto table_col_element = &as<HTMLTableColElement>(*child);
if (auto* table_col_element = as_if<HTMLTableColElement>(*child)) {
if (table_col_element->local_name() == TagNames::colgroup)
continue;
}
@ -311,8 +308,7 @@ GC::Ptr<HTMLTableSectionElement> HTMLTableElement::t_foot()
// The tFoot IDL attribute must return, on getting, the first tfoot element child of the table element,
// if any, or null otherwise.
for (auto* child = first_child(); child; child = child->next_sibling()) {
if (is<HTMLTableSectionElement>(*child)) {
auto table_section_element = &as<HTMLTableSectionElement>(*child);
if (auto* table_section_element = as_if<HTMLTableSectionElement>(*child)) {
if (table_section_element->local_name() == TagNames::tfoot)
return table_section_element;
}
@ -384,8 +380,7 @@ GC::Ref<HTMLTableSectionElement> HTMLTableElement::create_t_body()
for (auto* child = last_child(); child; child = child->previous_sibling()) {
if (!is<HTMLElement>(*child))
continue;
if (is<HTMLTableSectionElement>(*child)) {
auto table_section_element = &as<HTMLTableSectionElement>(*child);
if (auto* table_section_element = as_if<HTMLTableSectionElement>(*child)) {
if (table_section_element->local_name() == TagNames::tbody) {
// We have found an element which is a <tbody> we'll insert after this
child_to_insert_before = child->next_sibling();

View file

@ -1228,8 +1228,10 @@ static WebIDL::ExceptionOr<Navigable::NavigationParamsVariant> create_navigation
// 26. If navigable's container is an iframe, and response's timing allow passed flag is set,
// then set navigable's container's pending resource-timing start time to null.
if (navigable->container() && is<HTML::HTMLIFrameElement>(*navigable->container()) && response_holder->response()->timing_allow_passed())
static_cast<HTML::HTMLIFrameElement&>(*navigable->container()).set_pending_resource_start_time({});
if (navigable->container() && response_holder->response()->timing_allow_passed()) {
if (auto* iframe_element = as_if<HTML::HTMLIFrameElement>(*navigable->container()))
iframe_element->set_pending_resource_start_time({});
}
// 27. Return a new navigation params, with
// id: navigationId

View file

@ -766,8 +766,8 @@ HTMLParser::AdjustedInsertionLocation HTMLParser::find_appropriate_place_for_ins
// 3. If the adjusted insertion location is inside a template element,
// let it instead be inside the template element's template contents, after its last child (if any).
if (is<HTMLTemplateElement>(*adjusted_insertion_location.parent))
adjusted_insertion_location = { static_cast<HTMLTemplateElement const&>(*adjusted_insertion_location.parent).content().ptr(), nullptr };
if (auto* template_element = as_if<HTMLTemplateElement>(*adjusted_insertion_location.parent))
adjusted_insertion_location = { template_element->content().ptr(), nullptr };
// 4. Return the adjusted insertion location.
return adjusted_insertion_location;

View file

@ -1238,8 +1238,8 @@ WebIDL::ExceptionOr<void> Window::window_post_message_steps(JS::Value message, W
// FIXME: Use a FrozenArray
Vector<GC::Root<MessagePort>> new_ports;
for (auto const& object : deserialize_record.transferred_values) {
if (is<HTML::MessagePort>(*object)) {
new_ports.append(as<MessagePort>(*object));
if (auto* message_port = as_if<HTML::MessagePort>(*object)) {
new_ports.append(*message_port);
}
}