Skip to content

Commit dcc1116

Browse files
committed
More tests 'n stuff
1 parent fa1b6e1 commit dcc1116

File tree

5 files changed

+84
-2
lines changed

5 files changed

+84
-2
lines changed

lib/index.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@
4646
var processors = bin.processors || {};
4747
var meta = bin.meta || (bin.url ? '<!-- source: http://jsbin.com/' + bin.url + '/' + (bin.revision || '') + ' -->\n' : '');
4848

49+
if (meta && meta.slice(-1) !== '\n') {
50+
meta += '\n'; // a nice new line for the meta data
51+
}
52+
4953
/**
5054
* 1. strip the doctype and print it then add comment (<!-- file... etc)
5155
* 2. in remaining code:
@@ -117,13 +121,25 @@
117121

118122
// If we have the raw panel content - go ahead and stick that in scripts at the bottom.
119123
if (source) {
124+
125+
if (source.css === css) {
126+
delete source.css;
127+
}
128+
if (source.javascript === javascript) {
129+
delete source.javascript;
130+
}
131+
if (source.html === html) {
132+
delete source.html;
133+
}
134+
120135
var sourceScripts = ['html', 'css', 'javascript'].map(function (type) {
121136
if (source[type] === undefined) {
122137
return '';
123138
}
139+
124140
var content = safeForHTML(source[type]);
125141
if (content) {
126-
return '\n<script id="jsbin-source-' + type + '" type="text/' + (processors[type] || type) + '">\n' + content + '</script>';
142+
return '\n<script id="jsbin-source-' + type + '" type="text/' + (processors[type] || type) + '">' + content + '</script>';
127143
}
128144
}).join('\n');
129145

package.json

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

test/empty.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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('empty bin', 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 at end when missing </body>', function () {
16+
var file = toFile({ html: html, javascript: '', css: '' });
17+
18+
assert(file === html, 'output is exactly the same');
19+
});
20+
});

test/fixtures/messedup.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<body>
4+
<style>body { font-family: "Helvetica", "Myriad Pro", sans-serif; } h1{font-size:3em} #readme{width:700px;margin:0 auto}</style>
5+
<meta charset=utf-8 />
6+
<title>JS Bin</t</head>
7+
<body>
8+
<div id="readme">
9+
<article class="markdown-body">
10+
<h1>
11+
<a name="how-to-find-the-pin-url" class="anchor" href="#how-to-find-the-pin-url"><span class="mini-icon mini-icon-link"></span></a>How to find for the Pin URL</h1>

test/full.test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
var cheerio = require('cheerio');
8+
9+
describe('full bin insert', function () {
10+
var html = '';
11+
12+
beforeEach(function () {
13+
html = fs.readFileSync(path.join(__dirname, 'fixtures', 'simple.html'), 'utf8');
14+
});
15+
16+
it('should also store source panel content', function () {
17+
// var html = fs.readFileSync(path.join(__dirname, 'fixtures', 'messedup.html'), 'utf8');
18+
var javascript = 'alert("Hello world");';
19+
var css = 'body { background: red; }';
20+
var meta = '<!-- test -->';
21+
var modifiedHTML = html + '<!-- tested -->\n<script>alert("foo");</script>';
22+
var file = toFile({ html: html, javascript: javascript, css: css, meta: meta, source: { html: modifiedHTML, javascript: javascript + '\n// tested', css: css + '\n/* tested */'} });
23+
24+
var $ = cheerio.load(file);
25+
var sourceHTML = $('#jsbin-source-html').text();
26+
27+
assert(sourceHTML.indexOf('<\\!-- tested -->') !== -1, 'source HTML is present');
28+
29+
$ = cheerio.load(sourceHTML.replace(/<\\!--/, '<!--').replace(/<\\\/script>/i, '</script>'));
30+
31+
assert($.html() === modifiedHTML, 'pulling HTML back out is correct');
32+
33+
});
34+
})

0 commit comments

Comments
 (0)