diff --git a/src/js/misc.js b/src/js/misc.js index 9c86cec..3bbe75b 100644 --- a/src/js/misc.js +++ b/src/js/misc.js @@ -18,23 +18,28 @@ if (header_links) { ); } } - -function getHeightWidth(alt) { +/** + * + * @param {string} alt + * @param {number} originalWidth + * @param {number} originalHeight + * @returns number[] + */ +function getHeightWidth(alt, originalWidth, originalHeight) { const heightXwidthReg = new RegExp("\\d+x\\d+"); const widthReg = new RegExp("\\d+"); if (alt.match(heightXwidthReg)) { - var width = parseInt(alt.split("x")[0]); - var height = parseInt(alt.split("x")[1]); + const width = parseInt(alt.split("x")[0]); + const height = parseInt(alt.split("x")[1]); return [width, height]; } else if (alt.match(widthReg)) { - var width = parseInt(alt.match(widthReg)[0]); - return [width, 0]; + const width = parseInt(alt.match(widthReg)[0]); + return [width, originalHeight]; } else { - return [0, 0]; + return [originalWidth, originalHeight]; } } -const p_img = /\.+\\/gi; for (const i of document.querySelectorAll("img")) { const regAlt = new RegExp("\\|"); const alt = i.alt; @@ -42,18 +47,13 @@ for (const i of document.querySelectorAll("img")) { const altSplitted = alt.split("|"); for (const part of altSplitted) { if (part.match(new RegExp("\\d+", "g"))) { - var size = getHeightWidth(part); + var size = getHeightWidth(part, i.width, i.height); i.width = size[0] > 0 ? size[0] : i.width; i.height = size[1] > 0 ? size[1] : i.height; var partReg = new RegExp(`\\${part}`); i.alt = alt.replace(partReg, ""); } } - } else if (alt.match(/\d+/g)) { - var size = getHeightWidth(alt); - i.width = size[0] > 0 ? size[0] : i.width; - i.height = size[1] > 0 ? size[1] : i.height; - i.alt = alt.replace(p_img, ""); } }