Skip to content

Commit

Permalink
Fix similar css urls not updated (#53)
Browse files Browse the repository at this point in the history
* Fix similar css urls not updates, close #48
  • Loading branch information
s0ph1e committed Apr 21, 2016
1 parent 93331b2 commit 9732eb1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/file-handlers/css.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function loadCss (context, resource) {

return context.loadResource(cssResource).then(function handleLoadedSource (loadedResource) {
var relativePath = utils.getRelativePath(filename, loadedResource.getFilename());
text = text.replace(cssUrl, relativePath);
text = text.replace(new RegExp(cssUrl, 'g'), relativePath);
return Promise.resolve();
});
});
Expand Down
23 changes: 23 additions & 0 deletions test/unit/file-handlers/css-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,28 @@ describe('Css handler', function () {
done();
}).catch(done);
});

it('should replace all occurencies of the same sources in text with local files', function(done) {
sinon.stub(scraper, 'loadResource').returns(Promise.resolve(new Resource('http://example.com/img.jpg', 'local/img.jpg')));

var css = '\
.a {background: url("http://example.com/img.jpg")} \
.b {background: url("http://example.com/img.jpg")}\
.c {background: url("http://example.com/img.jpg")}\
';

var po = new Resource('http://example.com', '1.css');
po.setText(css);

return loadCss(scraper, po).then(function(){
var text = po.getText();
var numberOfLocalResourceMatches = text.match(/local\/img.jpg/g).length;

text.should.not.containEql('http://example.com/img.jpg');
text.should.containEql('local/img.jpg');
numberOfLocalResourceMatches.should.be.eql(3);
done();
}).catch(done);
});
});
});

0 comments on commit 9732eb1

Please sign in to comment.