Created
May 13, 2015 05:22
-
-
Save zbyte64/89ea49eb11ed38e16003 to your computer and use it in GitHub Desktop.
React iframe head
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function mergeElements(target, source) { | |
var seen = {}; | |
if (target.tagName != source.tagName) { | |
console.log("tagname change:", target.tagName, source.tagName, source) | |
target.parentNode.replaceChild(source, target); | |
return; | |
} | |
_.each(source.attributes, function(att) { | |
seen[att.nodeName] = true; | |
if (att.nodeName === 'data-reactid') { | |
return; | |
} | |
if (target.getAttribute(att.nodeName) !== att.value) { | |
target.setAttribute(att.nodeName, att.value); | |
} | |
}); | |
_.each(target.attributes, function(att) { | |
if (!att) { | |
console.log("target atributes yielded falsey", att, target) | |
} else if (!seen[att.nodeName]) { | |
target.removeAttribute(att.nodeName); | |
} | |
}); | |
if ((target.textContent || source.textContent) && | |
(target.textContent !== source.textContent)) { | |
target.textContent = source.textContent; | |
} | |
}; | |
var doc = this.getDOMNode().contentDocument; | |
var headMarkup = React.renderToString(head); | |
if (headMarkup) { | |
//this.refs.head = head; | |
var div = doc.createElement('div'); | |
div.innerHTML = headMarkup; | |
var newNodes = _.toArray(div.childNodes); //first child is "head" | |
var oldNodes = _.toArray(doc.querySelectorAll('head [data-reactid]')); | |
//console.log("head nodes:", newNodes, div, head); | |
var oldElements = {}; | |
var updatedElements = {}; | |
_.each(oldNodes, function(node, index) { | |
var reactID = node.getAttribute('data-reactid').split('$')[1]; | |
oldElements[reactID] = { | |
node: node, | |
index: index | |
}; | |
}); | |
_.each(newNodes, function(node, index) { | |
if (!node) { | |
console.log("why null node?", node, index); | |
return; //why? | |
} | |
var reactID = (node.getAttribute('data-reactid') || "").split('$')[1]; | |
if (!reactID) { | |
console.log("why no reactid?", node, reactID, index) | |
return; | |
} | |
var oldNode = oldElements[reactID]; | |
//childNode[4].parentNode.insertBefore(childNode[4], childNode[3]); | |
if (oldNode) { | |
mergeElements(oldNode.node, node); | |
if (oldNode.index !== index) { | |
if (oldNodes.length > index) { | |
doc.head.insertBefore(oldNode.node, oldNodes[index]); | |
} else { | |
//console.log("this shouldn't happen", index, oldNodes.length) | |
} | |
} | |
} else { | |
if (oldNodes.length <= index) { | |
doc.head.appendChild(node); | |
} else { | |
doc.head.insertBefore(node, oldNodes[index]); | |
} | |
console.log("appended:", node, doc.head) | |
} | |
updatedElements[reactID] = node; | |
}); | |
_.each(oldElements, function(oldNode, reactID) { | |
if (!updatedElements[reactID]) { | |
doc.head.removeChild(oldNode.node); | |
console.log("removed:", reactID); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment