Skip to content

Commit abc4a27

Browse files
committed
Add createChild method to Resource
1 parent 0665e47 commit abc4a27

File tree

4 files changed

+74
-7
lines changed

4 files changed

+74
-7
lines changed

lib/file-handlers/css.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
var _ = require('underscore');
22
var Promise = require('bluebird');
33
var getCssUrls = require('css-url-parser');
4-
var Resource = require('../resource');
54
var utils = require('../utils');
65

76
function loadCss (context, resource) {
@@ -12,8 +11,7 @@ function loadCss (context, resource) {
1211

1312
var promises = _.map(cssUrls, function loadResourceFromCssUrl (cssUrl) {
1413
var resourceUrl = utils.getUrl(url, cssUrl);
15-
var cssResource = new Resource(resourceUrl);
16-
cssResource.setParent(resource);
14+
var cssResource = resource.createChild(resourceUrl);
1715

1816
return context.loadResource(cssResource).then(function handleLoadedSource (loadedResource) {
1917
var relativePath = utils.getRelativePath(filename, loadedResource.getFilename());

lib/file-handlers/html.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
var cheerio = require('cheerio');
22
var Promise = require('bluebird');
33
var utils = require('../utils');
4-
var Resource = require('../resource');
54

65
function loadHtml (context, resource) {
76
var sources = context.getHtmlSources();
@@ -50,8 +49,7 @@ function loadResources (context, resource, source) {
5049

5150
if (attr) {
5251
var resourceUrl = utils.getUrl(url, attr);
53-
var htmlResource = new Resource(resourceUrl);
54-
htmlResource.setParent(resource);
52+
var htmlResource = resource.createChild(resourceUrl);
5553
htmlResource.setHtmlData({ tagName: el[0].name, attributeName: source.attr });
5654

5755
return context.loadResource(htmlResource).then(function handleLoadedSource (loadedResource) {

lib/resource.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,17 @@ function Resource (url, filename) {
1515
this.filename = filename;
1616
}
1717

18+
Resource.prototype.createChild = function createChild (url, filename) {
19+
var child = new Resource(url, filename);
20+
21+
var currentDepth = this.getDepth();
22+
23+
child.setParent(this);
24+
child.setDepth(++currentDepth);
25+
26+
return child;
27+
};
28+
1829
Resource.prototype.getUrl = function getUrl () {
1930
return this.url;
2031
};
@@ -43,6 +54,14 @@ Resource.prototype.setParent = function setParent (parent) {
4354
this.parent = parent;
4455
};
4556

57+
Resource.prototype.getDepth = function getDepth () {
58+
return this.depth || 0;
59+
};
60+
61+
Resource.prototype.setDepth = function setDepth (depth) {
62+
this.depth = depth;
63+
};
64+
4665
/**
4766
*
4867
* @param {Object} data - html element data

test/unit/resource-test.js

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var Resource = require('../../lib/resource');
44
var types = require('../../lib/config/resource-types');
55

66
describe('Resource', function() {
7-
describe('#Resource', function() {
7+
describe('#getType', function() {
88
it('should return correct type based on extension', function() {
99
var html = new Resource('http://example.com', 'index.html');
1010
var htm = new Resource('http://example.com', 'index.htm');
@@ -68,4 +68,56 @@ describe('Resource', function() {
6868
res.getType().should.be.eql(types.other);
6969
});
7070
});
71+
72+
describe('#setDepth', function () {
73+
it('should set depth', function() {
74+
var o = new Resource('http://google.com');
75+
o.setDepth(555);
76+
o.depth.should.be.eql(555);
77+
});
78+
});
79+
80+
describe('#getDepth', function () {
81+
it('should return depth if object has it', function() {
82+
var o = new Resource('http://google.com');
83+
o.setDepth(123);
84+
o.getDepth().should.be.eql(123);
85+
});
86+
87+
it('should return 0 if object has no depth', function() {
88+
var o = new Resource('http://google.com');
89+
o.getDepth().should.be.eql(0);
90+
});
91+
92+
});
93+
94+
describe('#createChild', function () {
95+
it('should return Resource', function() {
96+
var parent = new Resource('http://example.com');
97+
var child = parent.createChild('http://google.com');
98+
child.should.be.instanceOf(Resource);
99+
});
100+
101+
it('should set correct url and filename', function() {
102+
var parent = new Resource('http://example.com');
103+
var child = parent.createChild('http://google.com', 'google.html');
104+
child.getUrl().should.be.eql('http://google.com');
105+
child.getFilename().should.be.eql('google.html');
106+
});
107+
108+
it('should set parent', function() {
109+
var parent = new Resource('http://example.com');
110+
var child = parent.createChild('http://google.com');
111+
child.parent.should.be.equal(parent);
112+
});
113+
114+
it('should set depth', function() {
115+
var parent = new Resource('http://example.com');
116+
var child = parent.createChild('http://google.com');
117+
child.getDepth().should.be.eql(1);
118+
119+
var childOfChild = child.createChild('http://google.com.ua');
120+
childOfChild.getDepth().should.be.eql(2);
121+
});
122+
});
71123
});

0 commit comments

Comments
 (0)