Skip to content

Commit 78161c3

Browse files
authored
Merge pull request #78 from koppthe/fix-extract-url
Fix 'getImageUrl' function
2 parents a7e02d2 + a9f0c08 commit 78161c3

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/core.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ export function hasImageInRule(rule) {
476476
* @return {Array}
477477
*/
478478
export function getImageUrl(rule) {
479-
const matches = /background[^:]*:.*url\(([\S]+)\)/gi.exec(rule);
479+
const matches = /url(?:\(['"]?)(.*?)(?:['"]?\))/gi.exec(rule);
480480
let original = '';
481481
let normalized = '';
482482

test/02-utilities.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ test('should return the url of an image', (t) => {
4444
t.deepEqual(getImageUrl(backgroundColor)[1], '');
4545
});
4646

47+
test('shoud return the url of an image in compressed CSS rules', (t) => {
48+
const background = '.selector-a{background:url(square.png) no-repeat 0 0;transform:scale(0.5)}';
49+
const backgroundImage = '.selector-a{background-image:url(square.png);transform:scale(0.5)}';
50+
const backgroundColor = '.selector-a{background:#fff;transform:scale(0.5)}';
51+
52+
t.deepEqual(getImageUrl(background)[1], 'square.png');
53+
t.deepEqual(getImageUrl(backgroundImage)[1], 'square.png');
54+
t.deepEqual(getImageUrl(backgroundColor)[1], '');
55+
});
56+
4757
test('should remove get params', (t) => {
4858
const background = '.selector-b { background: url(square.png?v1234) no-repeat 0 0; }';
4959
t.deepEqual(getImageUrl(background)[1], 'square.png');

0 commit comments

Comments
 (0)