From 8a1db8acbf008f3d2c9d26f26667348ca27bcf9f Mon Sep 17 00:00:00 2001 From: Davi Ferreira Date: Tue, 1 Dec 2015 10:41:29 +0100 Subject: [PATCH] Insert meta tags inside --- lib/index.js | 15 +++++++++++---- test/integrity.test.js | 19 ++++++++++++++----- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/lib/index.js b/lib/index.js index a3c93c2..45ad7a8 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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 || ''); @@ -112,7 +113,7 @@ } else { // is there head tag? css = '\n'; - var head = insert(file, '', css); + head = insert(file, '', css); if (head) { file = head; } else { @@ -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, '', meta); + if (head) { + file = head; + } else { + // slap on the top + file = file.replace(doctypeRe, doctype + '\n' + meta); + } } else { file = meta + file; } @@ -191,4 +198,4 @@ return file; }; -}); \ No newline at end of file +}); diff --git a/test/integrity.test.js b/test/integrity.test.js index 1608a8f..0d529dc 100644 --- a/test/integrity.test.js +++ b/test/integrity.test.js @@ -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'); @@ -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; }'; @@ -34,6 +32,17 @@ describe('integrity', function () { done(); } }); + + fs.unlinkSync(filename); + }); + + it('should place meta tags inside head tag', function () { + var meta = ''; + var file = toFile({ html: html, javascript: null, css: null, meta: meta }); + + var $ = cheerio.load(file); + + assert.equal($('head').find('> meta').length, 2); }); -}); \ No newline at end of file +});