diff --git a/Libraries/LibWeb/HTML/HTMLTableCellElement.cpp b/Libraries/LibWeb/HTML/HTMLTableCellElement.cpp index a482a628d70..a745ce363ed 100644 --- a/Libraries/LibWeb/HTML/HTMLTableCellElement.cpp +++ b/Libraries/LibWeb/HTML/HTMLTableCellElement.cpp @@ -196,16 +196,31 @@ WebIDL::Long HTMLTableCellElement::cell_index() const Optional HTMLTableCellElement::default_role() const { - // TODO: For td: - // role=cell if the ancestor table element is exposed as a role=table - // role=gridcell if the ancestor table element is exposed as a role=grid or treegrid - // No corresponding role if the ancestor table element is not exposed as a role=table, grid or treegrid - // For th: - // role=columnheader, rowheader or cell if the ancestor table element is exposed as a role=table - // role=columnheader, rowheader or gridcell if the ancestor table element is exposed as a role=grid or treegrid - // No corresponding role if the ancestor table element is not exposed as a role=table, grid or treegrid - // https://www.w3.org/TR/html-aria/#el-td - // https://www.w3.org/TR/html-aria/#el-th + if (local_name() == TagNames::th) { + for (auto const* ancestor = parent_element(); ancestor; ancestor = ancestor->parent_element()) { + // AD-HOC: The ancestor checks here aren’t explicitly defined in the spec, but implicitly follow from what + // the spec does state, and from the physical placement/layout of elements. Also, the el-th and el-th-in-row + // tests at https://wpt.fyi/results/html-aam/table-roles.html require doing these ancestor checks — and + // implementing them causes the behavior to match that of other engines. + // https://w3c.github.io/html-aam/#el-th-columnheader + if (get_attribute(HTML::AttributeNames::scope) == "columnheader" || ancestor->local_name() == TagNames::thead) + return ARIA::Role::columnheader; + // https://w3c.github.io/html-aam/#el-th-rowheader + if (get_attribute(HTML::AttributeNames::scope) == "rowheader" || ancestor->local_name() == TagNames::tbody) + return ARIA::Role::rowheader; + } + } + auto const* table_element = first_ancestor_of_type(); + // https://w3c.github.io/html-aam/#el-td + // https://w3c.github.io/html-aam/#el-th/ + // (ancestor table element has table role) + if (table_element->role_or_default() == ARIA::Role::table) + return ARIA::Role::cell; + // https://w3c.github.io/html-aam/#el-td-gridcell + // https://w3c.github.io/html-aam/#el-th-gridcell + // (ancestor table element has grid or treegrid role) + if (first_is_one_of(table_element->role_or_default(), ARIA::Role::grid, ARIA::Role::gridcell)) + return ARIA::Role::gridcell; return {}; } diff --git a/Tests/LibWeb/Text/expected/wpt-import/html-aam/table-roles.txt b/Tests/LibWeb/Text/expected/wpt-import/html-aam/table-roles.txt new file mode 100644 index 00000000000..ee75a8ab898 --- /dev/null +++ b/Tests/LibWeb/Text/expected/wpt-import/html-aam/table-roles.txt @@ -0,0 +1,12 @@ +Harness status: OK + +Found 7 tests + +7 Pass +Pass el-table +Pass el-caption +Pass el-tr-thead +Pass el-th +Pass el-tr-tbody +Pass el-th-in-row +Pass el-td \ No newline at end of file diff --git a/Tests/LibWeb/Text/expected/wpt-import/wai-aria/role/table-roles.txt b/Tests/LibWeb/Text/expected/wpt-import/wai-aria/role/table-roles.txt new file mode 100644 index 00000000000..6111915e656 --- /dev/null +++ b/Tests/LibWeb/Text/expected/wpt-import/wai-aria/role/table-roles.txt @@ -0,0 +1,14 @@ +Harness status: OK + +Found 9 tests + +9 Pass +Pass div role is caption (in div with table role) +Pass orphan p role is caption +Pass span role is cell (in div with row role, in div with rowgroup role, in div with table role) +Pass orphan span role is cell +Pass span role is columnheader (in div with row role, in div with rowgroup role, in div with table role) +Pass div role is row (in div with rowgroup role, in div with table role) +Pass div role is rowgroup (in div with table role) +Pass role is rowheader (in div with row role, in div with rowgroup role, in div with table role) +Pass div role is table \ No newline at end of file diff --git a/Tests/LibWeb/Text/input/wpt-import/html-aam/table-roles.html b/Tests/LibWeb/Text/input/wpt-import/html-aam/table-roles.html new file mode 100644 index 00000000000..9ed64cd4f39 --- /dev/null +++ b/Tests/LibWeb/Text/input/wpt-import/html-aam/table-roles.html @@ -0,0 +1,53 @@ + + + + HTML-AAM Role Verification Tests + + + + + + + + + +

Tests the computedrole mappings for the table-related roles defined in HTML-AAM. Most test names correspond to unique ID defined in the spec.

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
caption
abc
123
456
xyz
+ + + + + \ No newline at end of file diff --git a/Tests/LibWeb/Text/input/wpt-import/wai-aria/role/table-roles.html b/Tests/LibWeb/Text/input/wpt-import/wai-aria/role/table-roles.html new file mode 100644 index 00000000000..9ce6b330349 --- /dev/null +++ b/Tests/LibWeb/Text/input/wpt-import/wai-aria/role/table-roles.html @@ -0,0 +1,148 @@ + + + + Table Role Verification Tests + + + + + + + + +

Tests table and related roles.

+ + + + + + + + + + + + + + + + + + + + + + + + + + +
+
x
+
+ +

x

+ + +
+
+
+ x + x +
+
+
+
+ x + x +
+
+
+ + x + + +
+
+
+ x + x + x +
+
+
+
+ x + x + x +
+
+
+ + +
+
+
+ x +
+
+
+
+ x +
+
+
+ + +
+
+
+ x + x +
+
+
+
+ x + x +
+
+
+ + +
+
+
+ x + x + x +
+
+
+
+ x + x + x + x +
+
+
+ + +
+
+ x + x +
+
+ x + x +
+
+ + + + + \ No newline at end of file