File size: 6,266 Bytes
e81f4e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
export type XmlTagInfo = {
	name: string;
	closing: boolean;
	selfClosing: boolean;
	start: number;
	end: number;
	attrs: string;
};

export type XmlElementBlock = {
	name: string;
	attrs: string;
	body: string;
	start: number;
	end: number;
};

const XML_TAG_NAME_RE = /[A-Za-z_][A-Za-z0-9_:-]*/y;

export function decodeCDATA(text: unknown): string {
	const raw = String(text || "");
	const closed = raw.replace(
		/<!\[CDATA\[([\s\S]*?)]]>/g,
		(_m, body: string) => body,
	);
	if (closed !== raw) return closed;
	if (raw.startsWith("<![CDATA[")) return raw.slice("<![CDATA[".length);
	return raw;
}

export function decodeXmlEntities(text: unknown): string {
	return String(text || "")
		.replace(/&quot;/g, '"')
		.replace(/&apos;/g, "'")
		.replace(/&lt;/g, "<")
		.replace(/&gt;/g, ">")
		.replace(/&amp;/g, "&");
}

export function appendMarkupValue(
	obj: Record<string, unknown>,
	key: string,
	value: unknown,
): void {
	if (Object.hasOwn(obj, key)) {
		if (Array.isArray(obj[key])) obj[key].push(value);
		else obj[key] = [obj[key], value];
	} else {
		obj[key] = value;
	}
}

export function parseTagAttributes(attrs: unknown): Record<string, string> {
	const out: Record<string, string> = {};
	const re = /\b([a-z0-9_:-]+)\s*=\s*("([^"]*)"|'([^']*)')/gi;
	const source = String(attrs || "");
	let m: RegExpExecArray | null = re.exec(source);
	while (m !== null) {
		const key = m[1];
		if (key) out[key] = decodeXmlEntities(m[3] != null ? m[3] : m[4] || "");
		m = re.exec(source);
	}
	const bare = /\b([a-z0-9_:-]+)\s*=\s*([^\s"'=<>`]+)/gi;
	m = bare.exec(source);
	while (m !== null) {
		const key = m[1];
		if (key && !(key in out)) out[key] = decodeXmlEntities(m[2] || "");
		m = bare.exec(source);
	}
	return out;
}

export function findXmlElementBlocks(
	text: unknown,
	tag: unknown,
): XmlElementBlock[] {
	const source = String(text || "");
	const name = String(tag || "").toLowerCase();
	const out: XmlElementBlock[] = [];
	let pos = 0;
	while (pos < source.length) {
		const start = findNextXmlTag(source, name, pos, false);
		if (!start) break;
		let depth = 1;
		let seek = start.end + 1;
		let end: XmlTagInfo | null = null;
		while (seek < source.length) {
			const next = findNextXmlTag(source, name, seek, null);
			if (!next) break;
			if (next.selfClosing) {
				seek = next.end + 1;
				continue;
			}
			if (next.closing) depth -= 1;
			else depth += 1;
			if (depth === 0) {
				end = next;
				break;
			}
			seek = next.end + 1;
		}
		if (!end) {
			pos = start.end + 1;
			continue;
		}
		out.push({
			name,
			attrs: start.attrs,
			body: source.slice(start.end + 1, end.start),
			start: start.start,
			end: end.end + 1,
		});
		pos = end.end + 1;
	}
	return out;
}

export function findTopLevelXmlElementBlocks(text: unknown): XmlElementBlock[] {
	const source = String(text || "");
	const out: XmlElementBlock[] = [];
	let pos = 0;
	while (pos < source.length) {
		const start = findNextAnyXmlTag(source, pos, false);
		if (!start) break;
		if (start.start > pos && source.slice(pos, start.start).trim()) break;
		if (start.selfClosing) {
			out.push({
				name: start.name,
				attrs: start.attrs,
				body: "",
				start: start.start,
				end: start.end + 1,
			});
			pos = start.end + 1;
			continue;
		}
		let depth = 1;
		let seek = start.end + 1;
		let end: XmlTagInfo | null = null;
		while (seek < source.length) {
			const next = findNextXmlTag(source, start.name, seek, null);
			if (!next) break;
			if (next.selfClosing) {
				seek = next.end + 1;
				continue;
			}
			if (next.closing) depth -= 1;
			else depth += 1;
			if (depth === 0) {
				end = next;
				break;
			}
			seek = next.end + 1;
		}
		if (!end) break;
		out.push({
			name: start.name,
			attrs: start.attrs,
			body: source.slice(start.end + 1, end.start),
			start: start.start,
			end: end.end + 1,
		});
		pos = end.end + 1;
	}
	if (pos < source.length && source.slice(pos).trim()) return [];
	return out;
}

export function findNextXmlTag(
	text: string,
	tag: unknown,
	from: number,
	closing: boolean | null,
): XmlTagInfo | null {
	const wanted = String(tag || "").toLowerCase();
	for (let i = Math.max(0, from || 0); i < text.length; ) {
		i = text.indexOf("<", i);
		if (i < 0) return null;
		const cdataEnd = skipCDATAAt(text, i);
		if (cdataEnd > i) {
			i = cdataEnd;
			continue;
		}
		const tagInfo = scanXmlTagAt(text, i);
		if (
			tagInfo &&
			tagInfo.name === wanted &&
			(closing === null || tagInfo.closing === closing)
		)
			return tagInfo;
		i += 1;
	}
	return null;
}

export function findNextAnyXmlTag(
	text: string,
	from: number,
	closing: boolean | null,
): XmlTagInfo | null {
	for (let i = Math.max(0, from || 0); i < text.length; ) {
		i = text.indexOf("<", i);
		if (i < 0) return null;
		const cdataEnd = skipCDATAAt(text, i);
		if (cdataEnd > i) {
			i = cdataEnd;
			continue;
		}
		const tagInfo = scanXmlTagAt(text, i);
		if (tagInfo && (closing === null || tagInfo.closing === closing))
			return tagInfo;
		i += 1;
	}
	return null;
}

export function skipCDATAAt(text: string, i: number): number {
	if (!text.startsWith("<![CDATA[", i)) return i;
	const end = text.indexOf("]]>", i + 9);
	return end < 0 ? i : end + 3;
}

export function scanXmlTagAt(text: string, i: number): XmlTagInfo | null {
	if (text[i] !== "<") return null;
	let p = i + 1;
	let closing = false;
	if (text[p] === "/") {
		closing = true;
		p += 1;
	}
	XML_TAG_NAME_RE.lastIndex = p;
	const m = XML_TAG_NAME_RE.exec(text);
	if (!m) return null;
	const name = m[0].toLowerCase();
	p += m[0].length;
	const nextChar = text[p];
	if (p < text.length && (nextChar === undefined || !/[\s/>]/.test(nextChar)))
		return null;
	const end = findXmlTagEnd(text, p);
	if (end < 0) return null;
	const attrsEnd = text[end - 1] === "/" ? end - 1 : end;
	return {
		name,
		closing,
		selfClosing: !closing && text[end - 1] === "/",
		start: i,
		end,
		attrs: text.slice(p, attrsEnd),
	};
}

export function findXmlTagEnd(text: string, from: number): number {
	let quote = "";
	for (let i = Math.max(0, from || 0); i < text.length; i++) {
		const ch = text[i];
		if (quote) {
			if (ch === quote) quote = "";
			continue;
		}
		if (ch === '"' || ch === "'") {
			quote = ch;
			continue;
		}
		if (ch === ">") return i;
	}
	return -1;
}