Skip to content

Commit

Permalink
feat: helper method to get an image from background image style (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
kptdobe committed Feb 24, 2022
1 parent b2daf24 commit 63a384d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 17 deletions.
12 changes: 10 additions & 2 deletions src/utils/DOMUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,14 +231,22 @@ export default class DOMUtils {
}

static replaceBackgroundByImg(element, document) {
const img = DOMUtils.getImgFromBackground(element, document);
if (img) {
element.replaceWith(img);
return img;
}
return element;
}

static getImgFromBackground(element, document) {
const url = element?.style?.['background-image'];
if (url) {
const src = url.replace(/url\(/gm, '').replace(/'/gm, '').replace(/\)/gm, '');
const img = document.createElement('img');
img.src = src;
element.replaceWith(img);
return img;
}
return element;
return null;
}
}
49 changes: 34 additions & 15 deletions test/utils/DOMUtils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,22 +324,22 @@ describe('DOM#createTable tests', () => {
});
});

describe('DOMUtils#replaceBackgroundByImg', () => {
const createElement = (tag, attrs, styles, innerHTML) => {
const { document } = (new JSDOM()).window;
const element = document.createElement(tag);
// eslint-disable-next-line no-restricted-syntax, guard-for-in
for (const a in attrs) {
element.setAttribute(a, attrs[a]);
}
// eslint-disable-next-line no-restricted-syntax, guard-for-in
for (const p in styles) {
element.style[p] = styles[p];
}
element.innerHTML = innerHTML;
return element;
};
const createElement = (tag, attrs, styles, innerHTML) => {
const { document } = (new JSDOM()).window;
const element = document.createElement(tag);
// eslint-disable-next-line no-restricted-syntax, guard-for-in
for (const a in attrs) {
element.setAttribute(a, attrs[a]);
}
// eslint-disable-next-line no-restricted-syntax, guard-for-in
for (const p in styles) {
element.style[p] = styles[p];
}
element.innerHTML = innerHTML;
return element;
};

describe('DOMUtils#replaceBackgroundByImg', () => {
const test = (element, expected) => {
const { document } = (new JSDOM()).window;
const ret = DOMUtils.replaceBackgroundByImg(element, document);
Expand All @@ -358,3 +358,22 @@ describe('DOMUtils#replaceBackgroundByImg', () => {
test(createElement('div', { class: 'class-is-lost' }, { 'background-image': 'url("https://www.server.com/image.jpg")' }, '<div><div>Some divs</div><div>More divs</div></div>'), '<img src="https://www.server.com/image.jpg">');
});
});

describe('DOMUtils#getImgFromBackground', () => {
const test = (element, expected) => {
const { document } = (new JSDOM()).window;
const ret = DOMUtils.getImgFromBackground(element, document);
strictEqual(ret ? ret.outerHTML : null, expected);
};

it('no background-image style', () => {
test(createElement('p', {}, {}, 'Some content'), null);

test(createElement('img', { src: 'https://www.server.com/image.jpg', title: 'Some title' }, {}, ''), null);
});

it('with background-image style', () => {
test(createElement('p', {}, { 'background-image': 'url(https://www.server.com/image.jpg)' }, 'Some content'), '<img src="https://www.server.com/image.jpg">');
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">');
});
});

0 comments on commit 63a384d

Please sign in to comment.