# Code coverage report for cheerio/lib/parse.js

## Statements: 100% (37 / 37) Branches: 100% (30 / 30) Functions: 100% (3 / 3) Lines: 100% (36 / 36) Ignored: none

All files » cheerio/lib/ » parse.js

    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
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87

    1

    1
    677

    677

    677

    677

    1

    1408

    1408
    1406

    2

    1408

    1

    724

    724
    698

    26

    724
    886

    886

    886
    17
    17
    1

    17
    4

    886
    855
    855

    31

    886
    834
    834

    52
    52

    724

     /*
      Module Dependencies
    */
    var htmlparser = require('htmlparser2');

    /*
      Parser
    */
    exports = module.exports = function(content, options) {
      var dom = exports.evaluate(content, options),
          // Generic root element
          root = exports.evaluate('<root></root>', options)[0];

      root.type = 'root';

      // Update the dom using the root
      exports.update(dom, root);

      return root;
    };

    exports.evaluate = function(content, options) {
      // options = options || $.fn.options;

      var dom;

      if (typeof content === 'string' || Buffer.isBuffer(content)) {
        dom = htmlparser.parseDOM(content, options);
      } else {
        dom = content;
      }

      return dom;
    };

    /*
      Update the dom structure, for one changed layer
    */
    exports.update = function(arr, parent) {
      // normalize
      if (!Array.isArray(arr)) arr = [arr];

      // Update parent
      if (parent) {
        parent.children = arr;
      } else {
        parent = null;
      }

      // Update neighbors
      for (var i = 0; i < arr.length; i++) {
        var node = arr[i];

        // Cleanly remove existing nodes from their previous structures.
        var oldParent = node.parent || node.root,
            oldSiblings = oldParent && oldParent.children;
        if (oldSiblings && oldSiblings !== arr) {
          oldSiblings.splice(oldSiblings.indexOf(node), 1);
          if (node.prev) {
            node.prev.next = node.next;
          }
          if (node.next) {
            node.next.prev = node.prev;
          }
        }

        if (parent) {
          node.prev = arr[i - 1] || null;
          node.next = arr[i + 1] || null;
        } else {
          node.prev = node.next = null;
        }

        if (parent && parent.type === 'root') {
          node.root = parent;
          node.parent = null;
        } else {
          node.root = null;
          node.parent = parent;
        }
      }

      return parent;
    };

    // module.exports = $.extend(exports);