Skip to content

Commit

Permalink
[selectors4] Implement :dir pseudo-class
Browse files Browse the repository at this point in the history
This patch adds support for the new ":dir" pseudo-class.

The patch is covered by several of tests imported from the web tests
including generic tests in addition to the tests from Firefox and
apart from some minor changes on current tests to add the new selector.

Intent-to-ship thread is available at:
https://groups.google.com/u/1/a/chromium.org/g/blink-dev/c/p0Wc66rbVOc

BUG=576815

Change-Id: I503c6af95d9c5817e3e71b1b2025a5562929f0d8
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2460849
Commit-Queue: Miyoung Shin <[email protected]>
Reviewed-by: Rune Lillesveen <[email protected]>
Cr-Commit-Position: refs/heads/master@{#833712}
  • Loading branch information
MyidShin authored and chromium-wpt-export-bot committed Dec 4, 2020
1 parent 274af4b commit 7741972
Show file tree
Hide file tree
Showing 13 changed files with 412 additions and 0 deletions.
93 changes: 93 additions & 0 deletions css/selectors/selectors-dir-selector-auto.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<!doctype html>
<html>
<head>
<link rel="help" href="http://www.w3.org/TR/selectors4/#dir-pseudo">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<style>
#div4_1 {
direction: rtl;
}
</style>
</head>

<!-- &#1514; is the Hebrew letter tav, i.e. RTL -->
<body>
<div id=testDivs>
<div id=div1 dir=auto>
<div id=div1_1>a</div>
</div>
<div id=div2 dir=auto>
<div id=div2_1>&#1514;</div>
</div>
<div id=div3 dir=auto>
<div id=div3_1 dir=rtl>&#1514;</div>
<div id=div3_2>a</div>
</div>
<div id=div4 dir=auto>
<div id=div4_1>
<div id=div4_1_1>a</div>
</div>
</div>
</div>
</body>

<script>
function test_directionality(message, element, expected) {
test(() => {
var isLTR = document.querySelector("#" + element.id + ":dir(ltr)") == element;
var isRTL = document.querySelector("#" + element.id + ":dir(rtl)") == element;
if (expected == "ltr") {
assert_true(isLTR);
assert_false(isRTL);
} else {
assert_false(isLTR);
assert_true(isRTL);
}
}, message + " directionality of element " + element.id + " is " + expected);
}

test_directionality("Initial ", div1, "ltr");
test_directionality("Initial ", div1_1, "ltr");
test_directionality("Initial ", div2, "rtl");
test_directionality("Initial ", div2_1, "rtl");
test_directionality("Initial ", div3, "ltr");
test_directionality("Initial ", div3_1, "rtl");
test_directionality("Initial ", div3_2, "ltr");
test_directionality("Initial ", div4, "ltr");
test_directionality("Initial ", div4_1, "ltr");
test_directionality("Initial ", div4_1_1, "ltr");

div1_1.innerText = "\u05EA";
div1_1.offsetTop;
test_directionality("Updated ", div1, "rtl");
test_directionality("Updated ", div1_1, "rtl");

div1_1.dir = "ltr";
div1_1.offsetTop;
test_directionality("Updated ", div1, "ltr");
test_directionality("Updated ", div1_1, "ltr");

div1_1.innerText = "a";
div1_1.offsetTop;
test_directionality("Reupdated ", div1, "ltr");
test_directionality("Reupdated ", div1_1, "ltr");

div2_1.remove();
div2.offsetTop;
test_directionality("Updated ", div2, "ltr");

div3_1.dir = "";
div3_1.offsetTop;
test_directionality("Updated ", div3, "rtl");
div3.appendChild(div3_1);
div3.offsetTop;
test_directionality("Updated ", div3, "ltr");

div4_1_1.innerText = "\u05EA";
div4_1_1.offsetTop;
test_directionality("Updated ", div4, "rtl");
test_directionality("Updated ", div4_1, "rtl");
test_directionality("Updated ", div4_1_1, "rtl");
</script>
</html>
16 changes: 16 additions & 0 deletions css/selectors/selectors-dir-selector-change-001-ref.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
span { background-color: lime }
</style>
</head>
<body>
<div>
<div>
<div></div>
<span>The background color should be lime</span>
</div>
</div>
</body>
</html>
25 changes: 25 additions & 0 deletions css/selectors/selectors-dir-selector-change-001.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<html>
<head>
<title>CSS Selectors Level 4 Test: Check for correctly updating :dir matching on dir attribute change from default(ltr) to rtl</title>
<link rel="author" title="Miyoung Shin" href="mailto:[email protected]">
<link rel="help" href="http://www.w3.org/TR/selectors4/#dir-pseudo">
<link rel="match" href="selectors-dir-selector-change-001-ref.html">
<style type="text/css">
#x:dir(rtl) + span { background-color: lime }
#outer { direction:ltr }
</style>
</head>
<body>
<div id="outer" style="-webkit-locale: 'en'">
<div>
<div id="x"></div>
<span>The background color should be lime</span>
</div>
</div>
<script>
outer.offsetTop;
outer.setAttribute("dir", "rtl");
</script>
</body>
</html>
28 changes: 28 additions & 0 deletions css/selectors/selectors-dir-selector-change-002.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!DOCTYPE html>
<html>
<head>
<title>CSS Selectors Level 4 Test: Check for correctly updating :dir matching on dir attribute change from default(ltr) to rtl</title>
<link rel="author" title="Miyoung Shin" href="mailto:[email protected]">
<link rel="help" href="http://www.w3.org/TR/selectors4/#dir-pseudo">
<link rel="match" href="../reference/ref-filled-green-100px-square.xht">
<style type="text/css">
div {
width: 100px;
height: 100px;
background-color: red;
}

div:dir(rtl) {
background-color: green;
}
</style>
</head>
<body>
<p>Test passes if there is a filled green square and <strong>no red</strong>.</p>
<div id="inner"></div>
<script>
inner.offsetTop;
inner.setAttribute("dir", "rtl");
</script>
</body>
</html>
16 changes: 16 additions & 0 deletions css/selectors/selectors-dir-selector-change-003-ref.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
span { background-color: lime }
</style>
</head>
<body>
<div>
<div>
<div></div>
<span>The background color should be lime</span>
</div>
</div>
</body>
</html>
24 changes: 24 additions & 0 deletions css/selectors/selectors-dir-selector-change-003.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html>
<head>
<title>CSS Selectors Level 4 Test: Check for correctly updating :dir matching on dir attribute change from rtl to auto</title>
<link rel="author" title="Miyoung Shin" href="mailto:[email protected]">
<link rel="help" href="http://www.w3.org/TR/selectors4/#dir-pseudo">
<link rel="match" href="selectors-dir-selector-change-003-ref.html">
<style type="text/css">
#x:dir(ltr) + span { background-color: lime }
</style>
</head>
<body>
<div id="outer" dir="rtl">
<div>
<div id="x"></div>
<span>The background color should be lime</span>
</div>
</div>
<script>
outer.offsetTop;
outer.setAttribute("dir", "auto");
</script>
</body>
</html>
17 changes: 17 additions & 0 deletions css/selectors/selectors-dir-selector-change-004-ref.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
span { background-color: lime }
</style>
</head>
<body>
<div>
<div dir="rtl">
<div></div>
<span>מקור השם עברית</span>
</div>
</div>
</body>
</html>
25 changes: 25 additions & 0 deletions css/selectors/selectors-dir-selector-change-004.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<html>
<head>
<title>CSS Selectors Level 4 Test: Check for correctly updating :dir matching on directionality change from ltr to rtl</title>
<link rel="author" title="Miyoung Shin" href="mailto:[email protected]">
<link rel="help" href="http://www.w3.org/TR/selectors4/#dir-pseudo">
<link rel="match" href="selectors-dir-selector-change-004-ref.html">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
#x:dir(rtl) + span { background-color: lime }
</style>
</head>
<body>
<div dir="auto">
<div>
<div id="x"></div>
<span id="inner">The background color should be lime</span>
</div>
</div>
<script>
inner.offsetTop;
inner.innerHTML = "מקור השם עברית";
</script>
</body>
</html>
26 changes: 26 additions & 0 deletions css/selectors/selectors-dir-selector-ltr-002.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!DOCTYPE html>
<html>
<head>
<title>CSS Selectors Level 4 Test: exception handling for an invalid identifier of dir()</title>
<link rel="author" title="Miyoung Shin" href="mailto:[email protected]">
<link rel="help" href="http://www.w3.org/TR/selectors4/#dir-pseudo">
<link rel="match" href="../reference/ref-filled-green-100px-square.xht">
<meta name="flags" content="">
<meta name="assert" content="The invalid identifier of :dir(ltrr) pseudo-class doesn't match an element that has a directionality of (ltr). Even if the div element has dir=ltr, the selector should not match.">
<style type="text/css">
div {
width: 100px;
height: 100px;
background-color: green;
}

div:dir(ltrr) {
background-color: red;
}
</style>
</head>
<body>
<p>Test passes if there is a filled green square and <strong>no red</strong>.</p>
<div dir="ltr"></div>
</body>
</html>
26 changes: 26 additions & 0 deletions css/selectors/selectors-dir-selector-ltr-003.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!DOCTYPE html>
<html>
<head>
<title>CSS Selectors Level 4 Test: exception handling for multiple identifiers of dir() </title>
<link rel="author" title="Miyoung Shin" href="mailto:[email protected]">
<link rel="help" href="http://www.w3.org/TR/selectors4/#dir-pseudo">
<link rel="match" href="../reference/ref-filled-green-100px-square.xht">
<meta name="flags" content="">
<meta name="assert" content="The multiple identifiers of :dir(ltr, rtl) pseudo-class don't match an element that has a directionality of (ltr). Even if the div element has dir=ltr, the selector should not match.">
<style type="text/css">
div {
width: 100px;
height: 100px;
background-color: green;
}

div:dir(ltr, rtl) {
background-color: red;
}
</style>
</head>
<body>
<p>Test passes if there is a filled green square and <strong>no red</strong>.</p>
<div dir="ltr"></div>
</body>
</html>
71 changes: 71 additions & 0 deletions css/selectors/selectors-dir-selector-querySelector.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<!doctype html>
<html>
<head>
<link rel="help" href="http://www.w3.org/TR/selectors4/#dir-pseudo">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
</head>

<body>
<div id=outer>
<div id=div1></div>
<div id=div2 dir=ltr>
<div id=div2_1></div>
<div id=div2_2 dir=ltr></div>
<div id=div2_3 dir=rtl></div>
</div>
<div id=div3 dir=rtl>
<div id=div3_1>
<div id=div3_1_1></div>
</div>
<div id=div3_2 dir=ltr></div>
<div id=div3_3 dir=rtl></div>
</div>
<div id=div4 dir=lol></div>
<div id=div5 dir=auto></div>
</div>
</body>

<script>
test(() => {
assert_equals(document.querySelector(":dir(lol)"), null);
assert_equals(document.querySelector(":dir(lol )"), null);
assert_equals(document.querySelector(":dir( auto)"), null);
assert_equals(document.querySelector(":dir(\nauto\t)"), null);
}, ":dir() allows any ident value but strings other than ltr/rtl don't match");

test(() => {
assert_throws_dom("SYNTAX_ERR", () => { document.querySelector(":dir()"); });
assert_throws_dom("SYNTAX_ERR", () => { document.querySelector(":dir(ltr, rtl)"); });
assert_throws_dom("SYNTAX_ERR", () => { document.querySelector(":dir('ltr')"); });
}, ":dir() requires exactly an ident argument");

test(() => {
assert_equals(document.querySelector(":dir(rtl)"), div2_3);
assert_equals(document.querySelector("*:dir(rtl)"), div2_3);
assert_equals(document.querySelector("div:dir(ltr)"), outer);
assert_equals(document.querySelector("div:dir(ltr):dir(ltr)"), outer);
assert_equals(document.querySelector(":dir(rtl)#div3_3"), div3_3);
assert_equals(document.querySelector(":nth-child(2):dir(rtl)"), null);
assert_equals(document.querySelector(":nth-child(3):dir(rtl)"), div2_3);
assert_equals(document.querySelector(":nth-child(4):dir(ltr)"), div4);
assert_equals(document.querySelector(":nth-last-child(3):dir(rtl)"), div3);
}, ":dir() works in compound selectors");

test(() => {
assert_equals(document.querySelector("#div2 :dir(ltr)"), div2_1);
assert_equals(document.querySelector(":dir(rtl) div"), div3_1);
assert_equals(document.querySelector("div + :dir(ltr)"), div2);
assert_equals(document.querySelector(":dir(ltr) + :dir(rtl)"), div2_3);
assert_equals(document.querySelector(":dir(rtl) :dir(rtl)"), div3_1);
assert_equals(document.querySelector(":dir(rtl) + :dir(ltr)"), div3_2);
assert_equals(document.querySelector(":dir(rtl) ~ :dir(rtl)"), div3_3);
assert_equals(document.querySelector(":dir(rtl) :dir(ltr)"), div3_2);
assert_equals(document.querySelector("* :dir(rtl) *"), div3_1);
assert_equals(document.querySelector("div :dir(rtl) div"), div3_1);
assert_equals(document.querySelector(":dir(ltr) :dir(rtl) + :dir(ltr)"), div3_2);
assert_equals(document.querySelector(":dir(ltr) + :dir(rtl) + * + *"), div5);
assert_equals(document.querySelector(":dir(rtl) > * > :dir(rtl)"), div3_1_1);
}, ":dir() works in complex selectors");
</script>
</html>
18 changes: 18 additions & 0 deletions css/selectors/selectors-dir-selector-white-space-001-ref.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
div {
width: 100px;
height: 100px;
background-color: green;
}
</style>
</head>
<body>
<p>Test passes if there is a filled green square and <strong>no red</strong>.</p>
<div></div>
<div></div>
<div></div>
</body>
</html>
Loading

0 comments on commit 7741972

Please sign in to comment.