diff --git a/Tests/LibWeb/Text/expected/table-cell-without-table-ancestor.txt b/Tests/LibWeb/Text/expected/table-cell-without-table-ancestor.txt
new file mode 100644
index 0000000000..39701378c5
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/table-cell-without-table-ancestor.txt
@@ -0,0 +1 @@
+ PASS (didn't crash)
diff --git a/Tests/LibWeb/Text/input/table-cell-without-table-ancestor.html b/Tests/LibWeb/Text/input/table-cell-without-table-ancestor.html
new file mode 100644
index 0000000000..4d8af8802c
--- /dev/null
+++ b/Tests/LibWeb/Text/input/table-cell-without-table-ancestor.html
@@ -0,0 +1,12 @@
+
+
+
+
diff --git a/Userland/Libraries/LibWeb/HTML/HTMLTableCellElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLTableCellElement.cpp
index fc18777427..e283c4cea1 100644
--- a/Userland/Libraries/LibWeb/HTML/HTMLTableCellElement.cpp
+++ b/Userland/Libraries/LibWeb/HTML/HTMLTableCellElement.cpp
@@ -34,14 +34,6 @@ void HTMLTableCellElement::initialize(JS::Realm& realm)
set_prototype(&Bindings::ensure_web_prototype(realm, "HTMLTableCellElement"_fly_string));
}
-static const HTML::HTMLTableElement& table_containing_cell(const HTML::HTMLTableCellElement& node)
-{
- auto parent_node = node.parent();
- while (!is(parent_node))
- parent_node = parent_node->parent();
- return static_cast(*parent_node);
-}
-
void HTMLTableCellElement::apply_presentational_hints(CSS::StyleProperties& style) const
{
for_each_attribute([&](auto& name, auto& value) {
@@ -84,23 +76,26 @@ void HTMLTableCellElement::apply_presentational_hints(CSS::StyleProperties& styl
return;
}
});
- auto const& table_element = table_containing_cell(*this);
- if (auto padding = table_element.padding()) {
+ auto const table_element = first_ancestor_of_type();
+ if (!table_element)
+ return;
+
+ if (auto padding = table_element->padding()) {
style.set_property(CSS::PropertyID::PaddingTop, CSS::LengthStyleValue::create(CSS::Length::make_px(padding)));
style.set_property(CSS::PropertyID::PaddingBottom, CSS::LengthStyleValue::create(CSS::Length::make_px(padding)));
style.set_property(CSS::PropertyID::PaddingLeft, CSS::LengthStyleValue::create(CSS::Length::make_px(padding)));
style.set_property(CSS::PropertyID::PaddingRight, CSS::LengthStyleValue::create(CSS::Length::make_px(padding)));
}
- auto border = table_element.border();
+ auto border = table_element->border();
if (!border)
return;
auto apply_border_style = [&](CSS::PropertyID style_property, CSS::PropertyID width_property, CSS::PropertyID color_property) {
style.set_property(style_property, CSS::IdentifierStyleValue::create(CSS::ValueID::Inset));
style.set_property(width_property, CSS::LengthStyleValue::create(CSS::Length::make_px(1)));
- style.set_property(color_property, table_element.computed_css_values()->property(color_property));
+ style.set_property(color_property, table_element->computed_css_values()->property(color_property));
};
apply_border_style(CSS::PropertyID::BorderLeftStyle, CSS::PropertyID::BorderLeftWidth, CSS::PropertyID::BorderLeftColor);
apply_border_style(CSS::PropertyID::BorderTopStyle, CSS::PropertyID::BorderTopWidth, CSS::PropertyID::BorderTopColor);