summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/trusted-types/default-policy-callback-arguments.html
blob: 4801b55387ca49867183e218105daa949d722668 (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
<!DOCTYPE html>
<head>
  <script src="/resources/testharness.js"></script>
  <script src="/resources/testharnessreport.js"></script>
  <meta http-equiv="Content-Security-Policy"
        content="require-trusted-types-for 'script'">
</head>
<body>
  <div id="log"></div>
  <div id="div"></div>
  <script id="script"></script>
  <script>
    var current_case = undefined;
    function checker(...args) {
      assert_equals(args.length, 3);
      assert_true(current_case && current_case.length == 4);
      assert_equals(args[0], current_case[0], "Expecting the value.");
      assert_equals(args[1], current_case[1], "Expecting the type name.");
      assert_equals(args[2], current_case[2], "Expecting the sink name.");
      return args[0];
    }

    trustedTypes.createPolicy("default", {
      createHTML: checker,
      createScript: checker,
      createScriptURL: checker
    });

    const div = document.getElementById("div");
    const script = document.getElementById("script");
    const cases = [
      [ "abc", "TrustedHTML", "Element innerHTML",
          _ => div.innerHTML = "abc" ],
      [ "2+2", "TrustedScript", "HTMLScriptElement textContent",
          _ => script.textContent = "2+2" ],
      [ "about:blank", "TrustedScriptURL", "HTMLScriptElement src",
          _ => script.src = "about:blank" ],
      [ "2+2", "TrustedScript", "eval", _ => eval("2+2") ],
      [ "(function anonymous(\n) {\nreturn 2+2\n})", "TrustedScript",
        "Function", _ => new Function("return 2+2") ],
    ];
    for (var tc of cases) {
      test(t => {
        current_case = tc;
        tc[3]();
      }, `Test callback arguments, case ${tc[2]}`);
    }
  </script>
</body>