Supports all ':has' relative argument cases

Currently the relative selector is not supported yet, so this CL
provides the relative argument cases as follows.
 - :has(:scope > <complex-selector>)
 - :has(:scope ~ <complex-selector>)
 - :has(:scope + <complex-selector>)

This CL provides a wpt test for the above usage.
 - has-relative-argument.html

While matching those argument selectors on the :has argument subtree
of an element, we can get the candidate scope elements. By marking
those elements as matched, we can reduce the duplicated ':has'
argument matching operation.

HasArgumentSubtreeIterator provides the way to traverse the ':has'
argument subtree (downward subtree of the :has scope element). The
iterator provides right-to-left post-order traversal to prevent
incorrect 'NotMatched' status. And it also provides a way to limit
traversal depth or adjacent distance to prevent unnecessary tree
traversal for direct relations (child/direct sibling).

About the issue of incorrect 'NotMatched' status, the previous caching
logic has a bug of incorrectly using the cached status to skip one
argument selector matching operation for one descendant of an element.
The cached status should be used to skip the argument matching
operations for all the descendants of an element because it is the
status of :has scope element.

To store the 'NotMatched' status correctly during the ':has' subtree
traversal for argument selector matching , the traversal order should
be right-to-left reversed post-order to guarantee that, the downward
subtree(descendants/next siblings/descendants of next siblings) of
an element was checked with the argument selector before checking the
element and the previous check didn't mark the element as 'Matched'.

The ':has(.descendant)' test case in the 'has-basic.html' missed to
have an expected return value related with this bug, so the test
result is fixed with this CL.

And to check the added/fixed logic, following test are added.
 - has-relative-argument.html

Change-Id: I71f8752ad90d32c6f9ee6bf87949b838e3843eba
Bug: 669058
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2914967
Commit-Queue: Byungwoo Lee <blee@igalia.com>
Reviewed-by: Rune Lillesveen <futhark@chromium.org>
Cr-Commit-Position: refs/heads/master@{#894067}
diff --git a/css/selectors/has-basic.html b/css/selectors/has-basic.html
index 3cd6e11..1cb1ec6 100644
--- a/css/selectors/has-basic.html
+++ b/css/selectors/has-basic.html
@@ -63,7 +63,7 @@
   test_selector_all(':has(#a)', []);
   test_selector_all(':has(.ancestor)', [a]);
   test_selector_all(':has(.target)', [a, b, f, h]);
-  test_selector_all(':has(.descendant)', [a, b, f, h, j]);
+  test_selector_all(':has(.descendant)', [a, b, c, f, h, j]);
   test_selector_all('.parent:has(.target)', [b, f, h]);
   test_selector_all(':has(.sibling ~ .target)', [a, b]);
   test_selector_all('.parent:has(.sibling ~ .target)', [b]);
@@ -78,8 +78,8 @@
   test_selector_all(':has(:scope > .parent)', [a]);
   test_selector_all(':has(> .target)', [b, f, h]);
   test_selector_all(':has(:scope > .target)', [b, f, h]);
-  test_selector_all('.target:has(+ .sibling)', [i]);
-  test_selector_all('.target:has(:scope + .sibling)', [i]);
+  test_selector_all(':has(+ #h)', [f]);
+  test_selector_all(':has(:scope + #h)', [f]);
   test_selector_all('.parent:has(~ #h)', [b, f]);
   test_selector_all('.parent:has(:scope ~ #h)', [b, f]);
   test_selector('.sibling:has(.descendant)', c);
diff --git a/css/selectors/has-relative-argument.html b/css/selectors/has-relative-argument.html
new file mode 100644
index 0000000..2ce3fd9
--- /dev/null
+++ b/css/selectors/has-relative-argument.html
@@ -0,0 +1,276 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>:has pseudo class behavior with various relative arguments</title>
+<link rel="author" title="Byungwoo Lee" href="mailto:blee@igalia.com">
+<script src="/resources/testharness.js"></script>
+<script src="/resources/testharnessreport.js"></script>
+<link rel="help" href="https://drafts.csswg.org/selectors/#relational">
+
+<main id=main>
+ <div id=d01>
+  <div id=d02 class="x">
+    <div id=d03 class="a"></div>
+    <div id=d04></div>
+    <div id=d05 class="b"></div>
+  </div>
+  <div id=d06 class="x">
+    <div id=d07 class="x">
+      <div id=d08 class="a"></div>
+    </div>
+  </div>
+  <div id=d09 class="x">
+    <div id=d10 class="a">
+      <div id=d11 class="b"></div>
+    </div>
+  </div>
+  <div id=d12 class="x">
+    <div id=d13 class="a">
+      <div id=d14>
+        <div id=d15 class="b"></div>
+      </div>
+    </div>
+    <div id=d16 class="b"></div>
+  </div>
+ </div>
+ <div id=d17>
+  <div id=d18 class="x"></div>
+  <div id=d19 class="x"></div>
+  <div id=d20 class="a"></div>
+  <div id=d21 class="x"></div>
+  <div id=d22 class="a">
+   <div id=d23 class="b"></div>
+  </div>
+  <div id=d24 class="x"></div>
+  <div id=d25 class="a">
+   <div id=d26>
+    <div id=d27 class="b"></div>
+   </div>
+  </div>
+  <div id=d28 class="x"></div>
+  <div id=d29 class="a"></div>
+  <div id=d30 class="b">
+   <div id=d31 class="c"></div>
+  </div>
+  <div id=d32 class="x"></div>
+  <div id=d33 class="a"></div>
+  <div id=d34 class="b">
+   <div id=d35>
+    <div id=d36 class="c"></div>
+   </div>
+  </div>
+  <div id=d37 class="x"></div>
+  <div id=d38 class="a"></div>
+  <div id=d39 class="b"></div>
+  <div id=d40 class="x"></div>
+  <div id=d41 class="a"></div>
+  <div id=d42></div>
+  <div id=d43 class="b">
+   <div id=d44 class="x">
+    <div id=d45 class="c"></div>
+   </div>
+  </div>
+  <div id=d46 class="x"></div>
+  <div id=d47 class="a">
+  </div>
+ </div>
+ <div>
+  <div id=d48 class="x">
+   <div id=d49 class="x">
+    <div id=d50 class="x d">
+     <div id=d51 class="x d">
+      <div id=d52 class="x">
+       <div id=d53 class="x e">
+        <div id=d54 class="f"></div>
+       </div>
+      </div>
+     </div>
+    </div>
+   </div>
+  </div>
+  <div id=d55 class="x"></div>
+  <div id=d56 class="x d"></div>
+  <div id=d57 class="x d"></div>
+  <div id=d58 class="x"></div>
+  <div id=d59 class="x e"></div>
+  <div id=d60 class="f"></div>
+ </div>
+ <div>
+  <div id=d61 class="x"></div>
+  <div id=d62 class="x y"></div>
+  <div id=d63 class="x y">
+   <div id=d64 class="y g">
+    <div id=d65 class="y">
+     <div id=d66 class="y h">
+      <div id=d67 class="i"></div>
+     </div>
+    </div>
+   </div>
+  </div>
+  <div id=d68 class="x y">
+   <div id=d69 class="x"></div>
+   <div id=d70 class="x"></div>
+   <div id=d71 class="x y">
+    <div id=d72 class="y g">
+     <div id=d73 class="y">
+      <div id=d74 class="y h">
+       <div id=d75 class="i"></div>
+      </div>
+     </div>
+    </div>
+   </div>
+   <div id=d76 class="x"></div>
+   <div id=d77 class="j"><div id=d78><div id=d79></div></div></div>
+  </div>
+  <div id=d80 class="j"></div>
+ </div>
+</main>
+
+<script>
+  function formatElements(elements) {
+    return elements.map(e => e.id).sort().join();
+  }
+
+  // Test that |selector| returns the given elements in #main.
+  function test_selector_all(selector, expected) {
+    test(function() {
+      let actual = Array.from(main.querySelectorAll(selector));
+      assert_equals(formatElements(actual), formatElements(expected));
+    }, `${selector} matches expected elements`);
+  }
+
+  test_selector_all('.x:has(:scope .a)', [d02, d06, d07, d09, d12]);
+  test_selector_all('.x:has(:scope .a > .b)', [d09]);
+  test_selector_all('.x:has(:scope .a .b)', [d09, d12]);
+  test_selector_all('.x:has(:scope .a + .b)', [d12]);
+  test_selector_all('.x:has(:scope .a ~ .b)', [d02, d12]);
+  test_selector_all(':has(.x:scope .a)', [d02, d06, d07, d09, d12]);
+  test_selector_all(':has(.x:scope .a > .b)', [d09]);
+  test_selector_all(':has(.x:scope .a .b)', [d09, d12]);
+  test_selector_all(':has(.x:scope .a + .b)', [d12]);
+  test_selector_all(':has(.x:scope .a ~ .b)', [d02, d12]);
+  test_selector_all(':has(:scope.x .a)', [d02, d06, d07, d09, d12]);
+  test_selector_all(':has(:scope.x .a > .b)', [d09]);
+  test_selector_all(':has(:scope.x .a .b)', [d09, d12]);
+  test_selector_all(':has(:scope.x .a + .b)', [d12]);
+  test_selector_all(':has(:scope.x .a ~ .b)', [d02, d12]);
+
+  test_selector_all('.x:has(:scope > .a)', [d02, d07, d09, d12]);
+  test_selector_all('.x:has(:scope > .a > .b)', [d09]);
+  test_selector_all('.x:has(:scope > .a .b)', [d09, d12]);
+  test_selector_all('.x:has(:scope > .a + .b)', [d12]);
+  test_selector_all('.x:has(:scope > .a ~ .b)', [d02, d12]);
+  test_selector_all(':has(.x:scope > .a)', [d02, d07, d09, d12]);
+  test_selector_all(':has(.x:scope > .a > .b)', [d09]);
+  test_selector_all(':has(.x:scope > .a .b)', [d09, d12]);
+  test_selector_all(':has(.x:scope > .a + .b)', [d12]);
+  test_selector_all(':has(.x:scope > .a ~ .b)', [d02, d12]);
+  test_selector_all(':has(:scope.x > .a)', [d02, d07, d09, d12]);
+  test_selector_all(':has(:scope.x > .a > .b)', [d09]);
+  test_selector_all(':has(:scope.x > .a .b)', [d09, d12]);
+  test_selector_all(':has(:scope.x > .a + .b)', [d12]);
+  test_selector_all(':has(:scope.x > .a ~ .b)', [d02, d12]);
+
+  test_selector_all('.x:has(:scope + .a)', [d19, d21, d24, d28, d32, d37, d40, d46]);
+  test_selector_all('.x:has(:scope + .a > .b)', [d21]);
+  test_selector_all('.x:has(:scope + .a .b)', [d21, d24]);
+  test_selector_all('.x:has(:scope + .a + .b)', [d28, d32, d37]);
+  test_selector_all('.x:has(:scope + .a ~ .b)', [d19, d21, d24, d28, d32, d37, d40]);
+  test_selector_all(':has(.x:scope + .a)', [d19, d21, d24, d28, d32, d37, d40, d46]);
+  test_selector_all(':has(.x:scope + .a > .b)', [d21]);
+  test_selector_all(':has(.x:scope + .a .b)', [d21, d24]);
+  test_selector_all(':has(.x:scope + .a + .b)', [d28, d32, d37]);
+  test_selector_all(':has(.x:scope + .a ~ .b)', [d19, d21, d24, d28, d32, d37, d40]);
+  test_selector_all(':has(:scope.x + .a)', [d19, d21, d24, d28, d32, d37, d40, d46]);
+  test_selector_all(':has(:scope.x + .a > .b)', [d21]);
+  test_selector_all(':has(:scope.x + .a .b)', [d21, d24]);
+  test_selector_all(':has(:scope.x + .a + .b)', [d28, d32, d37]);
+  test_selector_all(':has(:scope.x + .a ~ .b)', [d19, d21, d24, d28, d32, d37, d40]);
+
+  test_selector_all('.x:has(:scope ~ .a)', [d18, d19, d21, d24, d28, d32, d37, d40, d46]);
+  test_selector_all('.x:has(:scope ~ .a > .b)', [d18, d19, d21]);
+  test_selector_all('.x:has(:scope ~ .a .b)', [d18, d19, d21, d24]);
+  test_selector_all('.x:has(:scope ~ .a + .b)', [d18, d19, d21, d24, d28, d32, d37]);
+  test_selector_all('.x:has(:scope ~ .a + .b > .c)', [d18, d19, d21, d24, d28]);
+  test_selector_all('.x:has(:scope ~ .a + .b .c)', [d18, d19, d21, d24, d28, d32]);
+  test_selector_all(':has(.x:scope ~ .a)', [d18, d19, d21, d24, d28, d32, d37, d40, d46]);
+  test_selector_all(':has(.x:scope ~ .a > .b)', [d18, d19, d21]);
+  test_selector_all(':has(.x:scope ~ .a .b)', [d18, d19, d21, d24]);
+  test_selector_all(':has(.x:scope ~ .a + .b)', [d18, d19, d21, d24, d28, d32, d37]);
+  test_selector_all(':has(.x:scope ~ .a + .b > .c)', [d18, d19, d21, d24, d28]);
+  test_selector_all(':has(.x:scope ~ .a + .b .c)', [d18, d19, d21, d24, d28, d32]);
+  test_selector_all(':has(:scope.x ~ .a)', [d18, d19, d21, d24, d28, d32, d37, d40, d46]);
+  test_selector_all(':has(:scope.x ~ .a > .b)', [d18, d19, d21]);
+  test_selector_all(':has(:scope.x ~ .a .b)', [d18, d19, d21, d24]);
+  test_selector_all(':has(:scope.x ~ .a + .b)', [d18, d19, d21, d24, d28, d32, d37]);
+  test_selector_all(':has(:scope.x ~ .a + .b > .c)', [d18, d19, d21, d24, d28]);
+  test_selector_all(':has(:scope.x ~ .a + .b .c)', [d18, d19, d21, d24, d28, d32]);
+
+  test_selector_all('.x:has(.d .e)', [d48, d49, d50]);
+  test_selector_all('.x:has(.d .e) .f', [d54]);
+  test_selector_all('.x:has(:scope .d .e)', [d48, d49, d50]);
+  test_selector_all('.x:has(:scope .d .e) .f', [d54]);
+  test_selector_all('.x:has(:scope > .d)', [d49, d50]);
+  test_selector_all('.x:has(:scope > .d) .f', [d54]);
+  test_selector_all('.x:has(:scope ~ .d ~ .e)', [d48, d55, d56]);
+  test_selector_all('.x:has(:scope ~ .d ~ .e) ~ .f', [d60]);
+  test_selector_all('.x:has(:scope + .d ~ .e)', [d55, d56]);
+  test_selector_all('.x:has(:scope + .d ~ .e) ~ .f', [d60]);
+  test_selector_all(':has(.x:scope .d .e)', [d48, d49, d50]);
+  test_selector_all(':has(.x:scope .d .e) .f', [d54]);
+  test_selector_all(':has(.x:scope > .d)', [d49, d50]);
+  test_selector_all(':has(.x:scope > .d) .f', [d54]);
+  test_selector_all(':has(.x:scope ~ .d ~ .e)', [d48, d55, d56]);
+  test_selector_all(':has(.x:scope ~ .d ~ .e) ~ .f', [d60]);
+  test_selector_all(':has(.x:scope + .d ~ .e)', [d55, d56]);
+  test_selector_all(':has(.x:scope + .d ~ .e) ~ .f', [d60]);
+  test_selector_all(':has(:scope.x .d .e)', [d48, d49, d50]);
+  test_selector_all(':has(:scope.x .d .e) .f', [d54]);
+  test_selector_all(':has(:scope.x > .d)', [d49, d50]);
+  test_selector_all(':has(:scope.x > .d) .f', [d54]);
+  test_selector_all(':has(:scope.x ~ .d ~ .e)', [d48, d55, d56]);
+  test_selector_all(':has(:scope.x ~ .d ~ .e) ~ .f', [d60]);
+  test_selector_all(':has(:scope.x + .d ~ .e)', [d55, d56]);
+  test_selector_all(':has(:scope.x + .d ~ .e) ~ .f', [d60]);
+
+  test_selector_all('.y:has(:scope > .g .h)', [d63, d71])
+  test_selector_all('.y:has(:scope .g .h)', [d63, d68, d71])
+  test_selector_all('.y:has(:scope > .g .h) .i', [d67, d75])
+  test_selector_all('.y:has(:scope .g .h) .i', [d67, d75])
+  test_selector_all('.x:has(:scope + .y:has(:scope > .g .h) .i)', [d62, d70])
+  test_selector_all('.x:has(:scope + .y:has(:scope .g .h) .i)', [d62, d63, d70])
+  test_selector_all('.x:has(:scope + .y:has(:scope > .g .h) .i) ~ .j', [d77, d80])
+  test_selector_all('.x:has(:scope + .y:has(:scope .g .h) .i) ~ .j', [d77, d80])
+  test_selector_all('.x:has(:scope ~ .y:has(:scope > .g .h) .i)', [d61, d62, d69, d70])
+  test_selector_all('.x:has(:scope ~ .y:has(:scope .g .h) .i)', [d61, d62, d63, d69, d70])
+  test_selector_all(':has(.y:scope > .g .h)', [d63, d71])
+  test_selector_all(':has(.y:scope .g .h)', [d63, d68, d71])
+  test_selector_all(':has(.y:scope > .g .h) .i', [d67, d75])
+  test_selector_all(':has(.y:scope .g .h) .i', [d67, d75])
+  test_selector_all(':has(.x:scope + :has(.y:scope > .g .h) .i)', [d62, d70])
+  test_selector_all(':has(.x:scope + :has(.y:scope .g .h) .i)', [d62, d63, d70])
+  test_selector_all(':has(.x:scope + :has(.y:scope > .g .h) .i) ~ .j', [d77, d80])
+  test_selector_all(':has(.x:scope + :has(.y:scope .g .h) .i) ~ .j', [d77, d80])
+  test_selector_all(':has(.x:scope ~ :has(.y:scope > .g .h) .i)', [d61, d62, d69, d70])
+  test_selector_all(':has(.x:scope ~ :has(.y:scope .g .h) .i)', [d61, d62, d63, d69, d70])
+  test_selector_all(':has(:scope.y > .g .h)', [d63, d71])
+  test_selector_all(':has(:scope.y .g .h)', [d63, d68, d71])
+  test_selector_all(':has(:scope.y > .g .h) .i', [d67, d75])
+  test_selector_all(':has(:scope.y .g .h) .i', [d67, d75])
+  test_selector_all(':has(:scope.x + :has(:scope.y > .g .h) .i)', [d62, d70])
+  test_selector_all(':has(:scope.x + :has(:scope.y .g .h) .i)', [d62, d63, d70])
+  test_selector_all(':has(:scope.x + :has(:scope.y > .g .h) .i) ~ .j', [d77, d80])
+  test_selector_all(':has(:scope.x + :has(:scope.y .g .h) .i) ~ .j', [d77, d80])
+  test_selector_all(':has(:scope.x ~ :has(:scope.y > .g .h) .i)', [d61, d62, d69, d70])
+  test_selector_all(':has(:scope.x ~ :has(:scope.y .g .h) .i)', [d61, d62, d63, d69, d70])
+
+  test_selector_all('.x:has(.d :scope .e)', [d51, d52])
+  test_selector_all(':has(.d .x:scope .e)', [d51, d52])
+  test_selector_all(':has(.d :scope.x .e)', [d51, d52])
+
+  test_selector_all('.x:has(.d ~ :scope ~ .e)', [d57, d58])
+  test_selector_all(':has(.d ~ .x:scope ~ .e)', [d57, d58])
+  test_selector_all(':has(.d ~ :scope.x ~ .e)', [d57, d58])
+
+  test_selector_all(':has(:scope .d :scope)', [])
+  test_selector_all(':has(:scope ~ .d ~ :scope)', [])
+</script>
diff --git a/css/selectors/parsing/parse-has.html b/css/selectors/parsing/parse-has.html
index 61382fd..88366c3 100644
--- a/css/selectors/parsing/parse-has.html
+++ b/css/selectors/parsing/parse-has.html
@@ -17,12 +17,21 @@
   test_valid_selector('*:has(.a)', ['*:has(.a)', ':has(.a)']);
   test_valid_selector('.a:has(.b)');
   test_valid_selector('.a:has(:scope .b)');
+  test_valid_selector(':has(.a:scope .b)');
+  test_valid_selector(':has(.a .b:scope)');
   test_valid_selector('.a:has(> .b)');
   test_valid_selector('.a:has(:scope > .b)');
+  test_valid_selector(':has(.a:scope > .b)');
+  test_valid_selector(':has(> .a .b:scope)');
   test_valid_selector('.a:has(~ .b)');
   test_valid_selector('.a:has(:scope ~ .b)');
+  test_valid_selector(':has(.a:scope ~ .b)');
+  test_valid_selector(':has(~ .a .b:scope)');
   test_valid_selector('.a:has(+ .b)');
   test_valid_selector('.a:has(:scope + .b)');
+  test_valid_selector(':has(.a:scope + .b)');
+  test_valid_selector(':has(+ .a .b:scope)');
+  test_valid_selector('.a:has(:scope .b :scope)');
   test_valid_selector('.a:has(.b) .c');
   test_valid_selector('.a .b:has(.c)');
   test_valid_selector('.a .b:has(.c .d)');