From 612822f5a3363f0345e8d7a11dc90e09bef70de7 Mon Sep 17 00:00:00 2001 From: Jackson Harper Date: Fri, 18 Feb 2022 14:18:33 -0800 Subject: [PATCH] Handle non-number size attributes in images The previous code assumed we would always have number size values for width and height, but attributes like 100% are valid. In cases where we don't have a numeric value we can just fallback and let the item be sized by the reader CSS. --- packages/readabilityjs/Readability.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/readabilityjs/Readability.js b/packages/readabilityjs/Readability.js index 4398d59c6..9a2d75a04 100644 --- a/packages/readabilityjs/Readability.js +++ b/packages/readabilityjs/Readability.js @@ -512,11 +512,17 @@ Readability.prototype = { if (src) { const absoluteSrc = this.toAbsoluteURI(src); + const parseNumber = (str) => { + if (!str) { return 0; } + const res = parseNumber(str); + if (isNaN(res)) { return 0; } + return res + } - const width = image.getAttribute('width') || image.style.width; - const height = image.getAttribute('height') || image.style.height; + const width = parseNumber(image.getAttribute('width') || image.style.width); + const height = parseNumber(image.getAttribute('height') || image.style.height); - const proxySrc = this.createImageProxyUrl(absoluteSrc, width || 0, height || 0); + const proxySrc = this.createImageProxyUrl(absoluteSrc, width, height); image.setAttribute('src', proxySrc); } });