Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
});
});