UNPKG

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