# All files / src/utils get-by-class.js

93.33% Statements 28/30
81.82% Branches 18/22
100% Functions 5/5
93.33% Lines 28/30

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64

    18x
    2015x
    1682x

    333x

    18x
    1x
    1x

    1x

    18x
    1x
    1x

    1x
    1x
    1x
    1x
    1x
    1x

    1x
    1x

    1x

    18x
    18x
    2017x
    2017x
    2015x
    2x
    1x

    1x

     /**
     * A cross-browser implementation of getElementsByClass.
     * Heavily based on Dustin Diaz's function: http://dustindiaz.com/getelementsbyclass.
     *
     * Find all elements with class `className` inside `container`.
     * Use `single = true` to increase performance in older browsers
     * when only one element is needed.
     *
     * @param {String} className
     * @param {Element} container
     * @param {Boolean} single
     * @api public
     */

    var getElementsByClassName = function(container, className, single) {
      if (single) {
        return container.getElementsByClassName(className)[0];
      } else {
        return container.getElementsByClassName(className);
      }
    };

    var querySelector = function(container, className, single) {
      className = '.' + className;
      Iif (single) {
        return container.querySelector(className);
      } else {
        return container.querySelectorAll(className);
      }
    };

    var polyfill = function(container, className, single) {
      var classElements = [],
        tag = '*';

      var els = container.getElementsByTagName(tag);
      var elsLen = els.length;
      var pattern = new RegExp("(^|\\s)"+className+"(\\s|$)");
      for (var i = 0, j = 0; i < elsLen; i++) {
        Eif ( pattern.test(els[i].className) ) {
          Iif (single) {
            return els[i];
          } else {
            classElements[j] = els[i];
            j++;
          }
        }
      }
      return classElements;
    };

    module.exports = (function() {
      return function(container, className, single, options) {
        options = options || {};
        if ((options.test && options.getElementsByClassName) || (!options.test && document.getElementsByClassName)) {
          return getElementsByClassName(container, className, single);
        } else if ((options.test && options.querySelector) || (!options.test && document.querySelector)) {
          return querySelector(container, className, single);
        } else {
          return polyfill(container, className, single);
        }
      }
    })();