From 3288ba991f0978c19487e8976e3b6970bb5f409d Mon Sep 17 00:00:00 2001 From: Viacheslav Turovskyi Date: Tue, 19 Jul 2022 06:37:57 +0300 Subject: [PATCH] feat: parseFromUrl does not resolve relative references (#504) --- lib/parser.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/parser.js b/lib/parser.js index f756fcae9..141c420bd 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -133,10 +133,20 @@ function parseFromUrl(url, fetchOptions, options) { //To not break the API by changing argument position and to silet the linter it is just better to move adding if (!fetchOptions) fetchOptions = {}; + // When executed in multiple async calls, 'checkErrorWrapper' passes URL + // string as an object by reference. In this case 'String.replace()' method + // mutates original URL string causing test with multiple async calls to fail. + // Thus completely independent object containing URL string is created for + // future manipulation. + const urlCopy = new String(url); + + const indexLastSlashPosition = urlCopy.lastIndexOf('/'); + const baseUrl = urlCopy.substring(0, indexLastSlashPosition + 1); + return new Promise((resolve, reject) => { fetch(url, fetchOptions) .then(res => res.text()) - .then(doc => parse(doc, options)) + .then(doc => parse(doc, { path: baseUrl })) .then(result => resolve(result)) .catch(e => { if (e instanceof ParserError) return reject(e);