1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 16:47:36 +00:00

LibWeb: Switch to using AK::is and AK::downcast

This commit is contained in:
Andreas Kling 2020-07-26 17:16:18 +02:00
parent fe6474e692
commit 71556e39a4
73 changed files with 249 additions and 433 deletions

View file

@ -155,13 +155,13 @@ void LayoutBlock::layout_contained_boxes(LayoutMode layout_mode)
return IterationDecision::Continue;
box.layout(layout_mode);
if (box.is_replaced())
place_block_level_replaced_element_in_normal_flow(to<LayoutReplaced>(box));
place_block_level_replaced_element_in_normal_flow(downcast<LayoutReplaced>(box));
else if (box.is_block())
place_block_level_non_replaced_element_in_normal_flow(to<LayoutBlock>(box));
place_block_level_non_replaced_element_in_normal_flow(downcast<LayoutBlock>(box));
else
dbg() << "FIXME: LayoutBlock::layout_contained_boxes doesn't know how to place a " << box.class_name();
content_height = max(content_height, box.effective_offset().y() + box.height() + box.box_model().margin_box(*this).bottom);
content_width = max(content_width, to<LayoutBox>(box).width());
content_width = max(content_width, downcast<LayoutBox>(box).width());
return IterationDecision::Continue;
});
@ -257,7 +257,7 @@ void LayoutBlock::layout_inline_children(LayoutMode layout_mode)
}
if (fragment.layout_node().is_inline_block()) {
auto& inline_block = const_cast<LayoutBlock&>(to<LayoutBlock>(fragment.layout_node()));
auto& inline_block = const_cast<LayoutBlock&>(downcast<LayoutBlock>(fragment.layout_node()));
inline_block.set_size(fragment.size());
inline_block.layout(layout_mode);
}
@ -587,7 +587,7 @@ LayoutBlock::ShrinkToFitResult LayoutBlock::calculate_shrink_to_fit_width()
} else {
for_each_child([&](auto& child) {
if (child.is_box())
max_width = max(max_width, to<LayoutBox>(child).width());
max_width = max(max_width, downcast<LayoutBox>(child).width());
});
}
return max_width;
@ -725,11 +725,11 @@ HitTestResult LayoutBlock::hit_test(const Gfx::IntPoint& position) const
HitTestResult last_good_candidate;
for (auto& line_box : m_line_boxes) {
for (auto& fragment : line_box.fragments()) {
if (is<LayoutBox>(fragment.layout_node()) && to<LayoutBox>(fragment.layout_node()).stacking_context())
if (is<LayoutBox>(fragment.layout_node()) && downcast<LayoutBox>(fragment.layout_node()).stacking_context())
continue;
if (enclosing_int_rect(fragment.absolute_rect()).contains(position)) {
if (fragment.layout_node().is_block())
return to<LayoutBlock>(fragment.layout_node()).hit_test(position);
return downcast<LayoutBlock>(fragment.layout_node()).hit_test(position);
return { fragment.layout_node(), fragment.text_index_at(position.x()) };
}
if (fragment.absolute_rect().top() <= position.y())

View file

@ -53,10 +53,10 @@ public:
virtual HitTestResult hit_test(const Gfx::IntPoint&) const override;
LayoutBlock* previous_sibling() { return to<LayoutBlock>(LayoutNode::previous_sibling()); }
const LayoutBlock* previous_sibling() const { return to<LayoutBlock>(LayoutNode::previous_sibling()); }
LayoutBlock* next_sibling() { return to<LayoutBlock>(LayoutNode::next_sibling()); }
const LayoutBlock* next_sibling() const { return to<LayoutBlock>(LayoutNode::next_sibling()); }
LayoutBlock* previous_sibling() { return downcast<LayoutBlock>(LayoutNode::previous_sibling()); }
const LayoutBlock* previous_sibling() const { return downcast<LayoutBlock>(LayoutNode::previous_sibling()); }
LayoutBlock* next_sibling() { return downcast<LayoutBlock>(LayoutNode::next_sibling()); }
const LayoutBlock* next_sibling() const { return downcast<LayoutBlock>(LayoutNode::next_sibling()); }
template<typename Callback>
void for_each_fragment(Callback);
@ -119,10 +119,8 @@ void LayoutBlock::for_each_fragment(Callback callback) const
}
}
template<>
ALWAYS_INLINE bool is<LayoutBlock>(const LayoutNode& node)
{
return node.is_block();
}
}
AK_BEGIN_TYPE_TRAITS(Web::LayoutBlock)
static bool is_type(const Web::LayoutNode& layout_node) { return layout_node.is_block(); }
AK_END_TYPE_TRAITS()

View file

@ -231,7 +231,7 @@ HitTestResult LayoutBox::hit_test(const Gfx::IntPoint& position) const
// m_rect.contains() since inline text rects can't be trusted..
HitTestResult result { absolute_rect().contains(position.x(), position.y()) ? this : nullptr };
for_each_child([&](auto& child) {
if (is<LayoutBox>(child) && to<LayoutBox>(child).stacking_context())
if (is<LayoutBox>(child) && downcast<LayoutBox>(child).stacking_context())
return;
auto child_result = child.hit_test(position);
if (child_result.layout_node)
@ -297,7 +297,7 @@ StackingContext* LayoutBox::enclosing_stacking_context()
for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
if (!ancestor->is_box())
continue;
auto& ancestor_box = to<LayoutBox>(*ancestor);
auto& ancestor_box = downcast<LayoutBox>(*ancestor);
if (!ancestor_box.establishes_stacking_context())
continue;
ASSERT(ancestor_box.stacking_context());

View file

@ -98,10 +98,8 @@ private:
OwnPtr<StackingContext> m_stacking_context;
};
template<>
ALWAYS_INLINE bool is<LayoutBox>(const LayoutNode& node)
{
return node.is_box();
}
}
AK_BEGIN_TYPE_TRAITS(Web::LayoutBox)
static bool is_type(const Web::LayoutNode& layout_node) { return layout_node.is_box(); }
AK_END_TYPE_TRAITS()

View file

@ -36,11 +36,16 @@ public:
LayoutBreak(Document&, const HTMLBRElement&);
virtual ~LayoutBreak() override;
const HTMLBRElement& node() const { return to<HTMLBRElement>(*LayoutNode::node()); }
const HTMLBRElement& node() const { return downcast<HTMLBRElement>(*LayoutNode::node()); }
private:
virtual bool is_break() const override { return true; }
virtual const char* class_name() const override { return "LayoutBreak"; }
virtual void split_into_lines(LayoutBlock&, LayoutMode) override;
};
}
AK_BEGIN_TYPE_TRAITS(Web::LayoutBreak)
static bool is_type(const Web::LayoutNode& layout_node) { return layout_node.is_break(); }
AK_END_TYPE_TRAITS()

View file

@ -48,10 +48,9 @@ private:
virtual bool is_canvas() const override { return true; }
};
template<>
inline bool is<LayoutCanvas>(const LayoutNode& node)
{
return node.is_canvas();
}
}
AK_BEGIN_TYPE_TRAITS(Web::LayoutCanvas)
static bool is_type(const Web::LayoutNode& layout_node) { return layout_node.is_canvas(); }
AK_END_TYPE_TRAITS()

View file

@ -76,7 +76,7 @@ void LayoutDocument::layout(LayoutMode layout_mode)
float lowest_bottom = 0;
for_each_child([&](auto& child) {
ASSERT(is<LayoutBlock>(child));
auto& child_block = to<LayoutBlock>(child);
auto& child_block = downcast<LayoutBlock>(child);
lowest_bottom = max(lowest_bottom, child_block.absolute_rect().bottom());
});
set_height(lowest_bottom);

View file

@ -58,11 +58,8 @@ private:
LayoutRange m_selection;
};
template<>
inline bool is<LayoutDocument>(const LayoutNode& node)
{
return node.is_root();
}
}
AK_BEGIN_TYPE_TRAITS(Web::LayoutDocument)
static bool is_type(const Web::LayoutNode& layout_node) { return layout_node.is_root(); }
AK_END_TYPE_TRAITS()

View file

@ -48,10 +48,8 @@ private:
virtual void did_set_rect() override;
};
template<>
inline bool is<LayoutFrame>(const LayoutNode& node)
{
return node.is_frame();
}
}
AK_BEGIN_TYPE_TRAITS(Web::LayoutFrame)
static bool is_type(const Web::LayoutNode& layout_node) { return layout_node.is_frame(); }
AK_END_TYPE_TRAITS()

View file

@ -71,7 +71,7 @@ void LayoutImage::layout(LayoutMode layout_mode)
}
if (renders_as_alt_text()) {
auto& image_element = to<HTMLImageElement>(node());
auto& image_element = downcast<HTMLImageElement>(node());
auto& font = Gfx::Font::default_font();
auto alt = image_element.alt();
if (alt.is_empty())
@ -101,7 +101,7 @@ void LayoutImage::paint(PaintContext& context, PaintPhase phase)
if (phase == PaintPhase::Foreground) {
if (renders_as_alt_text()) {
auto& image_element = to<HTMLImageElement>(node());
auto& image_element = downcast<HTMLImageElement>(node());
context.painter().set_font(Gfx::Font::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

@ -57,10 +57,8 @@ private:
const ImageLoader& m_image_loader;
};
template<>
inline bool is<LayoutImage>(const LayoutNode& node)
{
return node.is_image();
}
}
AK_BEGIN_TYPE_TRAITS(Web::LayoutImage)
static bool is_type(const Web::LayoutNode& layout_node) { return layout_node.is_image(); }
AK_END_TYPE_TRAITS()

View file

@ -67,7 +67,7 @@ const LayoutBlock* LayoutNode::containing_block() const
auto* ancestor = parent();
while (ancestor && !is<LayoutBlock>(*ancestor))
ancestor = ancestor->parent();
return to<LayoutBlock>(ancestor);
return downcast<LayoutBlock>(ancestor);
};
if (is_text())
@ -81,7 +81,7 @@ const LayoutBlock* LayoutNode::containing_block() const
ancestor = ancestor->parent();
while (ancestor && (!is<LayoutBlock>(ancestor) || ancestor->is_anonymous()))
ancestor = ancestor->containing_block();
return to<LayoutBlock>(ancestor);
return downcast<LayoutBlock>(ancestor);
}
if (position == CSS::Position::Fixed)
@ -96,7 +96,7 @@ void LayoutNode::paint(PaintContext& context, PaintPhase phase)
return;
for_each_child([&](auto& child) {
if (child.is_box() && to<LayoutBox>(child).stacking_context())
if (child.is_box() && downcast<LayoutBox>(child).stacking_context())
return;
child.paint(context, phase);
});
@ -108,7 +108,7 @@ HitTestResult LayoutNode::hit_test(const Gfx::IntPoint& position) const
for_each_child([&](auto& child) {
// Skip over children that establish their own stacking context.
// The outer loop who called us will take care of those.
if (is<LayoutBox>(child) && to<LayoutBox>(child).stacking_context())
if (is<LayoutBox>(child) && downcast<LayoutBox>(child).stacking_context())
return;
auto child_result = child.hit_test(position);
if (child_result.layout_node)
@ -170,7 +170,7 @@ float LayoutNode::font_size() const
Gfx::FloatPoint LayoutNode::box_type_agnostic_position() const
{
if (is_box())
return to<LayoutBox>(*this).absolute_position();
return downcast<LayoutBox>(*this).absolute_position();
ASSERT(is_inline());
Gfx::FloatPoint position;
if (auto* block = containing_block()) {

View file

@ -27,6 +27,7 @@
#pragma once
#include <AK/NonnullRefPtr.h>
#include <AK/TypeCasts.h>
#include <AK/Vector.h>
#include <LibGfx/FloatRect.h>
#include <LibGfx/Rect.h>
@ -40,52 +41,6 @@
namespace Web {
template<typename T>
inline bool is(const LayoutNode&)
{
return false;
}
template<typename T>
inline bool is(const LayoutNode* node)
{
return !node || is<T>(*node);
}
template<>
inline bool is<LayoutNode>(const LayoutNode&)
{
return true;
}
template<typename T>
inline const T& to(const LayoutNode& node)
{
ASSERT(is<T>(node));
return static_cast<const T&>(node);
}
template<typename T>
inline T* to(LayoutNode* node)
{
ASSERT(is<T>(node));
return static_cast<T*>(node);
}
template<typename T>
inline const T* to(const LayoutNode* node)
{
ASSERT(is<T>(node));
return static_cast<const T*>(node);
}
template<typename T>
inline T& to(LayoutNode& node)
{
ASSERT(is<T>(node));
return static_cast<T&>(node);
}
struct HitTestResult {
RefPtr<LayoutNode> layout_node;
int index_in_node { 0 };
@ -130,7 +85,7 @@ public:
for (auto* node = first_child(); node; node = node->next_sibling()) {
if (!is<T>(node))
continue;
callback(to<T>(*node));
callback(downcast<T>(*node));
}
}
@ -140,7 +95,7 @@ public:
for (auto* node = first_child(); node; node = node->next_sibling()) {
if (!is<T>(node))
continue;
callback(to<T>(*node));
callback(downcast<T>(*node));
}
}
@ -158,6 +113,7 @@ public:
virtual bool is_table_row() const { return false; }
virtual bool is_table_cell() const { return false; }
virtual bool is_table_row_group() const { return false; }
virtual bool is_break() const { return false; }
bool has_style() const { return m_has_style; }
bool is_inline() const { return m_inline; }
@ -269,7 +225,6 @@ protected:
LayoutNodeWithStyle(Document&, const Node*, NonnullRefPtr<StyleProperties>);
private:
LayoutStyle m_style;
NonnullRefPtr<StyleProperties> m_specified_style;
@ -321,7 +276,7 @@ inline const T* LayoutNode::next_sibling_of_type() const
{
for (auto* sibling = next_sibling(); sibling; sibling = sibling->next_sibling()) {
if (is<T>(*sibling))
return &to<T>(*sibling);
return &downcast<T>(*sibling);
}
return nullptr;
}
@ -331,7 +286,7 @@ inline T* LayoutNode::next_sibling_of_type()
{
for (auto* sibling = next_sibling(); sibling; sibling = sibling->next_sibling()) {
if (is<T>(*sibling))
return &to<T>(*sibling);
return &downcast<T>(*sibling);
}
return nullptr;
}
@ -341,7 +296,7 @@ inline const T* LayoutNode::previous_sibling_of_type() const
{
for (auto* sibling = previous_sibling(); sibling; sibling = sibling->previous_sibling()) {
if (is<T>(*sibling))
return &to<T>(*sibling);
return &downcast<T>(*sibling);
}
return nullptr;
}
@ -351,7 +306,7 @@ inline T* LayoutNode::previous_sibling_of_type()
{
for (auto* sibling = previous_sibling(); sibling; sibling = sibling->previous_sibling()) {
if (is<T>(*sibling))
return &to<T>(*sibling);
return &downcast<T>(*sibling);
}
return nullptr;
}
@ -361,7 +316,7 @@ inline const T* LayoutNode::first_child_of_type() const
{
for (auto* child = first_child(); child; child = child->next_sibling()) {
if (is<T>(*child))
return &to<T>(*child);
return &downcast<T>(*child);
}
return nullptr;
}
@ -371,7 +326,7 @@ inline T* LayoutNode::first_child_of_type()
{
for (auto* child = first_child(); child; child = child->next_sibling()) {
if (is<T>(*child))
return &to<T>(*child);
return &downcast<T>(*child);
}
return nullptr;
}
@ -381,7 +336,7 @@ inline const T* LayoutNode::first_ancestor_of_type() const
{
for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
if (is<T>(*ancestor))
return &to<T>(*ancestor);
return &downcast<T>(*ancestor);
}
return nullptr;
}
@ -391,15 +346,13 @@ inline T* LayoutNode::first_ancestor_of_type()
{
for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
if (is<T>(*ancestor))
return &to<T>(*ancestor);
return &downcast<T>(*ancestor);
}
return nullptr;
}
template<>
inline bool is<LayoutNodeWithStyle>(const LayoutNode& node)
{
return node.has_style();
}
}
AK_BEGIN_TYPE_TRAITS(Web::LayoutNodeWithStyle)
static bool is_type(const Web::LayoutNode& node) { return node.has_style(); }
AK_END_TYPE_TRAITS()

View file

@ -36,8 +36,8 @@ public:
LayoutReplaced(Document&, const Element&, NonnullRefPtr<StyleProperties>);
virtual ~LayoutReplaced() override;
const Element& node() const { return to<Element>(*LayoutNode::node()); }
Element& node() { return to<Element>(*LayoutNode::node()); }
const Element& node() const { return downcast<Element>(*LayoutNode::node()); }
Element& node() { return downcast<Element>(*LayoutNode::node()); }
virtual bool is_replaced() const final { return true; }
@ -75,10 +75,8 @@ private:
float m_intrinsic_ratio { 0 };
};
template<>
inline bool is<LayoutReplaced>(const LayoutNode& node)
{
return node.is_replaced();
}
}
AK_BEGIN_TYPE_TRAITS(Web::LayoutReplaced)
static bool is_type(const Web::LayoutNode& layout_node) { return layout_node.is_replaced(); }
AK_END_TYPE_TRAITS()

View file

@ -44,10 +44,8 @@ private:
virtual const char* class_name() const override { return "LayoutTable"; }
};
template<>
inline bool is<LayoutTable>(const LayoutNode& node)
{
return node.is_table();
}
}
AK_BEGIN_TYPE_TRAITS(Web::LayoutTable)
static bool is_type(const Web::LayoutNode& layout_node) { return layout_node.is_table(); }
AK_END_TYPE_TRAITS()

View file

@ -42,7 +42,7 @@ LayoutTableCell::~LayoutTableCell()
size_t LayoutTableCell::colspan() const
{
ASSERT(node());
return to<Element>(*node()).attribute(HTML::AttributeNames::colspan).to_uint().value_or(1);
return downcast<Element>(*node()).attribute(HTML::AttributeNames::colspan).to_uint().value_or(1);
}
float LayoutTableCell::width_of_logical_containing_block() const

View file

@ -46,10 +46,8 @@ private:
virtual float width_of_logical_containing_block() const override;
};
template<>
inline bool is<LayoutTableCell>(const LayoutNode& node)
{
return node.is_table_cell();
}
}
AK_BEGIN_TYPE_TRAITS(Web::LayoutTableCell)
static bool is_type(const Web::LayoutNode& layout_node) { return layout_node.is_table_cell(); }
AK_END_TYPE_TRAITS()

View file

@ -46,10 +46,8 @@ private:
virtual const char* class_name() const override { return "LayoutTableRow"; }
};
template<>
inline bool is<LayoutTableRow>(const LayoutNode& node)
{
return node.is_table_row();
}
}
AK_BEGIN_TYPE_TRAITS(Web::LayoutTableRow)
static bool is_type(const Web::LayoutNode& layout_node) { return layout_node.is_table_row(); }
AK_END_TYPE_TRAITS()

View file

@ -44,10 +44,8 @@ private:
virtual const char* class_name() const override { return "LayoutTableRowGroup"; }
};
template<>
inline bool is<LayoutTableRowGroup>(const LayoutNode& node)
{
return node.is_table_row_group();
}
}
AK_BEGIN_TYPE_TRAITS(Web::LayoutTableRowGroup)
static bool is_type(const Web::LayoutNode& layout_node) { return layout_node.is_table_row_group(); }
AK_END_TYPE_TRAITS()

View file

@ -61,10 +61,8 @@ private:
String m_text_for_rendering;
};
template<>
inline bool is<LayoutText>(const LayoutNode& node)
{
return node.is_text();
}
}
AK_BEGIN_TYPE_TRAITS(Web::LayoutText)
static bool is_type(const Web::LayoutNode& layout_node) { return layout_node.is_text(); }
AK_END_TYPE_TRAITS()

View file

@ -50,7 +50,7 @@ static RefPtr<LayoutNode> create_layout_tree(Node& node, const StyleProperties*
bool have_inline_children = false;
bool have_noninline_children = false;
to<ParentNode>(node).for_each_child([&](Node& child) {
downcast<ParentNode>(node).for_each_child([&](Node& child) {
auto layout_child = create_layout_tree(child, &layout_node->specified_style());
if (!layout_child)
return;
@ -63,7 +63,7 @@ static RefPtr<LayoutNode> create_layout_tree(Node& node, const StyleProperties*
for (auto& layout_child : layout_children) {
if (have_noninline_children && have_inline_children && layout_child.is_inline()) {
if (is<LayoutText>(layout_child) && to<LayoutText>(layout_child).text_for_style(*parent_style) == " ")
if (is<LayoutText>(layout_child) && downcast<LayoutText>(layout_child).text_for_style(*parent_style) == " ")
continue;
layout_node->inline_wrapper().append_child(layout_child);
} else {

View file

@ -50,10 +50,8 @@ private:
NonnullRefPtr<GUI::Widget> m_widget;
};
template<>
inline bool is<LayoutWidget>(const LayoutNode& node)
{
return node.is_widget();
}
}
AK_BEGIN_TYPE_TRAITS(Web::LayoutWidget)
static bool is_type(const Web::LayoutNode& layout_node) { return layout_node.is_widget(); }
AK_END_TYPE_TRAITS()

View file

@ -47,7 +47,7 @@ void LineBox::add_fragment(const LayoutNode& layout_node, int start, int length,
m_width += width;
if (is<LayoutBox>(layout_node))
const_cast<LayoutBox&>(to<LayoutBox>(layout_node)).set_containing_line_box_fragment(m_fragments.last());
const_cast<LayoutBox&>(downcast<LayoutBox>(layout_node)).set_containing_line_box_fragment(m_fragments.last());
}
void LineBox::trim_trailing_whitespace()

View file

@ -42,7 +42,7 @@ void LineBoxFragment::paint(PaintContext& context)
}
if (is<LayoutText>(layout_node())) {
to<LayoutText>(layout_node()).paint_fragment(context, *this);
downcast<LayoutText>(layout_node()).paint_fragment(context, *this);
}
}
@ -63,7 +63,7 @@ StringView LineBoxFragment::text() const
{
if (!is<LayoutText>(layout_node()))
return {};
return to<LayoutText>(layout_node()).text_for_rendering().substring_view(m_start, m_length);
return downcast<LayoutText>(layout_node()).text_for_rendering().substring_view(m_start, m_length);
}
const Gfx::FloatRect LineBoxFragment::absolute_rect() const
@ -78,7 +78,7 @@ int LineBoxFragment::text_index_at(float x) const
{
if (!layout_node().is_text())
return 0;
auto& layout_text = to<LayoutText>(layout_node());
auto& layout_text = downcast<LayoutText>(layout_node());
auto& font = layout_text.specified_style().font();
Utf8View view(text());