-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Expand file tree
/
Copy pathTrustedTypePolicyFactory-createPolicy-non-tt-policy-name.html
More file actions
66 lines (58 loc) · 2.6 KB
/
Copy pathTrustedTypePolicyFactory-createPolicy-non-tt-policy-name.html
File metadata and controls
66 lines (58 loc) · 2.6 KB
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
<!DOCTYPE html>
<script src="/resources/testharness.js" ></script>
<script src="/resources/testharnessreport.js"></script>
<meta charset="UTF-8">
<link rel="help" href="https://w3c.github.io/trusted-types/dist/spec/#dom-trustedtypepolicyfactory-createpolicy">
<link rel="help" href="https://w3c.github.io/trusted-types/dist/spec/#tt-policy-name">
<link rel="help" href="https://github.com/w3c/trusted-types/issues/466">
<link rel="help" href="https://github.com/w3c/trusted-types/issues/504">
<body>
<script>
function is_tt_policy_name(aName) {
// tt-policy-name = 1*( ALPHA / DIGIT / "-" / "#" / "=" / "_" / "/" / "@" / "." / "%")
return /^([A-Za-z0-9\-\#\=\_\/\@$\.\%]+)$/.test(aName);
}
function tryCreatePolicy(aName) {
return window.trustedTypes.createPolicy(aName, { createHTML: s => s } );
}
test(() => {
assert_false(is_tt_policy_name(""));
assert_equals(tryCreatePolicy("").name, "");
}, "Creating an empty policy name works.");
test(() => {
for (let codePoint = 0; codePoint < 0x80; codePoint++) {
let policyName = String.fromCharCode(codePoint);
assert_equals(tryCreatePolicy(policyName).name, policyName);
}
}, "Creating policy names made of a single ASCII character works.");
test(() => {
for (let codePoint = 0; codePoint < 0x80; codePoint++) {
let policyName = `policy${String.fromCharCode(codePoint)}name`;
assert_equals(tryCreatePolicy(policyName).name, policyName);
}
}, "Creating policy names made of multiple ASCII characters works.");
test(() => {
// C1 Controls and Latin-1 Supplement
for (let codePoint = 0x80; codePoint <= 0xFF; codePoint++) {
let policyName = `policy${String.fromCharCode(codePoint)}name`;
assert_false(is_tt_policy_name(policyName));
assert_equals(tryCreatePolicy(policyName).name, policyName);
}
// Try U+1000, U+2000, U+3000, ..., U+F000.
for (let i = 0x1; i <= 0xF; i++) {
let codePoint = i * 0x1000;
let policyName = `policy${String.fromCharCode(codePoint)}name`;
assert_false(is_tt_policy_name(policyName));
assert_equals(tryCreatePolicy(policyName).name, policyName);
}
}, "Creating policy names containing a non-ASCII BMP character works.");
test(() => {
// Try U+1000, U+2000, U+3000, ..., U+1F000.
for (let i = 0x10; i <= 0x1F; i++) {
let codePoint = i * 0x1000;
let policyName = `policy${String.fromCharCode(codePoint)}name`;
assert_false(is_tt_policy_name(policyName));
assert_equals(tryCreatePolicy(policyName).name, policyName);
}
}, "Creating policy names containing a non-BMP character works.");
</script>