Skip to content

Commit

Permalink
fix: handle complex background image css property
Browse files Browse the repository at this point in the history
  • Loading branch information
catalan-adobe committed Jun 4, 2024
1 parent 9bf404b commit 551289b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/utils/DOMUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,9 @@ export default class DOMUtils {
const prop = split.shift();
const value = split.join(':').trim();
if (prop === 'background-image') {
const trimmedValue = value.replace(/\s/g, '');
const sanitizedValue = value.replace(/\s/g, '').split(',').shift();
const elStyle = element.style;
elStyle.backgroundImage = trimmedValue;
elStyle.backgroundImage = sanitizedValue;
}
});
const url = element.style.backgroundImage;
Expand Down
4 changes: 4 additions & 0 deletions test/utils/DOMUtils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,10 @@ describe('DOMUtils#getImgFromBackground', () => {
test(createElement('p', {}, { 'background-image': 'url(http://localhost:3001/image.jpg)' }, 'Some content'), '<img src="http://localhost:3001/image.jpg">');
});

it('with complex background-image style', () => {
test(createElement('p', { style: 'background-image:url("https://www.server.com/image.jpg"), linear-gradient(transparent, transparent);' }, {}, 'Some content'), '<img src="https://www.server.com/image.jpg">');
});

// `createElement` uses JSDOM to create the test-DOM
// the workaround in DOMUtils#getImgFromBackground exists _precisely_
// because of a potential bug in JSDOM due to which it doesn't
Expand Down

0 comments on commit 551289b

Please sign in to comment.