diff --git a/Readability.js b/Readability.js index 13fde74..639333d 100644 --- a/Readability.js +++ b/Readability.js @@ -1361,9 +1361,32 @@ Readability.prototype = { if (!this._isSingleImage(tmp)) return; // If noscript has previous sibling and it only contains image, - // replace it with noscript content. + // replace it with noscript content. However, in some case there + // is possibility that img inside noscript has lower quality + // than the one in previous sibling, so we will keep the src + // and srcset attribute from old img as data attribute for + // img from noscript. var prevElement = noscript.previousElementSibling; if (prevElement && this._isSingleImage(prevElement)) { + var prevImg = prevElement; + if (prevImg.tagName !== "IMG") { + prevImg = prevElement.getElementsByTagName("img")[0]; + } + + var newImg = tmp.getElementsByTagName("img")[0]; + var newImgSrc = newImg.getAttribute("src"); + var newImgSrcset = newImg.getAttribute("srcset"); + var prevImgSrc = prevImg.getAttribute("src"); + var prevImgSrcset = prevImg.getAttribute("srcset"); + + if (prevImgSrc && prevImgSrc !== newImgSrc) { + newImg.setAttribute("data-old-src", prevImgSrc); + } + + if (prevImgSrcset && prevImgSrcset !== newImgSrcset) { + newImg.setAttribute("data-old-srcset", prevImgSrcset); + } + noscript.parentNode.replaceChild(tmp.children[0], prevElement); } }); diff --git a/test/test-pages/citylab-1/expected.html b/test/test-pages/citylab-1/expected.html index 0d387e3..4b32807 100644 --- a/test/test-pages/citylab-1/expected.html +++ b/test/test-pages/citylab-1/expected.html @@ -12,7 +12,7 @@ - +
The Moulin Rouge cabaret in Paris Benoit Tessier/Reuters
diff --git a/test/test-pages/lazy-image-1/expected.html b/test/test-pages/lazy-image-1/expected.html index cc9061e..24895f7 100644 --- a/test/test-pages/lazy-image-1/expected.html +++ b/test/test-pages/lazy-image-1/expected.html @@ -19,7 +19,7 @@
-

+

@@ -36,7 +36,7 @@
-

+

@@ -57,7 +57,7 @@
-

+

@@ -71,7 +71,7 @@
-

+

@@ -85,7 +85,7 @@
-

+

@@ -134,7 +134,7 @@
-

+

@@ -179,7 +179,7 @@
-

+

@@ -196,7 +196,7 @@
-

+

diff --git a/test/test-pages/seattletimes-1/expected.html b/test/test-pages/seattletimes-1/expected.html index b8aec32..6af8fa3 100644 --- a/test/test-pages/seattletimes-1/expected.html +++ b/test/test-pages/seattletimes-1/expected.html @@ -24,7 +24,7 @@

As it tries to convince people it has lower prices, Whole Foods has been very careful to maintain the reputation built on products like MSC-certified halibut.

- Amazon-owned Whole Foods touted a price cut on halibut as part of an announcement recently about lower prices on hundreds of items. (Ellen M. Banner / The Seattle Times) + Amazon-owned Whole Foods touted a price cut on halibut as part of an announcement recently about lower prices on hundreds of items. (Ellen M. Banner / The Seattle Times)
Amazon-owned Whole Foods touted a price cut on halibut as part of an announcement recently about lower prices on hundreds of items. (Ellen M. Banner / The Seattle Times)