File size: 5,985 Bytes
d605f27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

import {DOMParser, XMLSerializer} from "xmldom";

import * as domUtils from "../inc/domUtils";



const isWordDirection = direction => {
	const directionTypes = domUtils.childrenWithTag(direction, "direction-type");

	for (const dt of directionTypes) {
		if (domUtils.hasChildrenWithTag(dt, "words"))
			return true;
	}

	return false;
};


const moveWordDirection = measure => {
	//console.log("measure:", measure);
	const words = [];

	for (let i = 0; i < measure.childNodes.length; ++i) {
		const child = measure.childNodes[i];
		switch (child.tagName) {
		case "direction":
			if (isWordDirection(child)) {
				words.push(child);
				measure.removeChild(child);
			}

			break;
		case "note":
			const next = measure.childNodes[i + 1];
			words.forEach(word => {
				if (next)
					measure.insertBefore(word, next);
				else
					measure.appendChild(word);
			});

			return;
		}
	}
};


const preprocessXml = (xml: string, {
	removeMeasureImplicit = true,
	replaceEncoding = true,
	removeNullDynamics = true,
	fixHeadMarkup = true,
	fixBackSlashes = true,
	roundTempo = true,
	escapedWordsDoubleQuotation = true,
	removeTrivialRests = true,
	removeBadMetronome = true,
	removeInvalidHarmonies = true,
	removeAllHarmonies = false,
	fixChordVoice = true,
	fixBarlines = true,
	removeInvalidClef = true,
} = {}): string => {
	if (!removeMeasureImplicit && !replaceEncoding && !removeNullDynamics && !fixHeadMarkup && !fixBackSlashes && !roundTempo
		&& !escapedWordsDoubleQuotation && !removeTrivialRests && !removeBadMetronome && !removeInvalidHarmonies && !removeAllHarmonies
		&& !fixChordVoice && !fixBarlines)
		return xml;

	// @ts-ignore
	const dom = new DOMParser().parseFromString(xml, "text/xml");

	if (replaceEncoding) {
		const headNode = Array.prototype.find.call(dom.childNodes, node => node.tagName === "xml");
		if (headNode)
			headNode.data = headNode.data.replace(/UTF-16/, "UTF-8");
	}

	const needTraverse = removeMeasureImplicit || removeNullDynamics || fixHeadMarkup || fixBackSlashes || roundTempo
		|| escapedWordsDoubleQuotation || removeTrivialRests || removeBadMetronome || removeInvalidHarmonies || removeAllHarmonies
		|| fixChordVoice || fixBarlines;
	if (needTraverse) {
		domUtils.traverse(dom, node => {
			if (removeMeasureImplicit) {
				if (node.tagName === "measure")
					node.removeAttribute("implicit");
			}

			if (removeNullDynamics) {
				if (node.tagName === "other-dynamics") {
					const content = node.textContent;
					if (!/\w/.test(content))
						node.parentNode.removeChild(node);
				}
			}

			if (fixHeadMarkup) {
				if (node.tagName === "measure") {
					//console.log("measure:", node);
					moveWordDirection(node);
				}
			}

			if (fixBackSlashes) {
				if (["words", "credit-words", "text"].includes(node.tagName)) {
					if (/^\\+/.test(node.textContent) || /\\+$/.test(node.textContent)) {
						console.debug("replaced invalid text:", node.textContent);
						node.textContent = node.textContent.replace(/^\\+/, "").replace(/\\+$/, "");
					}
				}
			}

			if (roundTempo) {
				if (node.tagName === "sound") {
					const tempo = Number(node.getAttribute("tempo"));
					if (Number.isFinite(tempo) && Math.floor(tempo) !== tempo)
						node.setAttribute("tempo", Math.round(tempo).toFixed(0));
				}
			}

			if (escapedWordsDoubleQuotation) {
				if (["words", "credit-words", "text"].includes(node.tagName)) {
					if (node.textContent && /"/.test(node.textContent))
						node.textContent = node.textContent.replace(/"/g, "'");
				}
			}

			if (removeTrivialRests) {
				if (node.tagName === "note") {
					if (domUtils.hasChildrenWithTag(node, "rest") && !domUtils.hasChildrenWithTag(node, "type")) {
						// append an empty tag: <type></type>
						const type = dom.createElement("type");
						node.appendChild(type);
					}
				}
			}

			if (removeBadMetronome) {
				if (node.tagName === "metronome") {
					if (!domUtils.hasChildrenWithTag(node, "per-minute")) {
						console.debug("metronome without 'per-minute' removed:", node.toString());
						node.parentNode.removeChild(node);
					}
				}
			}

			if (removeInvalidHarmonies || removeAllHarmonies) {
				if (node.tagName === "harmony") {
					if (removeAllHarmonies)
						node.parentNode.removeChild(node);
					else {
						let next = node.nextSibling;
						while (next && !next.tagName)
							next = next.nextSibling;
	
						//console.log("sibling:", next);
						if (!next || next.tagName !== "note") {
							node.parentNode.removeChild(node);
							console.debug("invalid harmony removed:", next && next.tagName);
						}
					}
				}
			}

			if (fixChordVoice) {
				if (node.tagName === "note" && domUtils.hasChildrenWithTag(node, "chord") && !domUtils.hasChildrenWithTag(node, "voice")) {
					//console.log("bad note:", node);
					const previousNote = domUtils.findPreviousSibling(node, "note");
					if (previousNote) {
						const voice = domUtils.childrenWithTag(previousNote, "voice")[0];
						//console.log("chord voice:", node, voice);
						if (voice)
							node.appendChild(voice.cloneNode(true));
					}
				}
			}

			if (fixBarlines) {
				if (node.tagName === "barline") {
					if ((node.getAttribute("location") === "right" || domUtils.hasChildrenWithTag(node, "bar-style"))
						&& domUtils.findNextSibling(node, "backup")) {
						// move the barline to the end of this measure
						node.parentNode.removeChild(node);
						node.parentNode.appendChild(node);
					}
				}
			}

			if(removeInvalidClef) {
				if (node.tagName === "attributes") {
					const clef = domUtils.childrenWithTag(node, "clef")[0];
					if (clef) {
						const n = Number(clef.getAttribute("number"));
						if (!domUtils.findPreviousSibling(node, "backup") && n > 1) {
							node.parentNode.removeChild(node);
							console.debug("invalid clef removed:", n);
						}
					}
				}
			}
		});
	}

	//console.log("dom:", dom);

	// @ts-ignore
	return new XMLSerializer().serializeToString(dom);
};



export {
	preprocessXml,
};