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

@ -904,9 +904,7 @@ HTML::HTMLHtmlElement* Document::html_element()
{
// The html element of a document is its document element, if it's an html element, and null otherwise.
auto* html = document_element();
if (is<HTML::HTMLHtmlElement>(html))
return as<HTML::HTMLHtmlElement>(html);
return nullptr;
return as_if<HTML::HTMLHtmlElement>(html);
}
// https://html.spec.whatwg.org/multipage/dom.html#the-head-element-2

View file

@ -1049,14 +1049,15 @@ WebIDL::ExceptionOr<void> Element::set_inner_html(StringView value)
auto fragment = TRY(as<Element>(*context).parse_fragment(value));
// 4. If context is a template element, then set context to the template element's template contents (a DocumentFragment).
if (is<HTML::HTMLTemplateElement>(*context))
context = as<HTML::HTMLTemplateElement>(*context).content();
auto* template_element = as_if<HTML::HTMLTemplateElement>(*context);
if (template_element)
context = template_element->content();
// 5. Replace all with fragment within context.
context->replace_all(fragment);
// NOTE: We don't invalidate style & layout for <template> elements since they don't affect rendering.
if (!is<HTML::HTMLTemplateElement>(*context)) {
if (!template_element) {
context->set_needs_style_update(true);
if (context->is_connected()) {

View file

@ -491,10 +491,11 @@ public:
void for_each_numbered_item_owned_by_list_owner(Callback callback)
{
for (auto* node = this->first_child(); node != nullptr; node = node->next_in_pre_order(this)) {
if (!is<Element>(*node))
auto* element = as_if<Element>(node);
if (!element)
continue;
static_cast<Element*>(node)->m_is_contained_in_list_subtree = true;
element->m_is_contained_in_list_subtree = true;
if (node->is_html_ol_ul_menu_element()) {
// Skip list nodes and their descendents. They have their own, unrelated ordinals.
@ -507,8 +508,6 @@ public:
if (!node->layout_node())
continue; // Skip nodes that do not participate in the layout.
auto* element = static_cast<Element*>(node);
if (!element->computed_properties()->display().is_list_item())
continue; // Skip nodes that are not list items.

View file

@ -164,8 +164,7 @@ static bool default_passive_value(FlyString const& type, EventTarget* event_targ
if (is<HTML::Window>(event_target))
return true;
if (is<Node>(event_target)) {
auto* node = as<Node>(event_target);
if (auto* node = as_if<Node>(event_target)) {
if (&node->document() == event_target || node->document().document_element() == event_target || node->document().body() == event_target)
return true;
}
@ -400,8 +399,7 @@ WebIDL::CallbackType* EventTarget::get_current_value_of_event_handler(FlyString
GC::Ptr<Element> element;
GC::Ptr<Document> document;
if (is<Element>(this)) {
auto* element_event_target = as<Element>(this);
if (auto* element_event_target = as_if<Element>(this)) {
element = element_event_target;
document = &element_event_target->document();
} else {

View file

@ -136,11 +136,11 @@ String Node::base_uri() const
const HTML::HTMLAnchorElement* Node::enclosing_link_element() const
{
for (auto* node = this; node; node = node->parent()) {
if (!is<HTML::HTMLAnchorElement>(*node))
auto const* anchor_element = as_if<HTML::HTMLAnchorElement>(*node);
if (!anchor_element)
continue;
auto const& anchor_element = static_cast<HTML::HTMLAnchorElement const&>(*node);
if (anchor_element.has_attribute(HTML::AttributeNames::href))
return &anchor_element;
if (anchor_element->has_attribute(HTML::AttributeNames::href))
return anchor_element;
}
return nullptr;
}
@ -153,8 +153,8 @@ const HTML::HTMLElement* Node::enclosing_html_element() const
const HTML::HTMLElement* Node::enclosing_html_element_with_attribute(FlyString const& attribute) const
{
for (auto* node = this; node; node = node->parent()) {
if (is<HTML::HTMLElement>(*node) && as<HTML::HTMLElement>(*node).has_attribute(attribute))
return as<HTML::HTMLElement>(node);
if (auto* html_element = as_if<HTML::HTMLElement>(*node); html_element && html_element->has_attribute(attribute))
return html_element;
}
return nullptr;
}
@ -354,13 +354,13 @@ Optional<String> Node::node_value() const
// The nodeValue getter steps are to return the following, switching on the interface this implements:
// If Attr, return thiss value.
if (is<Attr>(this)) {
return as<Attr>(this)->value();
if (auto* attr = as_if<Attr>(this)) {
return attr->value();
}
// If CharacterData, return thiss data.
if (is<CharacterData>(this)) {
return as<CharacterData>(this)->data().to_utf8_but_should_be_ported_to_utf16();
if (auto* character_data = as_if<CharacterData>(this)) {
return character_data->data().to_utf8_but_should_be_ported_to_utf16();
}
// Otherwise, return null.
@ -375,11 +375,11 @@ void Node::set_node_value(Optional<String> const& maybe_value)
auto value = maybe_value.value_or(String {});
// If Attr, set an existing attribute value with this and the given value.
if (is<Attr>(this)) {
as<Attr>(this)->set_value(move(value));
} else if (is<CharacterData>(this)) {
if (auto* attr = as_if<Attr>(this)) {
attr->set_value(move(value));
} else if (auto* character_data = as_if<CharacterData>(this)) {
// If CharacterData, replace data with node this, offset 0, count thiss length, and data the given value.
as<CharacterData>(this)->set_data(Utf16String::from_utf8(value));
character_data->set_data(Utf16String::from_utf8(value));
}
// Otherwise, do nothing.
@ -545,8 +545,8 @@ Node& Node::shadow_including_root()
// The shadow-including root of an object is its roots hosts shadow-including root,
// if the objects root is a shadow root; otherwise its root.
auto& node_root = root();
if (is<ShadowRoot>(node_root)) {
if (auto* host = static_cast<ShadowRoot&>(node_root).host(); host)
if (auto* shadow_root = as_if<ShadowRoot>(node_root)) {
if (auto* host = shadow_root->host(); host)
return host->shadow_including_root();
}
return node_root;