<!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">
<link rel="help" href="https://drafts.csswg.org/css-scoping/#shadow-names">
<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 shadowrootmode="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 shadowrootmode="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>


<style>
  @position-fallback --host-slot-part {
    @try {
      left: 1px;
    }
  }
  #host_slotted_part::part(part) {
    position-fallback: --host-slot-part;
  }
</style>
<div id="host_slotted_part">
  <template shadowrootmode="open">
    <style>
      @position-fallback --host-slot-part {
        @try {
          left: 2px;
        }
      }
      ::slotted(#slotted), :host {
        position: absolute;
        position-fallback: --host-slot-part;
      }
      #part {
        position: absolute;
      }
    </style>
    <div id="part" part="part"></div>
    <slot></slot>
  </template>
  <div id="slotted"></div>
</div>


<script>
  setup(() => {
    polyfill_declarative_shadow_dom(outer_host);
    polyfill_declarative_shadow_dom(host_slotted_part);
  });

  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");

  test(() => {
    assert_equals(host_slotted_part.offsetLeft, 2);
  }, "@position-fallback from same scope as :host rule");

  test(() => {
    assert_equals(slotted.offsetLeft, 2);
  }, "@position-fallback from same scope as ::slotted() rule");

  test(() => {
    assert_equals(host_slotted_part.shadowRoot.querySelector("#part").offsetLeft, 1);
  }, "@position-fallback from same scope as ::part() rule");
</script>
