Skip to content

Commit 9732eb1

Browse files
committed
Fix similar css urls not updated (#53)
* Fix similar css urls not updates, close #48
1 parent 93331b2 commit 9732eb1

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

lib/file-handlers/css.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function loadCss (context, resource) {
1515

1616
return context.loadResource(cssResource).then(function handleLoadedSource (loadedResource) {
1717
var relativePath = utils.getRelativePath(filename, loadedResource.getFilename());
18-
text = text.replace(cssUrl, relativePath);
18+
text = text.replace(new RegExp(cssUrl, 'g'), relativePath);
1919
return Promise.resolve();
2020
});
2121
});

test/unit/file-handlers/css-test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,5 +112,28 @@ describe('Css handler', function () {
112112
done();
113113
}).catch(done);
114114
});
115+
116+
it('should replace all occurencies of the same sources in text with local files', function(done) {
117+
sinon.stub(scraper, 'loadResource').returns(Promise.resolve(new Resource('http://example.com/img.jpg', 'local/img.jpg')));
118+
119+
var css = '\
120+
.a {background: url("http://example.com/img.jpg")} \
121+
.b {background: url("http://example.com/img.jpg")}\
122+
.c {background: url("http://example.com/img.jpg")}\
123+
';
124+
125+
var po = new Resource('http://example.com', '1.css');
126+
po.setText(css);
127+
128+
return loadCss(scraper, po).then(function(){
129+
var text = po.getText();
130+
var numberOfLocalResourceMatches = text.match(/local\/img.jpg/g).length;
131+
132+
text.should.not.containEql('http://example.com/img.jpg');
133+
text.should.containEql('local/img.jpg');
134+
numberOfLocalResourceMatches.should.be.eql(3);
135+
done();
136+
}).catch(done);
137+
});
115138
});
116139
});

0 commit comments

Comments
 (0)