UNPKG

@platform/css

Version:

Helpers for working with inline CSS.

39 lines (38 loc) 1.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var common_1 = require("../common"); function init() { var importStylesheet = function (url) { if (!common_1.is.browser) { return res; } if (exists('style', url)) { return res; } var head = document.head; var style = document.createElement('style'); style.type = 'text/css'; var css = "@import url('" + url + "')"; style.dataset.url = url; style.appendChild(document.createTextNode(css)); head.appendChild(style); return res; }; var res = { importStylesheet: importStylesheet, }; return res; } exports.init = init; function exists(tag, url) { return common_1.is.browser ? Boolean(findByUrl(tag, url)) : false; } function findByUrl(tag, url) { if (common_1.is.browser) { var items = Array.from(document.getElementsByTagName(tag)); return items.find(function (style) { return style.dataset.url === url; }); } else { return undefined; } }