forked from kripken/xml.js
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathlogic.js
46 lines (36 loc) · 959 Bytes
/
logic.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
import * as xmllint from './xmllint/index-browser.mjs';
function getXml() {
return document.getElementById('xml').value;
}
function getSchema() {
return document.getElementById('schema').value;
}
function setXml(xml) {
document.getElementById('xml').value = xml;
}
function setStatus(valid, messages) {
const elem = document.getElementById('messages');
elem.style.backgroundColor = valid ? 'green' : 'red';
elem.value = messages;
}
async function process(normalize) {
const xml = getXml();
const schema = getSchema();
const { valid, rawOutput, normalized } =
await xmllint.validateXML(
{
xml: { fileName: 'editor.xml', contents: xml },
schema: normalize ? undefined : schema,
normalization: 'format'
});
setStatus(valid, rawOutput);
if (valid && normalize) {
setXml(normalized);
}
}
export async function validate(_event) {
await process(false);
}
export async function normalize(_event) {
await process(true);
}