From cf47bbabfd153c9b86262e6bfc13332d18f5b253 Mon Sep 17 00:00:00 2001 From: "L. David Baron" Date: Tue, 9 Jan 2024 09:22:33 -0500 Subject: [PATCH] Add more tests for directionality of element. This adds tests related to https://github.com/whatwg/html/pull/10005 --- .../global-attributes/dir-assorted.window.js | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/html/dom/elements/global-attributes/dir-assorted.window.js b/html/dom/elements/global-attributes/dir-assorted.window.js index 038b3f78d4d373..0d4e4b82d9b953 100644 --- a/html/dom/elements/global-attributes/dir-assorted.window.js +++ b/html/dom/elements/global-attributes/dir-assorted.window.js @@ -80,3 +80,36 @@ test(() => { assert_false(e1.matches(":dir(ltr)"), "parent is RTL after changing text in child"); assert_false(e2.matches(":dir(ltr)"), "child is RTL after changing text in child"); }, "text changes apply to dir=auto on further ancestor after removing dir=auto from closer ancestor"); + +for (const bdi_test of [ + { markup: "A", expected: "ltr", desc: "dir=ltr with LTR contents" }, + { markup: "\u05d0", expected: "ltr", desc: "dir=ltr with RTL contents" }, + { markup: "", expected: "ltr", desc: "dir=ltr empty" }, + { markup: "A", expected: "rtl", desc: "dir=rtl with LTR contents" }, + { markup: "\u05d0", expected: "rtl", desc: "dir=rtl with RTL contents" }, + { markup: "", expected: "rtl", desc: "dir=rtl empty" }, + { markup: "A", expected: "ltr", desc: "dir=auto with LTR contents" }, + { markup: "\u05d0", expected: "rtl", desc: "dir=auto with RTL contents" }, + { markup: "", expected: "parent", desc: "dir=auto empty" }, + { markup: "A", expected: "ltr", desc: "no dir attribute with LTR contents" }, + { markup: "\u05d0", expected: "rtl", desc: "no dir attribute with RTL contents" }, + { markup: "", expected: "parent", desc: "no dir attribute empty" }, +]) { + for (const parent_dir of [ "ltr", "rtl" ]) { + test(() => { + const parent_element = document.createElement("div"); + parent_element.dir = parent_dir; + document.body.appendChild(parent_element); + parent_element.innerHTML = bdi_test.markup; + const bdi_element = parent_element.querySelector("bdi"); + let expected = bdi_test.expected; + if (expected == "parent") { + expected = parent_dir; + } + const not_expected = (expected == "ltr") ? "rtl" : "ltr"; + assert_true(bdi_element.matches(`:dir(${expected})`)); + assert_false(bdi_element.matches(`:dir(${not_expected})`)); + parent_element.remove(); + }, `directionality of bdi elements: ${bdi_test.desc} in ${parent_dir} parent`); + } +}