Skip to content

Commit

Permalink
Fix 'getImageUrl' function
Browse files Browse the repository at this point in the history
Fix the problem that can not extracts the correct url of image from compressed CSS rule.
  • Loading branch information
koppthe committed Jan 16, 2017
1 parent a7e02d2 commit a9f0c08
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 a9f0c08

Please sign in to comment.