1 line
No EOL
10 KiB
JavaScript
1 line
No EOL
10 KiB
JavaScript
let DOCUMENT_FRAGMENT_NODE=11;function morphAttrs(fromNode,toNode){var attrValue,toNodeAttrs=toNode.attributes;let attr,attrName,attrNamespaceURI,fromValue;if(toNode.nodeType!==DOCUMENT_FRAGMENT_NODE&&fromNode.nodeType!==DOCUMENT_FRAGMENT_NODE){for(let i=toNodeAttrs.length-1;0<=i;i--)attr=toNodeAttrs[i],attrName=attr.name,attrNamespaceURI=attr.namespaceURI,attrValue=attr.value,attrNamespaceURI?(attrName=attr.localName||attrName,(fromValue=fromNode.getAttributeNS(attrNamespaceURI,attrName))!==attrValue&&("xmlns"===attr.prefix&&(attrName=attr.name),fromNode.setAttributeNS(attrNamespaceURI,attrName,attrValue))):(fromValue=fromNode.getAttribute(attrName))!==attrValue&&fromNode.setAttribute(attrName,attrValue);var fromNodeAttrs=fromNode.attributes;for(let d=fromNodeAttrs.length-1;0<=d;d--)attr=fromNodeAttrs[d],attrName=attr.name,(attrNamespaceURI=attr.namespaceURI)?(attrName=attr.localName||attrName,toNode.hasAttributeNS(attrNamespaceURI,attrName)||fromNode.removeAttributeNS(attrNamespaceURI,attrName)):toNode.hasAttribute(attrName)||fromNode.removeAttribute(attrName)}}let range,NS_XHTML="http://www.w3.org/1999/xhtml",doc="undefined"==typeof document?void 0:document,HAS_TEMPLATE_SUPPORT=!!doc&&"content"in doc.createElement("template"),HAS_RANGE_SUPPORT=!!doc&&doc.createRange&&"createContextualFragment"in doc.createRange();function createFragmentFromTemplate(str){var template=doc.createElement("template");return template.innerHTML=str,template.content.childNodes[0]}function createFragmentFromRange(str){return range||(range=doc.createRange()).selectNode(doc.body),range.createContextualFragment(str).childNodes[0]}function createFragmentFromWrap(str){var fragment=doc.createElement("body");return fragment.innerHTML=str,fragment.childNodes[0]}function toElement(str){return str=str.trim(),(HAS_TEMPLATE_SUPPORT?createFragmentFromTemplate:HAS_RANGE_SUPPORT?createFragmentFromRange:createFragmentFromWrap)(str)}function compareNodeNames(fromEl,toEl){var fromCodeStart,toCodeStart,fromEl=fromEl.nodeName,toEl=toEl.nodeName;return fromEl===toEl||(fromCodeStart=fromEl.charCodeAt(0),toCodeStart=toEl.charCodeAt(0),fromCodeStart<=90&&97<=toCodeStart?fromEl===toEl.toUpperCase():toCodeStart<=90&&97<=fromCodeStart&&toEl===fromEl.toUpperCase())}function createElementNS(name,namespaceURI){return namespaceURI&&namespaceURI!==NS_XHTML?doc.createElementNS(namespaceURI,name):doc.createElement(name)}function moveChildren(fromEl,toEl){let curChild=fromEl.firstChild;for(;curChild;){var nextChild=curChild.nextSibling;toEl.appendChild(curChild),curChild=nextChild}return toEl}function syncBooleanAttrProp(fromEl,toEl,name){fromEl[name]!==toEl[name]&&(fromEl[name]=toEl[name],fromEl[name]?fromEl.setAttribute(name,""):fromEl.removeAttribute(name))}let specialElHandlers={OPTION:function(fromEl,toEl){let parentNode=fromEl.parentNode;if(parentNode){let parentName=parentNode.nodeName.toUpperCase();"OPTGROUP"===parentName&&(parentNode=parentNode.parentNode,parentName=parentNode&&parentNode.nodeName.toUpperCase()),"SELECT"!==parentName||parentNode.hasAttribute("multiple")||(fromEl.hasAttribute("selected")&&!toEl.selected&&(fromEl.setAttribute("selected","selected"),fromEl.removeAttribute("selected")),parentNode.selectedIndex=-1)}syncBooleanAttrProp(fromEl,toEl,"selected")},INPUT:function(fromEl,toEl){syncBooleanAttrProp(fromEl,toEl,"checked"),syncBooleanAttrProp(fromEl,toEl,"disabled"),fromEl.value!==toEl.value&&(fromEl.value=toEl.value),toEl.hasAttribute("value")||fromEl.removeAttribute("value")},TEXTAREA:function(fromEl,toEl){var oldValue,toEl=toEl.value,firstChild=(fromEl.value!==toEl&&(fromEl.value=toEl),fromEl.firstChild);firstChild&&(oldValue=firstChild.nodeValue)!==toEl&&(toEl||oldValue!==fromEl.placeholder)&&(firstChild.nodeValue=toEl)},SELECT:function(fromEl,toEl){if(!toEl.hasAttribute("multiple")){let selectedIndex=-1,i=0,curChild=fromEl.firstChild,optgroup;for(var nodeName;curChild;)if("OPTGROUP"===(nodeName=curChild.nodeName&&curChild.nodeName.toUpperCase()))curChild=(optgroup=curChild).firstChild;else{if("OPTION"===nodeName){if(curChild.hasAttribute("selected")){selectedIndex=i;break}i++}!(curChild=curChild.nextSibling)&&optgroup&&(curChild=optgroup.nextSibling,optgroup=null)}fromEl.selectedIndex=selectedIndex}}},ELEMENT_NODE=1,DOCUMENT_FRAGMENT_NODE$1=11,TEXT_NODE=3,COMMENT_NODE=8;function noop(){}function defaultGetNodeKey(node){if(node)return node.getAttribute&&node.getAttribute("id")||node.id}function onBeforeElUpdated(fromEl,toEl){return!fromEl.isEqualNode(toEl)}function morphdomFactory(morphAttrs){return function(fromNode,toNode,options){options=options||{},"string"==typeof toNode?"#document"===fromNode.nodeName||"HTML"===fromNode.nodeName||"BODY"===fromNode.nodeName?(toNodeHtml=toNode,(toNode=doc.createElement("html")).innerHTML=toNodeHtml):toNode=toElement(toNode):toNode.nodeType===DOCUMENT_FRAGMENT_NODE$1&&(toNode=toNode.firstElementChild);let getNodeKey=options.getNodeKey||defaultGetNodeKey,onBeforeNodeAdded=options.onBeforeNodeAdded||noop,onNodeAdded=options.onNodeAdded||noop,onBeforeNodeDiscarded=options.onBeforeNodeDiscarded||noop,onNodeDiscarded=options.onNodeDiscarded||noop,onBeforeElChildrenUpdated=options.onBeforeElChildrenUpdated||noop;var toNodeHtml=!0===options.childrenOnly;let fromNodesLookup=Object.create(null),keyedRemovalList=[];function addKeyedRemoval(key){keyedRemovalList.push(key)}function removeNode(node,parentNode,skipKeyedNodes){!1!==onBeforeNodeDiscarded(node)&&(parentNode&&parentNode.removeChild(node),onNodeDiscarded(node),function walkDiscardedChildNodes(node,skipKeyedNodes){if(node.nodeType===ELEMENT_NODE){let curChild=node.firstChild;for(;curChild;){let key;skipKeyedNodes&&(key=getNodeKey(curChild))?addKeyedRemoval(key):(onNodeDiscarded(curChild),curChild.firstChild&&walkDiscardedChildNodes(curChild,skipKeyedNodes)),curChild=curChild.nextSibling}}}(node,skipKeyedNodes))}function morphEl(fromEl,toEl,childrenOnly){var toElKey=getNodeKey(toEl);if(toElKey&&delete fromNodesLookup[toElKey],!childrenOnly){if(!1===onBeforeElUpdated(fromEl,toEl))return;if(morphAttrs(fromEl,toEl),!1===onBeforeElChildrenUpdated(fromEl,toEl))return}"TEXTAREA"!==fromEl.nodeName?((fromEl,toEl)=>{let curToNodeChild=toEl.firstChild,curFromNodeChild=fromEl.firstChild,curToNodeKey,curFromNodeKey,fromNextSibling,toNextSibling,matchingFromEl;outer:for(;curToNodeChild;){for(toNextSibling=curToNodeChild.nextSibling,curToNodeKey=getNodeKey(curToNodeChild);curFromNodeChild;){if(fromNextSibling=curFromNodeChild.nextSibling,curToNodeChild.isSameNode&&curToNodeChild.isSameNode(curFromNodeChild)){curToNodeChild=toNextSibling,curFromNodeChild=fromNextSibling;continue outer}curFromNodeKey=getNodeKey(curFromNodeChild);var curFromNodeType=curFromNodeChild.nodeType;let isCompatible;if(curFromNodeType===curToNodeChild.nodeType&&(curFromNodeType===ELEMENT_NODE?(curToNodeKey?curToNodeKey!==curFromNodeKey&&(!(matchingFromEl=fromNodesLookup[curToNodeKey])||fromNextSibling===matchingFromEl?isCompatible=!1:(fromEl.insertBefore(matchingFromEl,curFromNodeChild),curFromNodeKey?addKeyedRemoval(curFromNodeKey):removeNode(curFromNodeChild,fromEl,!0),curFromNodeChild=matchingFromEl)):curFromNodeKey&&(isCompatible=!1),(isCompatible=!1!==isCompatible&&compareNodeNames(curFromNodeChild,curToNodeChild))&&morphEl(curFromNodeChild,curToNodeChild)):curFromNodeType!==TEXT_NODE&&curFromNodeType!==COMMENT_NODE||(isCompatible=!0,curFromNodeChild.nodeValue!==curToNodeChild.nodeValue&&(curFromNodeChild.nodeValue=curToNodeChild.nodeValue))),isCompatible){curToNodeChild=toNextSibling,curFromNodeChild=fromNextSibling;continue outer}curFromNodeKey?addKeyedRemoval(curFromNodeKey):removeNode(curFromNodeChild,fromEl,!0),curFromNodeChild=fromNextSibling}var onBeforeNodeAddedResult;curToNodeKey&&(matchingFromEl=fromNodesLookup[curToNodeKey])&&compareNodeNames(matchingFromEl,curToNodeChild)?(fromEl.appendChild(matchingFromEl),morphEl(matchingFromEl,curToNodeChild)):!1!==(onBeforeNodeAddedResult=onBeforeNodeAdded(curToNodeChild))&&((curToNodeChild=onBeforeNodeAddedResult?onBeforeNodeAddedResult:curToNodeChild).actualize&&(curToNodeChild=curToNodeChild.actualize(fromEl.ownerDocument||doc)),fromEl.appendChild(curToNodeChild),function handleNodeAdded(el){onNodeAdded(el);let curChild=el.firstChild;for(;curChild;){var nextSibling=curChild.nextSibling,key=getNodeKey(curChild);(key=key&&fromNodesLookup[key])&&compareNodeNames(curChild,key)?(curChild.parentNode.replaceChild(key,curChild),morphEl(key,curChild)):handleNodeAdded(curChild),curChild=nextSibling}}(curToNodeChild)),curToNodeChild=toNextSibling,curFromNodeChild=fromNextSibling}((fromEl,curFromNodeChild,curFromNodeKey)=>{for(;curFromNodeChild;){var fromNextSibling=curFromNodeChild.nextSibling;(curFromNodeKey=getNodeKey(curFromNodeChild))?addKeyedRemoval(curFromNodeKey):removeNode(curFromNodeChild,fromEl,!0),curFromNodeChild=fromNextSibling}})(fromEl,curFromNodeChild,curFromNodeKey);var specialElHandler=specialElHandlers[fromEl.nodeName];specialElHandler&&specialElHandler(fromEl,toEl)})(fromEl,toEl):specialElHandlers.TEXTAREA(fromEl,toEl)}!function indexTree(node){if(node.nodeType===ELEMENT_NODE||node.nodeType===DOCUMENT_FRAGMENT_NODE$1){let curChild=node.firstChild;for(;curChild;){var key=getNodeKey(curChild);key&&(fromNodesLookup[key]=curChild),indexTree(curChild),curChild=curChild.nextSibling}}}(fromNode);let morphedNode=fromNode;var options=morphedNode.nodeType,toNodeType=toNode.nodeType;if(!toNodeHtml)if(options===ELEMENT_NODE)toNodeType===ELEMENT_NODE?compareNodeNames(fromNode,toNode)||(onNodeDiscarded(fromNode),morphedNode=moveChildren(fromNode,createElementNS(toNode.nodeName,toNode.namespaceURI))):morphedNode=toNode;else if(options===TEXT_NODE||options===COMMENT_NODE){if(toNodeType===options)return morphedNode.nodeValue!==toNode.nodeValue&&(morphedNode.nodeValue=toNode.nodeValue),morphedNode;morphedNode=toNode}if(morphedNode===toNode)onNodeDiscarded(fromNode);else{if(toNode.isSameNode&&toNode.isSameNode(morphedNode))return;if(morphEl(morphedNode,toNode,toNodeHtml),keyedRemovalList)for(let i=0,len=keyedRemovalList.length;i<len;i++){var elToRemove=fromNodesLookup[keyedRemovalList[i]];elToRemove&&removeNode(elToRemove,elToRemove.parentNode,!1)}}return!toNodeHtml&&morphedNode!==fromNode&&fromNode.parentNode&&(morphedNode.actualize&&(morphedNode=morphedNode.actualize(fromNode.ownerDocument||doc)),fromNode.parentNode.replaceChild(morphedNode,fromNode)),morphedNode}}let morphdom=morphdomFactory(morphAttrs);export default morphdom; |