Make position-fallback a tree-scoped name/reference

In preparation for doing tree-scoped name lookup for @position-fallback
rules. Also added tests for tree-scoped @position-fallback lookups.

Bug: 1381623
Change-Id: I6f9dc5ecaac2773c8deff9b9f6c66f389eb5e977
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4055466
Reviewed-by: Anders Hartvoll Ruud <andruud@chromium.org>
Commit-Queue: Rune Lillesveen <futhark@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1075628}
diff --git a/css/css-anchor-position/position-fallback-tree-scoped.html b/css/css-anchor-position/position-fallback-tree-scoped.html
new file mode 100644
index 0000000..5b5c25f
--- /dev/null
+++ b/css/css-anchor-position/position-fallback-tree-scoped.html
@@ -0,0 +1,112 @@
+<!DOCTYPE html>
+<title>CSS Anchor Positioning Test: @position-fallback - tree scoped names</title>
+<link rel="help" href="https://drafts.csswg.org/css-anchor-1/#fallback-rule">
+<script src="/resources/testharness.js"></script>
+<script src="/resources/testharnessreport.js"></script>
+<script src="/resources/declarative-shadow-dom-polyfill.js"></script>
+
+<style>
+  body { margin: 0; }
+
+  @position-fallback --doc {
+    @try {
+      left: 100px;
+    }
+  }
+
+  .abs { position: absolute; }
+
+  #doc_pf_doc { position-fallback: --doc; }
+  #doc_pf_outer { position-fallback: --outer; }
+  #doc_pf_inner { position-fallback: --inner; }
+</style>
+
+<div id="doc_pf_doc" class="abs"></div>
+<div id="doc_pf_outer" class="abs"></div>
+<div id="doc_pf_inner" class="abs"></div>
+<div id="outer_host">
+  <template shadowroot="open">
+    <style>
+      @position-fallback --outer {
+        @try {
+          left: 200px;
+        }
+      }
+
+      .abs { position: absolute; }
+
+      #outer_pf_doc { position-fallback: --doc; }
+      #outer_pf_outer { position-fallback: --outer; }
+      #outer_pf_inner { position-fallback: --inner; }
+    </style>
+    <div id="outer_pf_doc" class="abs"></div>
+    <div id="outer_pf_outer" class="abs"></div>
+    <div id="outer_pf_inner" class="abs"></div>
+    <div id="inner_host">
+      <template shadowroot="open">
+        <style>
+          @position-fallback --inner {
+            @try {
+              left: 300px;
+            }
+          }
+
+          .abs { position: absolute; }
+
+          #inner_pf_doc { position-fallback: --doc; }
+          #inner_pf_outer { position-fallback: --outer; }
+          #inner_pf_inner { position-fallback: --inner; }
+        </style>
+        <div id="inner_pf_doc" class="abs"></div>
+        <div id="inner_pf_outer" class="abs"></div>
+        <div id="inner_pf_inner" class="abs"></div>
+      </template>
+    </div>
+  </template>
+</div>
+
+<script>
+  setup(() => {
+    polyfill_declarative_shadow_dom(outer_host);
+  });
+
+  test(() => {
+    assert_equals(doc_pf_doc.offsetLeft, 100);
+  }, "Document position-fallback matches @position-fallback in document scope");
+
+  test(() => {
+    assert_equals(doc_pf_outer.offsetLeft, 0);
+  }, "Document position-fallback does not match @position-fallback in #outer_host scope");
+
+  test(() => {
+    assert_equals(doc_pf_inner.offsetLeft, 0);
+  }, "Document position-fallback does not match @position-fallback in #inner_host scope");
+
+  const outer_root = outer_host.shadowRoot;
+  const inner_root = outer_root.querySelector("#inner_host").shadowRoot;
+
+  test(() => {
+    assert_equals(outer_root.querySelector("#outer_pf_doc").offsetLeft, 100);
+  }, "Outer position-fallback matches @position-fallback in document scope");
+
+  test(() => {
+    assert_equals(outer_root.querySelector("#outer_pf_outer").offsetLeft, 200);
+  }, "Outer position-fallback matches @position-fallback in #outer_host scope");
+
+  test(() => {
+    assert_equals(outer_root.querySelector("#outer_pf_inner").offsetLeft, 0);
+  }, "Outer position-fallback does not match @position-fallback in #inner_host scope");
+
+  test(() => {
+    assert_equals(inner_root.querySelector("#inner_pf_doc").offsetLeft, 100)
+  }, "Inner position-fallback matches @position-fallback in document scope");
+
+  test(() => {
+    assert_equals(inner_root.querySelector("#inner_pf_outer").offsetLeft, 200);
+  }, "Inner position-fallback matches @position-fallback in #outer_host scope");
+
+  test(() => {
+    assert_equals(inner_root.querySelector("#inner_pf_inner").offsetLeft, 300);
+  }, "Inner position-fallback matches @position-fallback in #inner_host scope");
+
+</script>