Skip to content

Truly independent web browser

LibWeb: Don’t crash on the role of a table cell outside a table

sideshowbarkercfb47b5604ed
diff --git a/Libraries/LibWeb/HTML/HTMLTableCellElement.cpp b/Libraries/LibWeb/HTML/HTMLTableCellElement.cpp
index 6497cbfc023..f618cf21646 100644
--- a/Libraries/LibWeb/HTML/HTMLTableCellElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLTableCellElement.cpp
@@ -204,6 +204,11 @@ WebIDL::Long HTMLTableCellElement::cell_index() const
Optional<ARIA::Role> HTMLTableCellElement::default_role() const
{
+ // A cell that isn't in a table (one a script inserted into some other element, e.g.) has none of the roles below.
+ auto const* table_element = first_ancestor_of_type<HTMLTableElement>();
+ if (!table_element)
+ return {};
+
if (local_name() == TagNames::th) {
// https://w3c.github.io/html-aam/#el-th-columnheader
// th (is a column header or column group header): columnheader role
@@ -235,7 +240,6 @@ Optional<ARIA::Role> HTMLTableCellElement::default_role() const
return ARIA::Role::rowheader;
}
}
- auto const* table_element = first_ancestor_of_type<HTMLTableElement>();
// https://w3c.github.io/html-aam/#el-td
// https://w3c.github.io/html-aam/#el-th/
// (ancestor table element has table role)
diff --git a/Tests/LibWeb/Text/expected/accessibility-tree-table-cells-outside-a-table.txt b/Tests/LibWeb/Text/expected/accessibility-tree-table-cells-outside-a-table.txt
new file mode 100644
index 00000000000..590364baf7a
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/accessibility-tree-table-cells-outside-a-table.txt
@@ -0,0 +1,17 @@
+document
+ generic
+ generic
+ text leaf "Cell outside a table"
+ text leaf "Header outside a table"
+ text leaf "Column header outside a table"
+ text leaf "Row header outside a table"
+ rowgroup
+ text leaf "Header in a thead outside a table"
+ table
+ rowgroup
+ row "Header in a table Cell in a table"
+ rowheader "Header in a table"
+ text leaf "Header in a table"
+ cell "Cell in a table"
+ text leaf "Cell in a table"
+ generic
diff --git a/Tests/LibWeb/Text/input/accessibility-tree-table-cells-outside-a-table.html b/Tests/LibWeb/Text/input/accessibility-tree-table-cells-outside-a-table.html
new file mode 100644
index 00000000000..3ab6ab3ac5c
--- /dev/null
+++ b/Tests/LibWeb/Text/input/accessibility-tree-table-cells-outside-a-table.html
@@ -0,0 +1,30 @@
+<!DOCTYPE html>
+<script src="include.js"></script>
+<div id="host"></div>
+<table>
+ <tr><th>Header in a table</th><td>Cell in a table</td></tr>
+</table>
+<script>
+ test(() => {
+ // A script can put a table cell into any element; none of the cells below is in a table.
+ const cell = (tag, text, scope) => {
+ const element = document.createElement(tag);
+ element.textContent = text;
+ if (scope)
+ element.setAttribute("scope", scope);
+ return element;
+ };
+ const head = document.createElement("thead");
+ head.append(cell("th", "Header in a thead outside a table"));
+ host.append(cell("td", "Cell outside a table"), cell("th", "Header outside a table"),
+ cell("th", "Column header outside a table", "col"), cell("th", "Row header outside a table", "row"), head);
+
+ const dump = (node, depth) => {
+ const name = node.role === "text leaf" ? ` ${JSON.stringify(node.name)}` : node.name ? ` "${node.name}"` : "";
+ println(`${" ".repeat(depth)}${node.role || "(no role)"}${name}`);
+ for (const child of node.children ?? [])
+ dump(child, depth + 1);
+ };
+ dump(JSON.parse(internals.dumpAccessibilityTree()), 0);
+ });
+</script>