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

Commit e84e3da

Browse files
authored
Merge pull request #45 from rsoury/master
Added support for HTML as a parameter
2 parents 1baaee1 + ac99a81 commit e84e3da

File tree

4 files changed

+199
-83
lines changed

4 files changed

+199
-83
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
node_modules
2+
package-lock.json
3+
.DS_Store

index.js

Lines changed: 56 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +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('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){
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) {
1919
var deferred = q.defer();
2020
var options = options || {};
2121
options.headers = options.headers || {};
22-
options.headers['User-Agent'] = options.headers['User-Agent'] || ua;
22+
options.headers["User-Agent"] = options.headers["User-Agent"] || ua;
2323
options.timeout = options.timeout || 5000;
2424
options.gzip = true;
2525

26-
if (typeof url !== 'string' || isBlank(url) || !isUrl(url)) {
27-
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");
2828
}
2929

3030
url = normalizeUrl(url, { stripWWW: false });
3131
options.url = url;
3232

3333
if (options.ignoreCerts) {
34-
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
34+
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
3535
}
3636

3737
var status = {
@@ -42,7 +42,7 @@ module.exports = function(url, options){
4242
var result = {
4343
links: [],
4444
styles: [],
45-
css: ''
45+
css: ""
4646
};
4747

4848
function handleResolve() {
@@ -53,18 +53,18 @@ module.exports = function(url, options){
5353

5454
function parseHtml(html) {
5555
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-
}
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+
}
6565
});
6666

67-
$('style').each(function() {
67+
$("style").each(function() {
6868
result.styles.push(stripHtmlComments($(this).text()));
6969
});
7070

@@ -123,27 +123,36 @@ module.exports = function(url, options){
123123
});
124124
}
125125

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-
126+
function handleBody(body) {
139127
if (isCss(url)) {
140128
var link = createLink(url, url);
141129
result.links.push(link);
142130
handleCssFromLink(link, body);
143131
} else {
144132
parseHtml(body);
145133
}
146-
});
134+
}
135+
136+
if (html) {
137+
handleBody(html);
138+
} else {
139+
request(options, function(error, response, body) {
140+
if (error) {
141+
if (options.verbose) console.log("Error from " + url + " " + error);
142+
deferred.reject(error);
143+
return;
144+
}
145+
146+
if (response && response.statusCode != 200) {
147+
if (options.verbose)
148+
console.log("Received a " + response.statusCode + " from: " + url);
149+
deferred.reject({ url: url, statusCode: response.code });
150+
return;
151+
}
152+
153+
handleBody(body);
154+
});
155+
}
147156

148157
return deferred.promise;
149158
};

test/results.json

Lines changed: 128 additions & 36 deletions
Large diffs are not rendered by default.

test/utils/html-test.js

Lines changed: 13 additions & 0 deletions
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)