Skip to content

Commit

Permalink
feat: add support for base64 images
Browse files Browse the repository at this point in the history
  • Loading branch information
catalan-adobe committed May 23, 2023
1 parent 1855846 commit 966c807
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/importer/PageImporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,14 @@ export default class PageImporter {
}

src = img.getAttribute('src');
// try to handle b64 img
if (!src || src.indexOf('data:') === 0) {
// we cannot handle b64 asset for now, remove
img.remove();
const dataUrl = DOMUtils.getDataUrlFromB64Img(img.src);
if (dataUrl) {
img.setAttribute('src', dataUrl);

Check warning on line 240 in src/importer/PageImporter.js

View check run for this annotation

Codecov / codecov/patch

src/importer/PageImporter.js#L240

Added line #L240 was not covered by tests
} else {
img.remove();
}
}

const alt = img.getAttribute('alt');
Expand Down
20 changes: 20 additions & 0 deletions src/utils/DOMUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/

import { JSDOM } from 'jsdom';
import { Blob } from 'buffer';

export default class DOMUtils {
static EMPTY_TAGS_TO_PRESERVE = ['img', 'video', 'iframe', 'div', 'picture'];
Expand Down Expand Up @@ -280,4 +281,23 @@ export default class DOMUtils {
}, interval);
});
}

static getDataUrlFromB64Img(src) {
try {
const arr = src.split(',');
const bstr = atob(arr[1]);
let n = bstr.length - 1;
const u8arr = new Uint8Array(n);
while (n >= 0) {
u8arr[n] = bstr.charCodeAt(n);
n -= 1;
}
const blob = new Blob([u8arr]);
return URL.createObjectURL(blob);
} catch (e) {
// eslint-disable-next-line no-console
console.error(`get data url from a base64 image (${src}):`, e);
return null;
}
}
}
19 changes: 19 additions & 0 deletions test/utils/DOMUtils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,3 +411,22 @@ describe('DOMUtils#getImgFromBackground', () => {
test(createElement('div', { class: 'someclass' }, { 'background-image': 'url("https://www.server.com/image.jpg")', background: 'rgb(0, 0, 0) none repeat scroll 0% 0% / auto padding-box border-box' }, '<div><div>Some divs</div><div>More divs</div></div>'), '<img src="https://www.server.com/image.jpg">');
});
});

describe('DOMUtils#getDataUrlFromB64Img', () => {
const test = (img, expected) => {
const dataUrl = DOMUtils.getDataUrlFromB64Img(img.src);
return expected(dataUrl) === true;
};

it('no data url in original image', () => {
test(createElement('img', { src: 'https://www.server.com/image.jpg' }, {}, ''), (res) => res === null);
});

it('malformed base64 data url in original image', () => {
test(createElement('img', { src: 'data:image/png;base64,--dummy--' }, {}, ''), (res) => res === null);
});

it('base64 data url in original image', () => {
test(createElement('img', { src: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==' }, {}, ''), (res) => res.indexOf('blob:nodedata:') === 0);
});
});

0 comments on commit 966c807

Please sign in to comment.