46 lines
869 B
JavaScript
46 lines
869 B
JavaScript
'use strict'
|
|
|
|
module.exports = isElement
|
|
|
|
// Check if if `node` is an `element` and, if `tagNames` is given, `node`
|
|
// matches them `tagNames`.
|
|
function isElement(node, tagNames) {
|
|
var name
|
|
|
|
if (
|
|
!(
|
|
tagNames === null ||
|
|
tagNames === undefined ||
|
|
typeof tagNames === 'string' ||
|
|
(typeof tagNames === 'object' && tagNames.length !== 0)
|
|
)
|
|
) {
|
|
throw new Error(
|
|
'Expected `string` or `Array.<string>` for `tagNames`, not `' +
|
|
tagNames +
|
|
'`'
|
|
)
|
|
}
|
|
|
|
if (
|
|
!node ||
|
|
typeof node !== 'object' ||
|
|
node.type !== 'element' ||
|
|
typeof node.tagName !== 'string'
|
|
) {
|
|
return false
|
|
}
|
|
|
|
if (tagNames === null || tagNames === undefined) {
|
|
return true
|
|
}
|
|
|
|
name = node.tagName
|
|
|
|
if (typeof tagNames === 'string') {
|
|
return name === tagNames
|
|
}
|
|
|
|
return tagNames.indexOf(name) !== -1
|
|
}
|