From d0aca3bfcc7b5cd37a9191c7d72d7aea58402db5 Mon Sep 17 00:00:00 2001 From: hanbollar Date: Thu, 16 May 2024 14:15:01 -0700 Subject: [PATCH 1/2] remove unnecessary 'relativepath' warning/error case Signed-off-by: hanbollar --- dist/mr.js | 4 ++-- src/utils/HTML.js | 23 ++++++----------------- 2 files changed, 8 insertions(+), 19 deletions(-) diff --git a/dist/mr.js b/dist/mr.js index 8d4bb28e..50e4b1b7 100644 --- a/dist/mr.js +++ b/dist/mr.js @@ -1003,7 +1003,7 @@ eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpac /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => { "use strict"; -eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ html: () => (/* binding */ html)\n/* harmony export */ });\n/**\n * @namespace html\n * @description Useful namespace for helping with html utility functions\n */\nlet html = {};\n\n/**\n * @function\n * @memberof html\n * @param {string} path - either a relative or full path inputted to an element. This can also be a path that has items separated by ',' so\n * that you can resolve multiple items at once, since we allow users to send us multiple files that way.\n * @param {string} baseUrl - a separate entry for if you want your url to start differently. this defaults to your window.location.origin.\n * Additionally removes all queries from the end of the url, leaving the input as just the origin and its pathname.\n * For ex: 'https://example.com/images/photo.png?version=2' becomes 'https://example.com/images/photo.png'\n * @description Given the path returns an absolute path resolved so relative linking works as expected.\n * @returns {string} a.href - the absolute path (or paths)\n */\nhtml.resolvePath = function (path, baseUrl) {\n const fixPath = (path, baseUrl) => {\n let a = document.createElement('a');\n a.href = html.removeUrlQueries(path, baseUrl);\n return a.href;\n };\n\n // multiple paths\n if (path.includes(',')) {\n let returnedPathStr = '';\n let pathList = path.split(',');\n for (let i = 0; i < pathList.length; ++i) {\n returnedPathStr += fixPath(pathList[i], baseUrl) + (i != pathList.length - 1 ? ',' : '');\n }\n return returnedPathStr;\n }\n\n // singular path\n return fixPath(path, baseUrl);\n};\n\n/**\n * @function\n * @memberof html\n * @param {string} path - either a relative or full path inputted to an element.\n * @param {string} baseUrl - a separate entry for if you want your url to start differently. this defaults to your window.location.origin.\n * @description Removes all queries from the end of the url, leaving the input as just the origin and its pathname.\n * For ex: 'https://example.com/images/photo.png?version=2' becomes 'https://example.com/images/photo.png'\n * @returns {string} a.href - the absolute path\n */\nhtml.removeUrlQueries = function (path, baseUrl) {\n try {\n // Check if path is absolute. If not, use baseUrl as the second parameter\n let urlObj;\n if (baseUrl) {\n urlObj = new URL(path, baseUrl);\n } else {\n urlObj = new URL(path);\n }\n let cleanUrl = urlObj.origin + urlObj.pathname;\n return cleanUrl;\n } catch (error) {\n console.warn('Error processing URL:', error.message);\n return path; // Return the original path if there's an error\n }\n};\n\n\n\n\n//# sourceURL=webpack://mrjs/./src/utils/HTML.js?"); +eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ html: () => (/* binding */ html)\n/* harmony export */ });\n/**\n * @namespace html\n * @description Useful namespace for helping with html utility functions\n */\nlet html = {};\n\n/**\n * @function\n * @memberof html\n * @param {string} path - either a relative or full path inputted to an element. This can also be a path that has items separated by ',' so\n * that you can resolve multiple items at once, since we allow users to send us multiple files that way.\n * @param {string} baseUrl - a separate entry for if you want your url to start differently. this defaults to your window.location.origin.\n * Additionally removes all queries from the end of the url, leaving the input as just the origin and its pathname.\n * For ex: 'https://example.com/images/photo.png?version=2' becomes 'https://example.com/images/photo.png'\n * @description Given the path returns an absolute path resolved so relative linking works as expected.\n * @returns {string} a.href - the absolute path (or paths)\n */\nhtml.resolvePath = function (path, baseUrl = window.location.origin) {\n const fixPath = (path, baseUrl) => {\n let a = document.createElement('a');\n a.href = html.removeUrlQueries(path, baseUrl);\n return a.href;\n };\n\n // Handle multiple paths separated by commas\n if (path.includes(',')) {\n return path.split(',').map(p => fixPath(p.trim())).join(',');\n }\n\n // singular path\n return fixPath(path, baseUrl);\n};\n\n/**\n * @function\n * @memberof html\n * @param {string} path - either a relative or full path inputted to an element.\n * @param {string} baseUrl - a separate entry for if you want your url to start differently. this defaults to your window.location.origin.\n * @description Removes all queries from the end of the url, leaving the input as just the origin and its pathname.\n * For ex: 'https://example.com/images/photo.png?version=2' becomes 'https://example.com/images/photo.png'\n * @returns {string} a.href - the absolute path\n */\nhtml.removeUrlQueries = function (path, baseUrl = window.location.origin) {\n try {\n // Check if path is absolute. If not, use baseUrl as the second parameter\n let urlObj = new URL(path, baseUrl);\n return urlObj.origin + urlObj.pathname;\n } catch (error) {\n console.warn('Error processing URL:', error.message);\n return path; // Return the original path if there's an error\n }\n};\n\n\n\n\n//# sourceURL=webpack://mrjs/./src/utils/HTML.js?"); /***/ }), @@ -1382,7 +1382,7 @@ eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpac /******/ var scripts = document.getElementsByTagName("script"); /******/ if(scripts.length) { /******/ var i = scripts.length - 1; -/******/ while (i > -1 && (!scriptUrl || !/^http(s?):/.test(scriptUrl))) scriptUrl = scripts[i--].src; +/******/ while (i > -1 && !scriptUrl) scriptUrl = scripts[i--].src; /******/ } /******/ } /******/ } diff --git a/src/utils/HTML.js b/src/utils/HTML.js index 891cb3bd..77b141ef 100644 --- a/src/utils/HTML.js +++ b/src/utils/HTML.js @@ -15,21 +15,16 @@ let html = {}; * @description Given the path returns an absolute path resolved so relative linking works as expected. * @returns {string} a.href - the absolute path (or paths) */ -html.resolvePath = function (path, baseUrl) { +html.resolvePath = function (path, baseUrl = window.location.origin) { const fixPath = (path, baseUrl) => { let a = document.createElement('a'); a.href = html.removeUrlQueries(path, baseUrl); return a.href; }; - // multiple paths + // Handle multiple paths separated by commas if (path.includes(',')) { - let returnedPathStr = ''; - let pathList = path.split(','); - for (let i = 0; i < pathList.length; ++i) { - returnedPathStr += fixPath(pathList[i], baseUrl) + (i != pathList.length - 1 ? ',' : ''); - } - return returnedPathStr; + return path.split(',').map(p => fixPath(p.trim())).join(','); } // singular path @@ -45,17 +40,11 @@ html.resolvePath = function (path, baseUrl) { * For ex: 'https://example.com/images/photo.png?version=2' becomes 'https://example.com/images/photo.png' * @returns {string} a.href - the absolute path */ -html.removeUrlQueries = function (path, baseUrl) { +html.removeUrlQueries = function (path, baseUrl = window.location.origin) { try { // Check if path is absolute. If not, use baseUrl as the second parameter - let urlObj; - if (baseUrl) { - urlObj = new URL(path, baseUrl); - } else { - urlObj = new URL(path); - } - let cleanUrl = urlObj.origin + urlObj.pathname; - return cleanUrl; + let urlObj = new URL(path, baseUrl); + return urlObj.origin + urlObj.pathname; } catch (error) { console.warn('Error processing URL:', error.message); return path; // Return the original path if there's an error From df3843a4bdb3e71afd1b97057dab9538c50073ec Mon Sep 17 00:00:00 2001 From: hanbollar Date: Thu, 16 May 2024 14:18:10 -0700 Subject: [PATCH 2/2] prettier-fix Signed-off-by: hanbollar --- src/utils/HTML.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/utils/HTML.js b/src/utils/HTML.js index 77b141ef..8955afb0 100644 --- a/src/utils/HTML.js +++ b/src/utils/HTML.js @@ -24,7 +24,10 @@ html.resolvePath = function (path, baseUrl = window.location.origin) { // Handle multiple paths separated by commas if (path.includes(',')) { - return path.split(',').map(p => fixPath(p.trim())).join(','); + return path + .split(',') + .map((p) => fixPath(p.trim())) + .join(','); } // singular path