# All files / src parse.js

100% Statements 31/31
100% Branches 8/8
100% Functions 6/6
100% Lines 27/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    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
    4717x
    91x

    91x
    91x
    91x
    91x

    312x
    190x

    91x

    91x
    94x
    190x

    91x
    4x
    4x
    4x
    3x
    3x

    1x
    1x

    91x

    91x
    91x
    91x

    91x
    1x

    90x

     module.exports = function (list) {
      var Item = require('./item')(list)

      var getChildren = function (parent) {
        var nodes = parent.childNodes,
          items = []
        for (var i = 0, il = nodes.length; i < il; i++) {
          // Only textnodes have a data attribute
          if (nodes[i].data === undefined) {
            items.push(nodes[i])
          }
        }
        return items
      }

      var parse = function (itemElements, valueNames) {
        for (var i = 0, il = itemElements.length; i < il; i++) {
          list.items.push(new Item(valueNames, itemElements[i]))
        }
      }
      var parseAsync = function (itemElements, valueNames) {
        var itemsToIndex = itemElements.splice(0, 50) // TODO: If < 100 items, what happens in IE etc?
        parse(itemsToIndex, valueNames)
        if (itemElements.length > 0) {
          setTimeout(function () {
            parseAsync(itemElements, valueNames)
          }, 1)
        } else {
          list.update()
          list.trigger('parseComplete')
        }
      }

      list.handlers.parseComplete = list.handlers.parseComplete || []

      return function () {
        var itemsToIndex = getChildren(list.list),
          valueNames = list.valueNames

        if (list.indexAsync) {
          parseAsync(itemsToIndex, valueNames)
        } else {
          parse(itemsToIndex, valueNames)
        }
      }
    }