UNPKG

1.3 kBJavaScriptView Raw
1'use strict'
2
3function injectStyleTag (document, fileName, cb) {
4 var style = document.getElementById(fileName)
5
6 if (style) {
7 cb(style)
8 } else {
9 var head = document.getElementsByTagName('head')[0]
10
11 style = document.createElement('style')
12 if (fileName != null) style.id = fileName
13 cb(style)
14 head.appendChild(style)
15 }
16
17 return style
18}
19
20module.exports = function (css, customDocument, fileName) {
21 var doc = customDocument || document
22 /* istanbul ignore if: not supported by Electron */
23 if (doc.createStyleSheet) {
24 var sheet = doc.createStyleSheet()
25 sheet.cssText = css
26 return sheet.ownerNode
27 } else {
28 return injectStyleTag(doc, fileName, function (style) {
29 /* istanbul ignore if: not supported by Electron */
30 if (style.styleSheet) {
31 style.styleSheet.cssText = css
32 } else {
33 style.innerHTML = css
34 }
35 })
36 }
37}
38
39module.exports.byUrl = function (url) {
40 /* istanbul ignore if: not supported by Electron */
41 if (document.createStyleSheet) {
42 return document.createStyleSheet(url).ownerNode
43 } else {
44 var head = document.getElementsByTagName('head')[0]
45 var link = document.createElement('link')
46
47 link.rel = 'stylesheet'
48 link.href = url
49
50 head.appendChild(link)
51 return link
52 }
53}