Do not proxy image data uri (#421)

* do not proxy image data uri

* rename data uri

* rename data uri in test
This commit is contained in:
Hongbo Wu 2022-04-14 12:59:12 +08:00 committed by GitHub
parent 2c9c2a2adf
commit 72a231c97e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 3 deletions

View file

@ -504,15 +504,17 @@ Readability.prototype = {
});
},
/** Creates imageproxy links for all article images */
/** Creates imageproxy links for all article images with href source */
_createImageProxyLinks: function (articleContent) {
if (this.createImageProxyUrl !== undefined) {
// replace all image src's
// replace all images' href source
const images = articleContent.getElementsByTagName('img');
Array.from(images).forEach(image => {
const src = image.getAttribute("src");
const dataUriRegex = /^data:image\/(?:png|jpe?g|gif);base64,/;
if (src) {
// do not proxy data uri
if (src && !dataUriRegex.test(src)) {
const absoluteSrc = this.toAbsoluteURI(src);
const attToNumber = (str) => {
if (!str) { return 0; }

View file

@ -270,6 +270,17 @@ describe("Readability API", function() {
}).parse().content;
expect(content).eql(expected_xhtml);
});
it("should not proxy image with data uri", function() {
var dom = new JSDOM("My cat: <img src=\"data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUA" +
"AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==\"" +
" alt=\"Red dot\" />");
var expected_xhtml = "<div id=\"readability-page-1\" class=\"page\">My cat: <img src=\"data:image/png;base64," +
" iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0" +
"Y4OHwAAAABJRU5ErkJggg==\" alt=\"Red dot\"></div>";
var content = new Readability(dom.window.document).parse().content;
expect(content).eql(expected_xhtml);
});
});
});