npm-dom / is-dom Goto Github PK
View Code? Open in Web Editor NEWCheck if the given object is a dom node
Home Page: http://npm.im/is-dom
License: MIT License
Check if the given object is a dom node
Home Page: http://npm.im/is-dom
License: MIT License
You should use is-object.
Oops, you have a !val
check.
Would you be open to the idea of converting this into a multi-function library, similar to date-fns?:
import {
isDetachedDOMNode,
isDocumentDOMElement,
isDOMElement,
isDOMNode,
isVoidDOMElement
} from 'is-dom';
I have these published separately already, but the use of many of them bloats a project's package.json file. I'd like to move them to this project and unpublish them.
I have a ProcessingInstruction
Node that gets passed to this library to check if it's a Node. However, this specific type of node is returning false
- I believe it should return true.
Here's a representation of the Node I'm talking about (and MDN docs here):
ProcessingInstruction {ownerDocument: Document, target: "xml", tagName: "xml", data: "version="1.0" encoding="UTF-8"", nodeValue: "version="1.0" encoding="UTF-8"", …}
columnNumber: 1,
data: "version="1.0" encoding="UTF-8"",
lineNumber: 1,
nextSibling: Text {ownerDocument: Document, data: "↵", nodeValue: "↵", length: 1,
previousSibling: ProcessingInstruction, …},
nodeValue: "version="1.0" encoding="UTF-8"",
ownerDocument: Document {implementation: DOMImplementation, childNodes: NodeList, doctype: null, documentURI: undefined, firstChild: ProcessingInstruction, …},
parentNode: Document {implementation: DOMImplementation, childNodes: NodeList, doctype: null, documentURI: undefined, firstChild: ProcessingInstruction, …},
previousSibling : null,
tagName: "xml",
target: "xml",
textContent: (...),
__proto__:
nodeType: 7,
constructor: ƒ ProcessingInstruction(),
textContent: (...),
Note that this type of Node has a nodeType
, but not a nodeName
. The actual XML would look something like this:
<?xml version="1.0" encoding="UTF-8"?> // <-- This is the ProcessingInstruction
It fails because these types of Nodes have a nodeType
property, but not a nodeName
property:
function isNode (val) {
return (!val || typeof val !== 'object')
? false
// This check fails because `typeof window.Node === "function"`
: (typeof window === 'object' && typeof window.Node === 'object')
// Branch not taken, but would return true
? (val instanceof window.Node)
// **HERE**: `nodeName` does not exist on ProcessingInstruction nodes
: (typeof val.nodeType === 'number') &&
(typeof val.nodeName === 'string')
}
Do you agree this should be returning True on these types of nodes? I'd be happy to submit a PR with a fix, just not sure of the best way to go about.
One options: the typeof window === 'object' && typeof window.Node === 'object'
could have one more case to check || typeof window.Now === 'function'
. This would return true, and val instanceof window.Node
would return true as well.
Let me know what you think!
Because you use instance of Node
, nodes from other documents will return false
.
I am using is-dom through embed-js. I was wondering why it started to throw weird errors in IE and started digging deeper ...
Yarn silently updated my projects is-dom package from 1.0.9 to 1.1.0.
Following line causes the problem in IE:
Line 5 in ebdf6a0
typeof window.Node !== 'function'
always returns true in IE (window.Node is of type 'object')
My temporary workaround is to pin is-dom version to 1.0.9.
{
"name": "test",
"version": "0.0.0",
"dependencies": {
"embed-js": "^5.0.3",
"embed-plugin-noembed": "^5.0.3",
},
"resolutions": {
"is-dom": "1.0.9"
},
"module": "src/main.js"
}
In a jsdom scenario, those globals are not defined by default and could return false
for a perfectly fine Node
.
I was using "is-dom" indirectly via the "console-redirect" module from my electron-main.js. This was working forever until this last week when electron got an error about "window" not being defined during startup.
I had to change my package.json to explicitly require version 1.0.9 to avoid the problem.
The license does not have the copyright information and I was not able to find the author information anywhere. Could you please include it? Thanks.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.