Skip to content

Commit aafd955

Browse files
committed
Add missing updateChild in html handler, fixes #52 (#57)
1 parent dc4ab93 commit aafd955

File tree

3 files changed

+53
-32
lines changed

3 files changed

+53
-32
lines changed

lib/file-handlers/html.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ function loadImgSrcsetResource (context, parentResource, childResourceHtmlData)
8282

8383
return context.loadResource(childResource).then(function updateSrcsetPart (loadedResource) {
8484
if(loadedResource){
85+
parentResource.updateChild(childResource, loadedResource);
8586
imgScrsetPart.url = loadedResource.getFilename();
8687
}
8788
});
@@ -108,7 +109,8 @@ function loadGeneralResource (context, parentResource, childResourceHtmlData) {
108109
if(!loadedResource){
109110
return attr;
110111
}
111-
112+
parentResource.updateChild(htmlResource, loadedResource);
113+
112114
var relativePath = utils.getRelativePath(parentResource.getFilename(), loadedResource.getFilename());
113115
if(context.options.prettifyUrls){
114116
relativePath = relativePath.replace(context.options.defaultFilename, '');

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,17 +98,20 @@ describe('Css handler', function () {
9898
.c {background: url("img/c.jpg")}\
9999
';
100100

101-
var po = new Resource('http://example.com', '1.css');
102-
po.setText(css);
101+
var parentResource = new Resource('http://example.com', '1.css');
102+
parentResource.setText(css);
103+
var updateChildSpy = sinon.spy(parentResource, 'updateChild');
103104

104-
return loadCss(scraper, po).then(function(){
105-
var text = po.getText();
105+
return loadCss(scraper, parentResource).then(function(){
106+
var text = parentResource.getText();
106107
text.should.not.containEql('http://first.com/img/a.jpg');
107108
text.should.not.containEql('http://first.com/b.jpg');
108109
text.should.not.containEql('img/c.jpg');
109110
text.should.containEql('local/a.jpg');
110111
text.should.containEql('local/b.jpg');
111112
text.should.containEql('local/c.jpg');
113+
114+
updateChildSpy.calledThrice.should.be.eql(true);
112115
done();
113116
}).catch(done);
114117
});
@@ -127,18 +130,21 @@ describe('Css handler', function () {
127130
.c {background: url("img/c.jpg")}\
128131
';
129132

130-
var po = new Resource('http://example.com', '1.css');
131-
po.setText(css);
133+
var parentResource = new Resource('http://example.com', '1.css');
134+
parentResource.setText(css);
135+
var updateChildSpy = sinon.spy(parentResource, 'updateChild');
132136

133-
return loadCss(scraper, po).then(function(){
134-
var text = po.getText();
137+
return loadCss(scraper, parentResource).then(function(){
138+
var text = parentResource.getText();
135139
text.should.containEql('http://first.com/img/a.jpg');
136140
text.should.containEql('http://first.com/b.jpg');
137141
text.should.not.containEql('local/a.jpg');
138142
text.should.not.containEql('local/b.jpg');
139143

140144
text.should.not.containEql('img/c.jpg');
141145
text.should.containEql('local/c.jpg');
146+
147+
updateChildSpy.calledOnce.should.be.eql(true);
142148

143149
done();
144150
}).catch(done);

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

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
require('should');
22
var _ = require('lodash');
3+
var Promise = require('bluebird');
34
var sinon = require('sinon');
5+
require('sinon-as-promised')(Promise);
46
var nock = require('nock');
57
var fs = require('fs-extra');
6-
var Promise = require('bluebird');
78
var Scraper = require('../../../lib/scraper');
89
var Resource = require('../../../lib/resource');
910
var loadHtml = require('../../../lib/file-handlers/html');
@@ -167,15 +168,18 @@ describe('Html handler', function () {
167168
</html>\
168169
';
169170

170-
var po = new Resource('http://example.com', 'index.html');
171-
po.setText(html);
171+
var parentResource = new Resource('http://example.com', 'index.html');
172+
parentResource.setText(html);
173+
var updateChildSpy = sinon.spy(parentResource, 'updateChild');
172174

173175
// order of loading is determined by order of sources in scraper options
174-
loadHtml(scraper, po).then(function() {
176+
loadHtml(scraper, parentResource).then(function() {
175177
loadResourceSpy.calledThrice.should.be.eql(true);
176178
loadResourceSpy.args[0][0].url.should.be.eql('http://example.com/a.jpg');
177179
loadResourceSpy.args[1][0].url.should.be.eql('http://example.com/b.css');
178180
loadResourceSpy.args[2][0].url.should.be.eql('http://example.com/c.js');
181+
182+
updateChildSpy.calledThrice.should.be.eql(true);
179183
done();
180184
}).catch(done);
181185
});
@@ -200,11 +204,12 @@ describe('Html handler', function () {
200204
</html>\
201205
';
202206

203-
var po = new Resource('http://example.com', 'index.html');
204-
po.setText(html);
207+
var parentResource = new Resource('http://example.com', 'index.html');
208+
parentResource.setText(html);
209+
var updateChildSpy = sinon.spy(parentResource, 'updateChild');
205210

206-
return loadHtml(scraper, po).then(function(){
207-
var text = po.getText();
211+
return loadHtml(scraper, parentResource).then(function(){
212+
var text = parentResource.getText();
208213
text.should.containEql('http://example.com/public/img/a.jpg');
209214
text.should.containEql('http://example.com/b.css');
210215
text.should.not.containEql('local/a.jpg');
@@ -213,6 +218,8 @@ describe('Html handler', function () {
213218
text.should.not.containEql('scripts/c.js');
214219
text.should.containEql('local/c.js');
215220

221+
updateChildSpy.calledOnce.should.be.eql(true);
222+
216223
done();
217224
}).catch(done);
218225
});
@@ -325,14 +332,14 @@ describe('Html handler', function () {
325332
});
326333

327334
it('should handle img tag with srcset attribute correctly', function (done) {
328-
329335
var image45Stub = new Resource('http://example.com/image45.jpg', 'local/image45.jpg');
330336
var image150Stub = new Resource('http://example.com/image150.jpg', 'local/image150.jpg');
331337

332338
sinon.stub(scraper, 'loadResource')
333-
.onFirstCall().returns(Promise.resolve(image45Stub))
334-
.onSecondCall().returns(Promise.resolve(image150Stub))
335-
.onThirdCall().returns(Promise.resolve(image45Stub));
339+
.onFirstCall().resolves(image45Stub)
340+
.onSecondCall().resolves(image150Stub)
341+
.onThirdCall().resolves(image45Stub);
342+
336343

337344
var html = '\
338345
<html> \
@@ -344,11 +351,14 @@ describe('Html handler', function () {
344351
</html>\
345352
';
346353

347-
var po = new Resource('http://example.com', 'index.html');
348-
po.setText(html);
354+
var parentResource = new Resource('http://example.com', 'index.html');
355+
parentResource.setText(html);
356+
var updateChildSpy = sinon.spy(parentResource, 'updateChild');
349357

350-
return loadHtml(scraper, po).then(function () {
351-
var text = po.getText();
358+
return loadHtml(scraper, parentResource).then(function () {
359+
var text = parentResource.getText();
360+
361+
updateChildSpy.calledThrice.should.be.eql(true);
352362

353363
text.should.not.containEql('http://example.com/image45.jpg');
354364
text.should.not.containEql('http://example.com/image150.jpg');
@@ -363,9 +373,9 @@ describe('Html handler', function () {
363373
var image150Stub = new Resource('http://example.com/image150.jpg', 'local/image150.jpg');
364374

365375
sinon.stub(scraper, 'loadResource')
366-
.onFirstCall().returns(Promise.resolve(null))
367-
.onSecondCall().returns(Promise.resolve(image150Stub))
368-
.onThirdCall().returns(Promise.resolve(null));
376+
.onFirstCall().resolves(null)
377+
.onSecondCall().resolves(image150Stub)
378+
.onThirdCall().resolves(null);
369379

370380
var html = '\
371381
<html> \
@@ -377,11 +387,14 @@ describe('Html handler', function () {
377387
</html>\
378388
';
379389

380-
var po = new Resource('http://example.com', 'index.html');
381-
po.setText(html);
390+
var parentResource = new Resource('http://example.com', 'index.html');
391+
parentResource.setText(html);
392+
var updateChildSpy = sinon.spy(parentResource, 'updateChild');
382393

383-
return loadHtml(scraper, po).then(function () {
384-
var text = po.getText();
394+
return loadHtml(scraper, parentResource).then(function () {
395+
var text = parentResource.getText();
396+
397+
updateChildSpy.calledOnce.should.be.eql(true);
385398

386399
text.should.containEql('http://example.com/image45.jpg');
387400
text.should.not.containEql('src="local/image45.jpg"');

0 commit comments

Comments
 (0)