Commit 3cdcc2d6 authored by Julien Cigar's avatar Julien Cigar
Browse files

Fix document.importNode in IE9.

document.importNode exists in IE9 but fails. Just fails. It's not implemented, just says it's there and fails when called *sigh*.
parent fa6f48c0
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
* "THE BEER-WARE LICENSE" (Revision 42): * "THE BEER-WARE LICENSE" (Revision 42):
* <julien.cigar@gmail.com> wrote this file. As long as you retain this notice * <julien.cigar@gmail.com> wrote this file. As long as you retain this notice
* you can do whatever you want with this stuff. If we meet some day, and you * you can do whatever you want with this stuff. If we meet some day, and you
* think this stuff is worth it, you can buy me a beer in return Julien Cigar. * think this stuff is worth it, you can buy me a beer in return.
* ---------------------------------------------------------------------------- * ----------------------------------------------------------------------------
*/ */
...@@ -394,72 +394,76 @@ ...@@ -394,72 +394,76 @@
deep = true; deep = true;
} }
return document.importNode ? /* Note: document.importNode exists in IE9 but fails. Just fails. It's
document.importNode(node, deep) : * not implemented, just says it's there and fails when called.
(function(node, deep) { */
try {
/* Returns an integer code representing the type of the node. */ return document.importNode(node, deep) :
var NodeTypes = { } catch(e) {
ELEMENT_NODE : 1, return (function(node, deep) {
ATTRIBUTE_NODE : 2, /* Returns an integer code representing the type of the node. */
TEXT_NODE : 3, var NodeTypes = {
CDATA_SECTION_NODE : 4, ELEMENT_NODE : 1,
ENTITY_REFERENCE_NODE : 5, ATTRIBUTE_NODE : 2,
ENTITY_NODE : 6, TEXT_NODE : 3,
PROCESSING_INSTRUCTION_NODE : 7, CDATA_SECTION_NODE : 4,
COMMENT_NODE : 8, ENTITY_REFERENCE_NODE : 5,
DOCUMENT_NODE : 9, ENTITY_NODE : 6,
DOCUMENT_TYPE_NODE : 10, PROCESSING_INSTRUCTION_NODE : 7,
DOCUMENT_FRAGMENT_NODE : 11, COMMENT_NODE : 8,
NOTATION_NODE : 12 DOCUMENT_NODE : 9,
} DOCUMENT_TYPE_NODE : 10,
DOCUMENT_FRAGMENT_NODE : 11,
NOTATION_NODE : 12
}
switch (node.nodeType) { switch (node.nodeType) {
case NodeTypes.ELEMENT_NODE: case NodeTypes.ELEMENT_NODE:
var newNode = document.createElement(node.nodeName); var newNode = document.createElement(node.nodeName);
if (node.attributes && node.attributes.length > 0) { if (node.attributes && node.attributes.length > 0) {
for (var i=0, _len=node.attributes.length ; i<_len ; i++) { for (var i=0, _len=node.attributes.length ; i<_len ; i++) {
var attr_name = node.attributes[i].nodeName, var attr_name = node.attributes[i].nodeName,
attr_value = node.getAttribute(attr_name) attr_value = node.getAttribute(attr_name)
; ;
if (attr_name.toLowerCase() == 'style') { if (attr_name.toLowerCase() == 'style') {
newNode.style.cssText = attr_value; newNode.style.cssText = attr_value;
} else if (attr_name.toLowerCase() == 'class') { } else if (attr_name.toLowerCase() == 'class') {
newNode.className = attr_value; newNode.className = attr_value;
} else if (attr_name.slice(0,2) == 'on') { } else if (attr_name.slice(0,2) == 'on') {
// FIXME: slow... // FIXME: slow...
newNode[attr_name] = new Function(attr_value); newNode[attr_name] = new Function(attr_value);
} else { } else {
newNode.setAttribute(attr_name, attr_value); newNode.setAttribute(attr_name, attr_value);
}
} }
} }
}
if (deep && node.childNodes && node.childNodes.length > 0) { if (deep && node.childNodes && node.childNodes.length > 0) {
for (var i=0, _len=node.childNodes.length ; i<_len ; i++) { for (var i=0, _len=node.childNodes.length ; i<_len ; i++) {
newNode.appendChild(Yeti.DOM.importNode(node.childNodes[i], deep)); newNode.appendChild(Yeti.DOM.importNode(node.childNodes[i], deep));
}
} }
}
return newNode; return newNode;
break; break;
case NodeTypes.TEXT_NODE: case NodeTypes.TEXT_NODE:
return document.createTextNode(node.nodeValue); return document.createTextNode(node.nodeValue);
break; break;
case NodeTypes.CDATA_SECTION_NODE: case NodeTypes.CDATA_SECTION_NODE:
return document.createCDATASection(node.nodeValue); return document.createCDATASection(node.nodeValue);
break; break;
case NodeTypes.COMMENT_NODE: case NodeTypes.COMMENT_NODE:
return document.createComment(node.nodeValue); return document.createComment(node.nodeValue);
break; break;
} }
})(node, deep); })(node, deep);
}
} }
/* Yeti.DOM.firstElementChild /* Yeti.DOM.firstElementChild
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment