Skip to content

Truly independent web browser

LibWeb: Rebuild a table when a cell's span changes

Andreas Kling4733ced548db
diff --git a/Libraries/LibWeb/DOM/Element.cpp b/Libraries/LibWeb/DOM/Element.cpp
index 2ca6f34ff76..2fa6dba265b 100644
--- a/Libraries/LibWeb/DOM/Element.cpp
+++ b/Libraries/LibWeb/DOM/Element.cpp
@@ -124,6 +124,7 @@
#include <LibWeb/HTML/HTMLSelectElement.h>
#include <LibWeb/HTML/HTMLSlotElement.h>
#include <LibWeb/HTML/HTMLStyleElement.h>
+#include <LibWeb/HTML/HTMLTableCellElement.h>
#include <LibWeb/HTML/HTMLTableElement.h>
#include <LibWeb/HTML/HTMLTemplateElement.h>
#include <LibWeb/HTML/HTMLTextAreaElement.h>
@@ -1456,6 +1457,11 @@ void Element::run_attribute_change_steps(Utf16FlyString const& local_name, Optio
Layout::ForcedReadScope read { document() };
if (auto* layout_node = unsafe_layout_node(read))
layout_node->synchronize_table_span_data();
+ // NB: The spans decide which cells of the table's grid are missing, which its boxes fill.
+ if (auto* cell = as_if<HTML::HTMLTableCellElement>(*this); cell && cell->take_table_span_change()) {
+ if (auto table = first_ancestor_of_type<HTML::HTMLTableElement>())
+ table->set_needs_layout_tree_update(true, SetNeedsLayoutTreeUpdateReason::TableCellSpanAttribute);
+ }
}
if (!document().suppresses_attribute_style_invalidation()) {
CSS::Invalidation::invalidate_style_after_attribute_change(
diff --git a/Libraries/LibWeb/DOM/Node.h b/Libraries/LibWeb/DOM/Node.h
index dfeb763c5df..50c9fb0e7f8 100644
--- a/Libraries/LibWeb/DOM/Node.h
+++ b/Libraries/LibWeb/DOM/Node.h
@@ -129,6 +129,7 @@ enum class SetNeedsLayoutReason {
X(NodeInsertBefore) \
X(NodeInsertBeforeWithDisplayContents) \
X(NodeRemove) \
+ X(TableCellSpanAttribute) \
X(NodeSetTextContent) \
X(None) \
X(PseudoElementBoxEscapedRebuildRoot) \
diff --git a/Libraries/LibWeb/HTML/HTMLTableCellElement.cpp b/Libraries/LibWeb/HTML/HTMLTableCellElement.cpp
index f618cf21646..d8f0b2a1155 100644
--- a/Libraries/LibWeb/HTML/HTMLTableCellElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLTableCellElement.cpp
@@ -115,6 +115,14 @@ void HTMLTableCellElement::apply_presentational_hints(Vector<CSS::StyleProperty>
// This implements step 8 in the spec here:
// https://html.spec.whatwg.org/multipage/tables.html#algorithm-for-processing-rows
+bool HTMLTableCellElement::take_table_span_change()
+{
+ auto spans_changed = col_span() != m_last_col_span || row_span() != m_last_row_span;
+ m_last_col_span = col_span();
+ m_last_row_span = row_span();
+ return spans_changed;
+}
+
WebIDL::UnsignedLong HTMLTableCellElement::col_span() const
{
// If the current cell has a colspan attribute, then parse that attribute's value, and let colspan be the result.
diff --git a/Libraries/LibWeb/HTML/HTMLTableCellElement.h b/Libraries/LibWeb/HTML/HTMLTableCellElement.h
index ca9ef8a52d2..5548b19d23a 100644
--- a/Libraries/LibWeb/HTML/HTMLTableCellElement.h
+++ b/Libraries/LibWeb/HTML/HTMLTableCellElement.h
@@ -24,11 +24,16 @@ public:
void set_col_span(WebIDL::UnsignedLong);
void set_row_span(WebIDL::UnsignedLong);
+ // Whether the cell's effective spans differ from what they were when this was last asked.
+ bool take_table_span_change();
+
WebIDL::Long cell_index() const;
virtual Optional<ARIA::Role> default_role() const override;
private:
+ WebIDL::UnsignedLong m_last_col_span { 1 };
+ WebIDL::UnsignedLong m_last_row_span { 1 };
HTMLTableCellElement(DOM::Document&, DOM::QualifiedName);
virtual bool is_html_table_cell_element() const override { return true; }
diff --git a/Tests/LibWeb/Text/expected/HTML/table-cell-span-change-rebuilds-missing-cells.txt b/Tests/LibWeb/Text/expected/HTML/table-cell-span-change-rebuilds-missing-cells.txt
new file mode 100644
index 00000000000..bf9e330701b
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/table-cell-span-change-rebuilds-missing-cells.txt
@@ -0,0 +1,4 @@
+colSpan: the tree matches a full rebuild: true
+colSpan back: the tree matches a full rebuild: true
+rowSpan: the tree matches a full rebuild: true
+rowSpan back: the tree matches a full rebuild: true
diff --git a/Tests/LibWeb/Text/input/HTML/table-cell-span-change-rebuilds-missing-cells.html b/Tests/LibWeb/Text/input/HTML/table-cell-span-change-rebuilds-missing-cells.html
new file mode 100644
index 00000000000..28aa7172c10
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/table-cell-span-change-rebuilds-missing-cells.html
@@ -0,0 +1,18 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<body>
+<script>
+ test(() => {
+ for (const [attribute, value] of [["colSpan", 2], ["rowSpan", 2]]) {
+ const table = document.createElement("table");
+ table.innerHTML = "<tr><td id=target>a<td>b<tr><td>c<td>d";
+ document.body.append(table);
+ document.body.offsetWidth;
+ table.querySelector("#target")[attribute] = value;
+ println(`${attribute}: the tree matches a full rebuild: ${internals.compareLayoutTreeWithFullRebuild().matches}`);
+ table.querySelector("#target")[attribute] = 1;
+ println(`${attribute} back: the tree matches a full rebuild: ${internals.compareLayoutTreeWithFullRebuild().matches}`);
+ table.remove();
+ }
+ });
+</script>