ssr-window.umd.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /**
  2. * SSR Window 3.0.0
  3. * Better handling for window object in SSR environment
  4. * https://github.com/nolimits4web/ssr-window
  5. *
  6. * Copyright 2020, Vladimir Kharlampidi
  7. *
  8. * Licensed under MIT
  9. *
  10. * Released on: November 9, 2020
  11. */
  12. (function (global, factory) {
  13. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  14. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  15. (global = global || self, factory(global.ssrWindow = {}));
  16. }(this, (function (exports) { 'use strict';
  17. /* eslint-disable no-param-reassign */
  18. function isObject(obj) {
  19. return (obj !== null &&
  20. typeof obj === 'object' &&
  21. 'constructor' in obj &&
  22. obj.constructor === Object);
  23. }
  24. function extend(target, src) {
  25. if (target === void 0) { target = {}; }
  26. if (src === void 0) { src = {}; }
  27. Object.keys(src).forEach(function (key) {
  28. if (typeof target[key] === 'undefined')
  29. target[key] = src[key];
  30. else if (isObject(src[key]) &&
  31. isObject(target[key]) &&
  32. Object.keys(src[key]).length > 0) {
  33. extend(target[key], src[key]);
  34. }
  35. });
  36. }
  37. var ssrDocument = {
  38. body: {},
  39. addEventListener: function () { },
  40. removeEventListener: function () { },
  41. activeElement: {
  42. blur: function () { },
  43. nodeName: '',
  44. },
  45. querySelector: function () {
  46. return null;
  47. },
  48. querySelectorAll: function () {
  49. return [];
  50. },
  51. getElementById: function () {
  52. return null;
  53. },
  54. createEvent: function () {
  55. return {
  56. initEvent: function () { },
  57. };
  58. },
  59. createElement: function () {
  60. return {
  61. children: [],
  62. childNodes: [],
  63. style: {},
  64. setAttribute: function () { },
  65. getElementsByTagName: function () {
  66. return [];
  67. },
  68. };
  69. },
  70. createElementNS: function () {
  71. return {};
  72. },
  73. importNode: function () {
  74. return null;
  75. },
  76. location: {
  77. hash: '',
  78. host: '',
  79. hostname: '',
  80. href: '',
  81. origin: '',
  82. pathname: '',
  83. protocol: '',
  84. search: '',
  85. },
  86. };
  87. function getDocument() {
  88. var doc = typeof document !== 'undefined' ? document : {};
  89. extend(doc, ssrDocument);
  90. return doc;
  91. }
  92. var ssrWindow = {
  93. document: ssrDocument,
  94. navigator: {
  95. userAgent: '',
  96. },
  97. location: {
  98. hash: '',
  99. host: '',
  100. hostname: '',
  101. href: '',
  102. origin: '',
  103. pathname: '',
  104. protocol: '',
  105. search: '',
  106. },
  107. history: {
  108. replaceState: function () { },
  109. pushState: function () { },
  110. go: function () { },
  111. back: function () { },
  112. },
  113. CustomEvent: function CustomEvent() {
  114. return this;
  115. },
  116. addEventListener: function () { },
  117. removeEventListener: function () { },
  118. getComputedStyle: function () {
  119. return {
  120. getPropertyValue: function () {
  121. return '';
  122. },
  123. };
  124. },
  125. Image: function () { },
  126. Date: function () { },
  127. screen: {},
  128. setTimeout: function () { },
  129. clearTimeout: function () { },
  130. matchMedia: function () {
  131. return {};
  132. },
  133. requestAnimationFrame: function (callback) {
  134. if (typeof setTimeout === 'undefined') {
  135. callback();
  136. return null;
  137. }
  138. return setTimeout(callback, 0);
  139. },
  140. cancelAnimationFrame: function (id) {
  141. if (typeof setTimeout === 'undefined') {
  142. return;
  143. }
  144. clearTimeout(id);
  145. },
  146. };
  147. function getWindow() {
  148. var win = typeof window !== 'undefined' ? window : {};
  149. extend(win, ssrWindow);
  150. return win;
  151. }
  152. exports.extend = extend;
  153. exports.getDocument = getDocument;
  154. exports.getWindow = getWindow;
  155. exports.ssrDocument = ssrDocument;
  156. exports.ssrWindow = ssrWindow;
  157. Object.defineProperty(exports, '__esModule', { value: true });
  158. })));
  159. //# sourceMappingURL=ssr-window.umd.js.map