diff --git a/lib/chai/utils/inspect.js b/lib/chai/utils/inspect.js index e7ed13317..6f84680d6 100644 --- a/lib/chai/utils/inspect.js +++ b/lib/chai/utils/inspect.js @@ -27,24 +27,6 @@ function inspect(obj, showHidden, depth, colors) { return formatValue(ctx, obj, (typeof depth === 'undefined' ? 2 : depth)); } -// https://gist.github.com/1044128/ -var getOuterHTML = function(element) { - if ('outerHTML' in element) return element.outerHTML; - var ns = "http://www.w3.org/1999/xhtml"; - var container = document.createElementNS(ns, '_'); - var elemProto = (window.HTMLElement || window.Element).prototype; - var xmlSerializer = new XMLSerializer(); - var html; - if (document.xmlVersion) { - return xmlSerializer.serializeToString(element); - } else { - container.appendChild(element.cloneNode(false)); - html = container.innerHTML.replace('><', '>' + element.innerHTML + '<'); - container.innerHTML = ''; - return html; - } -}; - // Returns true if object is a DOM element. var isDOMElement = function (object) { if (typeof HTMLElement === 'object') { @@ -78,9 +60,37 @@ function formatValue(ctx, value, recurseTimes) { return primitive; } - // If it's DOM elem, get outer HTML. + // If this is a DOM element, try to get the outer HTML. if (isDOMElement(value)) { - return getOuterHTML(value); + if ('outerHTML' in value) { + return value.outerHTML; + // This value does not have an outerHTML attribute, + // it could still be an XML element + } else { + // Attempt to serialize it + try { + if (document.xmlVersion) { + var xmlSerializer = new XMLSerializer(); + return xmlSerializer.serializeToString(value); + } else { + // Firefox 11- do not support outerHTML + // It does, however, support innerHTML + // Use the following to render the element + var ns = "http://www.w3.org/1999/xhtml"; + var container = document.createElementNS(ns, '_'); + + container.appendChild(value.cloneNode(false)); + html = container.innerHTML + .replace('><', '>' + value.innerHTML + '<'); + container.innerHTML = ''; + return html; + } + } catch (err) { + // This could be a non-native DOM implementation, + // continue with the normal flow: + // printing the element as if it is an object. + } + } } // Look up the keys of the object.