1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 07:27: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

@ -88,11 +88,11 @@ HitTestResult BlockBox::hit_test(const Gfx::IntPoint& position, HitTestType type
HitTestResult last_good_candidate;
for (auto& line_box : m_line_boxes) {
for (auto& fragment : line_box.fragments()) {
if (is<Box>(fragment.layout_node()) && downcast<Box>(fragment.layout_node()).stacking_context())
if (is<Box>(fragment.layout_node()) && verify_cast<Box>(fragment.layout_node()).stacking_context())
continue;
if (enclosing_int_rect(fragment.absolute_rect()).contains(position)) {
if (is<BlockBox>(fragment.layout_node()))
return downcast<BlockBox>(fragment.layout_node()).hit_test(position, type);
return verify_cast<BlockBox>(fragment.layout_node()).hit_test(position, type);
return { fragment.layout_node(), fragment.text_index_at(position.x()) };
}
if (fragment.absolute_rect().top() <= position.y())

View file

@ -21,10 +21,10 @@ public:
virtual HitTestResult hit_test(const Gfx::IntPoint&, HitTestType) const override;
BlockBox* previous_sibling() { return downcast<BlockBox>(Node::previous_sibling()); }
const BlockBox* previous_sibling() const { return downcast<BlockBox>(Node::previous_sibling()); }
BlockBox* next_sibling() { return downcast<BlockBox>(Node::next_sibling()); }
const BlockBox* next_sibling() const { return downcast<BlockBox>(Node::next_sibling()); }
BlockBox* previous_sibling() { return verify_cast<BlockBox>(Node::previous_sibling()); }
const BlockBox* previous_sibling() const { return verify_cast<BlockBox>(Node::previous_sibling()); }
BlockBox* next_sibling() { return verify_cast<BlockBox>(Node::next_sibling()); }
const BlockBox* next_sibling() const { return verify_cast<BlockBox>(Node::next_sibling()); }
template<typename Callback>
void for_each_fragment(Callback);

View file

@ -72,7 +72,7 @@ void BlockFormattingContext::compute_width(Box& box)
if (is<ReplacedBox>(box)) {
// FIXME: This should not be done *by* ReplacedBox
auto& replaced = downcast<ReplacedBox>(box);
auto& replaced = verify_cast<ReplacedBox>(box);
replaced.prepare_for_replaced_layout();
compute_width_for_block_level_replaced_element_in_normal_flow(replaced);
return;
@ -304,7 +304,7 @@ float BlockFormattingContext::compute_theoretical_height(const Box& box)
// Then work out what the height is, based on box type and CSS properties.
float height = 0;
if (is<ReplacedBox>(box)) {
height = compute_height_for_replaced_element(downcast<ReplacedBox>(box));
height = compute_height_for_replaced_element(verify_cast<ReplacedBox>(box));
} else {
if (box.computed_values().height().is_undefined_or_auto()
|| (computed_values.height().is_percentage() && !containing_block.computed_values().height().is_absolute())) {
@ -410,10 +410,10 @@ void BlockFormattingContext::layout_block_level_children(Box& box, LayoutMode la
// FIXME: This should be factored differently. It's uncool that we mutate the tree *during* layout!
// Instead, we should generate the marker box during the tree build.
if (is<ListItemBox>(child_box))
downcast<ListItemBox>(child_box).layout_marker();
verify_cast<ListItemBox>(child_box).layout_marker();
content_height = max(content_height, child_box.effective_offset().y() + child_box.height() + child_box.box_model().margin_box().bottom);
content_width = max(content_width, downcast<Box>(child_box).width());
content_width = max(content_width, verify_cast<Box>(child_box).width());
return IterationDecision::Continue;
});
@ -526,7 +526,7 @@ void BlockFormattingContext::layout_initial_containing_block(LayoutMode layout_m
{
auto viewport_rect = context_box().browsing_context().viewport_rect();
auto& icb = downcast<Layout::InitialContainingBlockBox>(context_box());
auto& icb = verify_cast<Layout::InitialContainingBlockBox>(context_box());
icb.build_stacking_context_tree();
icb.set_width(viewport_rect.width());
@ -551,7 +551,7 @@ static Gfx::FloatRect rect_in_coordinate_space(const Box& box, const Box& contex
Gfx::FloatRect rect = box.margin_box_as_relative_rect();
for (auto* ancestor = box.parent(); ancestor; ancestor = ancestor->parent()) {
if (is<Box>(*ancestor)) {
auto offset = downcast<Box>(*ancestor).effective_offset();
auto offset = verify_cast<Box>(*ancestor).effective_offset();
rect.translate_by(offset);
}
if (ancestor == &context_box)

View file

@ -50,7 +50,7 @@ void Box::paint(PaintContext& context, PaintPhase phase)
context.painter().draw_rect(enclosing_int_rect(content_rect), Color::Magenta);
}
if (phase == PaintPhase::FocusOutline && dom_node() && dom_node()->is_element() && downcast<DOM::Element>(*dom_node()).is_focused()) {
if (phase == PaintPhase::FocusOutline && dom_node() && dom_node()->is_element() && verify_cast<DOM::Element>(*dom_node()).is_focused()) {
context.painter().draw_rect(enclosing_int_rect(absolute_rect()), context.palette().focus_outline());
}
}
@ -347,7 +347,7 @@ StackingContext* Box::enclosing_stacking_context()
for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
if (!is<Box>(ancestor))
continue;
auto& ancestor_box = downcast<Box>(*ancestor);
auto& ancestor_box = verify_cast<Box>(*ancestor);
if (!ancestor_box.establishes_stacking_context())
continue;
VERIFY(ancestor_box.stacking_context());

View file

@ -16,7 +16,7 @@ public:
BreakNode(DOM::Document&, HTML::HTMLBRElement&);
virtual ~BreakNode() override;
const HTML::HTMLBRElement& dom_node() const { return downcast<HTML::HTMLBRElement>(*Node::dom_node()); }
const HTML::HTMLBRElement& dom_node() const { return verify_cast<HTML::HTMLBRElement>(*Node::dom_node()); }
private:
virtual void split_into_lines(InlineFormattingContext&, LayoutMode) override;

View file

@ -254,7 +254,7 @@ float FormattingContext::tentative_width_for_replaced_element(const ReplacedBox&
void FormattingContext::compute_width_for_absolutely_positioned_element(Box& box)
{
if (is<ReplacedBox>(box))
compute_width_for_absolutely_positioned_replaced_element(downcast<ReplacedBox>(box));
compute_width_for_absolutely_positioned_replaced_element(verify_cast<ReplacedBox>(box));
else
compute_width_for_absolutely_positioned_non_replaced_element(box);
}
@ -262,7 +262,7 @@ void FormattingContext::compute_width_for_absolutely_positioned_element(Box& box
void FormattingContext::compute_height_for_absolutely_positioned_element(Box& box)
{
if (is<ReplacedBox>(box))
compute_height_for_absolutely_positioned_replaced_element(downcast<ReplacedBox>(box));
compute_height_for_absolutely_positioned_replaced_element(verify_cast<ReplacedBox>(box));
else
compute_height_for_absolutely_positioned_non_replaced_element(box);
}

View file

@ -19,8 +19,8 @@ public:
virtual void paint(PaintContext&, PaintPhase) override;
virtual void prepare_for_replaced_layout() override;
const HTML::HTMLIFrameElement& dom_node() const { return downcast<HTML::HTMLIFrameElement>(ReplacedBox::dom_node()); }
HTML::HTMLIFrameElement& dom_node() { return downcast<HTML::HTMLIFrameElement>(ReplacedBox::dom_node()); }
const HTML::HTMLIFrameElement& dom_node() const { return verify_cast<HTML::HTMLIFrameElement>(ReplacedBox::dom_node()); }
HTML::HTMLIFrameElement& dom_node() { return verify_cast<HTML::HTMLIFrameElement>(ReplacedBox::dom_node()); }
private:
virtual void did_set_rect() override;

View file

@ -60,7 +60,7 @@ void ImageBox::prepare_for_replaced_layout()
}
if (renders_as_alt_text()) {
auto& image_element = downcast<HTML::HTMLImageElement>(dom_node());
auto& image_element = verify_cast<HTML::HTMLImageElement>(dom_node());
auto& font = Gfx::FontDatabase::default_font();
auto alt = image_element.alt();
if (alt.is_empty())
@ -88,7 +88,7 @@ void ImageBox::paint(PaintContext& context, PaintPhase phase)
if (phase == PaintPhase::Foreground) {
if (renders_as_alt_text()) {
auto& image_element = downcast<HTML::HTMLImageElement>(dom_node());
auto& image_element = verify_cast<HTML::HTMLImageElement>(dom_node());
context.painter().set_font(Gfx::FontDatabase::default_font());
Gfx::StylePainter::paint_frame(context.painter(), enclosing_int_rect(absolute_rect()), context.palette(), Gfx::FrameShape::Container, Gfx::FrameShadow::Sunken, 2);
auto alt = image_element.alt();

View file

@ -75,7 +75,7 @@ void InlineFormattingContext::run(Box&, LayoutMode layout_mode)
containing_block().for_each_child([&](auto& child) {
VERIFY(child.is_inline());
if (is<Box>(child) && child.is_absolutely_positioned()) {
layout_absolutely_positioned_element(downcast<Box>(child));
layout_absolutely_positioned_element(verify_cast<Box>(child));
return;
}
@ -181,14 +181,14 @@ void InlineFormattingContext::run(Box&, LayoutMode layout_mode)
void InlineFormattingContext::dimension_box_on_line(Box& box, LayoutMode layout_mode)
{
if (is<ReplacedBox>(box)) {
auto& replaced = downcast<ReplacedBox>(box);
auto& replaced = verify_cast<ReplacedBox>(box);
replaced.set_width(compute_width_for_replaced_element(replaced));
replaced.set_height(compute_height_for_replaced_element(replaced));
return;
}
if (box.is_inline_block()) {
auto& inline_block = const_cast<BlockBox&>(downcast<BlockBox>(box));
auto& inline_block = const_cast<BlockBox&>(verify_cast<BlockBox>(box));
if (inline_block.computed_values().width().is_undefined_or_auto()) {
auto result = calculate_shrink_to_fit_widths(inline_block);

View file

@ -27,7 +27,7 @@ void LineBox::add_fragment(Node& layout_node, int start, int length, float width
m_width += width;
if (is<Box>(layout_node))
downcast<Box>(layout_node).set_containing_line_box_fragment(m_fragments.last());
verify_cast<Box>(layout_node).set_containing_line_box_fragment(m_fragments.last());
}
void LineBox::trim_trailing_whitespace()

View file

@ -41,7 +41,7 @@ StringView LineBoxFragment::text() const
{
if (!is<TextNode>(layout_node()))
return {};
return downcast<TextNode>(layout_node()).text_for_rendering().substring_view(m_start, m_length);
return verify_cast<TextNode>(layout_node()).text_for_rendering().substring_view(m_start, m_length);
}
const Gfx::FloatRect LineBoxFragment::absolute_rect() const
@ -56,7 +56,7 @@ int LineBoxFragment::text_index_at(float x) const
{
if (!is<TextNode>(layout_node()))
return 0;
auto& layout_text = downcast<TextNode>(layout_node());
auto& layout_text = verify_cast<TextNode>(layout_node());
auto& font = layout_text.font();
Utf8View view(text());

View file

@ -150,7 +150,7 @@ void Node::set_needs_display()
Gfx::FloatPoint Node::box_type_agnostic_position() const
{
if (is<Box>(*this))
return downcast<Box>(*this).absolute_position();
return verify_cast<Box>(*this).absolute_position();
VERIFY(is_inline());
Gfx::FloatPoint position;
if (auto* block = containing_block()) {

View file

@ -158,13 +158,13 @@ public:
void for_each_child_in_paint_order(Callback callback) const
{
for_each_child([&](auto& child) {
if (is<Box>(child) && downcast<Box>(child).stacking_context())
if (is<Box>(child) && verify_cast<Box>(child).stacking_context())
return;
if (!child.is_positioned())
callback(child);
});
for_each_child([&](auto& child) {
if (is<Box>(child) && downcast<Box>(child).stacking_context())
if (is<Box>(child) && verify_cast<Box>(child).stacking_context())
return;
if (child.is_positioned())
callback(child);

View file

@ -16,8 +16,8 @@ public:
ReplacedBox(DOM::Document&, DOM::Element&, NonnullRefPtr<CSS::StyleProperties>);
virtual ~ReplacedBox() override;
const DOM::Element& dom_node() const { return downcast<DOM::Element>(*Node::dom_node()); }
DOM::Element& dom_node() { return downcast<DOM::Element>(*Node::dom_node()); }
const DOM::Element& dom_node() const { return verify_cast<DOM::Element>(*Node::dom_node()); }
DOM::Element& dom_node() { return verify_cast<DOM::Element>(*Node::dom_node()); }
bool has_intrinsic_width() const { return m_has_intrinsic_width; }
bool has_intrinsic_height() const { return m_has_intrinsic_height; }

View file

@ -19,7 +19,7 @@ void SVGGraphicsBox::before_children_paint(PaintContext& context, PaintPhase pha
if (phase != PaintPhase::Foreground)
return;
auto& graphics_element = downcast<SVG::SVGGraphicsElement>(dom_node());
auto& graphics_element = verify_cast<SVG::SVGGraphicsElement>(dom_node());
if (graphics_element.fill_color().has_value())
context.svg_context().set_fill_color(graphics_element.fill_color().value());

View file

@ -16,7 +16,7 @@ public:
SVGPathBox(DOM::Document&, SVG::SVGPathElement&, NonnullRefPtr<CSS::StyleProperties>);
virtual ~SVGPathBox() override = default;
SVG::SVGPathElement& dom_node() { return downcast<SVG::SVGPathElement>(SVGGraphicsBox::dom_node()); }
SVG::SVGPathElement& dom_node() { return verify_cast<SVG::SVGPathElement>(SVGGraphicsBox::dom_node()); }
virtual void prepare_for_replaced_layout() override;
virtual void paint(PaintContext& context, PaintPhase phase) override;

View file

@ -16,7 +16,7 @@ public:
SVGSVGBox(DOM::Document&, SVG::SVGSVGElement&, NonnullRefPtr<CSS::StyleProperties>);
virtual ~SVGSVGBox() override = default;
SVG::SVGSVGElement& dom_node() { return downcast<SVG::SVGSVGElement>(SVGGraphicsBox::dom_node()); }
SVG::SVGSVGElement& dom_node() { return verify_cast<SVG::SVGSVGElement>(SVGGraphicsBox::dom_node()); }
virtual void prepare_for_replaced_layout() override;

View file

@ -28,7 +28,7 @@ size_t TableCellBox::colspan() const
{
if (!dom_node())
return 1;
return downcast<DOM::Element>(*dom_node()).attribute(HTML::AttributeNames::colspan).to_uint().value_or(1);
return verify_cast<DOM::Element>(*dom_node()).attribute(HTML::AttributeNames::colspan).to_uint().value_or(1);
}
float TableCellBox::width_of_logical_containing_block() const

View file

@ -261,7 +261,7 @@ void TextNode::handle_mousedown(Badge<EventHandler>, const Gfx::IntPoint& positi
{
if (!parent() || !is<Label>(*parent()))
return;
downcast<Label>(*parent()).handle_mousedown_on_label({}, position, button);
verify_cast<Label>(*parent()).handle_mousedown_on_label({}, position, button);
browsing_context().event_handler().set_mouse_event_tracking_layout_node(this);
}
@ -273,7 +273,7 @@ void TextNode::handle_mouseup(Badge<EventHandler>, const Gfx::IntPoint& position
// NOTE: Changing the state of the DOM node may run arbitrary JS, which could disappear this node.
NonnullRefPtr protect = *this;
downcast<Label>(*parent()).handle_mouseup_on_label({}, position, button);
verify_cast<Label>(*parent()).handle_mouseup_on_label({}, position, button);
browsing_context().event_handler().set_mouse_event_tracking_layout_node(nullptr);
}
@ -281,7 +281,7 @@ void TextNode::handle_mousemove(Badge<EventHandler>, const Gfx::IntPoint& positi
{
if (!parent() || !is<Label>(*parent()))
return;
downcast<Label>(*parent()).handle_mousemove_on_label({}, position, button);
verify_cast<Label>(*parent()).handle_mousemove_on_label({}, position, button);
}
TextNode::ChunkIterator::ChunkIterator(StringView const& text, LayoutMode layout_mode, bool wrap_lines, bool wrap_breaks)

View file

@ -103,13 +103,13 @@ void TreeBuilder::create_layout_tree(DOM::Node& dom_node)
}
}
auto* shadow_root = is<DOM::Element>(dom_node) ? downcast<DOM::Element>(dom_node).shadow_root() : nullptr;
auto* shadow_root = is<DOM::Element>(dom_node) ? verify_cast<DOM::Element>(dom_node).shadow_root() : nullptr;
if ((dom_node.has_children() || shadow_root) && layout_node->can_have_children()) {
push_parent(downcast<NodeWithStyle>(*layout_node));
push_parent(verify_cast<NodeWithStyle>(*layout_node));
if (shadow_root)
create_layout_tree(*shadow_root);
downcast<DOM::ParentNode>(dom_node).for_each_child([&](auto& dom_child) {
verify_cast<DOM::ParentNode>(dom_node).for_each_child([&](auto& dom_child) {
create_layout_tree(dom_child);
});
pop_parent();
@ -121,7 +121,7 @@ RefPtr<Node> TreeBuilder::build(DOM::Node& dom_node)
if (dom_node.parent()) {
// We're building a partial layout tree, so start by building up the stack of parent layout nodes.
for (auto* ancestor = dom_node.parent()->layout_node(); ancestor; ancestor = ancestor->parent())
m_parent_stack.prepend(downcast<NodeWithStyle>(ancestor));
m_parent_stack.prepend(verify_cast<NodeWithStyle>(ancestor));
}
create_layout_tree(dom_node);