virtual-document.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
  2. function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
  3. function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
  4. function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
  5. function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
  6. function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i["return"] && (_r = _i["return"](), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
  7. function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
  8. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  9. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
  10. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
  11. function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
  12. function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
  13. var FakeDomNode = /*#__PURE__*/function () {
  14. function FakeDomNode(tagName) {
  15. _classCallCheck(this, FakeDomNode);
  16. this._childNodes = [];
  17. this._attributes = {};
  18. this._parentNode = null;
  19. this.nodeType = 1;
  20. this.tagName = tagName;
  21. }
  22. _createClass(FakeDomNode, [{
  23. key: "appendChild",
  24. value: function appendChild(child) {
  25. this._childNodes.push(child);
  26. child.parentNode = this;
  27. }
  28. }, {
  29. key: "insertBefore",
  30. value: function insertBefore(child, before) {
  31. if (before === null) {
  32. this.appendChild(child);
  33. return;
  34. }
  35. var index = this._childNodes.indexOf(before);
  36. if (index === -1) {
  37. throw new Error('Node not found');
  38. }
  39. this._childNodes.splice(index, 0, child);
  40. child.parentNode = this;
  41. }
  42. }, {
  43. key: "addEventListener",
  44. value: function addEventListener(type, listener) {}
  45. }, {
  46. key: "removeEventListener",
  47. value: function removeEventListener(type, listener) {}
  48. }, {
  49. key: "setAttribute",
  50. value: function setAttribute(name, value) {
  51. this._attributes[name] = value;
  52. }
  53. }, {
  54. key: "removeAttribute",
  55. value: function removeAttribute(name) {
  56. delete this._attributes[name];
  57. }
  58. }, {
  59. key: "nextSibling",
  60. get: function get() {
  61. var _this$parentNode;
  62. var siblings = ((_this$parentNode = this.parentNode) === null || _this$parentNode === void 0 ? void 0 : _this$parentNode._childNodes) || [];
  63. var index = siblings.indexOf(this);
  64. return siblings[index + 1] || null;
  65. }
  66. }, {
  67. key: "parentNode",
  68. get: function get() {
  69. return this._parentNode;
  70. },
  71. set: function set(node) {
  72. this._parentNode = node;
  73. }
  74. }, {
  75. key: "innerHTML",
  76. get: function get() {
  77. var _this$_childNodes;
  78. var html = '';
  79. var serializeNode = function serializeNode(node) {
  80. var ret = '';
  81. if (node.nodeType === 1) {
  82. ret += "<".concat(node.tagName);
  83. for (var _i = 0, _Object$entries = Object.entries(node._attributes); _i < _Object$entries.length; _i++) {
  84. var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2),
  85. name = _Object$entries$_i[0],
  86. value = _Object$entries$_i[1];
  87. ret += " ".concat(name, "=\"").concat(value, "\"");
  88. }
  89. ret += ">".concat(node._childNodes.length ? node.innerHTML : node.textContent, "</").concat(node.tagName, ">");
  90. } else if (node.nodeType === 3) {
  91. ret += node.textContent;
  92. } else {
  93. throw new Error("unknown node type ".concat(node.nodeType));
  94. }
  95. return ret;
  96. };
  97. if ((_this$_childNodes = this._childNodes) !== null && _this$_childNodes !== void 0 && _this$_childNodes.length) {
  98. this._childNodes.forEach(function (child) {
  99. html += serializeNode(child);
  100. });
  101. } else {
  102. return serializeNode(this);
  103. }
  104. return html;
  105. }
  106. }, {
  107. key: "textContent",
  108. get: function get() {
  109. if (this.nodeType === 3) {
  110. return String(this.data) || '';
  111. } else {
  112. return this.innnerText;
  113. }
  114. },
  115. set: function set(text) {
  116. if (this.nodeType === 3) {
  117. this.data = text;
  118. } else {
  119. this.innnerText = text;
  120. }
  121. }
  122. }, {
  123. key: "firstChild",
  124. get: function get() {
  125. return this._childNodes[0];
  126. }
  127. }, {
  128. key: "removeChild",
  129. value: function removeChild(child) {
  130. var index = this._childNodes.indexOf(child);
  131. if (index === -1) {
  132. throw new Error('Node not found');
  133. }
  134. this._childNodes.splice(index, 1);
  135. child.parentNode = null;
  136. }
  137. }, {
  138. key: "childNodes",
  139. get: function get() {
  140. return this._childNodes;
  141. }
  142. }]);
  143. return FakeDomNode;
  144. }();
  145. function throwNotImplemented() {
  146. throw new Error('not implemented!');
  147. }
  148. var virtualDocument = {
  149. createElement: function createElement(tagName, options) {
  150. var node = new FakeDomNode(tagName);
  151. return node;
  152. },
  153. createTextNode: function createTextNode(text) {
  154. var node = new FakeDomNode('#text');
  155. node.nodeType = 3;
  156. node.textContent = text;
  157. return node;
  158. },
  159. createElementNS: throwNotImplemented
  160. };
  161. export { FakeDomNode, virtualDocument };