ssr-window.esm.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. /* eslint-disable no-param-reassign */
  13. function isObject(obj) {
  14. return (obj !== null &&
  15. typeof obj === 'object' &&
  16. 'constructor' in obj &&
  17. obj.constructor === Object);
  18. }
  19. function extend(target, src) {
  20. if (target === void 0) { target = {}; }
  21. if (src === void 0) { src = {}; }
  22. Object.keys(src).forEach(function (key) {
  23. if (typeof target[key] === 'undefined')
  24. target[key] = src[key];
  25. else if (isObject(src[key]) &&
  26. isObject(target[key]) &&
  27. Object.keys(src[key]).length > 0) {
  28. extend(target[key], src[key]);
  29. }
  30. });
  31. }
  32. var ssrDocument = {
  33. body: {},
  34. addEventListener: function () { },
  35. removeEventListener: function () { },
  36. activeElement: {
  37. blur: function () { },
  38. nodeName: '',
  39. },
  40. querySelector: function () {
  41. return null;
  42. },
  43. querySelectorAll: function () {
  44. return [];
  45. },
  46. getElementById: function () {
  47. return null;
  48. },
  49. createEvent: function () {
  50. return {
  51. initEvent: function () { },
  52. };
  53. },
  54. createElement: function () {
  55. return {
  56. children: [],
  57. childNodes: [],
  58. style: {},
  59. setAttribute: function () { },
  60. getElementsByTagName: function () {
  61. return [];
  62. },
  63. };
  64. },
  65. createElementNS: function () {
  66. return {};
  67. },
  68. importNode: function () {
  69. return null;
  70. },
  71. location: {
  72. hash: '',
  73. host: '',
  74. hostname: '',
  75. href: '',
  76. origin: '',
  77. pathname: '',
  78. protocol: '',
  79. search: '',
  80. },
  81. };
  82. function getDocument() {
  83. var doc = typeof document !== 'undefined' ? document : {};
  84. extend(doc, ssrDocument);
  85. return doc;
  86. }
  87. var ssrWindow = {
  88. document: ssrDocument,
  89. navigator: {
  90. userAgent: '',
  91. },
  92. location: {
  93. hash: '',
  94. host: '',
  95. hostname: '',
  96. href: '',
  97. origin: '',
  98. pathname: '',
  99. protocol: '',
  100. search: '',
  101. },
  102. history: {
  103. replaceState: function () { },
  104. pushState: function () { },
  105. go: function () { },
  106. back: function () { },
  107. },
  108. CustomEvent: function CustomEvent() {
  109. return this;
  110. },
  111. addEventListener: function () { },
  112. removeEventListener: function () { },
  113. getComputedStyle: function () {
  114. return {
  115. getPropertyValue: function () {
  116. return '';
  117. },
  118. };
  119. },
  120. Image: function () { },
  121. Date: function () { },
  122. screen: {},
  123. setTimeout: function () { },
  124. clearTimeout: function () { },
  125. matchMedia: function () {
  126. return {};
  127. },
  128. requestAnimationFrame: function (callback) {
  129. if (typeof setTimeout === 'undefined') {
  130. callback();
  131. return null;
  132. }
  133. return setTimeout(callback, 0);
  134. },
  135. cancelAnimationFrame: function (id) {
  136. if (typeof setTimeout === 'undefined') {
  137. return;
  138. }
  139. clearTimeout(id);
  140. },
  141. };
  142. function getWindow() {
  143. var win = typeof window !== 'undefined' ? window : {};
  144. extend(win, ssrWindow);
  145. return win;
  146. }
  147. export { extend, getDocument, getWindow, ssrDocument, ssrWindow };