-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
47 lines (36 loc) · 1.35 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"use strict";
/*!
* HTML Tag Injector
* -----------------
*
* Identifies the optimal place for top tags (e.g. CSS) and
* bottom tags (e.g. JS) to be injected into a HTML view.
*
* Copyright(c) 2013 Owen Barnes <[email protected]>
* MIT Licensed
*/
var topTag = '<head',
bottomTag = '</html>';
module.exports = function(originalHTML, options) {
options = options || {};
var topTags = options.top || "<TOP_TAGS/>";
var bottomTags = options.bottom || "<BOTTOM_TAGS/>";
var outputHTML,
lowecaseHTML = originalHTML.toLowerCase();
// Only process if critical tags are present
if (lowecaseHTML.indexOf(topTag) >= 0 && lowecaseHTML.indexOf(bottomTag) >= 0) {
// Insert topTagName (typically CSS) just after the <head> tag
var startOfTagIndex = lowecaseHTML.indexOf(topTag) + topTag.length;
var endOfTagIndex = lowecaseHTML.substring(startOfTagIndex).indexOf('>');
var topIndex = startOfTagIndex + endOfTagIndex + 1;
// Insert bottomTagName (typically JS) just after the closing </html> tag
var bottomIndex = lowecaseHTML.lastIndexOf(bottomTag);
// Compose HTML output
outputHTML = originalHTML.substring(0, topIndex) +
topTags +
originalHTML.substring(topIndex, bottomIndex) +
bottomTags + "\n" +
originalHTML.substring(bottomIndex, originalHTML.length);
}
return outputHTML;
};