RadLikeWhoa / Countable

Add live paragraph-, word- and character-counting to an HTML element.
https://sacha.me/Countable
MIT License
1.64k stars 134 forks source link

Validate on HTMLCollection #40

Closed Mrjaco12 closed 8 years ago

Mrjaco12 commented 8 years ago

The MDN docs and most browsers document functions such as getElementsByClassName and getElementsByTagName returning HTMLCollections rather than node lists. Unfortunately this doesn't pass the current validation which only checks for [object NodeList]. Something like:

  function _validateArguments (elements, callback) {
    var nodes = Object.prototype.toString.call(elements),
        validObject = nodes === '[object NodeList]' || nodes === '[object HTMLCollection]',
        elementsValid = elements && ((validObject && elements.length) || (elements.nodeType === 1)),
        callbackValid = callback && typeof callback === 'function'

    if ('console' in window && 'warn' in console) {
      if (!elementsValid) console.warn('Countable: No valid elements were found')
      if (!callbackValid) console.warn('Countable: "' + callback + '" is not a valid callback function')
    }

    return elementsValid && callbackValid
  }

But I'm not sure how the tests would work since they seem to be designed for single elements.

RadLikeWhoa commented 8 years ago

Good catch, this is fixed in b73cafb. Thanks.

Mrjaco12 commented 8 years ago

Awesome thanks!