summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/trusted-types/trusted-types-from-literal.tentative.html
blob: a7d5659e16a1471ac5cb900dce7b3a102a14af89 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<!DOCTYPE html>
<head>
<link rel="author" title="Daniel Vogelheim" href="mailto:vogelheim@chromium.org"></link>
<link rel="help" href="https://w3c.github.io/trusted-types/dist/spec/"></link>
<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>
<script>
[ TrustedHTML, TrustedScript, TrustedScriptURL ].forEach(type => {

  test(t => {
    assert_true("fromLiteral" in type);
  }, `${type.name}.fromLiteral is supported`);

  test(t => {
    const c = type.fromLiteral`abc`;
    assert_true(c instanceof type);
    assert_equals(c.toString(), "abc");
  }, `${type.name}.fromLiteral: Basic string literal works.`);

  test(t => {
    const c = type.fromLiteral``;
    assert_true(c instanceof type);
    assert_equals(c.toString(), "");
  }, `${type.name}.fromLiteral: Empty string literal works.`);

  test(t => {
    const c = type.fromLiteral`\u{1f4a9}`;
    assert_true(c instanceof type);
    assert_equals(c.toString(), "\u{1f4a9}");
  }, `${type.name}.fromLiteral: A very important emoji works.`);

  test(t => {
    const c = type.fromLiteral`A Multiline
  string
     works.`;
    assert_true(c instanceof type);
    assert_true(c.toString().includes("\n"));
  }, `${type.name}.fromLiteral: Multi-line string literal works.`);

  test(t => {
    const tag = type.fromLiteral.bind(type);
    const c = tag`abc`;
    assert_true(c instanceof type);
    assert_equals(c.toString(), "abc");
  }, `${type.name}.fromLiteral: Bound method works.`);

  test(t => {
    assert_throws_js(TypeError, _ => {
      type.fromLiteral("abc");
    });
  }, `${type.name}.fromLiteral: String throws.`);

  test(t => {
    assert_throws_js(TypeError, _ => {
      type.fromLiteral(null);
    });
  }, `${type.name}.fromLiteral: null throws.`);

  test(t => {
    assert_throws_js(TypeError, _ => {
      type.fromLiteral(undefined);
    });
  }, `${type.name}.fromLiteral: undefined throws.`);

  test(t => {
    assert_throws_js(TypeError, _ => {
      type.fromLiteral({});
    });
  }, `${type.name}.fromLiteral: Object throws.`);

  test(t => {
    assert_throws_js(TypeError, _ => {
      type.fromLiteral`Hello ${2+3} world`
    });
  }, `${type.name}.fromLiteral: template literal with expression throws.`);

  test(t => {
    assert_throws_js(TypeError, _ => {
      type.fromLiteral(["abc"]);
    });
  }, `${type.name}.fromLiteral: non-literal throws.`);

  test(t => {
    assert_throws_js(TypeError, _ => {
      type.fromLiteral(Object.freeze(["abc"]));
    });
  }, `${type.name}.fromLiteral: frozen non-literal throws.`);

  test(t => {
    assert_throws_js(TypeError, _ => {
      type.fromLiteral(["abc", "def"], "xxx");
    });
  }, `${type.name}.fromLiteral: non-literal with param throws.`);

});

// TrustedHTML requires normalization of the value. Let's test that TrustedHTML
// (and only TrustedHTML) does this.
test(t => {
  const tag = TrustedHTML.fromLiteral.bind(TrustedHTML);
  assert_equals(tag`<b>`.toString(), "<b></b>");
}, "TrustedHTML.fromLiteral: Normalization of value works.");

test(t => {
  const tag = TrustedScript.fromLiteral.bind(TrustedScript);
  assert_equals(tag`<b>`.toString(), "<b>");
}, "TrustedScript.fromLiteral: No normalization of value occurs.");

test(t => {
  const tag = TrustedScriptURL.fromLiteral.bind(TrustedScriptURL);
  assert_equals(tag`<b>`.toString(), "<b>");
}, "TrustedScriptURL.fromLiteral: No normalization of value occurs.");

</script>
</body>