Skip to content

Commit

Permalink
Add 'originalUrl' property to the image object
Browse files Browse the repository at this point in the history
  • Loading branch information
vvasilev- committed Nov 14, 2016
1 parent c5a5ece commit a7021b4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
6 changes: 5 additions & 1 deletion README.md
Expand Up @@ -191,9 +191,13 @@ Every filter or group function will be called with an ``Image`` object.

> An absolute path to the image - ``/Path/to/your/image.png``
###### originalUrl

> The url found in your stylesheet including the query params - ``../image.png?v1``
###### url

> The url found in your stylesheet - ``../image.png``
> A normalized version of the original url - ``../image.png``
###### ratio

Expand Down
25 changes: 16 additions & 9 deletions src/core.js
Expand Up @@ -144,6 +144,7 @@ export function extractImages(root, opts, result) {
const image = {
path: null,
url: null,
originalUrl: null,
retina: false,
ratio: 1,
groups: [],
Expand All @@ -153,7 +154,10 @@ export function extractImages(root, opts, result) {

// Manipulate only rules with image in them
if (hasImageInRule(rule.toString())) {
image.url = getImageUrl(rule.toString());
const imageUrl = getImageUrl(rule.toString());

image.originalUrl = imageUrl[0];
image.url = imageUrl[1];

if (isImageSupported(image.url)) {
// Search for retina images
Expand Down Expand Up @@ -234,7 +238,7 @@ export function setTokens(root, opts, images) {

// Manipulate only rules with image in them
if (hasImageInRule(ruleStr)) {
url = getImageUrl(ruleStr);
url = getImageUrl(ruleStr)[1];
image = _.find(images, { url });

if (image) {
Expand Down Expand Up @@ -461,18 +465,21 @@ export function hasImageInRule(rule) {
/**
* Extracts the url of image from the given rule.
* @param {String} rule
* @return {String}
* @return {Array}
*/
export function getImageUrl(rule) {
const matches = /background[^:]*:.*url\(([\S]+)\)/gi.exec(rule);

if (!matches) {
return '';
let original = '';
let normalized = '';

if (matches) {
original = matches[1];
normalized = original
.replace(/['"]/gi, '') // replace all quotes
.replace(/\?.*$/gi, ''); // replace query params
}

return matches[1]
.replace(/['"]/gi, '') // replace all quotes
.replace(/\?.*$/gi, ''); // replace query params
return [original, normalized];
}

/**
Expand Down
12 changes: 6 additions & 6 deletions test/02-utilities.js
Expand Up @@ -38,21 +38,21 @@ test('should return the url of an image', (t) => {
`;
const backgroundColor = '.selector-a { background: #fff; }';

t.deepEqual(getImageUrl(background), 'square.png');
t.deepEqual(getImageUrl(backgroundImage), 'circle.png');
t.deepEqual(getImageUrl(backgroundBlock), 'square.png');
t.deepEqual(getImageUrl(backgroundColor), '');
t.deepEqual(getImageUrl(background)[1], 'square.png');
t.deepEqual(getImageUrl(backgroundImage)[1], 'circle.png');
t.deepEqual(getImageUrl(backgroundBlock)[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), 'square.png');
t.deepEqual(getImageUrl(background)[1], 'square.png');
});


test('should remove the quotes', (t) => {
const background = '.selector-b { background: url("square.png") no-repeat 0 0; }';
t.deepEqual(getImageUrl(background), 'square.png');
t.deepEqual(getImageUrl(background)[1], 'square.png');
});

test('should allow only local files', (t) => {
Expand Down

0 comments on commit a7021b4

Please sign in to comment.