From 7c91bdd2753adabf801beb5063db6693c54e4e33 Mon Sep 17 00:00:00 2001 From: PalmerAL Date: Wed, 26 Feb 2020 21:20:44 -0600 Subject: [PATCH] preserve children when removing javascript: links --- Readability.js | 16 +++++++++++++--- .../js-link-replacement/expected-metadata.json | 7 +++++++ .../test-pages/js-link-replacement/expected.html | 7 +++++++ test/test-pages/js-link-replacement/source.html | 14 ++++++++++++++ 4 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 test/test-pages/js-link-replacement/expected-metadata.json create mode 100644 test/test-pages/js-link-replacement/expected.html create mode 100644 test/test-pages/js-link-replacement/source.html diff --git a/Readability.js b/Readability.js index b6abe20..3ef723c 100644 --- a/Readability.js +++ b/Readability.js @@ -332,11 +332,21 @@ Readability.prototype = { this._forEachNode(links, function(link) { var href = link.getAttribute("href"); if (href) { - // Replace links with javascript: URIs with text content, since + // Remove links with javascript: URIs, since // they won't work after scripts have been removed from the page. if (href.indexOf("javascript:") === 0) { - var text = this._doc.createTextNode(link.textContent); - link.parentNode.replaceChild(text, link); + // if the link only contains simple text content, it can be converted to a text node + if (link.childNodes.length === 1 && link.childNodes[0].nodeType === this.TEXT_NODE) { + var text = this._doc.createTextNode(link.textContent); + link.parentNode.replaceChild(text, link); + } else { + // if the link has multiple children, they should all be preserved + var container = this._doc.createElement('span') + while (link.childNodes.length > 0) { + container.appendChild(link.childNodes[0]) + } + link.parentNode.replaceChild(container, link); + } } else { link.setAttribute("href", toAbsoluteURI(href)); } diff --git a/test/test-pages/js-link-replacement/expected-metadata.json b/test/test-pages/js-link-replacement/expected-metadata.json new file mode 100644 index 0000000..e9eb7c5 --- /dev/null +++ b/test/test-pages/js-link-replacement/expected-metadata.json @@ -0,0 +1,7 @@ +{ + "title": "Replace javascript: links", + "byline": null, + "excerpt": "abc", + "siteName": null, + "readerable": false +} diff --git a/test/test-pages/js-link-replacement/expected.html b/test/test-pages/js-link-replacement/expected.html new file mode 100644 index 0000000..4a53dd2 --- /dev/null +++ b/test/test-pages/js-link-replacement/expected.html @@ -0,0 +1,7 @@ +
+ +

abc

+

def

+ ghi +
+
\ No newline at end of file diff --git a/test/test-pages/js-link-replacement/source.html b/test/test-pages/js-link-replacement/source.html new file mode 100644 index 0000000..ee63fc8 --- /dev/null +++ b/test/test-pages/js-link-replacement/source.html @@ -0,0 +1,14 @@ + + + + + Replace javascript: links + + + +

abc

+

def

+ ghi +
+ + \ No newline at end of file