-
Notifications
You must be signed in to change notification settings - Fork 31
/
index.html
123 lines (101 loc) · 3.02 KB
/
index.html
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
119
120
121
122
123
<html>
<head></head>
<script type="module">
import { URLPattern } from "./index.js";
import { runTests } from "/test/wpt/wpt-test-runner.js";
window.URLPattern = URLPattern;
function assert(result, expected) {
console.log("Result:", result, ", Expected:", expected, "=>", result === expected ? "PASS" : "FAIL");
}
let pattern = new URLPattern({
baseURL: self.origin,
pathname: "/product/:name*",
});
assert(pattern.test(self.origin + "/product/a/b"), true);
let relativePattern = new URLPattern({
baseURL: "https://example.com/shop",
pathname: "/product/:name*",
});
assert(relativePattern.test("https://example.com/product/a/b"), true);
const imagePattern = new URLPattern({
pathname: '/*.:imagetype(jpg|gif|png)'
});
let result = imagePattern.exec(self.origin + "/photo.jpg");
console.info(result);
assert(result.pathname.groups['imagetype'], "jpg");
let result2 = imagePattern.exec(self.origin + "/photo.jxl");
console.info(result2);
let p1 = new URLPattern({
baseURL: self.origin,
pathname: "/product/:name*",
});
let p2 = new URLPattern({
baseURL: self.origin,
pathname: "/employee/:name*",
});
const baseURL = 'https://example.com';
const test = (title, fn) => {
const t = new class {
failures = 0;
true(a) {
if (!a) this.failures++;
}
is(a, b) {
if (a !== b) this.failures++;
}
deepEqual(a, b) {
let _deepEqual = (a, b) => {
const isObject = o => typeof o === "object" && o !== null;
if (isObject(a) && isObject(b)) {
const keysA = Object.keys(a);
const keysB = Object.keys(b);
if (keysA.length !== keysB.length) {
return false;
}
for (let i = 0; i < keysA.length; i++) {
if (!_deepEqual(a[keysA[i]], b[keysA[i]])) {
return false;
}
}
} else if (a !== b) {
return false;
}
return true;
}
if (!_deepEqual(a, b)) this.failures++;
}
throws(fn, options = {}) {
let didPass = false;
try {
fn();
} catch (err) {
didPass = true;
if (options.instanceOf && !(err instanceof options.instanceOf)) {
didPass = false;
}
}
if (!didPass) this.failures++;
}
}
fn(t);
const child = document.createElement('div');
if (t.failures <= 0) {
child.innerHTML = `
<span style='background-color: green'>PASS</span>: ${title}
`;
} else {
child.innerHTML = `
<span style='background-color: red'>FAIL</span>: ${title}
`;
}
document.body.appendChild(child);
}
test('urlPattern', t => {
let pattern = new URLPattern({baseURL, pathname: '/product/*?'});
t.true(pattern.test(baseURL + '/product/a/b'));
});
const src = await fetch('/test/urlpatterntestdata.json');
const json = await src.json();
runTests(json, test);
</script>
</html>