Skip to content

Commit ce6f5ac

Browse files
committed
fix: inserting CSS when no head
1 parent 3eb1f62 commit ce6f5ac

File tree

8 files changed

+584
-704
lines changed

8 files changed

+584
-704
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
node_modules
2+
.vscode

__tests__/.eslintrc.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1-
module.exports = Object.assign(require('@remy/eslint/jest'), {});
1+
const res = Object.assign({}, require('@remy/eslint/jest'), {
2+
parserOptions: {
3+
sourceType: 'module',
4+
ecmaVersion: 8,
5+
},
6+
});
7+
8+
module.exports = res;

__tests__/bins.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const fs = require('fs');
2+
const readdir = fs.readdirSync;
3+
const { promisify } = require('util');
4+
const readFile = promisify(fs.readFile);
5+
const base = __dirname + '/fixtures/bins';
6+
const fixtures = readdir(base);
7+
const binToFile = require('../');
8+
9+
describe('bin fixtures', () => {
10+
fixtures
11+
.filter(f => f.endsWith('.json'))
12+
.map(f => [`${base}/${f}`, f])
13+
.forEach(([filename, fixture]) => {
14+
it(fixture, async () => {
15+
const bin = await readFile(filename, 'utf8');
16+
const html = binToFile(JSON.parse(bin));
17+
18+
const expecting = await readFile(
19+
filename.replace(/\.json$/, '.html'),
20+
'utf8'
21+
);
22+
expect(html).toEqual(expecting.trim());
23+
});
24+
});
25+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<title>घर घर घर घर</title>
2+
<style id="jsbin-css">
3+
body {
4+
background: blue;
5+
}
6+
</style>
7+
8+
## Links: [Netflix](netflix.com)
9+
10+
<!--boot js--><script id="jsbin-javascript" defer>
11+
//# sourceURL=holy-smoke-F0C.js
12+
</script>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"html": "<title>घर घर घर घर</title>\n## Links: [Netflix](netflix.com)\n",
3+
"javascript": "\n//# sourceURL=holy-smoke-F0C.js",
4+
"css": "body {\n background: blue;\n}"
5+
}

lib/index.js

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,22 @@
11
const doctypeRe = new RegExp(/^<!doctype[^>]*>\n?/im);
22

3-
function hashOf(string) {
4-
let hash = 0;
5-
6-
if (string.length === 0) {
7-
return hash;
3+
function insert(source, needle, value, after = false) {
4+
needle = needle.toLowerCase();
5+
const sourceLC = source.toLowerCase();
6+
if (!sourceLC.includes(needle)) {
7+
return null;
88
}
99

10-
for (let i = 0; i < string.length; i++) {
11-
const c = string.charCodeAt(i);
12-
hash = (hash << 5) - hash + c;
13-
hash = hash & hash; // Convert to 32bit integer
14-
}
10+
let left = source.substring(0, sourceLC.lastIndexOf(needle));
1511

16-
return hash.toString(16);
17-
}
12+
let ri = sourceLC.lastIndexOf(needle);
1813

19-
function insert(source, needle, value) {
20-
needle = needle.toLowerCase();
21-
const sourceLC = source.toLowerCase();
22-
if (!source.toLowerCase().includes(needle)) {
23-
return null;
14+
if (after) {
15+
ri += needle.length;
16+
left += needle + '\n';
2417
}
2518

26-
const left = source.substring(0, sourceLC.lastIndexOf(needle));
27-
const right = source.substring(sourceLC.lastIndexOf(needle));
19+
const right = source.substring(ri);
2820

2921
if (left && right) {
3022
return left + value + right;
@@ -54,8 +46,8 @@ function binToFile(bin, options = {}) {
5446

5547
let file = '';
5648
let html = (bin.html || '').replace(/(\r\n)/g, '\n'); // remove windows nl.
57-
var css = safeForHTML(bin.css);
58-
var javascript = safeForHTML(bin.javascript);
49+
let css = safeForHTML(bin.css);
50+
let javascript = safeForHTML(bin.javascript);
5951
const { source, processors = {} } = bin;
6052
let meta =
6153
bin.meta ||
@@ -101,7 +93,7 @@ function binToFile(bin, options = {}) {
10193
if (head) {
10294
file = head;
10395
} else {
104-
var title = insert(file, '</title>', css);
96+
const title = insert(file, '</title>', css, true);
10597
if (title) {
10698
file = title;
10799
} else {
@@ -113,7 +105,7 @@ function binToFile(bin, options = {}) {
113105
}
114106

115107
// only look for a doctype at the top of the document
116-
var doctype =
108+
const doctype =
117109
(html
118110
.trim()
119111
.split('\n')
@@ -133,9 +125,7 @@ function binToFile(bin, options = {}) {
133125
file = file.split('%code%').join(javascript);
134126
} else {
135127
// is there head tag?
136-
javascript = `<!--boot js--><script id="jsbin-javascript">${
137-
javascript
138-
}\n</script>`;
128+
javascript = `<!--boot js--><script id="jsbin-javascript" defer>${javascript}\n</script>`;
139129
const body = insert(file, '</body>', javascript + '\n');
140130
if (body) {
141131
file = body;
@@ -166,11 +156,9 @@ function binToFile(bin, options = {}) {
166156

167157
const content = safeForHTML(source[type]);
168158
if (content) {
169-
return `\n<script id="jsbin-source-${
159+
return `\n<script id="jsbin-source-${type}" type="text/source-${processors[
170160
type
171-
}" type="text/source-${processors[type] || type}">${
172-
content
173-
}\n</script>`;
161+
] || type}">${content}\n</script>`;
174162
}
175163

176164
return '';
@@ -185,9 +173,6 @@ function binToFile(bin, options = {}) {
185173
}
186174
}
187175

188-
// no longer used
189-
// file = file.split('^^hash^^').join(hashOf(file));
190-
191176
return file;
192177
}
193178

0 commit comments

Comments
 (0)