Skip to content

Commit 59ebd7e

Browse files
committed
Use modern pathToFileURL in tests
Also delete some unneeded utils.
1 parent 1ee4d61 commit 59ebd7e

File tree

3 files changed

+5
-49
lines changed

3 files changed

+5
-49
lines changed

lib/jsdom/utils.js

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,8 @@
11
"use strict";
2-
const path = require("path");
32
const whatwgURL = require("whatwg-url");
43
const { domSymbolTree } = require("./living/helpers/internal-constants");
54
const SYMBOL_TREE_POSITION = require("symbol-tree").TreePosition;
65

7-
exports.toFileUrl = function (fileName) {
8-
// Beyond just the `path.resolve`, this is mostly for the benefit of Windows,
9-
// where we need to convert "\" to "/" and add an extra "/" prefix before the
10-
// drive letter.
11-
let pathname = path.resolve(process.cwd(), fileName).replace(/\\/g, "/");
12-
if (pathname[0] !== "/") {
13-
pathname = "/" + pathname;
14-
}
15-
16-
// path might contain spaces, so convert those to %20
17-
return "file://" + encodeURI(pathname);
18-
};
19-
206
/**
217
* Define a set of properties on an object, by copying the property descriptors
228
* from the original object.
@@ -31,29 +17,6 @@ exports.define = function define(object, properties) {
3117
}
3218
};
3319

34-
/**
35-
* Define a list of constants on a constructor and its .prototype
36-
*
37-
* - `Constructor` {Function} the constructor to define the constants on
38-
* - `propertyMap` {Object} key/value map of properties to define
39-
*/
40-
exports.addConstants = function addConstants(Constructor, propertyMap) {
41-
for (const property in propertyMap) {
42-
const value = propertyMap[property];
43-
addConstant(Constructor, property, value);
44-
addConstant(Constructor.prototype, property, value);
45-
}
46-
};
47-
48-
function addConstant(object, property, value) {
49-
Object.defineProperty(object, property, {
50-
configurable: false,
51-
enumerable: true,
52-
writable: false,
53-
value
54-
});
55-
}
56-
5720
exports.mixin = (target, source) => {
5821
const keys = Reflect.ownKeys(source);
5922
for (let i = 0; i < keys.length; ++i) {

test/api/from-file.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
const path = require("path");
33
const { assert } = require("chai");
44
const { describe, it } = require("mocha-sugar-free");
5+
const { pathToFileURL } = require("url");
56

67
const { JSDOM } = require("../..");
78

@@ -81,12 +82,7 @@ describe("API: JSDOM.fromFile()", () => {
8182
});
8283

8384
describe("url option defaulting", () => {
84-
// Manually construct it as much as possible to avoid logic in the tests
85-
let pathWithLeadingSlash = path.resolve(__dirname);
86-
if (!pathWithLeadingSlash.startsWith("/")) {
87-
pathWithLeadingSlash = "/" + pathWithLeadingSlash;
88-
}
89-
const testURL = "file://" + pathWithLeadingSlash.replace(/\\/g, "/") + "/fixtures/from-file/test.html";
85+
const testURL = pathToFileURL(path.resolve(__dirname, "fixtures/from-file/test.html")).href;
9086

9187
it("should default to a file URL derived from the filename", () => {
9288
return fromFixtureFile("test.html").then(dom => {

test/util.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const https = require("https");
66
const enableDestroy = require("server-destroy");
77
const { JSDOM } = require("..");
88
const { Canvas } = require("../lib/jsdom/utils");
9+
const { pathToFileURL } = require("url");
910

1011
function toPathname(dirname, relativePath) {
1112
let pathname = path.resolve(dirname, relativePath).replace(/\\/g, "/");
@@ -15,13 +16,9 @@ function toPathname(dirname, relativePath) {
1516
return pathname;
1617
}
1718

18-
function toFileUrl(dirname, relativePath) {
19-
return "file://" + toPathname(dirname, relativePath);
20-
}
21-
2219
exports.toFileUrl = dirname => {
2320
return function (relativePath) {
24-
return toFileUrl(dirname, relativePath);
21+
return pathToFileURL(path.resolve(dirname, relativePath)).href;
2522
};
2623
};
2724

@@ -38,7 +35,7 @@ exports.load = dirname => {
3835
const file = path.resolve(dirname, "files/" + name + ".html");
3936

4037
if (!options.url) {
41-
options.url = toFileUrl(dirname, file);
38+
options.url = pathToFileURL(path.resolve(dirname, file)).href;
4239
}
4340

4441
const contents = fileCache[file] || fs.readFileSync(file, "utf8");

0 commit comments

Comments
 (0)