-
Notifications
You must be signed in to change notification settings - Fork 276
Expand file tree
/
Copy pathindex.ts
More file actions
130 lines (111 loc) · 3.49 KB
/
index.ts
File metadata and controls
130 lines (111 loc) · 3.49 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
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
import { SourceType, parseSourceType } from '../source-type'
import * as vsv from '../vendor-specific-values'
import { makeLi } from './issue-utils'
import * as eligible from './validate-eligible'
import * as info from './validate-info'
import * as os from './validate-os'
import * as source from './validate-source'
import * as trigger from './validate-trigger'
import * as validator from './validator'
const form = document.querySelector<HTMLFormElement>('form')!
const input = form.querySelector<HTMLTextAreaElement>('textarea')!
const headerRadios = form.elements.namedItem('header')! as RadioNodeList
const sourceTypeRadios = form.elements.namedItem(
'source-type'
)! as RadioNodeList
const errorList = document.querySelector('#errors')!
const warningList = document.querySelector('#warnings')!
const noteList = document.querySelector('#notes')!
const successDiv = document.querySelector('#success')!
const sourceTypeFieldset =
document.querySelector<HTMLFieldSetElement>('#source-type')!
const effective = document.querySelector('#effective')!
function sourceType(): SourceType {
return parseSourceType(sourceTypeRadios.value)
}
function validate(): void {
sourceTypeFieldset.disabled = true
let v: validator.Validator<unknown>
switch (headerRadios.value) {
case 'source':
sourceTypeFieldset.disabled = false
v = source.validator({
vsv: vsv.Chromium,
sourceType: sourceType(),
noteInfoGain: true,
})
break
case 'trigger':
v = trigger.validator({
vsv: vsv.Chromium,
})
break
case 'os-source':
case 'os-trigger':
v = os
break
case 'eligible':
v = eligible
break
case 'info':
v = info
break
default:
return
}
const result = validator.validate(input.value, v)
const successEl = document.createElement('div')
if (result.errors.length === 0 && result.warnings.length === 0) {
successEl.textContent = 'The header is valid.'
} else {
successEl.textContent = ''
}
successDiv.replaceChildren(successEl)
errorList.replaceChildren(...result.errors.map(makeLi))
warningList.replaceChildren(...result.warnings.map(makeLi))
noteList.replaceChildren(...result.notes.map(makeLi))
if (result.value === undefined) {
effective.replaceChildren()
} else {
effective.textContent = result.value
}
}
form.addEventListener('input', validate)
document.querySelector('#linkify')!.addEventListener('click', () => {
const url = new URL(location.toString())
url.search = ''
url.searchParams.set('header', headerRadios.value)
url.searchParams.set('json', input.value)
if (url.searchParams.get('header') === 'source') {
url.searchParams.set('source-type', sourceType())
}
void navigator.clipboard.writeText(url.toString())
})
// Note: The `json` and `header` query params are relied on by DevTools as of
// https://crrev.com/c/devtools/devtools-frontend/+/4076187, so they must not
// be changed in an incompatible way.
const params = new URLSearchParams(location.search)
const json = params.get('json')
if (json) {
input.value = json
}
const allowedValues = new Set([
'eligible',
'os-source',
'os-trigger',
'source',
'trigger',
])
let selection = params.get('header')
if (selection === null || !allowedValues.has(selection)) {
selection = 'source'
}
headerRadios.value = selection
let st = params.get('source-type')
if (st !== null && st in SourceType) {
st = st as SourceType
} else {
st = SourceType.event
}
sourceTypeRadios.value = st
validate()