diff --git a/lib/file-handlers/css.js b/lib/file-handlers/css.js index 17edc596..e0280ce5 100644 --- a/lib/file-handlers/css.js +++ b/lib/file-handlers/css.js @@ -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(); }); }); diff --git a/test/unit/file-handlers/css-test.js b/test/unit/file-handlers/css-test.js index 0ac211e7..426f2725 100644 --- a/test/unit/file-handlers/css-test.js +++ b/test/unit/file-handlers/css-test.js @@ -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); + }); }); });