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

Commit 2d3fb10

Browse files
authored
Merge pull request #46 from cssstats/prettier
Use prettier
2 parents 99143f3 + 176b3a6 commit 2d3fb10

File tree

5 files changed

+153
-132
lines changed

5 files changed

+153
-132
lines changed

index.js

+94-94
Original file line numberDiff line numberDiff line change
@@ -1,162 +1,162 @@
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");
16-
17-
module.exports = function(url, options, html){
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.stripWayback = options.stripWayback || false;
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");
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')
16+
17+
module.exports = function(url, options, html) {
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.stripWayback = options.stripWayback || false
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')
2828
}
2929

30-
url = normalizeUrl(url, { stripWWW: false });
31-
options.url = url;
30+
url = normalizeUrl(url, { stripWWW: false })
31+
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 = {
3838
parsed: 0,
3939
total: 0
40-
};
40+
}
4141

4242
var result = {
4343
links: [],
4444
styles: [],
45-
css: ""
46-
};
45+
css: ''
46+
}
4747

4848
function handleResolve() {
4949
if (status.parsed >= status.total) {
50-
deferred.resolve(result);
50+
deferred.resolve(result)
5151
}
5252
}
5353

5454
function parseHtml(html) {
5555
if (options.stripWayback) {
56-
html = stripWaybackToolbar(html);
56+
html = stripWaybackToolbar(html)
5757
}
58-
var $ = cheerio.load(html);
59-
result.pageTitle = $("head > title").text();
60-
result.html = html;
61-
62-
$("[rel=stylesheet]").each(function() {
63-
var link = $(this).attr("href");
64-
if (isPresent(link)) {
65-
result.links.push(createLink(link, url));
66-
} else{
67-
result.styles.push(stripHtmlComments($(this).text()));
68-
}
69-
});
70-
71-
$("style").each(function() {
72-
result.styles.push(stripHtmlComments($(this).text()));
73-
});
74-
75-
status.total = result.links.length + result.styles.length;
58+
var $ = cheerio.load(html)
59+
result.pageTitle = $('head > title').text()
60+
result.html = html
61+
62+
$('[rel=stylesheet]').each(function() {
63+
var link = $(this).attr('href')
64+
if (isPresent(link)) {
65+
result.links.push(createLink(link, url))
66+
} else {
67+
result.styles.push(stripHtmlComments($(this).text()))
68+
}
69+
})
70+
71+
$('style').each(function() {
72+
result.styles.push(stripHtmlComments($(this).text()))
73+
})
74+
75+
status.total = result.links.length + result.styles.length
7676
if (!status.total) {
77-
deferred.resolve(false);
77+
deferred.resolve(false)
7878
}
7979

8080
result.links.forEach(function(link) {
8181
getLinkContents(link.url, options)
8282
.then(function(css) {
83-
handleCssFromLink(link, css);
83+
handleCssFromLink(link, css)
8484
})
8585
.catch(function(error) {
86-
link.error = error;
87-
status.parsed++;
88-
handleResolve();
89-
});
90-
});
86+
link.error = error
87+
status.parsed++
88+
handleResolve()
89+
})
90+
})
9191

9292
result.styles.forEach(function(css) {
93-
result.css += css;
94-
status.parsed++;
95-
handleResolve();
96-
});
93+
result.css += css
94+
status.parsed++
95+
handleResolve()
96+
})
9797
}
9898

9999
function handleCssFromLink(link, css) {
100-
link.css += css;
100+
link.css += css
101101

102-
parseCssForImports(link, css);
102+
parseCssForImports(link, css)
103103

104-
status.parsed++;
105-
handleResolve();
104+
status.parsed++
105+
handleResolve()
106106
}
107107

108108
// Handle potential @import url(foo.css) statements in the CSS.
109109
function parseCssForImports(link, css) {
110-
link.imports = resolveCssImportUrls(link.url, css);
111-
status.total += link.imports.length;
112-
result.css += css;
110+
link.imports = resolveCssImportUrls(link.url, css)
111+
status.total += link.imports.length
112+
result.css += css
113113

114114
link.imports.forEach(function(importUrl) {
115-
var importLink = createLink(importUrl, importUrl);
116-
result.links.push(importLink);
115+
var importLink = createLink(importUrl, importUrl)
116+
result.links.push(importLink)
117117

118118
getLinkContents(importLink.url, options)
119119
.then(function(css) {
120-
handleCssFromLink(importLink, css);
120+
handleCssFromLink(importLink, css)
121121
})
122122
.catch(function(error) {
123-
link.error = error;
124-
status.parsed++;
125-
handleResolve();
126-
});
127-
});
123+
link.error = error
124+
status.parsed++
125+
handleResolve()
126+
})
127+
})
128128
}
129129

130130
function handleBody(body) {
131131
if (isCss(url)) {
132-
var link = createLink(url, url);
133-
result.links.push(link);
134-
handleCssFromLink(link, body);
132+
var link = createLink(url, url)
133+
result.links.push(link)
134+
handleCssFromLink(link, body)
135135
} else {
136-
parseHtml(body);
136+
parseHtml(body)
137137
}
138138
}
139139

140140
if (html) {
141-
handleBody(html);
141+
handleBody(html)
142142
} else {
143143
request(options, function(error, response, body) {
144144
if (error) {
145-
if (options.verbose) console.log("Error from " + url + " " + error);
146-
deferred.reject(error);
147-
return;
145+
if (options.verbose) console.log('Error from ' + url + ' ' + error)
146+
deferred.reject(error)
147+
return
148148
}
149149

150150
if (response && response.statusCode != 200) {
151151
if (options.verbose)
152-
console.log("Received a " + response.statusCode + " from: " + url);
153-
deferred.reject({ url: url, statusCode: response.code });
154-
return;
152+
console.log('Received a ' + response.statusCode + ' from: ' + url)
153+
deferred.reject({ url: url, statusCode: response.code })
154+
return
155155
}
156156

157-
handleBody(body);
158-
});
157+
handleBody(body)
158+
})
159159
}
160160

161-
return deferred.promise;
162-
};
161+
return deferred.promise
162+
}

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"description": "Get CSS from a URL",
55
"main": "index.js",
66
"scripts": {
7+
"format": "prettier --no-semi --single-quote --write {test,lib,bin}/**/*.js index.js",
78
"test": "node test/test && mocha test 'test/**/*.js'"
89
},
910
"bin": {
@@ -28,7 +29,8 @@
2829
"ua-string": "^1.0.0"
2930
},
3031
"devDependencies": {
31-
"mocha": "^3.2.0"
32+
"mocha": "^3.2.0",
33+
"prettier": "^1.11.1"
3234
},
3335
"repository": {
3436
"type": "git",

test/utils/create-link-test.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
var assert = require('assert');
2-
var createLink = require('../../utils/create-link');
1+
var assert = require('assert')
2+
var createLink = require('../../utils/create-link')
33

44
describe('create-link', function() {
5-
65
it('should create the correct link object', function() {
76
assert.deepEqual(
87
createLink('../bar.css', 'http://foo.com/css/my-css.css'),
9-
{ link: '../bar.css', url: 'http://foo.com/bar.css', css: '' });
10-
});
8+
{ link: '../bar.css', url: 'http://foo.com/bar.css', css: '' }
9+
)
10+
})
1111

1212
it('should correctly resolve full url links', function() {
1313
assert.deepEqual(
1414
createLink('http://foo.com/bar.css', 'http://foo.com/css/my-css.css'),
15-
{ link: 'http://foo.com/bar.css', url: 'http://foo.com/bar.css', css: '' });
16-
});
17-
});
15+
{ link: 'http://foo.com/bar.css', url: 'http://foo.com/bar.css', css: '' }
16+
)
17+
})
18+
})

test/utils/html-test.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
var assert = require("assert");
2-
var getCss = require("../../");
1+
var assert = require('assert')
2+
var getCss = require('../../')
33

4-
var css = "h1 { color: tomato; }";
5-
var html = "<style>" + css + "</style><h1>Hello, world!</h1>";
4+
var css = 'h1 { color: tomato; }'
5+
var html = '<style>' + css + '</style><h1>Hello, world!</h1>'
66

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-
});
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+
})

test/utils/resolve-url-test.js

+36-18
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,53 @@
1-
var assert = require('assert');
2-
var resolveUrl = require('../../utils/resolve-url');
1+
var assert = require('assert')
2+
var resolveUrl = require('../../utils/resolve-url')
33

44
describe('resolve-url', function() {
5-
65
it('should correctly resolve a .. relative link', function() {
7-
assert.equal(resolveUrl('http://foo.com/some/path', '../bar.css'), 'http://foo.com/some/bar.css');
8-
});
6+
assert.equal(
7+
resolveUrl('http://foo.com/some/path', '../bar.css'),
8+
'http://foo.com/some/bar.css'
9+
)
10+
})
911

1012
it('should correctly resolve a .. relative link when the url has a trailing /', function() {
11-
assert.equal(resolveUrl('http://foo.com/some/path/', '../bar.css'), 'http://foo.com/some/bar.css');
12-
});
13+
assert.equal(
14+
resolveUrl('http://foo.com/some/path/', '../bar.css'),
15+
'http://foo.com/some/bar.css'
16+
)
17+
})
1318

1419
it('should correctly resolve a relative link', function() {
15-
assert.equal(resolveUrl('http://foo.com/some/path', 'bar.css'), 'http://foo.com/some/path/bar.css');
16-
});
20+
assert.equal(
21+
resolveUrl('http://foo.com/some/path', 'bar.css'),
22+
'http://foo.com/some/path/bar.css'
23+
)
24+
})
1725

1826
it('should correctly return a full link', function() {
1927
assert.equal(
2028
resolveUrl('http://foo.com', 'http://foo.com/some/path/bar.css'),
21-
'http://foo.com/some/path/bar.css');
22-
});
29+
'http://foo.com/some/path/bar.css'
30+
)
31+
})
2332

2433
it('should correctly resolve an absolute link', function() {
25-
assert.equal(resolveUrl('http://foo.com/some/path', '/bar.css'), 'http://foo.com/bar.css');
26-
});
34+
assert.equal(
35+
resolveUrl('http://foo.com/some/path', '/bar.css'),
36+
'http://foo.com/bar.css'
37+
)
38+
})
2739

2840
it('should correctly resolve a relative url from an html file', function() {
29-
assert.equal(resolveUrl('http://foo.bar/awesome/baz.html', 'baz.css'), 'http://foo.bar/awesome/baz.css');
30-
});
41+
assert.equal(
42+
resolveUrl('http://foo.bar/awesome/baz.html', 'baz.css'),
43+
'http://foo.bar/awesome/baz.css'
44+
)
45+
})
3146

3247
it('should correctly resolve an absolute url from an html file', function() {
33-
assert.equal(resolveUrl('http://foo.bar/awesome/baz.html', '/baz.css'), 'http://foo.bar/baz.css');
34-
});
35-
});
48+
assert.equal(
49+
resolveUrl('http://foo.bar/awesome/baz.html', '/baz.css'),
50+
'http://foo.bar/baz.css'
51+
)
52+
})
53+
})

0 commit comments

Comments
 (0)