Permalink
Browse files

Merge pull request #31 from paulmillr/topics/add-dom-checks

Add check for DOM objects in util.inspect().
2 parents de2fdcb + b569e8f commit 654c8ddaddb6b9d9e976e2a15bce7634d79a5894 @rauchg rauchg committed Aug 3, 2012
Showing with 34 additions and 0 deletions.
  1. +34 −0 expect.js
View
@@ -509,6 +509,36 @@
return j <= i ? -1 : i;
};
+ // 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') {
+ return object instanceof HTMLElement;
+ } else {
+ return object &&
+ typeof object === 'object' &&
+ object.nodeType === 1 &&
+ typeof object.nodeName === 'string';
+ }
+ };
+
/**
* Inspects an object.
*
@@ -556,6 +586,10 @@
return stylize('null', 'null');
}
+ if (isDOMElement(value)) {
+ return getOuterHTML(value);
+ }
+
// Look up the keys of the object.
var visible_keys = keys(value);
var $keys = showHidden ? Object.getOwnPropertyNames(value) : visible_keys;

0 comments on commit 654c8dd

Please sign in to comment.