Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Remove clondeNode IE8 workaround" #78

Merged
merged 1 commit into from
May 29, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 89 additions & 3 deletions can-view-target.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
var getDocument = require('can-globals/document/document');
var domMutate = require('can-dom-mutate/node');
var namespace = require('can-namespace');
var MUTATION_OBSERVER = require('can-globals/mutation-observer/mutation-observer');

// if an object or a function
// convert into what it should look like
Expand Down Expand Up @@ -31,6 +32,44 @@ var processNodes = function(nodes, paths, location, document){

return cloned.firstChild.childNodes.length === 2;
})(),
clonesWork = typeof document !== "undefined" && (function(){
// Since html5shiv is required to support custom elements, assume cloning
// works in any browser that doesn't have html5shiv

// Clone an element containing a custom tag to see if the innerHTML is what we
// expect it to be, or if not it probably was created outside of the document's
// namespace.
var el = document.createElement('a');
el.innerHTML = "<xyz></xyz>";
var clone = el.cloneNode(true);
var works = clone.innerHTML === "<xyz></xyz>";
var MO, observer;

if(works) {
// Cloning text nodes with dashes seems to create multiple nodes in IE11 when
// MutationObservers of subtree modifications are used on the documentElement.
// Since this is not what we expect we have to include detecting it here as well.
el = document.createDocumentFragment();
el.appendChild(document.createTextNode('foo-bar'));

MO = MUTATION_OBSERVER();

if (MO) {
observer = new MO(function() {});
observer.observe(document.documentElement, { childList: true, subtree: true });

clone = el.cloneNode(true);

observer.disconnect();
} else {
clone = el.cloneNode(true);
}

return clone.childNodes.length === 1;
}

return works;
})(),
namespacesWork = typeof document !== "undefined" && !!document.createElementNS;

/**
Expand All @@ -44,9 +83,56 @@ var processNodes = function(nodes, paths, location, document){
* @param {DocumentFragment} frag A document fragment to clone
* @return {DocumentFragment} a new fragment that is a clone of the provided argument
*/
var cloneNode = function(el){
return el.cloneNode(true);
};
var cloneNode = clonesWork ?
function(el){
return el.cloneNode(true);
} :
function(node){
var document = node.ownerDocument;
var copy;

if(node.nodeType === 1) {
if(node.namespaceURI !== 'http://www.w3.org/1999/xhtml' && namespacesWork && document.createElementNS) {
copy = document.createElementNS(node.namespaceURI, node.nodeName);
}
else {
copy = document.createElement(node.nodeName);
}
} else if(node.nodeType === 3){
copy = document.createTextNode(node.nodeValue);
} else if(node.nodeType === 8) {
copy = document.createComment(node.nodeValue);
} else if(node.nodeType === 11) {
copy = document.createDocumentFragment();
}

if(node.attributes) {
var attributes = node.attributes;
for (var i = 0; i < attributes.length; i++) {
var attribute = attributes[i];
if (attribute && attribute.specified) {
// If the attribute has a namespace set the namespace
// otherwise it will be set to null
if (attribute.namespaceURI) {
copy.setAttributeNS(attribute.namespaceURI, attribute.nodeName || attribute.name, attribute.nodeValue || attribute.value);
} else {
copy.setAttribute(attribute.nodeName || attribute.name, attribute.nodeValue || attribute.value);
}
}
}
}

if(node && node.firstChild) {
var child = node.firstChild;

while(child) {
copy.appendChild( cloneNode(child) );
child = child.nextSibling;
}
}

return copy;
};

function processNode(node, paths, location, document){
var callback,
Expand Down