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

Commit a78cf9c

Browse files
author
Ryan Soury
committed
Added support html as parameter
1 parent 1baaee1 commit a78cf9c

File tree

1 file changed

+160
-148
lines changed

1 file changed

+160
-148
lines changed

index.js

Lines changed: 160 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -1,149 +1,161 @@
1-
'use strict';
2-
3-
var q = require('q');
4-
var isCss = require('is-css');
5-
var isPresent = require('is-present');
6-
var isBlank = require('is-blank');
7-
var isUrl = require('is-url-superb');
8-
var request = require('request');
9-
var cheerio = require('cheerio');
10-
var normalizeUrl = require('normalize-url');
11-
var stripHtmlComments = require('strip-html-comments');
12-
var resolveCssImportUrls = require('resolve-css-import-urls');
13-
var ua = require('ua-string');
14-
15-
var getLinkContents = require('./utils/get-link-contents');
16-
var createLink = require('./utils/create-link');
17-
18-
module.exports = function(url, options){
19-
var deferred = q.defer();
20-
var options = options || {};
21-
options.headers = options.headers || {};
22-
options.headers['User-Agent'] = options.headers['User-Agent'] || ua;
23-
options.timeout = options.timeout || 5000;
24-
options.gzip = true;
25-
26-
if (typeof url !== 'string' || isBlank(url) || !isUrl(url)) {
27-
throw new TypeError('get-css expected a url as a string')
28-
}
29-
30-
url = normalizeUrl(url, { stripWWW: false });
31-
options.url = url;
32-
33-
if (options.ignoreCerts) {
34-
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
35-
}
36-
37-
var status = {
38-
parsed: 0,
39-
total: 0
40-
};
41-
42-
var result = {
43-
links: [],
44-
styles: [],
45-
css: ''
46-
};
47-
48-
function handleResolve() {
49-
if (status.parsed >= status.total) {
50-
deferred.resolve(result);
51-
}
52-
}
53-
54-
function parseHtml(html) {
55-
var $ = cheerio.load(html);
56-
result.pageTitle = $('head > title').text();
57-
58-
$('[rel=stylesheet]').each(function() {
59-
var link = $(this).attr('href');
60-
if(isPresent(link)) {
61-
result.links.push(createLink(link, url));
62-
}else{
63-
result.styles.push(stripHtmlComments($(this).text()));
64-
}
65-
});
66-
67-
$('style').each(function() {
68-
result.styles.push(stripHtmlComments($(this).text()));
69-
});
70-
71-
status.total = result.links.length + result.styles.length;
72-
if (!status.total) {
73-
deferred.resolve(false);
74-
}
75-
76-
result.links.forEach(function(link) {
77-
getLinkContents(link.url, options)
78-
.then(function(css) {
79-
handleCssFromLink(link, css);
80-
})
81-
.catch(function(error) {
82-
link.error = error;
83-
status.parsed++;
84-
handleResolve();
85-
});
86-
});
87-
88-
result.styles.forEach(function(css) {
89-
result.css += css;
90-
status.parsed++;
91-
handleResolve();
92-
});
93-
}
94-
95-
function handleCssFromLink(link, css) {
96-
link.css += css;
97-
98-
parseCssForImports(link, css);
99-
100-
status.parsed++;
101-
handleResolve();
102-
}
103-
104-
// Handle potential @import url(foo.css) statements in the CSS.
105-
function parseCssForImports(link, css) {
106-
link.imports = resolveCssImportUrls(link.url, css);
107-
status.total += link.imports.length;
108-
result.css += css;
109-
110-
link.imports.forEach(function(importUrl) {
111-
var importLink = createLink(importUrl, importUrl);
112-
result.links.push(importLink);
113-
114-
getLinkContents(importLink.url, options)
115-
.then(function(css) {
116-
handleCssFromLink(importLink, css);
117-
})
118-
.catch(function(error) {
119-
link.error = error;
120-
status.parsed++;
121-
handleResolve();
122-
});
123-
});
124-
}
125-
126-
request(options, function(error, response, body) {
127-
if (error) {
128-
if (options.verbose) console.log('Error from ' + url + ' ' + error);
129-
deferred.reject(error);
130-
return;
131-
}
132-
133-
if (response && response.statusCode != 200) {
134-
if (options.verbose) console.log('Received a ' + response.statusCode + ' from: ' + url);
135-
deferred.reject({ url: url, statusCode: response.code });
136-
return;
137-
}
138-
139-
if (isCss(url)) {
140-
var link = createLink(url, url);
141-
result.links.push(link);
142-
handleCssFromLink(link, body);
143-
} else {
144-
parseHtml(body);
145-
}
146-
});
147-
148-
return deferred.promise;
1+
"use strict";
2+
3+
var q = require("q");
4+
var isCss = require("is-css");
5+
var isPresent = require("is-present");
6+
var isBlank = require("is-blank");
7+
var isUrl = require("is-url-superb");
8+
var request = require("request");
9+
var cheerio = require("cheerio");
10+
var normalizeUrl = require("normalize-url");
11+
var stripHtmlComments = require("strip-html-comments");
12+
var resolveCssImportUrls = require("resolve-css-import-urls");
13+
var ua = require("ua-string");
14+
15+
var getLinkContents = require("./utils/get-link-contents");
16+
var createLink = require("./utils/create-link");
17+
18+
module.exports = function(url, options, html) {
19+
var deferred = q.defer();
20+
var options = options || {};
21+
options.headers = options.headers || {};
22+
options.headers["User-Agent"] = options.headers["User-Agent"] || ua;
23+
options.timeout = options.timeout || 5000;
24+
options.gzip = true;
25+
26+
if (typeof url !== "string" || isBlank(url) || !isUrl(url)) {
27+
throw new TypeError("get-css expected a url as a string");
28+
}
29+
30+
url = normalizeUrl(url, { stripWWW: false });
31+
options.url = url;
32+
33+
if (options.ignoreCerts) {
34+
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
35+
}
36+
37+
var status = {
38+
parsed: 0,
39+
total: 0
40+
};
41+
42+
var result = {
43+
links: [],
44+
styles: [],
45+
css: ""
46+
};
47+
48+
function handleResolve() {
49+
if (status.parsed >= status.total) {
50+
deferred.resolve(result);
51+
}
52+
}
53+
54+
function parseHtml(html) {
55+
var $ = cheerio.load(html);
56+
result.pageTitle = $("head > title").text();
57+
58+
$("[rel=stylesheet]").each(function() {
59+
var link = $(this).attr("href");
60+
if (isPresent(link)) {
61+
result.links.push(createLink(link, url));
62+
} else {
63+
result.styles.push(stripHtmlComments($(this).text()));
64+
}
65+
});
66+
67+
$("style").each(function() {
68+
result.styles.push(stripHtmlComments($(this).text()));
69+
});
70+
71+
status.total = result.links.length + result.styles.length;
72+
if (!status.total) {
73+
deferred.resolve(false);
74+
}
75+
76+
result.links.forEach(function(link) {
77+
getLinkContents(link.url, options)
78+
.then(function(css) {
79+
handleCssFromLink(link, css);
80+
})
81+
.catch(function(error) {
82+
link.error = error;
83+
status.parsed++;
84+
handleResolve();
85+
});
86+
});
87+
88+
result.styles.forEach(function(css) {
89+
result.css += css;
90+
status.parsed++;
91+
handleResolve();
92+
});
93+
}
94+
95+
function handleCssFromLink(link, css) {
96+
link.css += css;
97+
98+
parseCssForImports(link, css);
99+
100+
status.parsed++;
101+
handleResolve();
102+
}
103+
104+
// Handle potential @import url(foo.css) statements in the CSS.
105+
function parseCssForImports(link, css) {
106+
link.imports = resolveCssImportUrls(link.url, css);
107+
status.total += link.imports.length;
108+
result.css += css;
109+
110+
link.imports.forEach(function(importUrl) {
111+
var importLink = createLink(importUrl, importUrl);
112+
result.links.push(importLink);
113+
114+
getLinkContents(importLink.url, options)
115+
.then(function(css) {
116+
handleCssFromLink(importLink, css);
117+
})
118+
.catch(function(error) {
119+
link.error = error;
120+
status.parsed++;
121+
handleResolve();
122+
});
123+
});
124+
}
125+
126+
function handleBody(body) {
127+
if (isCss(url)) {
128+
var link = createLink(url, url);
129+
result.links.push(link);
130+
handleCssFromLink(link, body);
131+
} else {
132+
parseHtml(body);
133+
}
134+
}
135+
136+
if (html) {
137+
handleBody(html);
138+
} else {
139+
request(options, function(error, response, body) {
140+
if (error) {
141+
if (options.verbose)
142+
console.log("Error from " + url + " " + error);
143+
deferred.reject(error);
144+
return;
145+
}
146+
147+
if (response && response.statusCode != 200) {
148+
if (options.verbose)
149+
console.log(
150+
"Received a " + response.statusCode + " from: " + url
151+
);
152+
deferred.reject({ url: url, statusCode: response.code });
153+
return;
154+
}
155+
156+
handleBody(body);
157+
});
158+
}
159+
160+
return deferred.promise;
149161
};

0 commit comments

Comments
 (0)