Skip to content

Commit

Permalink
Merge pull request #78 from koppthe/fix-extract-url
Browse files Browse the repository at this point in the history
Fix 'getImageUrl' function
  • Loading branch information
vvasilev- committed Feb 7, 2017
2 parents a7e02d2 + a9f0c08 commit 78161c3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/core.js
Expand Up @@ -476,7 +476,7 @@ export function hasImageInRule(rule) {
* @return {Array}
*/
export function getImageUrl(rule) {
const matches = /background[^:]*:.*url\(([\S]+)\)/gi.exec(rule);
const matches = /url(?:\(['"]?)(.*?)(?:['"]?\))/gi.exec(rule);
let original = '';
let normalized = '';

Expand Down
10 changes: 10 additions & 0 deletions test/02-utilities.js
Expand Up @@ -44,6 +44,16 @@ test('should return the url of an image', (t) => {
t.deepEqual(getImageUrl(backgroundColor)[1], '');
});

test('shoud return the url of an image in compressed CSS rules', (t) => {
const background = '.selector-a{background:url(square.png) no-repeat 0 0;transform:scale(0.5)}';
const backgroundImage = '.selector-a{background-image:url(square.png);transform:scale(0.5)}';
const backgroundColor = '.selector-a{background:#fff;transform:scale(0.5)}';

t.deepEqual(getImageUrl(background)[1], 'square.png');
t.deepEqual(getImageUrl(backgroundImage)[1], 'square.png');
t.deepEqual(getImageUrl(backgroundColor)[1], '');
});

test('should remove get params', (t) => {
const background = '.selector-b { background: url(square.png?v1234) no-repeat 0 0; }';
t.deepEqual(getImageUrl(background)[1], 'square.png');
Expand Down

0 comments on commit 78161c3

Please sign in to comment.