{"version":3,"file":"index.js","sources":["../src/diffDOM/helpers.ts","../src/diffDOM/dom/fromVirtual.ts","../src/diffDOM/dom/apply.ts","../src/diffDOM/dom/undo.ts","../node_modules/tslib/tslib.es6.js","../src/diffDOM/virtual/helpers.ts","../src/diffDOM/virtual/apply.ts","../src/diffDOM/virtual/fromDOM.ts","../src/diffDOM/virtual/fromString.ts","../src/diffDOM/virtual/diff.ts","../src/diffDOM/index.ts","../src/TraceLogger.ts"],"sourcesContent":["import { elementNodeType } from \"./types\"\n\nexport class Diff {\n constructor(options = {}) {\n Object.entries(options).forEach(([key, value]) => (this[key] = value))\n }\n\n toString() {\n return JSON.stringify(this)\n }\n\n setValue(\n aKey: string | number,\n aValue:\n | string\n | number\n | boolean\n | number[]\n | { [key: string]: string | { [key: string]: string } }\n | elementNodeType,\n ) {\n this[aKey] = aValue\n return this\n }\n}\n\nexport function checkElementType(element, ...elementTypeNames: string[]) {\n if (typeof element === \"undefined\" || element === null) {\n return false\n }\n return elementTypeNames.some(\n (elementTypeName) =>\n // We need to check if the specified type is defined\n // because otherwise instanceof throws an exception.\n typeof element?.ownerDocument?.defaultView?.[elementTypeName] ===\n \"function\" &&\n element instanceof\n element.ownerDocument.defaultView[elementTypeName],\n )\n}\n","import { DiffDOMOptions, elementNodeType, textNodeType } from \"../types\"\nimport { checkElementType } from \"../helpers\"\n\nexport function objToNode(\n objNode: elementNodeType,\n insideSvg: boolean,\n options: DiffDOMOptions,\n) {\n let node: Element | Text | Comment\n if (objNode.nodeName === \"#text\") {\n node = options.document.createTextNode((objNode as textNodeType).data)\n } else if (objNode.nodeName === \"#comment\") {\n node = options.document.createComment((objNode as textNodeType).data)\n } else {\n if (insideSvg) {\n node = options.document.createElementNS(\n \"http://www.w3.org/2000/svg\",\n objNode.nodeName,\n )\n } else if (objNode.nodeName.toLowerCase() === \"svg\") {\n node = options.document.createElementNS(\n \"http://www.w3.org/2000/svg\",\n \"svg\",\n )\n insideSvg = true\n } else {\n node = options.document.createElement(objNode.nodeName)\n }\n if (objNode.attributes) {\n Object.entries(objNode.attributes).forEach(([key, value]) =>\n (node as Element).setAttribute(key, value),\n )\n }\n if (objNode.childNodes) {\n node = node as Element\n objNode.childNodes.forEach(\n (childNode: elementNodeType | textNodeType) =>\n node.appendChild(objToNode(childNode, insideSvg, options)),\n )\n }\n if (options.valueDiffing) {\n if (\n objNode.value &&\n checkElementType(\n node,\n \"HTMLButtonElement\",\n \"HTMLDataElement\",\n \"HTMLInputElement\",\n \"HTMLLIElement\",\n \"HTMLMeterElement\",\n \"HTMLOptionElement\",\n \"HTMLProgressElement\",\n \"HTMLParamElement\",\n )\n ) {\n ;(\n node as\n | HTMLButtonElement\n | HTMLDataElement\n | HTMLInputElement\n | HTMLLIElement\n | HTMLMeterElement\n | HTMLOptionElement\n | HTMLProgressElement\n | HTMLParamElement\n ).value = objNode.value\n }\n if (objNode.checked && checkElementType(node, \"HTMLInputElement\")) {\n ;(node as HTMLInputElement).checked = objNode.checked\n }\n if (\n objNode.selected &&\n checkElementType(node, \"HTMLOptionElement\")\n ) {\n ;(node as HTMLOptionElement).selected = objNode.selected\n }\n }\n }\n return node\n}\n","import { DiffDOMOptions, diffType, nodeType } from \"../types\"\nimport { Diff, checkElementType } from \"../helpers\"\n\nimport { objToNode } from \"./fromVirtual\"\n\n// ===== Apply a diff =====\n\nconst getFromRoute = (\n node: Element,\n route: number[],\n): Element | Text | false => {\n route = route.slice()\n while (route.length > 0) {\n const c = route.splice(0, 1)[0]\n node = node.childNodes[c] as Element\n }\n return node\n}\n\nexport function applyDiff(\n tree: Element,\n diff: diffType,\n options: DiffDOMOptions, // {preDiffApply, postDiffApply, textDiff, valueDiffing, _const}\n) {\n const action = diff[options._const.action] as string | number\n const route = diff[options._const.route] as number[]\n let node\n\n if (\n ![options._const.addElement, options._const.addTextElement].includes(\n action,\n )\n ) {\n // For adding nodes, we calculate the route later on. It's different because it includes the position of the newly added item.\n node = getFromRoute(tree, route)\n }\n\n let newNode\n let reference: Element\n let nodeArray\n\n // pre-diff hook\n const info = {\n diff,\n node,\n }\n\n if (options.preDiffApply(info)) {\n return true\n }\n\n switch (action) {\n case options._const.addAttribute:\n if (!node || !checkElementType(node, \"Element\")) {\n return false\n }\n node.setAttribute(\n diff[options._const.name] as string,\n diff[options._const.value] as string,\n )\n break\n case options._const.modifyAttribute:\n if (!node || !checkElementType(node, \"Element\")) {\n return false\n }\n node.setAttribute(\n diff[options._const.name] as string,\n diff[options._const.newValue] as string,\n )\n if (\n checkElementType(node, \"HTMLInputElement\") &&\n diff[options._const.name] === \"value\"\n ) {\n node.value = diff[options._const.newValue] as string\n }\n break\n case options._const.removeAttribute:\n if (!node || !checkElementType(node, \"Element\")) {\n return false\n }\n node.removeAttribute(diff[options._const.name] as string)\n break\n case options._const.modifyTextElement:\n if (!node || !checkElementType(node, \"Text\")) {\n return false\n }\n options.textDiff(\n node,\n node.data,\n diff[options._const.oldValue] as string,\n diff[options._const.newValue] as string,\n )\n if (checkElementType(node.parentNode, \"HTMLTextAreaElement\")) {\n node.parentNode.value = diff[options._const.newValue] as string\n }\n break\n case options._const.modifyValue:\n if (!node || typeof node.value === \"undefined\") {\n return false\n }\n node.value = diff[options._const.newValue]\n break\n case options._const.modifyComment:\n if (!node || !checkElementType(node, \"Comment\")) {\n return false\n }\n options.textDiff(\n node,\n node.data,\n diff[options._const.oldValue] as string,\n diff[options._const.newValue] as string,\n )\n break\n case options._const.modifyChecked:\n if (!node || typeof node.checked === \"undefined\") {\n return false\n }\n node.checked = diff[options._const.newValue]\n break\n case options._const.modifySelected:\n if (!node || typeof node.selected === \"undefined\") {\n return false\n }\n node.selected = diff[options._const.newValue]\n break\n case options._const.replaceElement: {\n const insideSvg =\n (\n diff[options._const.newValue] as nodeType\n ).nodeName.toLowerCase() === \"svg\" ||\n node.parentNode.namespaceURI === \"http://www.w3.org/2000/svg\"\n node.parentNode.replaceChild(\n objToNode(\n diff[options._const.newValue] as nodeType,\n insideSvg,\n options,\n ),\n node,\n )\n break\n }\n case options._const.relocateGroup:\n nodeArray = Array(\n ...new Array(diff[options._const.groupLength]),\n ).map(() =>\n node.removeChild(\n node.childNodes[diff[options._const.from] as number],\n ),\n )\n nodeArray.forEach((childNode, index) => {\n if (index === 0) {\n reference =\n node.childNodes[diff[options._const.to] as number]\n }\n node.insertBefore(childNode, reference || null)\n })\n break\n case options._const.removeElement:\n node.parentNode.removeChild(node)\n break\n case options._const.addElement: {\n const parentRoute = route.slice()\n const c: number = parentRoute.splice(parentRoute.length - 1, 1)[0]\n node = getFromRoute(tree, parentRoute)\n if (!checkElementType(node, \"Element\")) {\n return false\n }\n node.insertBefore(\n objToNode(\n diff[options._const.element] as nodeType,\n node.namespaceURI === \"http://www.w3.org/2000/svg\",\n options,\n ),\n node.childNodes[c] || null,\n )\n break\n }\n case options._const.removeTextElement: {\n if (!node || node.nodeType !== 3) {\n return false\n }\n const parentNode = node.parentNode\n parentNode.removeChild(node)\n if (checkElementType(parentNode, \"HTMLTextAreaElement\")) {\n parentNode.value = \"\"\n }\n break\n }\n case options._const.addTextElement: {\n const parentRoute = route.slice()\n const c: number = parentRoute.splice(parentRoute.length - 1, 1)[0]\n newNode = options.document.createTextNode(\n diff[options._const.value] as string,\n )\n node = getFromRoute(tree, parentRoute)\n if (!node.childNodes) {\n return false\n }\n node.insertBefore(newNode, node.childNodes[c] || null)\n if (checkElementType(node.parentNode, \"HTMLTextAreaElement\")) {\n node.parentNode.value = diff[options._const.value] as string\n }\n break\n }\n default:\n console.log(\"unknown action\")\n }\n\n // if a new node was created, we might be interested in its\n // post diff hook\n options.postDiffApply({\n diff: info.diff,\n node: info.node,\n newNode,\n })\n\n return true\n}\n\nexport function applyDOM(\n tree: Element,\n diffs: (Diff | diffType)[],\n options: DiffDOMOptions,\n) {\n return diffs.every((diff: Diff | diffType) =>\n applyDiff(tree, diff as diffType, options),\n )\n}\n","import { DiffDOMOptions, diffType } from \"../types\"\nimport { Diff } from \"../helpers\"\nimport { applyDiff } from \"./apply\"\n\n// ===== Undo a diff =====\n\nfunction swap(obj: object, p1: string | number, p2: string | number) {\n const tmp = obj[p1]\n obj[p1] = obj[p2]\n obj[p2] = tmp\n}\n\nfunction undoDiff(\n tree: Element,\n diff: diffType,\n options: DiffDOMOptions, // {preDiffApply, postDiffApply, textDiff, valueDiffing, _const}\n) {\n switch (diff[options._const.action]) {\n case options._const.addAttribute:\n diff[options._const.action] = options._const.removeAttribute\n applyDiff(tree, diff, options)\n break\n case options._const.modifyAttribute:\n swap(diff, options._const.oldValue, options._const.newValue)\n applyDiff(tree, diff, options)\n break\n case options._const.removeAttribute:\n diff[options._const.action] = options._const.addAttribute\n applyDiff(tree, diff, options)\n break\n case options._const.modifyTextElement:\n swap(diff, options._const.oldValue, options._const.newValue)\n applyDiff(tree, diff, options)\n break\n case options._const.modifyValue:\n swap(diff, options._const.oldValue, options._const.newValue)\n applyDiff(tree, diff, options)\n break\n case options._const.modifyComment:\n swap(diff, options._const.oldValue, options._const.newValue)\n applyDiff(tree, diff, options)\n break\n case options._const.modifyChecked:\n swap(diff, options._const.oldValue, options._const.newValue)\n applyDiff(tree, diff, options)\n break\n case options._const.modifySelected:\n swap(diff, options._const.oldValue, options._const.newValue)\n applyDiff(tree, diff, options)\n break\n case options._const.replaceElement:\n swap(diff, options._const.oldValue, options._const.newValue)\n applyDiff(tree, diff, options)\n break\n case options._const.relocateGroup:\n swap(diff, options._const.from, options._const.to)\n applyDiff(tree, diff, options)\n break\n case options._const.removeElement:\n diff[options._const.action] = options._const.addElement\n applyDiff(tree, diff, options)\n break\n case options._const.addElement:\n diff[options._const.action] = options._const.removeElement\n applyDiff(tree, diff, options)\n break\n case options._const.removeTextElement:\n diff[options._const.action] = options._const.addTextElement\n applyDiff(tree, diff, options)\n break\n case options._const.addTextElement:\n diff[options._const.action] = options._const.removeTextElement\n applyDiff(tree, diff, options)\n break\n default:\n console.log(\"unknown action\")\n }\n}\n\nexport function undoDOM(\n tree: Element,\n diffs: (diffType | Diff)[],\n options: DiffDOMOptions,\n) {\n diffs = diffs.slice()\n diffs.reverse()\n diffs.forEach((diff: diffType | Diff) => {\n undoDiff(tree, diff as diffType, options)\n })\n}\n","/******************************************************************************\r\nCopyright (c) Microsoft Corporation.\r\n\r\nPermission to use, copy, modify, and/or distribute this software for any\r\npurpose with or without fee is hereby granted.\r\n\r\nTHE SOFTWARE IS PROVIDED \"AS IS\" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH\r\nREGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY\r\nAND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,\r\nINDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM\r\nLOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR\r\nOTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR\r\nPERFORMANCE OF THIS SOFTWARE.\r\n***************************************************************************** */\r\n/* global Reflect, Promise, SuppressedError, Symbol */\r\n\r\nvar extendStatics = function(d, b) {\r\n extendStatics = Object.setPrototypeOf ||\r\n ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||\r\n function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };\r\n return extendStatics(d, b);\r\n};\r\n\r\nexport function __extends(d, b) {\r\n if (typeof b !== \"function\" && b !== null)\r\n throw new TypeError(\"Class extends value \" + String(b) + \" is not a constructor or null\");\r\n extendStatics(d, b);\r\n function __() { this.constructor = d; }\r\n d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());\r\n}\r\n\r\nexport var __assign = function() {\r\n __assign = Object.assign || function __assign(t) {\r\n for (var s, i = 1, n = arguments.length; i < n; i++) {\r\n s = arguments[i];\r\n for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];\r\n }\r\n return t;\r\n }\r\n return __assign.apply(this, arguments);\r\n}\r\n\r\nexport function __rest(s, e) {\r\n var t = {};\r\n for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)\r\n t[p] = s[p];\r\n if (s != null && typeof Object.getOwnPropertySymbols === \"function\")\r\n for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {\r\n if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))\r\n t[p[i]] = s[p[i]];\r\n }\r\n return t;\r\n}\r\n\r\nexport function __decorate(decorators, target, key, desc) {\r\n var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;\r\n if (typeof Reflect === \"object\" && typeof Reflect.decorate === \"function\") r = Reflect.decorate(decorators, target, key, desc);\r\n else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;\r\n return c > 3 && r && Object.defineProperty(target, key, r), r;\r\n}\r\n\r\nexport function __param(paramIndex, decorator) {\r\n return function (target, key) { decorator(target, key, paramIndex); }\r\n}\r\n\r\nexport function __esDecorate(ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {\r\n function accept(f) { if (f !== void 0 && typeof f !== \"function\") throw new TypeError(\"Function expected\"); return f; }\r\n var kind = contextIn.kind, key = kind === \"getter\" ? \"get\" : kind === \"setter\" ? \"set\" : \"value\";\r\n var target = !descriptorIn && ctor ? contextIn[\"static\"] ? ctor : ctor.prototype : null;\r\n var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});\r\n var _, done = false;\r\n for (var i = decorators.length - 1; i >= 0; i--) {\r\n var context = {};\r\n for (var p in contextIn) context[p] = p === \"access\" ? {} : contextIn[p];\r\n for (var p in contextIn.access) context.access[p] = contextIn.access[p];\r\n context.addInitializer = function (f) { if (done) throw new TypeError(\"Cannot add initializers after decoration has completed\"); extraInitializers.push(accept(f || null)); };\r\n var result = (0, decorators[i])(kind === \"accessor\" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);\r\n if (kind === \"accessor\") {\r\n if (result === void 0) continue;\r\n if (result === null || typeof result !== \"object\") throw new TypeError(\"Object expected\");\r\n if (_ = accept(result.get)) descriptor.get = _;\r\n if (_ = accept(result.set)) descriptor.set = _;\r\n if (_ = accept(result.init)) initializers.unshift(_);\r\n }\r\n else if (_ = accept(result)) {\r\n if (kind === \"field\") initializers.unshift(_);\r\n else descriptor[key] = _;\r\n }\r\n }\r\n if (target) Object.defineProperty(target, contextIn.name, descriptor);\r\n done = true;\r\n};\r\n\r\nexport function __runInitializers(thisArg, initializers, value) {\r\n var useValue = arguments.length > 2;\r\n for (var i = 0; i < initializers.length; i++) {\r\n value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);\r\n }\r\n return useValue ? value : void 0;\r\n};\r\n\r\nexport function __propKey(x) {\r\n return typeof x === \"symbol\" ? x : \"\".concat(x);\r\n};\r\n\r\nexport function __setFunctionName(f, name, prefix) {\r\n if (typeof name === \"symbol\") name = name.description ? \"[\".concat(name.description, \"]\") : \"\";\r\n return Object.defineProperty(f, \"name\", { configurable: true, value: prefix ? \"\".concat(prefix, \" \", name) : name });\r\n};\r\n\r\nexport function __metadata(metadataKey, metadataValue) {\r\n if (typeof Reflect === \"object\" && typeof Reflect.metadata === \"function\") return Reflect.metadata(metadataKey, metadataValue);\r\n}\r\n\r\nexport function __awaiter(thisArg, _arguments, P, generator) {\r\n function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }\r\n return new (P || (P = Promise))(function (resolve, reject) {\r\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\r\n function rejected(value) { try { step(generator[\"throw\"](value)); } catch (e) { reject(e); } }\r\n function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }\r\n step((generator = generator.apply(thisArg, _arguments || [])).next());\r\n });\r\n}\r\n\r\nexport function __generator(thisArg, body) {\r\n var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;\r\n return g = { next: verb(0), \"throw\": verb(1), \"return\": verb(2) }, typeof Symbol === \"function\" && (g[Symbol.iterator] = function() { return this; }), g;\r\n function verb(n) { return function (v) { return step([n, v]); }; }\r\n function step(op) {\r\n if (f) throw new TypeError(\"Generator is already executing.\");\r\n while (g && (g = 0, op[0] && (_ = 0)), _) try {\r\n if (f = 1, y && (t = op[0] & 2 ? y[\"return\"] : op[0] ? y[\"throw\"] || ((t = y[\"return\"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;\r\n if (y = 0, t) op = [op[0] & 2, t.value];\r\n switch (op[0]) {\r\n case 0: case 1: t = op; break;\r\n case 4: _.label++; return { value: op[1], done: false };\r\n case 5: _.label++; y = op[1]; op = [0]; continue;\r\n case 7: op = _.ops.pop(); _.trys.pop(); continue;\r\n default:\r\n if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }\r\n if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }\r\n if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }\r\n if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }\r\n if (t[2]) _.ops.pop();\r\n _.trys.pop(); continue;\r\n }\r\n op = body.call(thisArg, _);\r\n } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }\r\n if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };\r\n }\r\n}\r\n\r\nexport var __createBinding = Object.create ? (function(o, m, k, k2) {\r\n if (k2 === undefined) k2 = k;\r\n var desc = Object.getOwnPropertyDescriptor(m, k);\r\n if (!desc || (\"get\" in desc ? !m.__esModule : desc.writable || desc.configurable)) {\r\n desc = { enumerable: true, get: function() { return m[k]; } };\r\n }\r\n Object.defineProperty(o, k2, desc);\r\n}) : (function(o, m, k, k2) {\r\n if (k2 === undefined) k2 = k;\r\n o[k2] = m[k];\r\n});\r\n\r\nexport function __exportStar(m, o) {\r\n for (var p in m) if (p !== \"default\" && !Object.prototype.hasOwnProperty.call(o, p)) __createBinding(o, m, p);\r\n}\r\n\r\nexport function __values(o) {\r\n var s = typeof Symbol === \"function\" && Symbol.iterator, m = s && o[s], i = 0;\r\n if (m) return m.call(o);\r\n if (o && typeof o.length === \"number\") return {\r\n next: function () {\r\n if (o && i >= o.length) o = void 0;\r\n return { value: o && o[i++], done: !o };\r\n }\r\n };\r\n throw new TypeError(s ? \"Object is not iterable.\" : \"Symbol.iterator is not defined.\");\r\n}\r\n\r\nexport function __read(o, n) {\r\n var m = typeof Symbol === \"function\" && o[Symbol.iterator];\r\n if (!m) return o;\r\n var i = m.call(o), r, ar = [], e;\r\n try {\r\n while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);\r\n }\r\n catch (error) { e = { error: error }; }\r\n finally {\r\n try {\r\n if (r && !r.done && (m = i[\"return\"])) m.call(i);\r\n }\r\n finally { if (e) throw e.error; }\r\n }\r\n return ar;\r\n}\r\n\r\n/** @deprecated */\r\nexport function __spread() {\r\n for (var ar = [], i = 0; i < arguments.length; i++)\r\n ar = ar.concat(__read(arguments[i]));\r\n return ar;\r\n}\r\n\r\n/** @deprecated */\r\nexport function __spreadArrays() {\r\n for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;\r\n for (var r = Array(s), k = 0, i = 0; i < il; i++)\r\n for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)\r\n r[k] = a[j];\r\n return r;\r\n}\r\n\r\nexport function __spreadArray(to, from, pack) {\r\n if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {\r\n if (ar || !(i in from)) {\r\n if (!ar) ar = Array.prototype.slice.call(from, 0, i);\r\n ar[i] = from[i];\r\n }\r\n }\r\n return to.concat(ar || Array.prototype.slice.call(from));\r\n}\r\n\r\nexport function __await(v) {\r\n return this instanceof __await ? (this.v = v, this) : new __await(v);\r\n}\r\n\r\nexport function __asyncGenerator(thisArg, _arguments, generator) {\r\n if (!Symbol.asyncIterator) throw new TypeError(\"Symbol.asyncIterator is not defined.\");\r\n var g = generator.apply(thisArg, _arguments || []), i, q = [];\r\n return i = {}, verb(\"next\"), verb(\"throw\"), verb(\"return\"), i[Symbol.asyncIterator] = function () { return this; }, i;\r\n function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }\r\n function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }\r\n function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }\r\n function fulfill(value) { resume(\"next\", value); }\r\n function reject(value) { resume(\"throw\", value); }\r\n function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }\r\n}\r\n\r\nexport function __asyncDelegator(o) {\r\n var i, p;\r\n return i = {}, verb(\"next\"), verb(\"throw\", function (e) { throw e; }), verb(\"return\"), i[Symbol.iterator] = function () { return this; }, i;\r\n function verb(n, f) { i[n] = o[n] ? function (v) { return (p = !p) ? { value: __await(o[n](v)), done: false } : f ? f(v) : v; } : f; }\r\n}\r\n\r\nexport function __asyncValues(o) {\r\n if (!Symbol.asyncIterator) throw new TypeError(\"Symbol.asyncIterator is not defined.\");\r\n var m = o[Symbol.asyncIterator], i;\r\n return m ? m.call(o) : (o = typeof __values === \"function\" ? __values(o) : o[Symbol.iterator](), i = {}, verb(\"next\"), verb(\"throw\"), verb(\"return\"), i[Symbol.asyncIterator] = function () { return this; }, i);\r\n function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }\r\n function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }\r\n}\r\n\r\nexport function __makeTemplateObject(cooked, raw) {\r\n if (Object.defineProperty) { Object.defineProperty(cooked, \"raw\", { value: raw }); } else { cooked.raw = raw; }\r\n return cooked;\r\n};\r\n\r\nvar __setModuleDefault = Object.create ? (function(o, v) {\r\n Object.defineProperty(o, \"default\", { enumerable: true, value: v });\r\n}) : function(o, v) {\r\n o[\"default\"] = v;\r\n};\r\n\r\nexport function __importStar(mod) {\r\n if (mod && mod.__esModule) return mod;\r\n var result = {};\r\n if (mod != null) for (var k in mod) if (k !== \"default\" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);\r\n __setModuleDefault(result, mod);\r\n return result;\r\n}\r\n\r\nexport function __importDefault(mod) {\r\n return (mod && mod.__esModule) ? mod : { default: mod };\r\n}\r\n\r\nexport function __classPrivateFieldGet(receiver, state, kind, f) {\r\n if (kind === \"a\" && !f) throw new TypeError(\"Private accessor was defined without a getter\");\r\n if (typeof state === \"function\" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError(\"Cannot read private member from an object whose class did not declare it\");\r\n return kind === \"m\" ? f : kind === \"a\" ? f.call(receiver) : f ? f.value : state.get(receiver);\r\n}\r\n\r\nexport function __classPrivateFieldSet(receiver, state, value, kind, f) {\r\n if (kind === \"m\") throw new TypeError(\"Private method is not writable\");\r\n if (kind === \"a\" && !f) throw new TypeError(\"Private accessor was defined without a setter\");\r\n if (typeof state === \"function\" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError(\"Cannot write private member to an object whose class did not declare it\");\r\n return (kind === \"a\" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;\r\n}\r\n\r\nexport function __classPrivateFieldIn(state, receiver) {\r\n if (receiver === null || (typeof receiver !== \"object\" && typeof receiver !== \"function\")) throw new TypeError(\"Cannot use 'in' operator on non-object\");\r\n return typeof state === \"function\" ? receiver === state : state.has(receiver);\r\n}\r\n\r\nexport function __addDisposableResource(env, value, async) {\r\n if (value !== null && value !== void 0) {\r\n if (typeof value !== \"object\" && typeof value !== \"function\") throw new TypeError(\"Object expected.\");\r\n var dispose;\r\n if (async) {\r\n if (!Symbol.asyncDispose) throw new TypeError(\"Symbol.asyncDispose is not defined.\");\r\n dispose = value[Symbol.asyncDispose];\r\n }\r\n if (dispose === void 0) {\r\n if (!Symbol.dispose) throw new TypeError(\"Symbol.dispose is not defined.\");\r\n dispose = value[Symbol.dispose];\r\n }\r\n if (typeof dispose !== \"function\") throw new TypeError(\"Object not disposable.\");\r\n env.stack.push({ value: value, dispose: dispose, async: async });\r\n }\r\n else if (async) {\r\n env.stack.push({ async: true });\r\n }\r\n return value;\r\n}\r\n\r\nvar _SuppressedError = typeof SuppressedError === \"function\" ? SuppressedError : function (error, suppressed, message) {\r\n var e = new Error(message);\r\n return e.name = \"SuppressedError\", e.error = error, e.suppressed = suppressed, e;\r\n};\r\n\r\nexport function __disposeResources(env) {\r\n function fail(e) {\r\n env.error = env.hasError ? new _SuppressedError(e, env.error, \"An error was suppressed during disposal.\") : e;\r\n env.hasError = true;\r\n }\r\n function next() {\r\n while (env.stack.length) {\r\n var rec = env.stack.pop();\r\n try {\r\n var result = rec.dispose && rec.dispose.call(rec.value);\r\n if (rec.async) return Promise.resolve(result).then(next, function(e) { fail(e); return next(); });\r\n }\r\n catch (e) {\r\n fail(e);\r\n }\r\n }\r\n if (env.hasError) throw env.error;\r\n }\r\n return next();\r\n}\r\n\r\nexport default {\r\n __extends: __extends,\r\n __assign: __assign,\r\n __rest: __rest,\r\n __decorate: __decorate,\r\n __param: __param,\r\n __metadata: __metadata,\r\n __awaiter: __awaiter,\r\n __generator: __generator,\r\n __createBinding: __createBinding,\r\n __exportStar: __exportStar,\r\n __values: __values,\r\n __read: __read,\r\n __spread: __spread,\r\n __spreadArrays: __spreadArrays,\r\n __spreadArray: __spreadArray,\r\n __await: __await,\r\n __asyncGenerator: __asyncGenerator,\r\n __asyncDelegator: __asyncDelegator,\r\n __asyncValues: __asyncValues,\r\n __makeTemplateObject: __makeTemplateObject,\r\n __importStar: __importStar,\r\n __importDefault: __importDefault,\r\n __classPrivateFieldGet: __classPrivateFieldGet,\r\n __classPrivateFieldSet: __classPrivateFieldSet,\r\n __classPrivateFieldIn: __classPrivateFieldIn,\r\n __addDisposableResource: __addDisposableResource,\r\n __disposeResources: __disposeResources,\r\n};\r\n","import {\n diffNodeType,\n elementDiffNodeType,\n elementNodeType,\n nodeType,\n subsetType,\n textDiffNodeType,\n textNodeType,\n} from \"../types\"\nimport { Diff } from \"../helpers\"\nconst elementDescriptors = (el: diffNodeType) => {\n const output = []\n output.push(el.nodeName)\n if (el.nodeName !== \"#text\" && el.nodeName !== \"#comment\") {\n el = el as elementDiffNodeType\n if (el.attributes) {\n if (el.attributes[\"class\"]) {\n output.push(\n `${el.nodeName}.${el.attributes[\"class\"].replace(\n / /g,\n \".\",\n )}`,\n )\n }\n if (el.attributes.id) {\n output.push(`${el.nodeName}#${el.attributes.id}`)\n }\n }\n }\n return output\n}\n\nconst findUniqueDescriptors = (li: diffNodeType[]) => {\n const uniqueDescriptors = {}\n const duplicateDescriptors = {}\n\n li.forEach((node: nodeType) => {\n elementDescriptors(node).forEach((descriptor) => {\n const inUnique = descriptor in uniqueDescriptors\n const inDupes = descriptor in duplicateDescriptors\n if (!inUnique && !inDupes) {\n uniqueDescriptors[descriptor] = true\n } else if (inUnique) {\n delete uniqueDescriptors[descriptor]\n duplicateDescriptors[descriptor] = true\n }\n })\n })\n\n return uniqueDescriptors\n}\n\nexport const uniqueInBoth = (l1: diffNodeType[], l2: diffNodeType[]) => {\n const l1Unique = findUniqueDescriptors(l1)\n const l2Unique = findUniqueDescriptors(l2)\n const inBoth = {}\n\n Object.keys(l1Unique).forEach((key) => {\n if (l2Unique[key]) {\n inBoth[key] = true\n }\n })\n\n return inBoth\n}\n\nexport const removeDone = (tree: elementDiffNodeType) => {\n delete tree.outerDone\n delete tree.innerDone\n delete tree.valueDone\n if (tree.childNodes) {\n return tree.childNodes.every(removeDone)\n } else {\n return true\n }\n}\n\nexport const cleanNode = (diffNode: diffNodeType) => {\n if (Object.prototype.hasOwnProperty.call(diffNode, \"data\")) {\n const textNode: textNodeType = {\n nodeName: diffNode.nodeName === \"#text\" ? \"#text\" : \"#comment\",\n data: (diffNode as textDiffNodeType).data,\n }\n return textNode\n } else {\n const elementNode: elementNodeType = {\n nodeName: diffNode.nodeName,\n }\n diffNode = diffNode as elementDiffNodeType\n if (Object.prototype.hasOwnProperty.call(diffNode, \"attributes\")) {\n elementNode.attributes = { ...diffNode.attributes }\n }\n if (Object.prototype.hasOwnProperty.call(diffNode, \"checked\")) {\n elementNode.checked = diffNode.checked\n }\n if (Object.prototype.hasOwnProperty.call(diffNode, \"value\")) {\n elementNode.value = diffNode.value\n }\n if (Object.prototype.hasOwnProperty.call(diffNode, \"selected\")) {\n elementNode.selected = diffNode.selected\n }\n if (Object.prototype.hasOwnProperty.call(diffNode, \"childNodes\")) {\n elementNode.childNodes = diffNode.childNodes.map((diffChildNode) =>\n cleanNode(diffChildNode),\n )\n }\n return elementNode\n }\n}\n\nexport const isEqual = (e1: diffNodeType, e2: diffNodeType) => {\n if (\n ![\"nodeName\", \"value\", \"checked\", \"selected\", \"data\"].every(\n (element) => {\n if (e1[element] !== e2[element]) {\n return false\n }\n return true\n },\n )\n ) {\n return false\n }\n if (Object.prototype.hasOwnProperty.call(e1, \"data\")) {\n // Comment or Text\n return true\n }\n e1 = e1 as elementDiffNodeType\n e2 = e2 as elementDiffNodeType\n if (Boolean(e1.attributes) !== Boolean(e2.attributes)) {\n return false\n }\n\n if (Boolean(e1.childNodes) !== Boolean(e2.childNodes)) {\n return false\n }\n if (e1.attributes) {\n const e1Attributes = Object.keys(e1.attributes)\n const e2Attributes = Object.keys(e2.attributes)\n\n if (e1Attributes.length !== e2Attributes.length) {\n return false\n }\n if (\n !e1Attributes.every((attribute) => {\n if (\n (e1 as elementDiffNodeType).attributes[attribute] !==\n (e2 as elementDiffNodeType).attributes[attribute]\n ) {\n return false\n }\n return true\n })\n ) {\n return false\n }\n }\n if (e1.childNodes) {\n if (e1.childNodes.length !== e2.childNodes.length) {\n return false\n }\n if (\n !e1.childNodes.every((childNode: nodeType, index: number) =>\n isEqual(childNode, e2.childNodes[index]),\n )\n ) {\n return false\n }\n }\n\n return true\n}\n\nexport const roughlyEqual = (\n e1: diffNodeType,\n e2: diffNodeType,\n uniqueDescriptors: { [key: string]: boolean },\n sameSiblings: boolean,\n preventRecursion = false,\n) => {\n if (!e1 || !e2) {\n return false\n }\n\n if (e1.nodeName !== e2.nodeName) {\n return false\n }\n\n if ([\"#text\", \"#comment\"].includes(e1.nodeName)) {\n // Note that we initially don't care what the text content of a node is,\n // the mere fact that it's the same tag and \"has text\" means it's roughly\n // equal, and then we can find out the true text difference later.\n return preventRecursion\n ? true\n : (e1 as textDiffNodeType).data === (e2 as textDiffNodeType).data\n }\n\n e1 = e1 as elementDiffNodeType\n e2 = e2 as elementDiffNodeType\n\n if (e1.nodeName in uniqueDescriptors) {\n return true\n }\n\n if (e1.attributes && e2.attributes) {\n if (e1.attributes.id) {\n if (e1.attributes.id !== e2.attributes.id) {\n return false\n } else {\n const idDescriptor = `${e1.nodeName}#${e1.attributes.id}`\n if (idDescriptor in uniqueDescriptors) {\n return true\n }\n }\n }\n if (\n e1.attributes[\"class\"] &&\n e1.attributes[\"class\"] === e2.attributes[\"class\"]\n ) {\n const classDescriptor = `${e1.nodeName}.${e1.attributes[\n \"class\"\n ].replace(/ /g, \".\")}`\n if (classDescriptor in uniqueDescriptors) {\n return true\n }\n }\n }\n\n if (sameSiblings) {\n return true\n }\n\n const nodeList1 = e1.childNodes ? e1.childNodes.slice().reverse() : []\n const nodeList2 = e2.childNodes ? e2.childNodes.slice().reverse() : []\n\n if (nodeList1.length !== nodeList2.length) {\n return false\n }\n\n if (preventRecursion) {\n return nodeList1.every(\n (element: nodeType, index: number) =>\n element.nodeName === nodeList2[index].nodeName,\n )\n } else {\n // note: we only allow one level of recursion at any depth. If 'preventRecursion'\n // was not set, we must explicitly force it to true for child iterations.\n const childUniqueDescriptors = uniqueInBoth(nodeList1, nodeList2)\n return nodeList1.every((element: nodeType, index: number) =>\n roughlyEqual(\n element,\n nodeList2[index],\n childUniqueDescriptors,\n true,\n true,\n ),\n )\n }\n}\n\n/**\n * based on https://en.wikibooks.org/wiki/Algorithm_implementation/Strings/Longest_common_substring#JavaScript\n */\nconst findCommonSubsets = (\n c1: diffNodeType[],\n c2: diffNodeType[],\n marked1: boolean[],\n marked2: boolean[],\n) => {\n let lcsSize = 0\n let index: number[] = []\n const c1Length = c1.length\n const c2Length = c2.length\n\n const // set up the matching table\n matches = Array(...new Array(c1Length + 1)).map(() => [])\n\n const uniqueDescriptors = uniqueInBoth(c1, c2)\n\n let // If all of the elements are the same tag, id and class, then we can\n // consider them roughly the same even if they have a different number of\n // children. This will reduce removing and re-adding similar elements.\n subsetsSame = c1Length === c2Length\n\n if (subsetsSame) {\n c1.some((element: nodeType, i: number) => {\n const c1Desc = elementDescriptors(element)\n const c2Desc = elementDescriptors(c2[i])\n if (c1Desc.length !== c2Desc.length) {\n subsetsSame = false\n return true\n }\n c1Desc.some((description, i) => {\n if (description !== c2Desc[i]) {\n subsetsSame = false\n return true\n }\n })\n if (!subsetsSame) {\n return true\n }\n })\n }\n\n // fill the matches with distance values\n for (let c1Index = 0; c1Index < c1Length; c1Index++) {\n const c1Element = c1[c1Index]\n for (let c2Index = 0; c2Index < c2Length; c2Index++) {\n const c2Element = c2[c2Index]\n if (\n !marked1[c1Index] &&\n !marked2[c2Index] &&\n roughlyEqual(\n c1Element,\n c2Element,\n uniqueDescriptors,\n subsetsSame,\n )\n ) {\n matches[c1Index + 1][c2Index + 1] = matches[c1Index][c2Index]\n ? matches[c1Index][c2Index] + 1\n : 1\n if (matches[c1Index + 1][c2Index + 1] >= lcsSize) {\n lcsSize = matches[c1Index + 1][c2Index + 1]\n index = [c1Index + 1, c2Index + 1]\n }\n } else {\n matches[c1Index + 1][c2Index + 1] = 0\n }\n }\n }\n\n if (lcsSize === 0) {\n return false\n }\n\n return {\n oldValue: index[0] - lcsSize,\n newValue: index[1] - lcsSize,\n length: lcsSize,\n }\n}\n\nconst makeBooleanArray = (n: number, v: boolean) =>\n Array(...new Array(n)).map(() => v)\n\n/**\n * Generate arrays that indicate which node belongs to which subset,\n * or whether it's actually an orphan node, existing in only one\n * of the two trees, rather than somewhere in both.\n *\n * So if t1 =
, t2 =
.\n * The longest subset is \"
\" (length 2), so it will group 0.\n * The second longest is \"\" (length 1), so it will be group 1.\n * gaps1 will therefore be [1,0,0] and gaps2 [0,0,1].\n *\n * If an element is not part of any group, it will stay being 'true', which\n * is the initial value. For example:\n * t1 =


, t2 =
\n *\n * The \"

\" and \"\" do only show up in one of the two and will\n * therefore be marked by \"true\". The remaining parts are parts of the\n * groups 0 and 1:\n * gaps1 = [1, true, 0, 0], gaps2 = [true, 0, 0, 1]\n *\n */\nexport const getGapInformation = (\n t1: elementDiffNodeType,\n t2: elementDiffNodeType,\n stable: subsetType[],\n) => {\n const gaps1: (true | number)[] = t1.childNodes\n ? (makeBooleanArray(t1.childNodes.length, true) as true[])\n : []\n const gaps2: (true | number)[] = t2.childNodes\n ? (makeBooleanArray(t2.childNodes.length, true) as true[])\n : []\n let group = 0\n\n // give elements from the same subset the same group number\n stable.forEach((subset: subsetType) => {\n const endOld = subset.oldValue + subset.length\n const endNew = subset.newValue + subset.length\n\n for (let j = subset.oldValue; j < endOld; j += 1) {\n gaps1[j] = group\n }\n for (let j = subset.newValue; j < endNew; j += 1) {\n gaps2[j] = group\n }\n group += 1\n })\n\n return {\n gaps1,\n gaps2,\n }\n}\n\n/**\n * Find all matching subsets, based on immediate child differences only.\n */\nconst markBoth = (marked1, marked2, subset: subsetType, i: number) => {\n marked1[subset.oldValue + i] = true\n marked2[subset.newValue + i] = true\n}\n\nexport const markSubTrees = (\n oldTree: elementDiffNodeType,\n newTree: elementDiffNodeType,\n) => {\n // note: the child lists are views, and so update as we update old/newTree\n const oldChildren = oldTree.childNodes ? oldTree.childNodes : []\n\n const newChildren = newTree.childNodes ? newTree.childNodes : []\n const marked1 = makeBooleanArray(oldChildren.length, false)\n const marked2 = makeBooleanArray(newChildren.length, false)\n const subsets = []\n\n const returnIndex = function () {\n return arguments[1]\n }\n\n let foundAllSubsets = false\n\n while (!foundAllSubsets) {\n const subset = findCommonSubsets(\n oldChildren,\n newChildren,\n marked1,\n marked2,\n )\n if (subset) {\n subsets.push(subset)\n const subsetArray = Array(...new Array(subset.length)).map(\n returnIndex,\n )\n subsetArray.forEach((item) =>\n markBoth(marked1, marked2, subset, item),\n )\n } else {\n foundAllSubsets = true\n }\n }\n\n oldTree.subsets = subsets\n oldTree.subsetsAge = 100\n return subsets\n}\n\nexport class DiffTracker {\n list: Diff[]\n constructor() {\n this.list = []\n }\n\n add(diffs: Diff[]) {\n this.list.push(...diffs)\n }\n forEach(fn: (Diff) => void) {\n this.list.forEach((li: Diff) => fn(li))\n }\n}\n","import { DiffDOMOptions, elementNodeType, nodeType, subsetType } from \"../types\"\nimport { Diff } from \"../helpers\"\nimport { cleanNode } from \"./helpers\"\n// ===== Apply a virtual diff =====\n\nfunction getFromVirtualRoute(tree: elementNodeType, route: number[]) {\n let node = tree\n let parentNode\n let nodeIndex\n\n route = route.slice()\n while (route.length > 0) {\n nodeIndex = route.splice(0, 1)[0]\n parentNode = node\n node = node.childNodes ? node.childNodes[nodeIndex] : undefined\n }\n return {\n node,\n parentNode,\n nodeIndex,\n }\n}\n\nfunction applyVirtualDiff(\n tree: elementNodeType,\n diff: Diff,\n options: DiffDOMOptions, // {preVirtualDiffApply, postVirtualDiffApply, _const}\n) {\n let node, parentNode, nodeIndex\n\n if (\n ![options._const.addElement, options._const.addTextElement].includes(\n diff[options._const.action],\n )\n ) {\n // For adding nodes, we calculate the route later on. It's different because it includes the position of the newly added item.\n const routeInfo = getFromVirtualRoute(tree, diff[options._const.route])\n node = routeInfo.node\n parentNode = routeInfo.parentNode\n nodeIndex = routeInfo.nodeIndex\n }\n\n const newSubsets: subsetType[] = []\n\n // pre-diff hook\n const info = {\n diff,\n node,\n }\n\n if (options.preVirtualDiffApply(info)) {\n return true\n }\n\n let newNode\n let nodeArray\n let route\n\n switch (diff[options._const.action]) {\n case options._const.addAttribute:\n if (!node.attributes) {\n node.attributes = {}\n }\n\n node.attributes[diff[options._const.name]] =\n diff[options._const.value]\n\n if (diff[options._const.name] === \"checked\") {\n node.checked = true\n } else if (diff[options._const.name] === \"selected\") {\n node.selected = true\n } else if (\n node.nodeName === \"INPUT\" &&\n diff[options._const.name] === \"value\"\n ) {\n node.value = diff[options._const.value]\n }\n\n break\n case options._const.modifyAttribute:\n node.attributes[diff[options._const.name]] =\n diff[options._const.newValue]\n break\n case options._const.removeAttribute:\n delete node.attributes[diff[options._const.name]]\n\n if (Object.keys(node.attributes).length === 0) {\n delete node.attributes\n }\n\n if (diff[options._const.name] === \"checked\") {\n node.checked = false\n } else if (diff[options._const.name] === \"selected\") {\n delete node.selected\n } else if (\n node.nodeName === \"INPUT\" &&\n diff[options._const.name] === \"value\"\n ) {\n delete node.value\n }\n\n break\n case options._const.modifyTextElement:\n node.data = diff[options._const.newValue]\n if (parentNode.nodeName === \"TEXTAREA\") {\n parentNode.value = diff[options._const.newValue]\n }\n break\n case options._const.modifyValue:\n node.value = diff[options._const.newValue]\n break\n case options._const.modifyComment:\n node.data = diff[options._const.newValue]\n break\n case options._const.modifyChecked:\n node.checked = diff[options._const.newValue]\n break\n case options._const.modifySelected:\n node.selected = diff[options._const.newValue]\n break\n case options._const.replaceElement:\n newNode = cleanNode(diff[options._const.newValue])\n parentNode.childNodes[nodeIndex] = newNode\n break\n case options._const.relocateGroup:\n nodeArray = node.childNodes\n .splice(\n diff[options._const.from],\n diff[options._const.groupLength],\n )\n .reverse()\n nodeArray.forEach((movedNode: nodeType) =>\n node.childNodes.splice(diff[options._const.to], 0, movedNode),\n )\n if (node.subsets) {\n node.subsets.forEach((map: subsetType) => {\n if (\n diff[options._const.from] < diff[options._const.to] &&\n map.oldValue <= diff[options._const.to] &&\n map.oldValue > diff[options._const.from]\n ) {\n map.oldValue -= diff[options._const.groupLength]\n const splitLength =\n map.oldValue + map.length - diff[options._const.to]\n if (splitLength > 0) {\n // new insertion splits map.\n newSubsets.push({\n oldValue:\n diff[options._const.to] +\n diff[options._const.groupLength],\n newValue:\n map.newValue + map.length - splitLength,\n length: splitLength,\n })\n map.length -= splitLength\n }\n } else if (\n diff[options._const.from] > diff[options._const.to] &&\n map.oldValue > diff[options._const.to] &&\n map.oldValue < diff[options._const.from]\n ) {\n map.oldValue += diff[options._const.groupLength]\n const splitLength =\n map.oldValue + map.length - diff[options._const.to]\n if (splitLength > 0) {\n // new insertion splits map.\n newSubsets.push({\n oldValue:\n diff[options._const.to] +\n diff[options._const.groupLength],\n newValue:\n map.newValue + map.length - splitLength,\n length: splitLength,\n })\n map.length -= splitLength\n }\n } else if (map.oldValue === diff[options._const.from]) {\n map.oldValue = diff[options._const.to]\n }\n })\n }\n\n break\n case options._const.removeElement:\n parentNode.childNodes.splice(nodeIndex, 1)\n if (parentNode.subsets) {\n parentNode.subsets.forEach((map: subsetType) => {\n if (map.oldValue > nodeIndex) {\n map.oldValue -= 1\n } else if (map.oldValue === nodeIndex) {\n map.delete = true\n } else if (\n map.oldValue < nodeIndex &&\n map.oldValue + map.length > nodeIndex\n ) {\n if (map.oldValue + map.length - 1 === nodeIndex) {\n map.length--\n } else {\n newSubsets.push({\n newValue:\n map.newValue + nodeIndex - map.oldValue,\n oldValue: nodeIndex,\n length:\n map.length - nodeIndex + map.oldValue - 1,\n })\n map.length = nodeIndex - map.oldValue\n }\n }\n })\n }\n node = parentNode\n break\n case options._const.addElement: {\n route = diff[options._const.route].slice()\n const c: number = route.splice(route.length - 1, 1)[0]\n node = getFromVirtualRoute(tree, route)?.node\n newNode = cleanNode(diff[options._const.element])\n\n if (!node.childNodes) {\n node.childNodes = []\n }\n\n if (c >= node.childNodes.length) {\n node.childNodes.push(newNode)\n } else {\n node.childNodes.splice(c, 0, newNode)\n }\n if (node.subsets) {\n node.subsets.forEach((map: subsetType) => {\n if (map.oldValue >= c) {\n map.oldValue += 1\n } else if (\n map.oldValue < c &&\n map.oldValue + map.length > c\n ) {\n const splitLength = map.oldValue + map.length - c\n newSubsets.push({\n newValue: map.newValue + map.length - splitLength,\n oldValue: c + 1,\n length: splitLength,\n })\n map.length -= splitLength\n }\n })\n }\n break\n }\n case options._const.removeTextElement:\n parentNode.childNodes.splice(nodeIndex, 1)\n if (parentNode.nodeName === \"TEXTAREA\") {\n delete parentNode.value\n }\n if (parentNode.subsets) {\n parentNode.subsets.forEach((map: subsetType) => {\n if (map.oldValue > nodeIndex) {\n map.oldValue -= 1\n } else if (map.oldValue === nodeIndex) {\n map.delete = true\n } else if (\n map.oldValue < nodeIndex &&\n map.oldValue + map.length > nodeIndex\n ) {\n if (map.oldValue + map.length - 1 === nodeIndex) {\n map.length--\n } else {\n newSubsets.push({\n newValue:\n map.newValue + nodeIndex - map.oldValue,\n oldValue: nodeIndex,\n length:\n map.length - nodeIndex + map.oldValue - 1,\n })\n map.length = nodeIndex - map.oldValue\n }\n }\n })\n }\n node = parentNode\n break\n case options._const.addTextElement: {\n route = diff[options._const.route].slice()\n const c: number = route.splice(route.length - 1, 1)[0]\n newNode = {\n nodeName: \"#text\",\n data: diff[options._const.value],\n }\n node = getFromVirtualRoute(tree, route).node\n if (!node.childNodes) {\n node.childNodes = []\n }\n\n if (c >= node.childNodes.length) {\n node.childNodes.push(newNode)\n } else {\n node.childNodes.splice(c, 0, newNode)\n }\n if (node.nodeName === \"TEXTAREA\") {\n node.value = diff[options._const.newValue]\n }\n if (node.subsets) {\n node.subsets.forEach((map: subsetType) => {\n if (map.oldValue >= c) {\n map.oldValue += 1\n }\n if (map.oldValue < c && map.oldValue + map.length > c) {\n const splitLength = map.oldValue + map.length - c\n newSubsets.push({\n newValue: map.newValue + map.length - splitLength,\n oldValue: c + 1,\n length: splitLength,\n })\n map.length -= splitLength\n }\n })\n }\n break\n }\n default:\n console.log(\"unknown action\")\n }\n\n if (node.subsets) {\n node.subsets = node.subsets.filter(\n (map: subsetType) => !map.delete && map.oldValue !== map.newValue,\n )\n if (newSubsets.length) {\n node.subsets = node.subsets.concat(newSubsets)\n }\n }\n\n options.postVirtualDiffApply({\n node: info.node,\n diff: info.diff,\n newNode,\n })\n\n return\n}\n\nexport function applyVirtual(\n tree: elementNodeType,\n diffs: Diff[],\n options: DiffDOMOptions,\n) {\n diffs.forEach((diff: Diff) => {\n applyVirtualDiff(tree, diff, options)\n })\n return true\n}\n","import { DiffDOMOptionsPartial, elementNodeType, textNodeType } from \"../types\"\nimport { checkElementType } from \"../helpers\"\n\nexport function nodeToObj(\n aNode: Element,\n options: DiffDOMOptionsPartial = { valueDiffing: true },\n) {\n const objNode: elementNodeType | textNodeType = {\n nodeName: aNode.nodeName,\n }\n if (checkElementType(aNode, \"Text\", \"Comment\")) {\n ;(objNode as unknown as textNodeType).data = (\n aNode as unknown as Text | Comment\n ).data\n } else {\n if (aNode.attributes && aNode.attributes.length > 0) {\n objNode.attributes = {}\n const nodeArray = Array.prototype.slice.call(aNode.attributes)\n nodeArray.forEach(\n (attribute) =>\n (objNode.attributes[attribute.name] = attribute.value),\n )\n }\n if (aNode.childNodes && aNode.childNodes.length > 0) {\n objNode.childNodes = []\n const nodeArray = Array.prototype.slice.call(aNode.childNodes)\n nodeArray.forEach((childNode) =>\n objNode.childNodes.push(nodeToObj(childNode, options)),\n )\n }\n if (options.valueDiffing) {\n if (checkElementType(aNode, \"HTMLTextAreaElement\")) {\n objNode.value = (aNode as HTMLTextAreaElement).value\n }\n if (\n checkElementType(aNode, \"HTMLInputElement\") &&\n [\"radio\", \"checkbox\"].includes(\n (aNode as HTMLInputElement).type.toLowerCase(),\n ) &&\n (aNode as HTMLInputElement).checked !== undefined\n ) {\n objNode.checked = (aNode as HTMLInputElement).checked\n } else if (\n checkElementType(\n aNode,\n \"HTMLButtonElement\",\n \"HTMLDataElement\",\n \"HTMLInputElement\",\n \"HTMLLIElement\",\n \"HTMLMeterElement\",\n \"HTMLOptionElement\",\n \"HTMLProgressElement\",\n \"HTMLParamElement\",\n )\n ) {\n objNode.value = (\n aNode as\n | HTMLButtonElement\n | HTMLDataElement\n | HTMLInputElement\n | HTMLLIElement\n | HTMLMeterElement\n | HTMLOptionElement\n | HTMLProgressElement\n | HTMLParamElement\n ).value\n }\n if (checkElementType(aNode, \"HTMLOptionElement\")) {\n objNode.selected = (aNode as HTMLOptionElement).selected\n }\n }\n }\n return objNode\n}\n","import { DiffDOMOptionsPartial, nodeType } from \"../types\"\n\n// from html-parse-stringify (MIT)\n\nconst tagRE =\n /<\\s*\\/*[a-zA-Z:_][a-zA-Z0-9:_\\-.]*\\s*(?:\"[^\"]*\"['\"]*|'[^']*'['\"]*|[^'\"/>])*\\/*\\s*>|/g\n\nconst attrRE = /\\s([^'\"/\\s><]+?)[\\s/>]|([^\\s=]+)=\\s?(\".*?\"|'.*?')/g\n\nfunction unescape(string: string) {\n return string\n .replace(/</g, \"<\")\n .replace(/>/g, \">\")\n .replace(/&/g, \"&\")\n}\n\n// create optimized lookup object for\n// void elements as listed here:\n// https://www.w3.org/html/wg/drafts/html/master/syntax.html#void-elements\nconst lookup = {\n area: true,\n base: true,\n br: true,\n col: true,\n embed: true,\n hr: true,\n img: true,\n input: true,\n keygen: true,\n link: true,\n menuItem: true,\n meta: true,\n param: true,\n source: true,\n track: true,\n wbr: true,\n}\n\nconst parseTag = (tag: string, caseSensitive: boolean) => {\n const res = {\n nodeName: \"\",\n attributes: {},\n }\n let voidElement = false\n let type = \"tag\"\n\n let tagMatch = tag.match(/<\\/?([^\\s]+?)[/\\s>]/)\n if (tagMatch) {\n res.nodeName =\n caseSensitive || tagMatch[1] === \"svg\"\n ? tagMatch[1]\n : tagMatch[1].toUpperCase()\n if (lookup[tagMatch[1]] || tag.charAt(tag.length - 2) === \"/\") {\n voidElement = true\n }\n\n // handle comment tag\n if (res.nodeName.startsWith(\"!--\")) {\n const endIndex = tag.indexOf(\"-->\")\n return {\n type: \"comment\",\n node: {\n nodeName: \"#comment\",\n data: endIndex !== -1 ? tag.slice(4, endIndex) : \"\",\n },\n voidElement,\n }\n }\n }\n\n let reg = new RegExp(attrRE)\n let result = null\n let done = false\n while (!done) {\n result = reg.exec(tag)\n\n if (result === null) {\n done = true\n } else if (result[0].trim()) {\n if (result[1]) {\n let attr = result[1].trim()\n let arr = [attr, \"\"]\n\n if (attr.indexOf(\"=\") > -1) arr = attr.split(\"=\")\n res.attributes[arr[0]] = arr[1]\n reg.lastIndex--\n } else if (result[2])\n res.attributes[result[2]] = result[3]\n .trim()\n .substring(1, result[3].length - 1)\n }\n }\n\n return {\n type,\n node: res,\n voidElement,\n }\n}\n\nexport const stringToObj = (\n html: string,\n options: DiffDOMOptionsPartial = {\n valueDiffing: true,\n caseSensitive: false,\n },\n) => {\n const result: nodeType[] = []\n let current: { type: string; node: nodeType; voidElement: boolean }\n let level = -1\n const arr: { type: string; node: nodeType; voidElement: boolean }[] = []\n let inComponent = false,\n insideSvg = false\n\n // handle text at top level\n if (html.indexOf(\"<\") !== 0) {\n const end = html.indexOf(\"<\")\n result.push({\n nodeName: \"#text\",\n data: end === -1 ? html : html.substring(0, end),\n })\n }\n\n html.replace(tagRE, (tag: string, index: number) => {\n if (inComponent) {\n if (tag !== ``) {\n return \"\"\n } else {\n inComponent = false\n }\n }\n const isOpen = tag.charAt(1) !== \"/\"\n const isComment = tag.startsWith(\"