From 4e6b8cd6994e233385664399cbd7b8d89232c5cd Mon Sep 17 00:00:00 2001 From: Mike Bostock Date: Thu, 3 Jun 2021 14:38:37 -0700 Subject: [PATCH] fix #154; stricter instanceof Element --- src/index.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index bb3fd51..9944d87 100644 --- a/src/index.js +++ b/src/index.js @@ -17,7 +17,7 @@ export class Inspector { } fulfilled(value, name) { const {_node} = this; - if (!(value instanceof Element || value instanceof Text) || (value.parentNode && value.parentNode !== _node)) { + if (!isnode(value) || (value.parentNode && value.parentNode !== _node)) { value = inspect(value, false, _node.firstChild // TODO Do this better. && _node.firstChild.classList && _node.firstChild.classList.contains("observablehq--expanded"), name); @@ -57,3 +57,13 @@ Inspector.into = function(container) { return new Inspector(container.appendChild(document.createElement("div"))); }; }; + +// Returns true if the given value is something that should be added to the DOM +// by the inspector, rather than being inspected. This deliberately excludes +// DocumentFragment since appending a fragment “dissolves” (mutates) the +// fragment, and we wish for the inspector to not have side-effects. Also, +// HTMLElement.prototype is an instanceof Element, but not an element! +function isnode(value) { + return (value instanceof Element || value instanceof Text) + && (value instanceof value.constructor); +}