Skip to content

Commit 2113a35

Browse files
Merge branch 'master' into master
2 parents 86ea5f8 + ca8cf9b commit 2113a35

File tree

5 files changed

+193
-79
lines changed

5 files changed

+193
-79
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
node_modules
2+
package-lock.json
3+
.DS_Store

index.js

+49-42
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,37 @@
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('requestretry');
9-
var cheerio = require('cheerio');
10-
var normalizeUrl = require('normalize-url');
11-
var stripHtmlComments = require('strip-html-comments');
12-
var stripWaybackToolbar = require('strip-wayback-toolbar')
13-
var resolveCssImportUrls = require('resolve-css-import-urls');
14-
var ua = require('ua-string');
15-
16-
var getLinkContents = require('./utils/get-link-contents');
17-
var createLink = require('./utils/create-link');
1+
var q = require("q");
2+
var isCss = require("is-css");
3+
var isPresent = require("is-present");
4+
var isBlank = require("is-blank");
5+
var isUrl = require("is-url-superb");
6+
var request = require("requestretry");
7+
var cheerio = require("cheerio");
8+
var normalizeUrl = require("normalize-url");
9+
var stripHtmlComments = require("strip-html-comments");
10+
var stripWaybackToolbar = require("strip-wayback-toolbar")
11+
var resolveCssImportUrls = require("resolve-css-import-urls");
12+
var ua = require("ua-string");
13+
14+
var getLinkContents = require("./utils/get-link-contents");
15+
var createLink = require("./utils/create-link");
1816

1917
module.exports = function(url, options){
2018
var deferred = q.defer();
2119
var options = options || {};
2220
options.headers = options.headers || {};
23-
options.headers['User-Agent'] = options.headers['User-Agent'] || ua;
21+
options.headers["User-Agent"] = options.headers["User-Agent"] || ua;
2422
options.timeout = options.timeout || 5000;
2523
options.stripWayback = options.stripWayback || false;
2624
options.gzip = true;
2725

28-
if (typeof url !== 'string' || isBlank(url) || !isUrl(url)) {
29-
throw new TypeError('get-css expected a url as a string')
26+
if (typeof url !== "string" || isBlank(url) || !isUrl(url)) {
27+
throw new TypeError("get-css expected a url as a string");
3028
}
3129

3230
url = normalizeUrl(url, { stripWWW: false });
3331
options.url = url;
3432

3533
if (options.ignoreCerts) {
36-
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
34+
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
3735
}
3836

3937
var status = {
@@ -44,7 +42,7 @@ module.exports = function(url, options){
4442
var result = {
4543
links: [],
4644
styles: [],
47-
css: ''
45+
css: ""
4846
};
4947

5048
function handleResolve() {
@@ -58,19 +56,19 @@ module.exports = function(url, options){
5856
html = stripWaybackToolbar(html);
5957
}
6058
var $ = cheerio.load(html);
61-
result.pageTitle = $('head > title').text();
59+
result.pageTitle = $("head > title").text();
6260
result.html = html;
6361

64-
$('[rel=stylesheet]').each(function() {
65-
var link = $(this).attr('href');
66-
if(isPresent(link)) {
62+
$("[rel=stylesheet]").each(function() {
63+
var link = $(this).attr("href");
64+
if (isPresent(link)) {
6765
result.links.push(createLink(link, url));
68-
}else{
66+
} else{
6967
result.styles.push(stripHtmlComments($(this).text()));
7068
}
7169
});
7270

73-
$('style').each(function() {
71+
$("style").each(function() {
7472
result.styles.push(stripHtmlComments($(this).text()));
7573
});
7674

@@ -129,27 +127,36 @@ module.exports = function(url, options){
129127
});
130128
}
131129

132-
request(options, function(error, response, body) {
133-
if (error) {
134-
if (options.verbose) console.log('Error from ' + url + ' ' + error);
135-
deferred.reject(error);
136-
return;
137-
}
138-
139-
if (response && response.statusCode != 200) {
140-
if (options.verbose) console.log('Received a ' + response.statusCode + ' from: ' + url);
141-
deferred.reject({ url: url, statusCode: response.code });
142-
return;
143-
}
144-
130+
function handleBody(body) {
145131
if (isCss(url)) {
146132
var link = createLink(url, url);
147133
result.links.push(link);
148134
handleCssFromLink(link, body);
149135
} else {
150136
parseHtml(body);
151137
}
152-
});
138+
}
139+
140+
if (html) {
141+
handleBody(html);
142+
} else {
143+
request(options, function(error, response, body) {
144+
if (error) {
145+
if (options.verbose) console.log("Error from " + url + " " + error);
146+
deferred.reject(error);
147+
return;
148+
}
149+
150+
if (response && response.statusCode != 200) {
151+
if (options.verbose)
152+
console.log("Received a " + response.statusCode + " from: " + url);
153+
deferred.reject({ url: url, statusCode: response.code });
154+
return;
155+
}
156+
157+
handleBody(body);
158+
});
159+
}
153160

154161
return deferred.promise;
155162
};

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "get-css",
3-
"version": "1.2.5",
3+
"version": "1.2.6",
44
"description": "Get CSS from a URL",
55
"main": "index.js",
66
"scripts": {

test/results.json

+128-36
Large diffs are not rendered by default.

test/utils/html-test.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var assert = require("assert");
2+
var getCss = require("../../");
3+
4+
var css = "h1 { color: tomato; }";
5+
var html = "<style>" + css + "</style><h1>Hello, world!</h1>";
6+
7+
describe("html", function() {
8+
it("should correctly extract css from raw html", function() {
9+
getCss("http://example.com/", null, html).then(function(response) {
10+
asset.deepEqual(css, response.css);
11+
});
12+
});
13+
});

0 commit comments

Comments
 (0)