(parentNode, // where changes happen currentNodes, // Array of current items/nodes futureNodes, // Array of future items/nodes options // optional object with one of the following properties // before: domNode // compare(generic, generic) => true if same generic // node(generic) => Node )
| 436 | /*! (c) 2018 Andrea Giammarchi (ISC) */ |
| 437 | |
| 438 | var domdiff = function domdiff(parentNode, // where changes happen |
| 439 | currentNodes, // Array of current items/nodes |
| 440 | futureNodes, // Array of future items/nodes |
| 441 | options // optional object with one of the following properties |
| 442 | // before: domNode |
| 443 | // compare(generic, generic) => true if same generic |
| 444 | // node(generic) => Node |
| 445 | ) { |
| 446 | if (!options) options = {}; |
| 447 | var compare = options.compare || eqeq; |
| 448 | var get = options.node || identity; |
| 449 | var before = options.before == null ? null : get(options.before, 0); |
| 450 | var currentLength = currentNodes.length; |
| 451 | var currentEnd = currentLength; |
| 452 | var currentStart = 0; |
| 453 | var futureEnd = futureNodes.length; |
| 454 | var futureStart = 0; // common prefix |
| 455 | |
| 456 | while (currentStart < currentEnd && futureStart < futureEnd && compare(currentNodes[currentStart], futureNodes[futureStart])) { |
| 457 | currentStart++; |
| 458 | futureStart++; |
| 459 | } // common suffix |
| 460 | |
| 461 | |
| 462 | while (currentStart < currentEnd && futureStart < futureEnd && compare(currentNodes[currentEnd - 1], futureNodes[futureEnd - 1])) { |
| 463 | currentEnd--; |
| 464 | futureEnd--; |
| 465 | } |
| 466 | |
| 467 | var currentSame = currentStart === currentEnd; |
| 468 | var futureSame = futureStart === futureEnd; // same list |
| 469 | |
| 470 | if (currentSame && futureSame) return futureNodes; // only stuff to add |
| 471 | |
| 472 | if (currentSame && futureStart < futureEnd) { |
| 473 | append(get, parentNode, futureNodes, futureStart, futureEnd, next(get, currentNodes, currentStart, currentLength, before)); |
| 474 | return futureNodes; |
| 475 | } // only stuff to remove |
| 476 | |
| 477 | |
| 478 | if (futureSame && currentStart < currentEnd) { |
| 479 | remove(get, currentNodes, currentStart, currentEnd); |
| 480 | return futureNodes; |
| 481 | } |
| 482 | |
| 483 | var currentChanges = currentEnd - currentStart; |
| 484 | var futureChanges = futureEnd - futureStart; |
| 485 | var i = -1; // 2 simple indels: the shortest sequence is a subsequence of the longest |
| 486 | |
| 487 | if (currentChanges < futureChanges) { |
| 488 | i = indexOf$1(futureNodes, futureStart, futureEnd, currentNodes, currentStart, currentEnd, compare); // inner diff |
| 489 | |
| 490 | if (-1 < i) { |
| 491 | append(get, parentNode, futureNodes, futureStart, i, get(currentNodes[currentStart], 0)); |
| 492 | append(get, parentNode, futureNodes, i + currentChanges, futureEnd, next(get, currentNodes, currentEnd, currentLength, before)); |
| 493 | return futureNodes; |
| 494 | } |
| 495 | } |
no test coverage detected