From a6fa6238a29295fbe02ea12eaf95e908af389853 Mon Sep 17 00:00:00 2001 From: rma4ok Date: Thu, 13 Dec 2012 15:39:34 -0500 Subject: [PATCH] [fix] don't fail in old browsers --- lib/index.js | 75 +++++++++++++++++++++++++++------------------------- 1 file changed, 39 insertions(+), 36 deletions(-) diff --git a/lib/index.js b/lib/index.js index 935ac64..7a287a7 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,49 +1,52 @@ var doc = document - , elementProto = HTMLElement.prototype - , nodeProto = Node.prototype + , win = window + , elementProto = win.HTMLElement && HTMLElement.prototype + , nodeProto = win.Node && Node.prototype // Add support for Node.contains for Firefox < 9 -if (!doc.contains) { +if (!doc.contains && nodeProto) { nodeProto.contains = function(node) { return !!(this.compareDocumentPosition(node) & 16) } } -// Add support for insertAdjacentHTML for Firefox < 8 -// Based on insertAdjacentHTML.js by Eli Grey, http://eligrey.com -if (!doc.body.insertAdjacentHTML) { - elementProto.insertAdjacentHTML = function(position, html) { - var position = position.toLowerCase() - , ref = this - , parent = ref.parentNode - , container = doc.createElement(parent.tagName) - , firstChild, nextSibling, node +if (elementProto){ + // Add support for insertAdjacentHTML for Firefox < 8 + // Based on insertAdjacentHTML.js by Eli Grey, http://eligrey.com + if (!doc.body.insertAdjacentHTML) { + elementProto.insertAdjacentHTML = function(position, html) { + var position = position.toLowerCase() + , ref = this + , parent = ref.parentNode + , container = doc.createElement(parent.tagName) + , firstChild, nextSibling, node - container.innerHTML = html - if (position === 'beforeend') { - while (node = container.firstChild) { - ref.appendChild(node) - } - } else if (position === 'beforebegin') { - while (node = container.firstChild) { - parent.insertBefore(node, ref) - } - } else if (position === 'afterend') { - nextSibling = ref.nextSibling - while (node = container.lastChild) { - nextSibling = parent.insertBefore(node, nextSibling) - } - } else if (position === 'afterbegin') { - firstChild = ref.firstChild - while (node = container.lastChild) { - firstChild = ref.insertBefore(node, firstChild) + container.innerHTML = html + if (position === 'beforeend') { + while (node = container.firstChild) { + ref.appendChild(node) + } + } else if (position === 'beforebegin') { + while (node = container.firstChild) { + parent.insertBefore(node, ref) + } + } else if (position === 'afterend') { + nextSibling = ref.nextSibling + while (node = container.lastChild) { + nextSibling = parent.insertBefore(node, nextSibling) + } + } else if (position === 'afterbegin') { + firstChild = ref.firstChild + while (node = container.lastChild) { + firstChild = ref.insertBefore(node, firstChild) + } } } } -} -elementProto.matches = - elementProto.webkitMatchesSelector || - elementProto.mozMatchesSelector || - elementProto.oMatchesSelector || - elementProto.msMatchesSelector + elementProto.matches = + elementProto.webkitMatchesSelector || + elementProto.mozMatchesSelector || + elementProto.oMatchesSelector || + elementProto.msMatchesSelector +} \ No newline at end of file