browser.js 3.6 KB

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