{"version":3,"file":"syncNode.cjs","sources":["../../src/utils/syncNode.js"],"sourcesContent":["/**\n * Properties that should be mirrored from source to target.\n * @type {string[]}\n * @private\n */\nconst SYNC_PROPS = ['value', 'checked', 'selected'];\n\n/**\n * Compares two elements to see if their child nodes are structurally equal.\n * @param {Element} a First element to compare.\n * @param {Element} b Second element to compare.\n * @return {boolean} True if all child nodes are deeply equal.\n * @private\n */\nconst areChildNodesEqual = (a, b) => {\n    const aChildren = a.childNodes;\n    const bChildren = b.childNodes;\n    const aLength = aChildren.length;\n\n    if (aLength !== bChildren.length) return false;\n\n    for (let i = 0; i < aLength; i++) {\n        if (!aChildren[i].isEqualNode(bChildren[i])) {\n            return false;\n        }\n    }\n    return true;\n};\n\n/**\n * Synchronizes attributes and key DOM properties (value, checked, selected).\n * Assumes tagName is already the same.\n * @param {Element} targetEl Target DOM element to update.\n * @param {Element} sourceEl Source DOM element to copy attributes from.\n * @private\n */\nconst syncAttributes = (targetEl, sourceEl) => {\n    // Add, update and remove attributes.\n    const srcAttrs = sourceEl.attributes;\n    const tgtAttrs = targetEl.attributes;\n    const srcAttrNames = new Set();\n\n    for (let i = 0, l = srcAttrs.length; i < l; i++) {\n        const { name, value } = srcAttrs[i];\n        srcAttrNames.add(name);\n        if (targetEl.getAttribute(name) !== value) targetEl.setAttribute(name, value);\n    }\n\n    for (let i = tgtAttrs.length - 1; i >= 0; i--) {\n        const { name } = tgtAttrs[i];\n        if (!srcAttrNames.has(name)) targetEl.removeAttribute(name);\n    }\n    // Sync key DOM properties.\n    for (let i = 0, l = SYNC_PROPS.length; i < l; i++) {\n        const prop = SYNC_PROPS[i];\n        if (prop in targetEl && targetEl[prop] !== sourceEl[prop]) {\n            targetEl[prop] = sourceEl[prop];\n        }\n    }\n};\n\n/**\n * Replaces all child nodes of targetEl with those from sourceEl.\n * Moves the nodes without cloning.\n * @param {Element} targetEl The element whose children will be replaced.\n * @param {Element} sourceEl The element providing new children.\n * @module\n * @private\n */\nconst syncNodeContent = (targetEl, sourceEl) => {\n    const children = Array.from(sourceEl.childNodes);\n    targetEl.replaceChildren(...children);\n};\n\n/**\n * Synchronizes the contents of one DOM node to match another.\n * It performs a shallow sync: tag name, attributes, properties, and child nodes.\n * If tag names or node types differ, it replaces the node entirely.\n * @param {Node} targetNode The node currently in the DOM to be updated.\n * @param {Node} sourceNode The reference node to sync from (not in the DOM).\n * @private\n */\nexport default function syncNode(targetNode, sourceNode) {\n    // Replace if node types are different (e.g. element vs text).\n    if (targetNode.nodeType !== sourceNode.nodeType) {\n        targetNode.replaceWith(sourceNode);\n        return;\n    }\n    // Text node: sync value.\n    if (targetNode.nodeType === Node.TEXT_NODE) {\n        if (targetNode.nodeValue !== sourceNode.nodeValue) {\n            targetNode.nodeValue = sourceNode.nodeValue;\n        }\n        return;\n    }\n    // Element node: check tag.\n    if (targetNode.tagName !== sourceNode.tagName) {\n        targetNode.replaceWith(sourceNode);\n        return;\n    }\n    // Sync attributes and DOM properties.\n    syncAttributes(targetNode, sourceNode);\n    // Sync child nodes if necessary.\n    if (!areChildNodesEqual(targetNode, sourceNode)) {\n        syncNodeContent(targetNode, sourceNode);\n    }\n}\n"],"names":[],"mappings":";;AAAA;AACA;AACA;AACA;AACA;AACA,MAAM,UAAU,GAAG,CAAC,OAAO,EAAE,SAAS,EAAE,UAAU,CAAC;;AAEnD;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAM,kBAAkB,GAAG,CAAC,CAAC,EAAE,CAAC,KAAK;AACrC,IAAI,MAAM,SAAS,GAAG,CAAC,CAAC,UAAU;AAClC,IAAI,MAAM,SAAS,GAAG,CAAC,CAAC,UAAU;AAClC,IAAI,MAAM,OAAO,GAAG,SAAS,CAAC,MAAM;;AAEpC,IAAI,IAAI,OAAO,KAAK,SAAS,CAAC,MAAM,EAAE,OAAO,KAAK;;AAElD,IAAI,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,OAAO,EAAE,CAAC,EAAE,EAAE;AACtC,QAAQ,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