Skip to content

Commit 9741311

Browse files
committed
Fix loading of subresources with Unicode filenames
Closes #3016. Closes #3027.
1 parent 5e46553 commit 9741311

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed

lib/jsdom/browser/resources/resource-loader.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"use strict";
22
const fs = require("fs");
3+
const { fileURLToPath } = require("url");
34
const { parseURL } = require("whatwg-url");
45
const dataURLFromRecord = require("data-urls").fromURLRecord;
56
const request = require("request-promise-native");
@@ -107,13 +108,11 @@ module.exports = class ResourceLoader {
107108
}
108109

109110
case "file": {
110-
// TODO: Improve the URL => file algorithm. See https://github.com/jsdom/jsdom/pull/2279#discussion_r199977987
111-
const filePath = urlString
112-
.replace(/^file:\/\//, "")
113-
.replace(/^\/([a-z]):\//i, "$1:/")
114-
.replace(/%20/g, " ");
115-
116-
return this._readFile(filePath);
111+
try {
112+
return this._readFile(fileURLToPath(urlString));
113+
} catch (e) {
114+
return Promise.reject(e);
115+
}
117116
}
118117

119118
default: {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
p {
2+
color: red;
3+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<!DOCTYPE html>
2+
<meta charset="utf-8">
3+
<title>Unicode filenames test</title>
4+
<link rel="stylesheet" href="unicode-진 シーン-i 🥰 you.css">
5+
6+
<p>This should be red

test/api/from-file.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ describe("API: JSDOM.fromFile()", { skipIfBrowser: true }, () => {
2626
});
2727
});
2828

29+
it("should work even for Unicode main resource and subresource filenames (GH-3016)", async () => {
30+
const dom = await fromFixtureFile("unicode-진 シーン-i 🥰 you.html", { resources: "usable" });
31+
32+
await new Promise(resolve => {
33+
dom.window.onload = resolve;
34+
});
35+
36+
assert.strictEqual(dom.window.getComputedStyle(dom.window.document.querySelector("p")).color, "red");
37+
});
38+
2939
describe("contentType option defaulting", () => {
3040
it("should default to text/html Content-Type even with no file extension", () => {
3141
return fromFixtureFile("no-extension").then(dom => {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy