forked from web-platform-tests/wpt.fyi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
path.js
213 lines (194 loc) · 5.53 KB
/
path.js
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/**
* Copyright 2018 The WPT Dashboard Project. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
/*
`<path-part>` is a stateless component for displaying part of a test path.
*/
import '../node_modules/@polymer/paper-styles/color.js';
import { html, PolymerElement } from '../node_modules/@polymer/polymer/polymer-element.js';
const PathInfo = (superClass) => class extends superClass {
static get properties() {
return {
path: {
type: String,
},
encodedPath: {
type: String,
computed: 'encodeTestPath(path)'
},
scheme: {
type: String,
computed: 'computeTestScheme(path)'
},
pathIsATestFile: {
type: Boolean,
computed: 'computePathIsATestFile(path)'
},
pathIsASubfolder: {
type: Boolean,
computed: 'computePathIsASubfolder(path)'
},
pathIsRootDir: {
type: Boolean,
computed: 'computePathIsRootDir(path)'
}
};
}
encodeTestPath(path) {
path = path || '/';
console.assert(path.startsWith('/'));
const url = new URL(path || '/', window.location);
let parts = url.pathname.split('/');
parts.pop();
let lastPart = path.substr(parts.join('/').length + 1);
parts.push(encodeURIComponent(lastPart));
return parts.join('/');
}
computeTestScheme(path) {
// This should (close enough) match up with the logic in:
// https://github.com/web-platform-tests/wpt/blob/master/tools/manifest/item.py
// https://github.com/web-platform-tests/wpt/blob/master/tools/wptrunner/wptrunner/wpttest.py
path = path || '';
return ['.https.', '.serviceworker.'].some(x => path.includes(x)) ? 'https' : 'http';
}
computePathIsASubfolder(path) {
if (!path || this.computePathIsATestFile(path)) {
return false;
}
// Strip out query params/anchors.
path = new URL(path, window.location).pathname;
return path.split('/').filter(p => p).length > 0;
}
computePathIsATestFile(path) {
// Strip out query params/anchors.
path = new URL(path || '', window.location).pathname;
return /(\.(html|htm|py|svg|xhtml|xht|xml)(\?.*)?$)/.test(path);
}
computePathIsRootDir(path) {
return path && path === '/';
}
isParentDir(path, dir) {
if (dir.startsWith(path)) {
const relativePath = dir.substring(path.length);
return relativePath.split('/').filter(p => p).length === 1;
}
return false;
}
getDirname(path) {
path = path || '/';
console.assert(path.startsWith('/'));
return path.substring(0, path.lastIndexOf('/'));
}
splitPathIntoLinkedParts(inputPath) {
// Remove the leading slash.
const encoded = this.encodeTestPath(inputPath).slice(1);
if (encoded === '') {
// Return an empty array instead of an array with an empty string.
return [];
}
const parts = encoded.split('/');
let path = '';
const linkedParts = parts.map(part => {
path += `/${part}`;
return {
name: part,
path: path,
};
});
// Decode the last part's name (in case it was escaped).
let last = linkedParts.pop();
last.name = decodeURIComponent(last.name);
linkedParts.push(last);
return linkedParts;
}
};
class PathPart extends PathInfo(PolymerElement) {
static get template() {
return html`
<style>
a {
text-decoration: none;
color: var(--paper-blue-700);
font-family: monospace;
}
a:hover {
cursor: pointer;
color: var(--paper-blue-900);
}
.dir-path {
font-weight: bold;
}
</style>
<a class\$="{{ styleClass }}" href="{{ href }}" onclick="{{ navigate }}">
{{ relativePath }}
</a>
`;
}
static get is() {
return 'path-part';
}
static get properties() {
return {
query: {
type: String
},
// Domain path-prefix, e.g. '/result/'
prefix: {
type: String,
default: '/'
},
isDir: {
type: Boolean
},
navigate: {
type: Function
},
relativePath: {
type: String,
computed: 'computeDisplayableRelativePath(path)'
},
href: {
type: Location,
computed: 'computeHref(prefix, path, query, isTriageMode)'
},
isTriageMode: {
type: Boolean,
value: false
},
styleClass: {
type: String,
computed: 'computePathClass(isDir)'
}
};
}
computeHref(prefix, path, query, isTriageMode) {
// Disable the link if triage mode is enabled
// and this cell is viable for triage (a test file).
if (isTriageMode && this.pathIsATestFile) {
return 'javascript:void(0)';
}
const encodedPath = this.encodeTestPath(path);
const href = new URL(window.location);
href.pathname = `${prefix || ''}${encodedPath}`;
if (query) {
href.search = query;
}
return href;
}
computeDisplayableRelativePath(path) {
if (!this.isDir) {
path = this.encodeTestPath(path || '');
return decodeURIComponent(path.substr(path.lastIndexOf('/') + 1));
}
const windowPath = window.location.pathname.replace(`${this.prefix || ''}`, '');
const pathPrefix = new RegExp(`^${windowPath}${windowPath.endsWith('/') ? '' : '/'}`);
return `${path.replace(pathPrefix, '')}${this.isDir ? '/' : ''}`;
}
computePathClass(isDir) {
return isDir ? 'dir-path' : 'file-path';
}
}
window.customElements.define(PathPart.is, PathPart);
export { PathPart, PathInfo };