Add positive and negative testcases for isdomElement

This commit is contained in:
Melvin Philips 2017-10-05 13:39:34 -07:00
parent d32db3c312
commit 97121dcf83
2 changed files with 16 additions and 2 deletions

View file

@ -162,8 +162,9 @@ is.inRange = (x, range) => {
};
is.domElement = x => {
const ELEMENT_NODE = 1;
const propsToCheck = ['innerHTML', 'ownerDocument', 'style', 'attributes', 'nodeValue'];
return is.object(x) && x.nodeType === 1 && is.string(x.nodeName) &&
return is.object(x) && x.nodeType === ELEMENT_NODE && is.string(x.nodeName) &&
!is.plainObject(x) && propsToCheck.every(prop => prop in x);
};

15
test.js
View file

@ -93,7 +93,20 @@ const types = new Map([
new Object() // eslint-disable-line no-new-object
]],
['integer', 6],
['domElement', document.createElement('div')]
['domElement', ['div',
'input',
'span',
'img',
'canvas',
'script'].map(el => document.createElement(el))],
['non-domElements', [
document.createTextNode('data'),
document.createProcessingInstruction('xml-stylesheet', 'href="mycss.css" type="text/css"'),
document.createComment('This is a comment'),
document,
document.implementation.createDocumentType('svg:svg', '-//W3C//DTD SVG 1.1//EN', 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'),
document.createDocumentFragment()
]]
]);
// This ensures a certain method matches only the types