Skip to content

Commit accaa17

Browse files
committed
More tests and fixes
1 parent abb4c5c commit accaa17

File tree

7 files changed

+123
-39
lines changed

7 files changed

+123
-39
lines changed

lib/index.js

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,17 @@
1313

1414
var doctypeRe = new RegExp(/^<!doctype[^>]*>\n?/im);
1515

16-
function insert(source, needle, value, options) {
16+
function insert(source, needle, value) {
1717
if (source.toLowerCase().indexOf(needle.toLowerCase()) === -1) {
1818
return null;
1919
}
2020

21-
if (!options) {
22-
options = {};
23-
}
24-
2521
var left = source.substring(0, source.toLowerCase().lastIndexOf(needle.toLowerCase()));
2622
var right = source.substring(source.toLowerCase().lastIndexOf(needle.toLowerCase()));
27-
28-
// var parts = source.split(needle);
29-
// var last = parts.pop();
3023
var result = '';
24+
3125
if (left && right) {
32-
// Add it just after before the end head tag if we can
33-
if (options.after) {
34-
result = left + needle + value + right;
35-
} else {
36-
result = left + value + '\n' + needle + right;
37-
}
26+
result = left + value + right;
3827
}
3928
return result;
4029
}
@@ -83,12 +72,12 @@
8372
file = file.split('%css%').join(bin.css);
8473
} else {
8574
// is there head tag?
86-
css = '<style>\n' + css + '\n<style>\n';
75+
css = '<style>\n' + css + '\n</style>\n';
8776
var head = insert(file, '</head>', css);
8877
if (head) {
8978
file = head;
9079
} else {
91-
var title = insert(file, '</title>', css, { after: true });
80+
var title = insert(file, '</title>', css);
9281
if (title) {
9382
file = title;
9483
} else {
@@ -115,8 +104,8 @@
115104
file = file.split('%code%').join(javascript);
116105
} else {
117106
// is there head tag?
118-
javascript = '<script>\n' + javascript + '\n</script>\n';
119-
var body = insert(file, '</body>', javascript);
107+
javascript = '<script>\n' + javascript + '\n</script>';
108+
var body = insert(file, '</body>', javascript + '\n');
120109
if (body) {
121110
file = body;
122111
} else {

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
},
2424
"homepage": "https://github.com/jsbin/bin-to-file",
2525
"devDependencies": {
26-
"mocha": "~1.20.1"
26+
"mocha": "~1.20.1",
27+
"w3cjs": "~0.1.25"
2728
}
2829
}

test/fixtures/barebones.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<meta charset="utf8">
2+
<title>Just a simple example</title>
3+
<body>
4+
<p>Nicely done sir.</p>

test/fixtures/no-doctype.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<html>
2+
<head>
3+
<meta charset="utf8">
4+
<title>Just a simple example</title>
5+
</head>
6+
<body>
7+
<p>Nicely done sir.</p>
8+
</body>
9+
</html>

test/html-only.test.js

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,4 @@ describe('simple html', function () {
2121

2222
assert(html === file, 'content matches');
2323
});
24-
25-
it('should insert meta description below doctype', function () {
26-
var meta = '<!-- testing -->';
27-
var file = toFile({ html: html, meta: meta });
28-
29-
assert(html !== file, 'content does not match');
30-
assert(file.indexOf(meta) !== -1, 'contains the metadata: ' + file);
31-
assert(file.split('\n').shift().indexOf('<!DOCTYPE') === 0, 'has doctype as first line: ' + file.split('\n').shift());
32-
});
33-
34-
it('should insert JS before the closing body', function () {
35-
var javascript = 'alert("Hello world");';
36-
var file = toFile({ html: html, javascript: javascript });
37-
38-
assert(file.indexOf(javascript) !== -1, 'contains the javascript: ' + file);
39-
40-
var lines = file.split('\n');
41-
var pos = lines.indexOf(javascript);
42-
assert(lines[pos + 2].indexOf('</body>') === 0, lines.join('\n'));
43-
});
4424
});

test/insert.test.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
'use strict';
2+
/*global describe, it, beforeEach */
3+
var assert = require('assert');
4+
var toFile = require('../');
5+
var fs = require('fs');
6+
var path = require('path');
7+
8+
describe('content insert', function () {
9+
var html = '';
10+
11+
beforeEach(function () {
12+
html = fs.readFileSync(path.join(__dirname, 'fixtures', 'simple.html'), 'utf8');
13+
});
14+
15+
it('should insert JS before the closing body', function () {
16+
var javascript = 'alert("Hello world");';
17+
var file = toFile({ html: html, javascript: javascript });
18+
19+
assert(file.indexOf(javascript) !== -1, 'contains the javascript: ' + file);
20+
21+
var lines = file.split('\n');
22+
var pos = lines.indexOf(javascript);
23+
assert(lines[pos + 2].indexOf('</body>') === 0, lines.join('\n'));
24+
});
25+
26+
it('should insert CSS before the closing head', function () {
27+
var css = 'body { background: red; }';
28+
var file = toFile({ html: html, css: css });
29+
30+
assert(file.indexOf(css) !== -1, 'contains the css: ' + file);
31+
32+
var lines = file.split('\n');
33+
var pos = lines.indexOf(css);
34+
assert(lines[pos + 2].indexOf('</head>') === 0, lines[pos+2]);
35+
});
36+
37+
it('should load after <title>', function () {
38+
var html = fs.readFileSync(path.join(__dirname, 'fixtures', 'barebones.html'), 'utf8');
39+
var css = 'body { background: red; }';
40+
var file = toFile({ html: html, css: css });
41+
42+
assert(file.indexOf(css) !== -1, 'contains the css: ' + file);
43+
44+
var lines = file.split('\n');
45+
var pos = lines.indexOf(css);
46+
assert(lines[pos + 2].indexOf('</title>') === 0, lines[pos + 2]);
47+
});
48+
49+
it('should insert JS at end when missing </body>', function () {
50+
var html = fs.readFileSync(path.join(__dirname, 'fixtures', 'barebones.html'), 'utf8');
51+
var javascript = 'alert("Hello world");';
52+
var file = toFile({ html: html, javascript: javascript });
53+
54+
assert(file.indexOf(javascript) !== -1, 'contains the javascript: ' + file);
55+
56+
var lines = file.split('\n');
57+
var pos = lines.indexOf(javascript);
58+
59+
assert(lines.slice(-1)[0] === '</script>', '???: ' + lines.slice(-2));
60+
});
61+
});

test/integrity.test.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict';
2+
/*global describe, it, beforeEach, after */
3+
var assert = require('assert');
4+
var w3cjs = require('w3cjs');
5+
var toFile = require('../');
6+
var fs = require('fs');
7+
var path = require('path');
8+
9+
describe('integrity', function () {
10+
var filename = path.join(__dirname, 'fixtures', 'tmp-' + (Math.random() * 1000 | 0) + '.html');
11+
var html = '';
12+
13+
beforeEach(function () {
14+
html = fs.readFileSync(path.join(__dirname, 'fixtures', 'simple.html'), 'utf8');
15+
});
16+
17+
after(function () {
18+
fs.unlinkSync(filename);
19+
});
20+
21+
it('should create valid document with just HTML, CSS & JS', function (done) {
22+
var javascript = 'alert("Hello world");';
23+
var css = 'body { background-color: red; }';
24+
var meta = '<!-- created during a test -->';
25+
var file = toFile({ html: html, javascript: javascript, css: css, meta: meta });
26+
27+
fs.writeFileSync(filename, file, 'utf8');
28+
29+
w3cjs.validate({
30+
file: filename,
31+
callback: function (res) {
32+
if (res.messages && res.messages.length > 0 ) {
33+
throw {error: 'html errors have been found', results: res};
34+
};
35+
done();
36+
}
37+
});
38+
});
39+
40+
});

0 commit comments

Comments
 (0)