Skip to content
This repository was archived by the owner on Jan 27, 2019. It is now read-only.

Commit 46e0cbe

Browse files
committed
proper check for inlinestyles
1 parent e54910a commit 46e0cbe

File tree

1 file changed

+115
-111
lines changed

1 file changed

+115
-111
lines changed

index.js

Lines changed: 115 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -14,131 +14,135 @@ var ua = require('ua-string');
1414
var getLinkContents = require('./utils/get-link-contents');
1515
var createLink = require('./utils/create-link');
1616

17-
module.exports = function(url, options){
18-
var deferred = q.defer();
19-
var options = options || {};
20-
options.headers = options.headers || {};
21-
options.headers['User-Agent'] = options.headers['User-Agent'] || ua;
22-
options.timeout = options.timeout || 5000;
23-
options.gzip = true;
24-
25-
if (typeof url !== 'string' || isBlank(url) || !isUrl(url)) {
26-
throw new TypeError('get-css expected a url as a string')
27-
}
28-
29-
url = normalizeUrl(url, { stripWWW: false });
30-
options.url = url;
31-
32-
if (options.ignoreCerts) {
33-
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
34-
}
35-
36-
var status = {
37-
parsed: 0,
38-
total: 0
39-
};
40-
41-
var result = {
42-
links: [],
43-
styles: [],
44-
css: ''
45-
};
46-
47-
function handleResolve() {
48-
if (status.parsed >= status.total) {
49-
deferred.resolve(result);
17+
module.exports = function (url, options) {
18+
var deferred = q.defer();
19+
var options = options || {};
20+
options.headers = options.headers || {};
21+
options.headers['User-Agent'] = options.headers['User-Agent'] || ua;
22+
options.timeout = options.timeout || 5000;
23+
options.gzip = true;
24+
25+
if (typeof url !== 'string' || isBlank(url) || !isUrl(url)) {
26+
throw new TypeError('get-css expected a url as a string')
5027
}
51-
}
5228

53-
function parseHtml(html) {
54-
var $ = cheerio.load(html);
55-
result.pageTitle = $('head > title').text();
29+
url = normalizeUrl(url, {stripWWW: false});
30+
options.url = url;
5631

57-
$('[rel=stylesheet]').each(function() {
58-
var link = $(this).attr('href');
59-
result.links.push(createLink(link, url));
60-
});
61-
62-
$('style').each(function() {
63-
result.styles.push(stripHtmlComments($(this).text()));
64-
});
32+
if (options.ignoreCerts) {
33+
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
34+
}
6535

66-
status.total = result.links.length + result.styles.length;
67-
if (!status.total) {
68-
deferred.resolve(false);
36+
var status = {
37+
parsed: 0,
38+
total: 0
39+
};
40+
41+
var result = {
42+
links: [],
43+
styles: [],
44+
css: ''
45+
};
46+
47+
function handleResolve() {
48+
if (status.parsed >= status.total) {
49+
deferred.resolve(result);
50+
}
6951
}
7052

71-
result.links.forEach(function(link) {
72-
getLinkContents(link.url, options)
73-
.then(function(css) {
74-
handleCssFromLink(link, css);
75-
})
76-
.catch(function(error) {
77-
link.error = error;
78-
status.parsed++;
79-
handleResolve();
53+
function parseHtml(html) {
54+
var $ = cheerio.load(html);
55+
result.pageTitle = $('head > title').text();
56+
57+
$('[rel=stylesheet]').each(function () {
58+
var link = $(this).attr('href');
59+
if (!(typeof link !== typeof undefined && link !== false)) {
60+
result.styles.push(stripHtmlComments($(this).text()));
61+
return;
62+
}
63+
result.links.push(createLink(link, url));
8064
});
81-
});
8265

83-
result.styles.forEach(function(css) {
84-
result.css += css;
85-
status.parsed++;
86-
handleResolve();
87-
});
88-
}
89-
90-
function handleCssFromLink(link, css) {
91-
link.css += css;
92-
93-
parseCssForImports(link, css);
94-
95-
status.parsed++;
96-
handleResolve();
97-
}
98-
99-
// Handle potential @import url(foo.css) statements in the CSS.
100-
function parseCssForImports(link, css) {
101-
link.imports = resolveCssImportUrls(link.url, css);
102-
status.total += link.imports.length;
103-
result.css += css;
104-
105-
link.imports.forEach(function(importUrl) {
106-
var importLink = createLink(importUrl, importUrl);
107-
result.links.push(importLink);
108-
109-
getLinkContents(importLink.url, options)
110-
.then(function(css) {
111-
handleCssFromLink(importLink, css);
112-
})
113-
.catch(function(error) {
114-
link.error = error;
115-
status.parsed++;
116-
handleResolve();
66+
$('style').each(function () {
67+
result.styles.push(stripHtmlComments($(this).text()));
11768
});
118-
});
119-
}
12069

121-
request(options, function(error, response, body) {
122-
if (error) {
123-
if (options.verbose) console.log('Error from ' + url + ' ' + error);
124-
deferred.reject(error);
125-
return;
70+
status.total = result.links.length + result.styles.length;
71+
if (!status.total) {
72+
deferred.resolve(false);
73+
}
74+
75+
result.links.forEach(function (link) {
76+
getLinkContents(link.url, options)
77+
.then(function (css) {
78+
handleCssFromLink(link, css);
79+
})
80+
.catch(function (error) {
81+
link.error = error;
82+
status.parsed++;
83+
handleResolve();
84+
});
85+
});
86+
87+
result.styles.forEach(function (css) {
88+
result.css += css;
89+
status.parsed++;
90+
handleResolve();
91+
});
12692
}
12793

128-
if (response && response.statusCode != 200) {
129-
if (options.verbose) console.log('Received a ' + response.statusCode + ' from: ' + url);
130-
deferred.reject({ url: url, statusCode: response.code });
131-
return;
94+
function handleCssFromLink(link, css) {
95+
link.css += css;
96+
97+
parseCssForImports(link, css);
98+
99+
status.parsed++;
100+
handleResolve();
132101
}
133102

134-
if (isCss(url)) {
135-
var link = createLink(url, url);
136-
result.links.push(link);
137-
handleCssFromLink(link, body);
138-
} else {
139-
parseHtml(body);
103+
// Handle potential @import url(foo.css) statements in the CSS.
104+
function parseCssForImports(link, css) {
105+
link.imports = resolveCssImportUrls(link.url, css);
106+
status.total += link.imports.length;
107+
result.css += css;
108+
109+
link.imports.forEach(function (importUrl) {
110+
var importLink = createLink(importUrl, importUrl);
111+
result.links.push(importLink);
112+
113+
getLinkContents(importLink.url, options)
114+
.then(function (css) {
115+
handleCssFromLink(importLink, css);
116+
})
117+
.catch(function (error) {
118+
link.error = error;
119+
status.parsed++;
120+
handleResolve();
121+
});
122+
});
140123
}
141-
});
142124

143-
return deferred.promise;
125+
request(options, function (error, response, body) {
126+
if (error) {
127+
if (options.verbose) console.log('Error from ' + url + ' ' + error);
128+
deferred.reject(error);
129+
return;
130+
}
131+
132+
if (response && response.statusCode != 200) {
133+
if (options.verbose) console.log('Received a ' + response.statusCode + ' from: ' + url);
134+
deferred.reject({url: url, statusCode: response.code});
135+
return;
136+
}
137+
138+
if (isCss(url)) {
139+
var link = createLink(url, url);
140+
result.links.push(link);
141+
handleCssFromLink(link, body);
142+
} else {
143+
parseHtml(body);
144+
}
145+
});
146+
147+
return deferred.promise;
144148
};

0 commit comments

Comments
 (0)