# Code coverage report for cheerio/lib/api/manipulation.js

## Statements: 97.84% (136 / 139) Branches: 96.59% (85 / 88) Functions: 100% (28 / 28) Lines: 100% (135 / 135) Ignored: none

All files » cheerio/lib/api/ » manipulation.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
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    3041

    1
    206
    12
    194
    45
    149
    66
    83
    33

    50

    1
    2
    33

    33
    8
    24
    24

    25
    25

    1
    117

    117

    117
    115
    115
    115

    115
    59
    59
    2

    115
    115

    115
    7

    115
    11

    115
    115

    117
    70

    117
    83

    117

    1
    27

    1
    22

    1
    14

    14
    22
    22
    1

    21

    21

    21
    12

    21

    14

    1
    14

    14
    22
    22
    1

    21

    21

    21
    12

    21

    14

    1
    20

    20
    1

    20
    13
    13
    1

    12

    12

    12
    12
    1

    12
    9

    12

    20

    1
    15

    15
    27
    27
    1

    26

    26

    26

    26
    26

    15

    1
    3
    3
    9

    3

    3

    1
    22
    16
    15

    6

    6
    8
    14

    8

    8

    6

    1
    5

    1

    29
    19
    10

    1
    1
    1

    9
    13
    11

    13

    13

    9

    1

    4

     var _ = require('lodash'),
        parse = require('../parse'),
        $ = require('../static'),
        updateDOM = parse.update,
        evaluate = parse.evaluate,
        utils = require('../utils'),
        domEach = utils.domEach,
        slice = Array.prototype.slice;

    // Create an array of nodes, recursing into arrays and parsing strings if
    // necessary
    exports._makeDomArray = function makeDomArray(elem) {
      if (elem == null) {
        return [];
      } else if (elem.cheerio) {
        return elem.get();
      } else if (Array.isArray(elem)) {
        return _.flatten(elem.map(makeDomArray, this));
      } else if (typeof elem === 'string') {
        return evaluate(elem, this.options);
      } else {
        return [elem];
      }
    };

    var _insert = function(concatenator) {
      return function() {
        var self = this,
            elems = slice.call(arguments),
            dom = this._makeDomArray(elems);

        if (typeof elems[0] === 'function') {
          return domEach(this, function(i, el) {
            dom = self._makeDomArray(elems[0].call(el, i, $.html(el.children)));
            concatenator(dom, el.children, el);
          });
        } else {
          return domEach(this, function(i, el) {
            concatenator(dom, el.children, el);
          });
        }
      };
    };

    /*
     * Modify an array in-place, removing some number of elements and adding new
     * elements directly following them.
     *
     * @param {Array} array Target array to splice.
     * @param {Number} spliceIdx Index at which to begin changing the array.
     * @param {Number} spliceCount Number of elements to remove from the array.
     * @param {Array} newElems Elements to insert into the array.
     *
     * @api private
     */
    var uniqueSplice = function(array, spliceIdx, spliceCount, newElems, parent) {
      var spliceArgs = [spliceIdx, spliceCount].concat(newElems),
          prev = array[spliceIdx - 1] || null,
          next = array[spliceIdx] || null;
      var idx, len, prevIdx, node, oldParent;

      // Before splicing in new elements, ensure they do not already appear in the
      // current array.
      for (idx = 0, len = newElems.length; idx < len; ++idx) {
        node = newElems[idx];
        oldParent = node.parent || node.root;
        prevIdx = oldParent && oldParent.children.indexOf(newElems[idx]);

        if (oldParent && prevIdx > -1) {
          oldParent.children.splice(prevIdx, 1);
          if (parent === oldParent && spliceIdx > prevIdx) {
            spliceArgs[0]--;
          }
        }

        node.root = null;
        node.parent = parent;

        if (node.prev) {
          node.prev.next = node.next || null;
        }

        if (node.next) {
          node.next.prev = node.prev || null;
        }

        node.prev = newElems[idx - 1] || prev;
        node.next = newElems[idx + 1] || next;
      }

      if (prev) {
        prev.next = newElems[0];
      }
      if (next) {
        next.prev = newElems[newElems.length - 1];
      }

      return array.splice.apply(array, spliceArgs);
    };

    exports.append = _insert(function(dom, children, parent) {
      uniqueSplice(children, children.length, 0, dom, parent);
    });

    exports.prepend = _insert(function(dom, children, parent) {
      uniqueSplice(children, 0, 0, dom, parent);
    });

    exports.after = function() {
      var elems = slice.call(arguments),
          dom = this._makeDomArray(elems),
          self = this;

      domEach(this, function(i, el) {
        var parent = el.parent || el.root;
        if (!parent) {
          return;
        }

        var siblings = parent.children,
            index = siblings.indexOf(el);

        // If not found, move on
        Iif (!~index) return;

        if (typeof elems[0] === 'function') {
          dom = self._makeDomArray(elems[0].call(el, i));
        }

        // Add element after `this` element
        uniqueSplice(siblings, ++index, 0, dom, parent);
      });

      return this;
    };

    exports.before = function() {
      var elems = slice.call(arguments),
          dom = this._makeDomArray(elems),
          self = this;

      domEach(this, function(i, el) {
        var parent = el.parent || el.root;
        if (!parent) {
          return;
        }

        var siblings = parent.children,
            index = siblings.indexOf(el);

        // If not found, move on
        Iif (!~index) return;

        if (typeof elems[0] === 'function') {
          dom = self._makeDomArray(elems[0].call(el, i));
        }

        // Add element before `el` element
        uniqueSplice(siblings, index, 0, dom, parent);
      });

      return this;
    };

    /*
      remove([selector])
    */
    exports.remove = function(selector) {
      var elems = this;

      // Filter if we have selector
      if (selector)
        elems = elems.filter(selector);

      domEach(elems, function(i, el) {
        var parent = el.parent || el.root;
        if (!parent) {
          return;
        }

        var siblings = parent.children,
            index = siblings.indexOf(el);

        Iif (!~index) return;

        siblings.splice(index, 1);
        if (el.prev) {
          el.prev.next = el.next;
        }
        if (el.next) {
          el.next.prev = el.prev;
        }
        el.prev = el.next = el.parent = el.root = null;
      });

      return this;
    };

    exports.replaceWith = function(content) {
      var self = this;

      domEach(this, function(i, el) {
        var parent = el.parent || el.root;
        if (!parent) {
          return;
        }

        var siblings = parent.children,
            dom = self._makeDomArray(typeof content === 'function' ? content.call(el, i, el) : content),
            index;

        // In the case that `dom` contains nodes that already exist in other
        // structures, ensure those nodes are properly removed.
        updateDOM(dom, null);

        index = siblings.indexOf(el);

        // Completely remove old element
        uniqueSplice(siblings, index, 1, dom, parent);
        el.parent = el.prev = el.next = el.root = null;
      });

      return this;
    };

    exports.empty = function() {
      domEach(this, function(i, el) {
        _.each(el.children, function(el) {
          el.next = el.prev = el.parent = null;
        });

        el.children.length = 0;
      });
      return this;
    };

    /**
     * Set/Get the HTML
     */
    exports.html = function(str) {
      if (str === undefined) {
        if (!this[0] || !this[0].children) return null;
        return $.html(this[0].children, this.options);
      }

      var opts = this.options;

      domEach(this, function(i, el) {
        _.each(el.children, function(el) {
          el.next = el.prev = el.parent = null;
        });

        var content = str.cheerio ? str.clone().get() : evaluate(str, opts);

        updateDOM(content, el);
      });

      return this;
    };

    exports.toString = function() {
      return $.html(this);
    };

    exports.text = function(str) {
      // If `str` is undefined, act as a "getter"
      if (str === undefined) {
        return $.text(this);
      } else if (typeof str === 'function') {
        // Function support
        return domEach(this, function(i, el) {
          var $el = [el];
          return exports.text.call($el, str.call(el, i, $.text($el)));
        });
      }

      // Append text node to each selected elements
      domEach(this, function(i, el) {
        _.each(el.children, function(el) {
          el.next = el.prev = el.parent = null;
        });

        var elem = {
          data: str,
          type: 'text',
          parent: el,
          prev: null,
          next: null,
          children: []
        };

        updateDOM(elem, el);
      });

      return this;
    };

    exports.clone = function() {
      // Turn it into HTML, then recreate it,
      // Seems to be the easiest way to reconnect everything correctly
      return this._make($.html(this, this.options));
    };