From 416ec0ec47d7ef3486932d2346cd5ce382b172e7 Mon Sep 17 00:00:00 2001 From: s0ph1e Date: Thu, 21 Apr 2016 00:10:25 +0300 Subject: [PATCH 1/2] Fix css handler bugs --- lib/file-handlers/css.js | 2 +- test/unit/file-handlers/css-test.js | 69 +++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) 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..00fbbd2a 100644 --- a/test/unit/file-handlers/css-test.js +++ b/test/unit/file-handlers/css-test.js @@ -112,5 +112,74 @@ 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); + }); + + it('should replace resource only if it completely equals to path (should not change partially matched names)', function(done) { + + var loadStub = sinon.stub(scraper, 'loadResource'); + loadStub.onCall(0).returns(Promise.resolve(new Resource('http://example.com/style.css', 'local/style.css'))); + loadStub.onCall(1).returns(Promise.resolve(new Resource('http://example.com/style.css', 'local/style.css'))); + loadStub.onCall(2).returns(Promise.resolve(new Resource('http://example.com/mystyle.css', 'local/mystyle.css'))); + loadStub.onCall(3).returns(Promise.resolve(new Resource('http://example.com/style.css', 'local/style.css'))); + loadStub.onCall(4).returns(Promise.resolve(new Resource('http://example.com/another_style.css', 'local/another_style.css'))); + loadStub.onCall(5).returns(Promise.resolve(new Resource('http://example.com/image.png', 'local/image.png'))); + loadStub.onCall(6).returns(Promise.resolve(new Resource('http://example.com/image.png', 'local/image.png'))); + loadStub.onCall(7).returns(Promise.resolve(new Resource('http://example.com/another-image.png', 'local/another-image.png'))); + loadStub.onCall(8).returns(Promise.resolve(new Resource('http://example.com/new-another-image.png', 'local/new-another-image.png'))); + + var css = '\ + @import "style.css" \ + @import style.css \ + @import \'mystyle.css\' \ + @import (\'style.css\') \ + @import url(\'another-style.css\') \ + .a {background: url("image.png")} \ + .b {background: url(image.png)} \ + .c {background: url("another-image.png")}\ + .d {background: url(\'new-another-image.png\')}\ + '; + + var po = new Resource('http://example.com', '1.css'); + po.setText(css); + + return loadCss(scraper, po).then(function(){ + var text = po.getText(); + + text.should.containEql('@import "local/style.css"'); + text.should.containEql('@import local/style.css'); + text.should.containEql('@import \'local/mystyle.css\''); + text.should.containEql('@import (\'local/style.css\') '); + text.should.containEql('@import url(\local/another_style.css\')'); + + text.should.containEql('.a {background: url("local/image.png")}'); + text.should.containEql('.b {background: url(local/image.png)}'); + text.should.containEql('.c {background: url("local/another-image.png")}'); + text.should.containEql('.d {background: url(\'local/new-another-image.png\')}'); + + done(); + }).catch(done); + }); }); }); From 42c94ba97a1a4ca38b6d0598cc684d2acb31e44a Mon Sep 17 00:00:00 2001 From: s0ph1e Date: Fri, 22 Apr 2016 00:18:12 +0300 Subject: [PATCH 2/2] Remove test for partially matched urls --- test/unit/file-handlers/css-test.js | 46 ----------------------------- 1 file changed, 46 deletions(-) diff --git a/test/unit/file-handlers/css-test.js b/test/unit/file-handlers/css-test.js index 00fbbd2a..426f2725 100644 --- a/test/unit/file-handlers/css-test.js +++ b/test/unit/file-handlers/css-test.js @@ -135,51 +135,5 @@ describe('Css handler', function () { done(); }).catch(done); }); - - it('should replace resource only if it completely equals to path (should not change partially matched names)', function(done) { - - var loadStub = sinon.stub(scraper, 'loadResource'); - loadStub.onCall(0).returns(Promise.resolve(new Resource('http://example.com/style.css', 'local/style.css'))); - loadStub.onCall(1).returns(Promise.resolve(new Resource('http://example.com/style.css', 'local/style.css'))); - loadStub.onCall(2).returns(Promise.resolve(new Resource('http://example.com/mystyle.css', 'local/mystyle.css'))); - loadStub.onCall(3).returns(Promise.resolve(new Resource('http://example.com/style.css', 'local/style.css'))); - loadStub.onCall(4).returns(Promise.resolve(new Resource('http://example.com/another_style.css', 'local/another_style.css'))); - loadStub.onCall(5).returns(Promise.resolve(new Resource('http://example.com/image.png', 'local/image.png'))); - loadStub.onCall(6).returns(Promise.resolve(new Resource('http://example.com/image.png', 'local/image.png'))); - loadStub.onCall(7).returns(Promise.resolve(new Resource('http://example.com/another-image.png', 'local/another-image.png'))); - loadStub.onCall(8).returns(Promise.resolve(new Resource('http://example.com/new-another-image.png', 'local/new-another-image.png'))); - - var css = '\ - @import "style.css" \ - @import style.css \ - @import \'mystyle.css\' \ - @import (\'style.css\') \ - @import url(\'another-style.css\') \ - .a {background: url("image.png")} \ - .b {background: url(image.png)} \ - .c {background: url("another-image.png")}\ - .d {background: url(\'new-another-image.png\')}\ - '; - - var po = new Resource('http://example.com', '1.css'); - po.setText(css); - - return loadCss(scraper, po).then(function(){ - var text = po.getText(); - - text.should.containEql('@import "local/style.css"'); - text.should.containEql('@import local/style.css'); - text.should.containEql('@import \'local/mystyle.css\''); - text.should.containEql('@import (\'local/style.css\') '); - text.should.containEql('@import url(\local/another_style.css\')'); - - text.should.containEql('.a {background: url("local/image.png")}'); - text.should.containEql('.b {background: url(local/image.png)}'); - text.should.containEql('.c {background: url("local/another-image.png")}'); - text.should.containEql('.d {background: url(\'local/new-another-image.png\')}'); - - done(); - }).catch(done); - }); }); });