Skip to content

Commit a829024

Browse files
authored
Merge pull request #1341 from melonjs/fix/loader-parser-bugs
Fix bugs in loader parsers
2 parents fd72b1e + 96e5a6e commit a829024

File tree

4 files changed

+92
-9
lines changed

4 files changed

+92
-9
lines changed

packages/melonjs/src/loader/parsers/fontface.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import { fontList } from "../cache.js";
88
* @returns {number} the amount of corresponding resource parsed/preloaded
99
* @ignore
1010
* @example
11-
* preloadFontFace(
12-
* name: "'kenpixel'", type: "fontface", src: "data/font/kenvector_future.woff2"
11+
* preloadFontFace([
12+
* { name: "'kenpixel'", type: "fontface", src: "data/font/kenvector_future.woff2" }
1313
* ]);
1414
*/
1515
export function preloadFontFace(data, onload, onerror) {
@@ -39,10 +39,10 @@ export function preloadFontFace(data, onload, onerror) {
3939
onload();
4040
}
4141
},
42-
() => {
42+
(error) => {
4343
// rejected
4444
if (typeof onerror === "function") {
45-
onerror(data.name);
45+
onerror(error);
4646
}
4747
},
4848
);

packages/melonjs/src/loader/parsers/script.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export function preloadJavascript(data, onload, onerror, settings) {
1212

1313
script.src = data.src;
1414
script.type = "text/javascript";
15-
if (typeof crossOrigin === "string") {
15+
if (typeof settings.crossOrigin === "string") {
1616
script.crossOrigin = settings.crossOrigin;
1717
}
1818
script.defer = true;

packages/melonjs/src/loader/parsers/video.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { hasVideoFormat } from "../../system/device.js";
22
import * as fileUtil from "../../utils/file.ts";
3-
import { isDataUrl } from "./../../utils/string.ts";
3+
import { isDataUrl } from "../../utils/string.ts";
44
import { videoList } from "../cache.js";
55
import { fetchData } from "./fetchdata.js";
66

@@ -23,7 +23,8 @@ export function preloadVideo(data, onload, onerror, settings) {
2323
globalThis.document.createElement("video"));
2424

2525
if (isDataUrl(data.src)) {
26-
const mimeType = data.src.match(/[^:]\w+\/[\w-+\d.]+(?=;|,)/)[0];
26+
const mimeMatch = data.src.match(/[^:]\w+\/[\w-+\d.]+(?=;|,)/);
27+
const mimeType = mimeMatch ? mimeMatch[0] : null;
2728
if (!mimeType || videoElement.canPlayType(mimeType) === "") {
2829
throw new Error(
2930
`Invalid dataURL or Video file format not supported: ${mimeType}`,
@@ -86,8 +87,8 @@ export function preloadVideo(data, onload, onerror, settings) {
8687
}
8788

8889
if (typeof onerror === "function") {
89-
videoElement.onerror = () => {
90-
onerror();
90+
videoElement.onerror = (error) => {
91+
onerror(error);
9192
};
9293
}
9394

packages/melonjs/tests/loader.spec.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,4 +393,86 @@ describe("loader", () => {
393393
expect(loader.unload({ name: "nope", type: "tmx" })).toBe(false);
394394
expect(loader.unload({ name: "nope", type: "video" })).toBe(false);
395395
});
396+
397+
it("should call onerror for invalid image sources", async () => {
398+
await expect(
399+
new Promise((resolve, reject) => {
400+
loader.load(
401+
{
402+
name: "bad_image",
403+
type: "image",
404+
src: "nonexistent/path/image.png",
405+
},
406+
() => {
407+
reject(new Error("should not succeed"));
408+
},
409+
() => {
410+
resolve(true);
411+
},
412+
);
413+
}),
414+
).resolves.toBe(true);
415+
});
416+
417+
it("should call onerror for invalid JSON sources", async () => {
418+
await expect(
419+
new Promise((resolve, reject) => {
420+
loader.load(
421+
{
422+
name: "bad_json",
423+
type: "json",
424+
src: "nonexistent/path/data.json",
425+
},
426+
() => {
427+
reject(new Error("should not succeed"));
428+
},
429+
() => {
430+
resolve(true);
431+
},
432+
);
433+
}),
434+
).resolves.toBe(true);
435+
});
436+
437+
it("should call onerror for invalid binary sources", async () => {
438+
await expect(
439+
new Promise((resolve, reject) => {
440+
loader.load(
441+
{
442+
name: "bad_binary",
443+
type: "binary",
444+
src: "nonexistent/path/data.bin",
445+
},
446+
() => {
447+
reject(new Error("should not succeed"));
448+
},
449+
() => {
450+
resolve(true);
451+
},
452+
);
453+
}),
454+
).resolves.toBe(true);
455+
});
456+
457+
it("should handle inline TMX data and return 1", () => {
458+
const result = loader.load(
459+
{
460+
name: "inline_tmx",
461+
type: "tmx",
462+
data: {
463+
width: 1,
464+
height: 1,
465+
tilewidth: 32,
466+
tileheight: 32,
467+
orientation: "orthogonal",
468+
renderorder: "right-down",
469+
version: "1.10",
470+
layers: [],
471+
tilesets: [],
472+
},
473+
},
474+
() => {},
475+
);
476+
expect(result).toBe(1);
477+
});
396478
});

0 commit comments

Comments
 (0)