ssr-window.esm.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**
  2. * SSR Window 2.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: May 12, 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 doc = typeof document !== 'undefined' ? document : {};
  33. var ssrDocument = {
  34. body: {},
  35. addEventListener: function () { },
  36. removeEventListener: function () { },
  37. activeElement: {
  38. blur: function () { },
  39. nodeName: '',
  40. },
  41. querySelector: function () {
  42. return null;
  43. },
  44. querySelectorAll: function () {
  45. return [];
  46. },
  47. getElementById: function () {
  48. return null;
  49. },
  50. createEvent: function () {
  51. return {
  52. initEvent: function () { },
  53. };
  54. },
  55. createElement: function () {
  56. return {
  57. children: [],
  58. childNodes: [],
  59. style: {},
  60. setAttribute: function () { },
  61. getElementsByTagName: function () {
  62. return [];
  63. },
  64. };
  65. },
  66. createElementNS: function () {
  67. return {};
  68. },
  69. importNode: function () {
  70. return null;
  71. },
  72. location: {
  73. hash: '',
  74. host: '',
  75. hostname: '',
  76. href: '',
  77. origin: '',
  78. pathname: '',
  79. protocol: '',
  80. search: '',
  81. },
  82. };
  83. extend(doc, ssrDocument);
  84. var win = typeof window !== 'undefined' ? window : {};
  85. var ssrWindow = {
  86. document: ssrDocument,
  87. navigator: {
  88. userAgent: '',
  89. },
  90. location: {
  91. hash: '',
  92. host: '',
  93. hostname: '',
  94. href: '',
  95. origin: '',
  96. pathname: '',
  97. protocol: '',
  98. search: '',
  99. },
  100. history: {
  101. replaceState: function () { },
  102. pushState: function () { },
  103. go: function () { },
  104. back: function () { },
  105. },
  106. CustomEvent: function CustomEvent() {
  107. return this;
  108. },
  109. addEventListener: function () { },
  110. removeEventListener: function () { },
  111. getComputedStyle: function () {
  112. return {
  113. getPropertyValue: function () {
  114. return '';
  115. },
  116. };
  117. },
  118. Image: function () { },
  119. Date: function () { },
  120. screen: {},
  121. setTimeout: function () { },
  122. clearTimeout: function () { },
  123. matchMedia: function () {
  124. return {};
  125. },
  126. };
  127. extend(win, ssrWindow);
  128. export { doc as document, extend, win as window };