summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/trusted-types/TrustedTypePolicyFactory-getPropertyType.tentative.html
blob: e7218e9333a213df8643b628fc8dae294ef47ee0 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<!DOCTYPE html>
<script src="/resources/testharness.js" ></script>
<script src="/resources/testharnessreport.js"></script>
<script src="support/helper.sub.js"></script>

<body>
<div id="target"></div>
<script>
  test(t => {
    assert_equals(trustedTypes.getPropertyType("script", "text"), "TrustedScript");
    assert_equals(trustedTypes.getPropertyType("script", "src"), "TrustedScriptURL");
    assert_equals(trustedTypes.getPropertyType("script", "id"), null);
    assert_equals(trustedTypes.getPropertyType("script", "b"), null);
  }, "sanity check trustedTypes.getPropertyType for the HTML script element.");

  test(t => {
    assert_equals(trustedTypes.getAttributeType("img", "onerror"), "TrustedScript");
    assert_equals(trustedTypes.getAttributeType("img", "width"), null);
    assert_equals(trustedTypes.getAttributeType("img", "madeup"), null);
  }, "sanity check trustedTypes.getAttributeType.");

  // getPropertyType tests adapted from w3c/trusted-types polyfill:
  test(t => {
    // returns the proper type for attribute-related properties
    assert_equals(trustedTypes.getPropertyType("script", "src"), "TrustedScriptURL");

    // is case insensitive for tag names
    assert_equals(trustedTypes.getPropertyType("SCRIPT", "src"), "TrustedScriptURL");

    // is case sensitive for property names
    assert_equals(trustedTypes.getPropertyType("script", "sRc"), null);
    assert_equals(trustedTypes.getPropertyType("div", "innerhtml"), null);

    // returns the proper type for innerHTML
    assert_equals(trustedTypes.getPropertyType("div", "innerHTML"), "TrustedHTML");

    // returns the proper type for outerHTML
    assert_equals(trustedTypes.getPropertyType("div", "outerHTML"), "TrustedHTML");

    // returns the proper type for script.prop
    ["text", "innerText", "textContent"].forEach((prop) => {
      assert_equals(trustedTypes.getPropertyType("script", prop), "TrustedScript");
    });
  }, "getPropertyType tests adapted from w3c/trusted-types polyfill");

  test(t => {
    // returns the proper type
    assert_equals(trustedTypes.getAttributeType('script', 'src'), 'TrustedScriptURL');

    // ignores attributes from unknown namespaces
    assert_equals(trustedTypes.getAttributeType(
          'a', 'href', '', 'http://foo.bar'), null);

    // is case insensitive for element names
    assert_equals(trustedTypes.getAttributeType('SCRIPT', 'src'), 'TrustedScriptURL');

    // is case insensitive for the attribute names
    assert_equals(trustedTypes.getAttributeType('script', 'SRC'), 'TrustedScriptURL');

    // supports the inline event handlers
    assert_equals(trustedTypes.getAttributeType('img', 'onerror'), 'TrustedScript');
    assert_equals(trustedTypes.getAttributeType('unknown', 'onerror'), 'TrustedScript');

    // defaults to undefined
    assert_equals(trustedTypes.getAttributeType('unknown', 'src'), null);
    assert_equals(trustedTypes.getAttributeType('img', 'bar'), null);
  }, "getAttributeType tests adapted from w3c/trusted-types polyfill");

  // Test case handling for both attributes and properties.
  for (let attr of ["srcDoc", "SRCDOC", "srcdoc"]) {
    for (let elem of ["iframe", "IFRAME", "iFrAmE"]) {
      test(t => {
        // attributes are case-insensitive, so all variants should be defined.
        assert_true(trustedTypes.getAttributeType(elem, attr) != undefined);
      }, `${elem}[${attr}] is defined`);
      test(t => {
        // properties are case-sensitive, so only the "correct" spelling
        // should be defined.
        assert_equals(trustedTypes.getPropertyType(elem, attr) != undefined,
                      attr == "srcdoc");
      }, `${elem}.${attr} is maybe defined`);
    }
  }

  test(t => {
    assert_equals(trustedTypes.getPropertyType("img", "onerror"), null);
    assert_equals(trustedTypes.getAttributeType("img", "onerror"), "TrustedScript");
  }, "getPropertyType vs getAttributeType for event handler.");
</script>
</body>