summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/dom/nodes/Element-closest.html
blob: 386e3bd251bdb41587173bb89bcc0471733e0699 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<!DOCTYPE HTML>
<meta charset=utf8>
<title>Test for Element.closest</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<body id="body">
  <div id="test8" class="div3" style="display:none">
    <div id="test7" class="div2">
      <div id="test6" class="div1">
        <form id="test10" class="form2"></form>
        <form id="test5" class="form1" name="form-a">
          <input id="test1" class="input1" required>
          <fieldset class="fieldset2" id="test2">
            <select id="test3" class="select1" required>
              <option default id="test4" value="">Test4</option>
              <option selected id="test11">Test11</option>
              <option id="test12">Test12</option>
              <option id="test13">Test13</option>
            </select>
            <input id="test9" type="text" required>
          </fieldset>
        </form>
      </div>
    </div>
  </div>
  <div id=log></div>
<script>
  do_test("select"                     , "test12", "test3");
  do_test("fieldset"                   , "test13", "test2");
  do_test("div"                        , "test13", "test6");
  do_test("body"                       , "test3" , "body");

  do_test("[default]"                  , "test4" , "test4");
  do_test("[selected]"                 , "test4" , "");
  do_test("[selected]"                 , "test11", "test11");
  do_test('[name="form-a"]'            , "test12", "test5");
  do_test('form[name="form-a"]'        , "test13", "test5");
  do_test("input[required]"            , "test9" , "test9");
  do_test("select[required]"           , "test9" , "");

  do_test("div:not(.div1)"             , "test13", "test7");
  do_test("div.div3"                   , "test6" , "test8");
  do_test("div#test7"                  , "test1" , "test7");

  do_test(".div3 > .div2"              , "test12", "test7");
  do_test(".div3 > .div1"              , "test12", "");
  do_test("form > input[required]"     , "test9" , "");
  do_test("fieldset > select[required]", "test12", "test3");

  do_test("input + fieldset"           , "test6" , "");
  do_test("form + form"                , "test3" , "test5");
  do_test("form + form"                , "test5" , "test5");

  do_test(":empty"                     , "test10", "test10");
  do_test(":last-child"                , "test11", "test2");
  do_test(":first-child"               , "test12", "test3");
  do_test(":invalid"                   , "test11", "test2");

  do_test(":scope"                     , "test4",  "test4");
  do_test("select > :scope"            , "test4",  "test4");
  do_test("div > :scope"               , "test4",  "");
  do_test(":has(> :scope)"             , "test4",  "test3");
function do_test(aSelector, aElementId, aTargetId) {
  test(function() {
    var el = document.getElementById(aElementId).closest(aSelector);
    if (el === null) {
      assert_equals("", aTargetId, aSelector);
    } else {
      assert_equals(el.id, aTargetId, aSelector);
    }
  }, "Element.closest with context node '" + aElementId + "' and selector '" + aSelector + "'");
}
</script>