Skip to content

Commit

Permalink
Validate MusicXML in parseMusicXML() and update demo to better reject…
Browse files Browse the repository at this point in the history
… invalid files
  • Loading branch information
infojunkie committed Jan 16, 2024
1 parent 81380da commit bd3c0d0
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 110 deletions.
7 changes: 4 additions & 3 deletions demo/demo.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -322,13 +322,14 @@ function handleSheetSelect(e) {
}

async function handleFileBuffer(buffer) {
const parseResult = await MusicXMLPlayer.parseMusicXML(buffer);
if (parseResult) {
try {
const parseResult = await MusicXMLPlayer.parseMusicXML(buffer);
console.log(parseResult);
g_state.musicXml = parseResult.musicXml;
g_state.params.delete('sheet');
createPlayer();
}
else {
catch (error) {
try {
const ireal = new TextDecoder().decode(buffer);
populateSheets(ireal);
Expand Down
15 changes: 10 additions & 5 deletions dist/musicxml-player.esm.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* musicxml-player v0.14.0
* musicxml-player v0.15.0
* (c) Karim Ratib <[email protected]> (https://github.com/infojunkie)
* Released under the GPL-3.0-only License.
*/
Expand Down Expand Up @@ -5844,8 +5844,9 @@ function _parseCompressedMusicXML(mxml, queries) {
text: decoder.decode(containerBuf),
});
const rootFile = SaxonJS2_rtExports.XPath.evaluate('//rootfile[1]/@full-path', doc);
if (!rootFile)
throw new Error('Invalid compressed MusicXML file does not contain rootfile/@full-path.');
if (!rootFile) {
throw new Error('[parseMusicXML] Invalid compressed MusicXML file does not contain rootfile/@full-path.');
}
// Parse root document as MusicXML.
const rootBuf = yield entries[rootFile.value].arrayBuffer();
return _parseUncompressedMusicXML(decoder.decode(rootBuf), queries);
Expand All @@ -5859,7 +5860,11 @@ function _parseUncompressedMusicXML(musicXml, queries) {
encoding: 'utf8',
text: musicXml,
});
const version = (_a = SaxonJS2_rtExports.XPath.evaluate('//score-partwise/@version', doc)) !== null && _a !== void 0 ? _a : {
const valid = SaxonJS2_rtExports.XPath.evaluate('boolean(//score-partwise | //score-timewise)', doc);
if (!valid) {
throw new Error('[parseMusicXML] Invalid MusicXML file contains neither score-partwise nor score-timewise.');
}
const version = (_a = SaxonJS2_rtExports.XPath.evaluate('//score-partwise/@version | //score-timewise/@version', doc)) !== null && _a !== void 0 ? _a : {
value: '(unknown)',
};
console.debug(`[parseMusicXML] MusicXML version ${version.value}`);
Expand Down Expand Up @@ -13795,7 +13800,7 @@ class WebAudioFontOutput {
}

var name = "musicxml-player";
var version = "0.14.0";
var version = "0.15.0";
var description = "A simple JavaScript component that loads and plays MusicXML files in the browser using Web Audio and Web MIDI.";
var main = "dist/musicxml-player.esm.js";
var type = "module";
Expand Down
2 changes: 1 addition & 1 deletion dist/musicxml-player.esm.js.map

Large diffs are not rendered by default.

194 changes: 97 additions & 97 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "musicxml-player",
"version": "0.14.0",
"version": "0.15.0",
"description": "A simple JavaScript component that loads and plays MusicXML files in the browser using Web Audio and Web MIDI.",
"main": "dist/musicxml-player.esm.js",
"type": "module",
Expand Down
Loading

0 comments on commit bd3c0d0

Please sign in to comment.