browser.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var tslib_1 = require("tslib");
  4. var utils = tslib_1.__importStar(require("./utils"));
  5. exports.default = {
  6. createCSS: function (document, styles, sheet) {
  7. // Strip the query-string
  8. var href = sheet.href || '';
  9. // If there is no title set, use the filename, minus the extension
  10. var id = "less:" + (sheet.title || utils.extractId(href));
  11. // If this has already been inserted into the DOM, we may need to replace it
  12. var oldStyleNode = document.getElementById(id);
  13. var keepOldStyleNode = false;
  14. // Create a new stylesheet node for insertion or (if necessary) replacement
  15. var styleNode = document.createElement('style');
  16. styleNode.setAttribute('type', 'text/css');
  17. if (sheet.media) {
  18. styleNode.setAttribute('media', sheet.media);
  19. }
  20. styleNode.id = id;
  21. if (!styleNode.styleSheet) {
  22. styleNode.appendChild(document.createTextNode(styles));
  23. // If new contents match contents of oldStyleNode, don't replace oldStyleNode
  24. keepOldStyleNode = (oldStyleNode !== null && oldStyleNode.childNodes.length > 0 && styleNode.childNodes.length > 0 &&
  25. oldStyleNode.firstChild.nodeValue === styleNode.firstChild.nodeValue);
  26. }
  27. var head = document.getElementsByTagName('head')[0];
  28. // If there is no oldStyleNode, just append; otherwise, only append if we need
  29. // to replace oldStyleNode with an updated stylesheet
  30. if (oldStyleNode === null || keepOldStyleNode === false) {
  31. var nextEl = sheet && sheet.nextSibling || null;
  32. if (nextEl) {
  33. nextEl.parentNode.insertBefore(styleNode, nextEl);
  34. }
  35. else {
  36. head.appendChild(styleNode);
  37. }
  38. }
  39. if (oldStyleNode && keepOldStyleNode === false) {
  40. oldStyleNode.parentNode.removeChild(oldStyleNode);
  41. }
  42. // For IE.
  43. // This needs to happen *after* the style element is added to the DOM, otherwise IE 7 and 8 may crash.
  44. // See http://social.msdn.microsoft.com/Forums/en-US/7e081b65-878a-4c22-8e68-c10d39c2ed32/internet-explorer-crashes-appending-style-element-to-head
  45. if (styleNode.styleSheet) {
  46. try {
  47. styleNode.styleSheet.cssText = styles;
  48. }
  49. catch (e) {
  50. throw new Error('Couldn\'t reassign styleSheet.cssText.');
  51. }
  52. }
  53. },
  54. currentScript: function (window) {
  55. var document = window.document;
  56. return document.currentScript || (function () {
  57. var scripts = document.getElementsByTagName('script');
  58. return scripts[scripts.length - 1];
  59. })();
  60. }
  61. };
  62. //# sourceMappingURL=browser.js.map