visual-dom-diff
Version:
Highlight differences between two DOM trees.
94 lines (93 loc) • 3.08 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var DomIterator = /** @class */ (function () {
function DomIterator(rootNode, config) {
this.rootNode = rootNode;
this.config = config;
this.descend = true;
this.nextNode = this.rootNode;
if (this.skipSelf(this.nextNode)) {
this.next();
}
}
DomIterator.prototype.toArray = function () {
var _a;
var array = [];
var _b = this.next(), done = _b.done, value = _b.value;
while (!done) {
array.push(value);
(_a = this.next(), done = _a.done, value = _a.value);
}
return array;
};
DomIterator.prototype.forEach = function (fn) {
var _a;
var _b = this.next(), done = _b.done, value = _b.value;
while (!done) {
fn(value);
(_a = this.next(), done = _a.done, value = _a.value);
}
};
DomIterator.prototype.reduce = function (fn, initial) {
var _a;
var result = initial;
var _b = this.next(), done = _b.done, value = _b.value;
while (!done) {
result = fn(result, value);
(_a = this.next(), done = _a.done, value = _a.value);
}
return result;
};
DomIterator.prototype.some = function (fn) {
var _a;
var _b = this.next(), done = _b.done, value = _b.value;
while (!done) {
if (fn(value)) {
return true;
}
;
(_a = this.next(), done = _a.done, value = _a.value);
}
return false;
};
DomIterator.prototype.next = function () {
if (!this.nextNode) {
return { done: true, value: this.rootNode };
}
var value = this.nextNode;
var done = false;
if (this.descend &&
this.nextNode.firstChild &&
!this.skipChildren(this.nextNode)) {
this.nextNode = this.nextNode.firstChild;
}
else if (this.nextNode === this.rootNode) {
this.nextNode = null;
}
else if (this.nextNode.nextSibling) {
this.nextNode = this.nextNode.nextSibling;
this.descend = true;
}
else {
this.nextNode = this.nextNode.parentNode;
this.descend = false;
this.next(); // Skip this node, as we've visited it already.
}
if (this.nextNode && this.skipSelf(this.nextNode)) {
this.next(); // Skip this node, as directed by the config.
}
return { done: done, value: value };
};
DomIterator.prototype.skipSelf = function (node) {
return this.config && this.config.skipSelf
? this.config.skipSelf(node)
: false;
};
DomIterator.prototype.skipChildren = function (node) {
return this.config && this.config.skipChildren
? this.config.skipChildren(node)
: false;
};
return DomIterator;
}());
exports.DomIterator = DomIterator;