1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 00:37:45 +00:00

AK: Rename downcast<T> => verify_cast<T>

This makes it much clearer what this cast actually does: it will
VERIFY that the thing we're casting is a T (using is<T>()).
This commit is contained in:
Andreas Kling 2021-06-24 19:53:42 +02:00
parent 6215a9c2cb
commit ee3a73ddbb
61 changed files with 262 additions and 262 deletions

View file

@ -188,7 +188,7 @@ HTML::HTMLHtmlElement* Document::html_element()
{
auto* html = document_element();
if (is<HTML::HTMLHtmlElement>(html))
return downcast<HTML::HTMLHtmlElement>(html);
return verify_cast<HTML::HTMLHtmlElement>(html);
return nullptr;
}
@ -423,7 +423,7 @@ static void update_style_recursively(DOM::Node& node)
node.for_each_child([&](auto& child) {
if (child.needs_style_update()) {
if (is<Element>(child))
downcast<Element>(child).recompute_style();
verify_cast<Element>(child).recompute_style();
child.set_needs_style_update(false);
}
if (child.child_needs_style_update()) {
@ -836,7 +836,7 @@ ExceptionOr<NonnullRefPtr<Node>> Document::adopt_node_binding(NonnullRefPtr<Node
if (is<ShadowRoot>(*node))
return DOM::HierarchyRequestError::create("Cannot adopt a shadow root into a document");
if (is<DocumentFragment>(*node) && downcast<DocumentFragment>(*node).host())
if (is<DocumentFragment>(*node) && verify_cast<DocumentFragment>(*node).host())
return node;
adopt_node(*node);

View file

@ -290,7 +290,7 @@ String Element::inner_html() const
Function<void(const Node&)> recurse = [&](auto& node) {
for (auto* child = node.first_child(); child; child = child->next_sibling()) {
if (child->is_element()) {
auto& element = downcast<Element>(*child);
auto& element = verify_cast<Element>(*child);
builder.append('<');
builder.append(element.local_name());
element.for_each_attribute([&](auto& name, auto& value) {
@ -311,7 +311,7 @@ String Element::inner_html() const
builder.append('>');
}
if (child->is_text()) {
auto& text = downcast<Text>(*child);
auto& text = verify_cast<Text>(*child);
builder.append(escape_string(text.data(), false));
}
// FIXME: Also handle Comment, ProcessingInstruction, DocumentType

View file

@ -18,11 +18,11 @@ void Event::append_to_path(EventTarget& invocation_target, RefPtr<EventTarget> s
bool root_of_closed_tree = false;
if (is<Node>(invocation_target)) {
auto& invocation_target_node = downcast<Node>(invocation_target);
auto& invocation_target_node = verify_cast<Node>(invocation_target);
if (is<ShadowRoot>(invocation_target_node.root()))
invocation_target_in_shadow_tree = true;
if (is<ShadowRoot>(invocation_target_node)) {
auto& invocation_target_shadow_root = downcast<ShadowRoot>(invocation_target_node);
auto& invocation_target_shadow_root = verify_cast<ShadowRoot>(invocation_target_node);
root_of_closed_tree = invocation_target_shadow_root.closed();
}
}

View file

@ -35,14 +35,14 @@ static EventTarget* retarget(EventTarget* left, [[maybe_unused]] EventTarget* ri
if (!is<Node>(left))
return left;
auto* left_node = downcast<Node>(left);
auto* left_node = verify_cast<Node>(left);
auto* left_root = left_node->root();
if (!is<ShadowRoot>(left_root))
return left;
// FIXME: If right is a node and lefts root is a shadow-including inclusive ancestor of right, return left.
auto* left_shadow_root = downcast<ShadowRoot>(left_root);
auto* left_shadow_root = verify_cast<ShadowRoot>(left_root);
left = left_shadow_root->host();
}
}
@ -76,7 +76,7 @@ bool EventDispatcher::inner_invoke(Event& event, Vector<EventTarget::EventListen
RefPtr<Event> current_event;
if (is<Bindings::WindowObject>(global)) {
auto& bindings_window_global = downcast<Bindings::WindowObject>(global);
auto& bindings_window_global = verify_cast<Bindings::WindowObject>(global);
auto& window_impl = bindings_window_global.impl();
current_event = window_impl.current_event();
if (!invocation_target_in_shadow_tree)
@ -97,7 +97,7 @@ bool EventDispatcher::inner_invoke(Event& event, Vector<EventTarget::EventListen
event.set_in_passive_listener(false);
if (is<Bindings::WindowObject>(global)) {
auto& bindings_window_global = downcast<Bindings::WindowObject>(global);
auto& bindings_window_global = verify_cast<Bindings::WindowObject>(global);
auto& window_impl = bindings_window_global.impl();
window_impl.set_current_event(current_event);
}
@ -162,7 +162,7 @@ bool EventDispatcher::dispatch(NonnullRefPtr<EventTarget> target, NonnullRefPtr<
target_override = target;
} else {
// NOTE: This can be done because legacy_target_override is only set for events targeted at Window.
target_override = downcast<Window>(*target).document();
target_override = verify_cast<Window>(*target).document();
}
RefPtr<EventTarget> activation_target;
@ -232,13 +232,13 @@ bool EventDispatcher::dispatch(NonnullRefPtr<EventTarget> target, NonnullRefPtr<
VERIFY(clear_targets_struct.has_value());
if (is<Node>(clear_targets_struct.value().shadow_adjusted_target.ptr())) {
auto& shadow_adjusted_target_node = downcast<Node>(*clear_targets_struct.value().shadow_adjusted_target);
auto& shadow_adjusted_target_node = verify_cast<Node>(*clear_targets_struct.value().shadow_adjusted_target);
if (is<ShadowRoot>(shadow_adjusted_target_node.root()))
clear_targets = true;
}
if (!clear_targets && is<Node>(clear_targets_struct.value().related_target.ptr())) {
auto& related_target_node = downcast<Node>(*clear_targets_struct.value().related_target);
auto& related_target_node = verify_cast<Node>(*clear_targets_struct.value().related_target);
if (is<ShadowRoot>(related_target_node.root()))
clear_targets = true;
}
@ -246,7 +246,7 @@ bool EventDispatcher::dispatch(NonnullRefPtr<EventTarget> target, NonnullRefPtr<
if (!clear_targets) {
for (auto touch_target : clear_targets_struct.value().touch_target_list) {
if (is<Node>(*touch_target.ptr())) {
auto& touch_target_node = downcast<Node>(*touch_target.ptr());
auto& touch_target_node = verify_cast<Node>(*touch_target.ptr());
if (is<ShadowRoot>(touch_target_node.root())) {
clear_targets = true;
break;

View file

@ -51,8 +51,8 @@ Node::~Node()
const HTML::HTMLAnchorElement* Node::enclosing_link_element() const
{
for (auto* node = this; node; node = node->parent()) {
if (is<HTML::HTMLAnchorElement>(*node) && downcast<HTML::HTMLAnchorElement>(*node).has_attribute(HTML::AttributeNames::href))
return downcast<HTML::HTMLAnchorElement>(node);
if (is<HTML::HTMLAnchorElement>(*node) && verify_cast<HTML::HTMLAnchorElement>(*node).has_attribute(HTML::AttributeNames::href))
return verify_cast<HTML::HTMLAnchorElement>(node);
}
return nullptr;
}
@ -65,8 +65,8 @@ const HTML::HTMLElement* Node::enclosing_html_element() const
const HTML::HTMLElement* Node::enclosing_html_element_with_attribute(const FlyString& attribute) const
{
for (auto* node = this; node; node = node->parent()) {
if (is<HTML::HTMLElement>(*node) && downcast<HTML::HTMLElement>(*node).has_attribute(attribute))
return downcast<HTML::HTMLElement>(node);
if (is<HTML::HTMLElement>(*node) && verify_cast<HTML::HTMLElement>(*node).has_attribute(attribute))
return verify_cast<HTML::HTMLElement>(node);
}
return nullptr;
}
@ -83,7 +83,7 @@ String Node::text_content() const
void Node::set_text_content(const String& content)
{
if (is_text()) {
downcast<Text>(this)->set_data(content);
verify_cast<Text>(this)->set_data(content);
} else {
remove_all_children();
append_child(document().create_text_node(content));
@ -123,9 +123,9 @@ String Node::child_text_content() const
return String::empty();
StringBuilder builder;
downcast<ParentNode>(*this).for_each_child([&](auto& child) {
verify_cast<ParentNode>(*this).for_each_child([&](auto& child) {
if (is<Text>(child))
builder.append(downcast<Text>(child).text_content());
builder.append(verify_cast<Text>(child).text_content());
});
return builder.build();
}
@ -142,7 +142,7 @@ Node* Node::shadow_including_root()
{
auto node_root = root();
if (is<ShadowRoot>(node_root))
return downcast<ShadowRoot>(node_root)->host()->shadow_including_root();
return verify_cast<ShadowRoot>(node_root)->host()->shadow_including_root();
return node_root;
}
@ -155,14 +155,14 @@ Element* Node::parent_element()
{
if (!parent() || !is<Element>(parent()))
return nullptr;
return downcast<Element>(parent());
return verify_cast<Element>(parent());
}
const Element* Node::parent_element() const
{
if (!parent() || !is<Element>(parent()))
return nullptr;
return downcast<Element>(parent());
return verify_cast<Element>(parent());
}
// https://dom.spec.whatwg.org/#concept-node-ensure-pre-insertion-validity
@ -187,7 +187,7 @@ ExceptionOr<void> Node::ensure_pre_insertion_validity(NonnullRefPtr<Node> node,
if (is<Document>(this)) {
if (is<DocumentFragment>(*node)) {
auto node_element_child_count = downcast<DocumentFragment>(*node).child_element_count();
auto node_element_child_count = verify_cast<DocumentFragment>(*node).child_element_count();
if ((node_element_child_count > 1 || node->has_child_of_type<Text>())
|| (node_element_child_count == 1 && (has_child_of_type<Element>() || is<DocumentType>(child.ptr()) /* FIXME: or child is non-null and a doctype is following child. */))) {
return DOM::HierarchyRequestError::create("Invalid node type for insertion");
@ -209,7 +209,7 @@ void Node::insert_before(NonnullRefPtr<Node> node, RefPtr<Node> child, bool supp
{
NonnullRefPtrVector<Node> nodes;
if (is<DocumentFragment>(*node))
nodes = downcast<DocumentFragment>(*node).child_nodes();
nodes = verify_cast<DocumentFragment>(*node).child_nodes();
else
nodes.append(node);
@ -369,7 +369,7 @@ ExceptionOr<NonnullRefPtr<Node>> Node::replace_child(NonnullRefPtr<Node> node, N
if (is<Document>(this)) {
if (is<DocumentFragment>(*node)) {
auto node_element_child_count = downcast<DocumentFragment>(*node).child_element_count();
auto node_element_child_count = verify_cast<DocumentFragment>(*node).child_element_count();
if ((node_element_child_count > 1 || node->has_child_of_type<Text>())
|| (node_element_child_count == 1 && (first_child_of_type<Element>() != child /* FIXME: or a doctype is following child. */))) {
return DOM::HierarchyRequestError::create("Invalid node type for insertion");
@ -411,7 +411,7 @@ NonnullRefPtr<Node> Node::clone_node(Document* document, bool clone_children) co
document = m_document;
RefPtr<Node> copy;
if (is<Element>(this)) {
auto& element = *downcast<Element>(this);
auto& element = *verify_cast<Element>(this);
auto qualified_name = QualifiedName(element.local_name(), element.prefix(), element.namespace_());
auto element_copy = adopt_ref(*new Element(*document, move(qualified_name)));
element.for_each_attribute([&](auto& name, auto& value) {
@ -419,7 +419,7 @@ NonnullRefPtr<Node> Node::clone_node(Document* document, bool clone_children) co
});
copy = move(element_copy);
} else if (is<Document>(this)) {
auto document_ = downcast<Document>(this);
auto document_ = verify_cast<Document>(this);
auto document_copy = Document::create(document_->url());
document_copy->set_encoding(document_->encoding());
document_copy->set_content_type(document_->content_type());
@ -428,22 +428,22 @@ NonnullRefPtr<Node> Node::clone_node(Document* document, bool clone_children) co
// FIXME: Set type ("xml" or "html")
copy = move(document_copy);
} else if (is<DocumentType>(this)) {
auto document_type = downcast<DocumentType>(this);
auto document_type = verify_cast<DocumentType>(this);
auto document_type_copy = adopt_ref(*new DocumentType(*document));
document_type_copy->set_name(document_type->name());
document_type_copy->set_public_id(document_type->public_id());
document_type_copy->set_system_id(document_type->system_id());
copy = move(document_type_copy);
} else if (is<Text>(this)) {
auto text = downcast<Text>(this);
auto text = verify_cast<Text>(this);
auto text_copy = adopt_ref(*new Text(*document, text->data()));
copy = move(text_copy);
} else if (is<Comment>(this)) {
auto comment = downcast<Comment>(this);
auto comment = verify_cast<Comment>(this);
auto comment_copy = adopt_ref(*new Comment(*document, comment->data()));
copy = move(comment_copy);
} else if (is<ProcessingInstruction>(this)) {
auto processing_instruction = downcast<ProcessingInstruction>(this);
auto processing_instruction = verify_cast<ProcessingInstruction>(this);
auto processing_instruction_copy = adopt_ref(*new ProcessingInstruction(*document, processing_instruction->data(), processing_instruction->target()));
copy = move(processing_instruction_copy);
} else {
@ -490,7 +490,7 @@ JS::Object* Node::create_wrapper(JS::GlobalObject& global_object)
void Node::removed_last_ref()
{
if (is<Document>(*this)) {
downcast<Document>(*this).removed_last_ref();
verify_cast<Document>(*this).removed_last_ref();
return;
}
m_deletion_has_begun = true;
@ -534,8 +534,8 @@ void Node::inserted()
ParentNode* Node::parent_or_shadow_host()
{
if (is<ShadowRoot>(*this))
return downcast<ShadowRoot>(*this).host();
return downcast<ParentNode>(parent());
return verify_cast<ShadowRoot>(*this).host();
return verify_cast<ParentNode>(parent());
}
NonnullRefPtrVector<Node> Node::child_nodes() const
@ -595,7 +595,7 @@ u16 Node::compare_document_position(RefPtr<Node> other)
// https://dom.spec.whatwg.org/#concept-tree-host-including-inclusive-ancestor
bool Node::is_host_including_inclusive_ancestor_of(const Node& other) const
{
return is_inclusive_ancestor_of(other) || (is<DocumentFragment>(other.root()) && downcast<DocumentFragment>(other.root())->host() && is_inclusive_ancestor_of(*downcast<DocumentFragment>(other.root())->host().ptr()));
return is_inclusive_ancestor_of(other) || (is<DocumentFragment>(other.root()) && verify_cast<DocumentFragment>(other.root())->host() && is_inclusive_ancestor_of(*verify_cast<DocumentFragment>(other.root())->host().ptr()));
}
// https://dom.spec.whatwg.org/#dom-node-ownerdocument

View file

@ -19,7 +19,7 @@ public:
{
for (auto* sibling = static_cast<NodeType*>(this)->previous_sibling(); sibling; sibling = sibling->previous_sibling()) {
if (is<Element>(*sibling))
return downcast<Element>(sibling);
return verify_cast<Element>(sibling);
}
return nullptr;
}
@ -28,7 +28,7 @@ public:
{
for (auto* sibling = static_cast<NodeType*>(this)->next_sibling(); sibling; sibling = sibling->next_sibling()) {
if (is<Element>(*sibling))
return downcast<Element>(sibling);
return verify_cast<Element>(sibling);
}
return nullptr;
}
@ -37,7 +37,7 @@ public:
{
for (auto* node = static_cast<NodeType*>(this)->next_in_pre_order(); node; node = node->next_in_pre_order()) {
if (is<Element>(*node))
return downcast<Element>(node);
return verify_cast<Element>(node);
}
return nullptr;
}

View file

@ -34,7 +34,7 @@ bool Position::increment_offset()
if (!is<DOM::Text>(*m_node))
return false;
auto& node = downcast<DOM::Text>(*m_node);
auto& node = verify_cast<DOM::Text>(*m_node);
auto text = Utf8View(node.data());
for (auto iterator = text.begin(); !iterator.done(); ++iterator) {
@ -53,7 +53,7 @@ bool Position::decrement_offset()
if (m_offset == 0 || !is<DOM::Text>(*m_node))
return false;
auto& node = downcast<DOM::Text>(*m_node);
auto& node = verify_cast<DOM::Text>(*m_node);
auto text = Utf8View(node.data());
size_t last_smaller_offset = 0;
@ -76,7 +76,7 @@ bool Position::offset_is_at_end_of_node() const
if (!is<DOM::Text>(*m_node))
return false;
auto& node = downcast<DOM::Text>(*m_node);
auto& node = verify_cast<DOM::Text>(*m_node);
auto text = node.data();
return m_offset == text.length();
}

View file

@ -19,7 +19,7 @@ ShadowRoot::ShadowRoot(Document& document, Element& host)
EventTarget* ShadowRoot::get_parent(const Event& event)
{
if (!event.composed()) {
auto& events_first_invocation_target = downcast<Node>(*event.path().first().invocation_target);
auto& events_first_invocation_target = verify_cast<Node>(*event.path().first().invocation_target);
if (events_first_invocation_target.root() == this)
return nullptr;
}