eyemind 0.8.8 → 0.8.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/xml.js ADDED
@@ -0,0 +1,142 @@
1
+ // Self-contained XML 1.1 content (RDF 1.2 Concepts, Appendix A.2), with
2
+ // Namespaces in XML 1.0. The scanner needs no document wrapper, DTD or I/O.
3
+ // https://www.w3.org/TR/xml11/#sec-well-formed
4
+ // https://www.w3.org/TR/xml-names/#Conformance
5
+ const NC_START = String.raw`A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD\u{10000}-\u{EFFFF}`;
6
+ const NC_CHAR = NC_START + String.raw`0-9.\-\u00B7\u0300-\u036F\u203F-\u2040`;
7
+ const NC_NAME = `[${NC_START}][${NC_CHAR}]*`;
8
+ const QNAME = new RegExp(NC_NAME + '(?::' + NC_NAME + ')?', 'uy');
9
+ const PI_NAME = new RegExp(NC_NAME, 'uy');
10
+ const SPACE = /[ \t\n]+/y;
11
+ const DATA = /[^<&]+/y;
12
+ const REFERENCE = /&(?:(amp|lt|gt|apos|quot)|#([0-9]+)|#x([0-9a-fA-F]+));/y;
13
+ const ENTITIES = { amp: '&', lt: '<', gt: '>', apos: "'", quot: '"' };
14
+ const XML = 'http://www.w3.org/XML/1998/namespace';
15
+ const XMLNS = 'http://www.w3.org/2000/xmlns/';
16
+ const INVALID_RAW = /[\u0000-\u0008\u000B\u000C\u000E-\u001F\u007F-\u0084\u0086-\u009F\uD800-\uDFFF\uFFFE\uFFFF]/u;
17
+
18
+ function match(pattern, text, at) {
19
+ pattern.lastIndex = at;
20
+ return pattern.exec(text);
21
+ }
22
+
23
+ function reference(text, at) {
24
+ const ref = match(REFERENCE, text, at);
25
+ if (!ref) return null;
26
+ const [raw, name, decimal, hex] = ref;
27
+ if (name) return [raw.length, ENTITIES[name]];
28
+ const code = decimal === undefined ? Number.parseInt(hex, 16) : Number(decimal);
29
+ if (code === 0 || code > 0x10FFFF || (code >= 0xD800 && code <= 0xDFFF) || code === 0xFFFE || code === 0xFFFF) return null;
30
+ return [raw.length, String.fromCodePoint(code)];
31
+ }
32
+
33
+ export function isXmlContent(text) {
34
+ if (INVALID_RAW.test(text)) return false;
35
+ text = text.replace(/\r[\n\u0085]?|[\u0085\u2028]/gu, '\n');
36
+ const namespaces = new Map([['xml', XML]]), open = [];
37
+ let at = 0;
38
+ const take = (token) => {
39
+ if (!text.startsWith(token, at)) return false;
40
+ at += token.length;
41
+ return true;
42
+ };
43
+ const read = (pattern) => {
44
+ const found = match(pattern, text, at)?.[0];
45
+ if (found) at += found.length;
46
+ return found;
47
+ };
48
+ const attributes = () => {
49
+ const found = new Map();
50
+ while (read(SPACE) && text[at] !== '>' && text[at] !== '/') {
51
+ const name = read(QNAME);
52
+ if (!name || found.has(name)) return null;
53
+ read(SPACE);
54
+ if (!take('=')) return null;
55
+ read(SPACE);
56
+ const quote = text[at++];
57
+ if (quote !== '"' && quote !== "'") return null;
58
+ const end = text.indexOf(quote, at);
59
+ if (end < 0) return null;
60
+ const raw = text.slice(at, end);
61
+ if (raw.includes('<')) return null;
62
+ let value = '', start = 0;
63
+ for (let amp = raw.indexOf('&'); amp >= 0; amp = raw.indexOf('&', start)) {
64
+ const ref = reference(raw, amp);
65
+ if (!ref) return null;
66
+ value += raw.slice(start, amp).replace(/[\t\n]/g, ' ') + ref[1];
67
+ start = amp + ref[0];
68
+ }
69
+ found.set(name, value + raw.slice(start).replace(/[\t\n]/g, ' '));
70
+ at = end + 1;
71
+ }
72
+ return found;
73
+ };
74
+ const restore = (changes) => {
75
+ for (const [prefix, value] of changes) {
76
+ if (value === undefined) namespaces.delete(prefix);
77
+ else namespaces.set(prefix, value);
78
+ }
79
+ };
80
+ while (at < text.length) {
81
+ if (take('<!--')) {
82
+ const end = text.indexOf('--', at);
83
+ if (end < 0 || !text.startsWith('-->', end)) return false;
84
+ at = end + 3;
85
+ } else if (take('<![CDATA[')) {
86
+ const end = text.indexOf(']]>', at);
87
+ if (end < 0) return false;
88
+ at = end + 3;
89
+ } else if (take('<?')) {
90
+ const target = read(PI_NAME);
91
+ if (!target || target.toLowerCase() === 'xml') return false;
92
+ if (take('?>')) continue;
93
+ if (!read(SPACE)) return false;
94
+ const end = text.indexOf('?>', at);
95
+ if (end < 0) return false;
96
+ at = end + 2;
97
+ } else if (take('</')) {
98
+ const name = read(QNAME), frame = open.pop();
99
+ if (!frame || frame.name !== name) return false;
100
+ read(SPACE);
101
+ if (!take('>')) return false;
102
+ restore(frame.changes);
103
+ } else if (take('<')) {
104
+ const name = read(QNAME);
105
+ if (!name) return false;
106
+ const attrs = attributes();
107
+ if (!attrs) return false;
108
+ const empty = take('/>');
109
+ if (!empty && !take('>')) return false;
110
+ const changes = new Map();
111
+ // Declarations apply to the whole tag, including attributes before them.
112
+ for (const [key, value] of attrs) {
113
+ if (key !== 'xmlns' && !key.startsWith('xmlns:')) continue;
114
+ const prefix = key === 'xmlns' ? '' : key.slice(6);
115
+ if (prefix === 'xmlns' || value === XMLNS || (prefix === 'xml') !== (value === XML) || (prefix && !value)) return false;
116
+ changes.set(prefix, namespaces.get(prefix));
117
+ namespaces.set(prefix, value);
118
+ attrs.delete(key);
119
+ }
120
+ const [prefix, local] = name.split(':');
121
+ if (local && !namespaces.has(prefix)) return false;
122
+ const expanded = new Set();
123
+ for (const key of attrs.keys()) {
124
+ const [prefix, local] = key.split(':');
125
+ const namespace = local ? namespaces.get(prefix) : '';
126
+ if (namespace === undefined) return false;
127
+ const expandedName = namespace + '\0' + (local ?? prefix);
128
+ if (expanded.has(expandedName)) return false;
129
+ expanded.add(expandedName);
130
+ }
131
+ if (empty) restore(changes);
132
+ else open.push({ name, changes });
133
+ } else if (text[at] === '&') {
134
+ const ref = reference(text, at);
135
+ if (!ref) return false;
136
+ at += ref[0];
137
+ } else if (read(DATA).includes(']]>')) {
138
+ return false;
139
+ }
140
+ }
141
+ return open.length === 0;
142
+ }