From 30f9670a5f56ea6dc31d53475b90b989d087be9d Mon Sep 17 00:00:00 2001 From: Gijs Kruitbosch Date: Mon, 7 Jan 2019 12:45:14 +0100 Subject: [PATCH] Avoid setAttribute errors from invalid attributes, fixes #392 --- Readability.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Readability.js b/Readability.js index 9d84071..12e2de6 100644 --- a/Readability.js +++ b/Readability.js @@ -538,7 +538,16 @@ Readability.prototype = { replacement.readability = node.readability; for (var i = 0; i < node.attributes.length; i++) { - replacement.setAttribute(node.attributes[i].name, node.attributes[i].value); + try { + replacement.setAttribute(node.attributes[i].name, node.attributes[i].value); + } catch (ex) { + /* it's possible for setAttribute() to throw if the attribute name + * isn't a valid XML Name. Such attributes can however be parsed from + * source in HTML docs, see https://github.com/whatwg/html/issues/4275, + * so we can hit them here and then throw. We don't care about such + * attributes so we ignore them. + */ + } } return replacement; },