/** * Serializer module for Rangy. * Serializes Ranges and Selections. An example use would be to store a user's selection on a particular page in a * cookie or local storage and restore it on the user's next visit to the same page. * * Part of Rangy, a cross-browser JavaScript range and selection library * https://github.com/timdown/rangy * * Depends on Rangy core. * * Copyright 2024, Tim Down * Licensed under the MIT license. * Version: 1.3.2 * Build date: 2 November 2024 */ export declare const serializer = "\n(function (factory, root) {\n if (typeof define == \"function\" && define.amd) {\n // AMD. Register as an anonymous module with a dependency on Rangy.\n define([\"./rangy-core\"], factory);\n } else if (typeof module != \"undefined\" && typeof exports == \"object\") {\n // Node/CommonJS style\n module.exports = factory(require(\"rangy\"));\n } else {\n // No AMD or CommonJS support so we use the rangy property of root (probably the global variable)\n factory(root.rangy);\n }\n})(function (rangy) {\n rangy.createModule(\n \"Serializer\",\n [\"WrappedSelection\"],\n function (api, module) {\n var UNDEF = \"undefined\";\n var util = api.util;\n\n // encodeURIComponent and decodeURIComponent are required for cookie handling\n if (\n typeof encodeURIComponent == UNDEF ||\n typeof decodeURIComponent == UNDEF\n ) {\n module.fail(\n \"encodeURIComponent and/or decodeURIComponent method is missing\"\n );\n }\n\n // Checksum for checking whether range can be serialized\n var crc32 = (function () {\n function utf8encode(str) {\n var utf8CharCodes = [];\n\n for (var i = 0, len = str.length, c; i < len; ++i) {\n c = str.charCodeAt(i);\n if (c < 128) {\n utf8CharCodes.push(c);\n } else if (c < 2048) {\n utf8CharCodes.push((c >> 6) | 192, (c & 63) | 128);\n } else {\n utf8CharCodes.push(\n (c >> 12) | 224,\n ((c >> 6) & 63) | 128,\n (c & 63) | 128\n );\n }\n }\n return utf8CharCodes;\n }\n\n var cachedCrcTable = null;\n\n function buildCRCTable() {\n var table = [];\n for (var i = 0, j, crc; i < 256; ++i) {\n crc = i;\n j = 8;\n while (j--) {\n if ((crc & 1) == 1) {\n crc = (crc >>> 1) ^ 0xedb88320;\n } else {\n crc >>>= 1;\n }\n }\n table[i] = crc >>> 0;\n }\n return table;\n }\n\n function getCrcTable() {\n if (!cachedCrcTable) {\n cachedCrcTable = buildCRCTable();\n }\n return cachedCrcTable;\n }\n\n return function (str) {\n var utf8CharCodes = utf8encode(str),\n crc = -1,\n crcTable = getCrcTable();\n for (var i = 0, len = utf8CharCodes.length, y; i < len; ++i) {\n y = (crc ^ utf8CharCodes[i]) & 0xff;\n crc = (crc >>> 8) ^ crcTable[y];\n }\n return (crc ^ -1) >>> 0;\n };\n })();\n\n var dom = api.dom;\n\n function escapeTextForHtml(str) {\n return str.replace(//g, \">\");\n }\n\n function nodeToInfoString(node, infoParts) {\n infoParts = infoParts || [];\n var nodeType = node.nodeType,\n children = node.childNodes,\n childCount = children.length;\n var nodeInfo = [nodeType, node.nodeName, childCount].join(\":\");\n var start = \"\",\n end = \"\";\n switch (nodeType) {\n case 3: // Text node\n start = escapeTextForHtml(node.nodeValue);\n break;\n case 8: // Comment\n start = \"\";\n break;\n default:\n start = \"<\" + nodeInfo + \">\";\n end = \"\";\n break;\n }\n if (start) {\n infoParts.push(start);\n }\n for (var i = 0; i < childCount; ++i) {\n nodeToInfoString(children[i], infoParts);\n }\n if (end) {\n infoParts.push(end);\n }\n return infoParts;\n }\n\n // Creates a string representation of the specified element's contents that is similar to innerHTML but omits all\n // attributes and comments and includes child node counts. This is done instead of using innerHTML to work around\n // IE <= 8's policy of including element properties in attributes, which ruins things by changing an element's\n // innerHTML whenever the user changes an input within the element.\n function getElementChecksum(el) {\n var info = nodeToInfoString(el).join(\"\");\n return crc32(info).toString(16);\n }\n\n function serializePosition(node, offset, rootNode) {\n var pathParts = [],\n n = node;\n rootNode = rootNode || dom.getDocument(node).documentElement;\n while (n && n != rootNode) {\n pathParts.push(dom.getNodeIndex(n, true));\n n = n.parentNode;\n }\n return pathParts.join(\"/\") + \":\" + offset;\n }\n\n function deserializePosition(serialized, rootNode, doc) {\n if (!rootNode) {\n rootNode = (doc || document).documentElement;\n }\n var parts = serialized.split(\":\");\n var node = rootNode;\n var nodeIndices = parts[0] ? parts[0].split(\"/\") : [],\n i = nodeIndices.length,\n nodeIndex;\n\n while (i--) {\n nodeIndex = parseInt(nodeIndices[i], 10);\n if (nodeIndex < node.childNodes.length) {\n node = node.childNodes[nodeIndex];\n } else {\n throw module.createError(\n \"deserializePosition() failed: node \" +\n dom.inspectNode(node) +\n \" has no child with index \" +\n nodeIndex +\n \", \" +\n i\n );\n }\n }\n\n return new dom.DomPosition(node, parseInt(parts[1], 10));\n }\n\n function serializeRange(range, omitChecksum, rootNode) {\n rootNode =\n rootNode || api.DomRange.getRangeDocument(range).documentElement;\n if (!dom.isOrIsAncestorOf(rootNode, range.commonAncestorContainer)) {\n throw module.createError(\n \"serializeRange(): range \" +\n range.inspect() +\n \" is not wholly contained within specified root node \" +\n dom.inspectNode(rootNode)\n );\n }\n var serialized =\n serializePosition(range.startContainer, range.startOffset, rootNode) +\n \",\" +\n serializePosition(range.endContainer, range.endOffset, rootNode);\n if (!omitChecksum) {\n serialized += \"{\" + getElementChecksum(rootNode) + \"}\";\n }\n return serialized;\n }\n\n var deserializeRegex = /^([^,]+),([^,\\{]+)(\\{([^}]+)\\})?$/;\n\n function deserializeRange(serialized, rootNode, doc) {\n if (rootNode) {\n doc = doc || dom.getDocument(rootNode);\n } else {\n doc = doc || document;\n rootNode = doc.documentElement;\n }\n var result = deserializeRegex.exec(serialized);\n var checksum = result[4];\n if (checksum) {\n var rootNodeChecksum = getElementChecksum(rootNode);\n if (checksum !== rootNodeChecksum) {\n throw module.createError(\n \"deserializeRange(): checksums of serialized range root node (\" +\n checksum +\n \") and target root node (\" +\n rootNodeChecksum +\n \") do not match\"\n );\n }\n }\n var start = deserializePosition(result[1], rootNode, doc),\n end = deserializePosition(result[2], rootNode, doc);\n var range = api.createRange(doc);\n range.setStartAndEnd(start.node, start.offset, end.node, end.offset);\n return range;\n }\n\n function canDeserializeRange(serialized, rootNode, doc) {\n if (!rootNode) {\n rootNode = (doc || document).documentElement;\n }\n var result = deserializeRegex.exec(serialized);\n var checksum = result[3];\n return !checksum || checksum === getElementChecksum(rootNode);\n }\n\n function serializeSelection(selection, omitChecksum, rootNode) {\n selection = api.getSelection(selection);\n var ranges = selection.getAllRanges(),\n serializedRanges = [];\n for (var i = 0, len = ranges.length; i < len; ++i) {\n serializedRanges[i] = serializeRange(\n ranges[i],\n omitChecksum,\n rootNode\n );\n }\n return serializedRanges.join(\"|\");\n }\n\n function deserializeSelection(serialized, rootNode, win) {\n if (rootNode) {\n win = win || dom.getWindow(rootNode);\n } else {\n win = win || window;\n rootNode = win.document.documentElement;\n }\n var serializedRanges = serialized.split(\"|\");\n var sel = api.getSelection(win);\n var ranges = [];\n\n for (var i = 0, len = serializedRanges.length; i < len; ++i) {\n ranges[i] = deserializeRange(\n serializedRanges[i],\n rootNode,\n win.document\n );\n }\n sel.setRanges(ranges);\n\n return sel;\n }\n\n function canDeserializeSelection(serialized, rootNode, win) {\n var doc;\n if (rootNode) {\n doc = win ? win.document : dom.getDocument(rootNode);\n } else {\n win = win || window;\n rootNode = win.document.documentElement;\n }\n var serializedRanges = serialized.split(\"|\");\n\n for (var i = 0, len = serializedRanges.length; i < len; ++i) {\n if (!canDeserializeRange(serializedRanges[i], rootNode, doc)) {\n return false;\n }\n }\n\n return true;\n }\n\n var cookieName = \"rangySerializedSelection\";\n\n function getSerializedSelectionFromCookie(cookie) {\n var parts = cookie.split(/[;,]/);\n for (var i = 0, len = parts.length, nameVal, val; i < len; ++i) {\n nameVal = parts[i].split(\"=\");\n if (nameVal[0].replace(/^\\s+/, \"\") == cookieName) {\n val = nameVal[1];\n if (val) {\n return decodeURIComponent(val.replace(/\\s+$/, \"\"));\n }\n }\n }\n return null;\n }\n\n function restoreSelectionFromCookie(win) {\n win = win || window;\n var serialized = getSerializedSelectionFromCookie(win.document.cookie);\n if (serialized) {\n deserializeSelection(serialized, win.doc);\n }\n }\n\n function saveSelectionCookie(win, props) {\n win = win || window;\n props = typeof props == \"object\" ? props : {};\n var expires = props.expires\n ? \";expires=\" + props.expires.toUTCString()\n : \"\";\n var path = props.path ? \";path=\" + props.path : \"\";\n var domain = props.domain ? \";domain=\" + props.domain : \"\";\n var secure = props.secure ? \";secure\" : \"\";\n var serialized = serializeSelection(api.getSelection(win));\n win.document.cookie =\n encodeURIComponent(cookieName) +\n \"=\" +\n encodeURIComponent(serialized) +\n expires +\n path +\n domain +\n secure;\n }\n\n util.extend(api, {\n serializePosition: serializePosition,\n deserializePosition: deserializePosition,\n serializeRange: serializeRange,\n deserializeRange: deserializeRange,\n canDeserializeRange: canDeserializeRange,\n serializeSelection: serializeSelection,\n deserializeSelection: deserializeSelection,\n canDeserializeSelection: canDeserializeSelection,\n restoreSelectionFromCookie: restoreSelectionFromCookie,\n saveSelectionCookie: saveSelectionCookie,\n getElementChecksum: getElementChecksum,\n nodeToInfoString: nodeToInfoString,\n });\n\n util.crc32 = crc32;\n }\n );\n\n return rangy;\n}, this);\n"; //# sourceMappingURL=rangy-serializer.d.ts.map