Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Insert meta tags inside <head> #2

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
}

var file = '';
var head = '';
var html = (bin.html || '').replace(/(\r\n)/g, '\n'); // remove windows nl.
var source = bin.source;
var css = safeForHTML(bin.css || '');
Expand Down Expand Up @@ -112,7 +113,7 @@
} else {
// is there head tag?
css = '<style id="jsbin-css">\n' + css + '\n</style>\n';
var head = insert(file, '</head>', css);
head = insert(file, '</head>', css);
if (head) {
file = head;
} else {
Expand All @@ -131,8 +132,14 @@
var doctype = (html.trim().split('\n').shift().trim().match(doctypeRe) || [])[0] || '';

if (doctype) {
file = file.replace(doctypeRe, doctype + '\n' + meta);
// strip from original html
// is there head tag?
head = insert(file, '</head>', meta);
if (head) {
file = head;
} else {
// slap on the top
file = file.replace(doctypeRe, doctype + '\n' + meta);
}
} else {
file = meta + file;
}
Expand Down Expand Up @@ -191,4 +198,4 @@

return file;
};
});
});
19 changes: 14 additions & 5 deletions test/integrity.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
'use strict';
/*global describe, it, beforeEach, after */
var w3cjs = require('w3cjs');
var assert = require('assert');
var toFile = require('../');
var fs = require('fs');
var path = require('path');
var cheerio = require('cheerio');

describe('integrity', function () {
var filename = path.join(__dirname, 'fixtures', 'tmp-' + (Math.random() * 1000 | 0) + '.html');
Expand All @@ -13,10 +15,6 @@ describe('integrity', function () {
html = fs.readFileSync(path.join(__dirname, 'fixtures', 'simple.html'), 'utf8');
});

after(function () {
fs.unlinkSync(filename);
});

it('should create valid document with just HTML, CSS & JS', function (done) {
var javascript = 'alert("Hello world");';
var css = 'body { background-color: red; }';
Expand All @@ -34,6 +32,17 @@ describe('integrity', function () {
done();
}
});

fs.unlinkSync(filename);
});

it('should place meta tags inside head tag', function () {
var meta = '<meta name="robots" content="noindex">';
var file = toFile({ html: html, javascript: null, css: null, meta: meta });

var $ = cheerio.load(file);

assert.equal($('head').find('> meta').length, 2);
});

});
});