Add additional checks for dom Element check

This commit is contained in:
Melvin Philips 2017-10-05 02:43:46 -07:00
parent 2e58d40c7d
commit d32db3c312
2 changed files with 10 additions and 13 deletions

View file

@ -161,6 +161,10 @@ is.inRange = (x, range) => {
throw new TypeError(`Invalid range: ${util.inspect(range)}`);
};
is.domElement = x => is.object(x) && x.nodeType === 1 && is.string(x.nodeName) && !is.plainObject(x);
is.domElement = x => {
const propsToCheck = ['innerHTML', 'ownerDocument', 'style', 'attributes', 'nodeValue'];
return is.object(x) && x.nodeType === 1 && is.string(x.nodeName) &&
!is.plainObject(x) && propsToCheck.every(prop => prop in x);
};
module.exports = is;

17
test.js
View file

@ -8,6 +8,8 @@ const isNode8orHigher = Number(process.versions.node.split('.')[0]) >= 8;
const PromiseSubclassFixture = class extends Promise {};
const ErrorSubclassFixture = class extends Error {};
const document = jsdom();
const types = new Map([
['undefined', undefined],
['null', null],
@ -90,7 +92,8 @@ const types = new Map([
Object.create(null),
new Object() // eslint-disable-line no-new-object
]],
['integer', 6]
['integer', 6],
['domElement', document.createElement('div')]
]);
// This ensures a certain method matches only the types
@ -361,16 +364,6 @@ test('is.inRange', t => {
});
test('is.domElement', t => {
const document = jsdom();
t.true(m.domElement(document.createElement('div')));
t.false(m.domElement('hello world'));
t.false(m.domElement([]));
t.false(m.domElement(new Map()));
t.false(m.domElement(null));
t.false(m.domElement(undefined));
t.false(m.domElement(0));
t.false(m.domElement(NaN));
t.false(m.domElement(Infinity));
testType(t, 'domElement');
t.false(m.domElement({nodeType: 1, nodeName: 'div'}));
});