2015-10-27 18:11:45 +03:00
|
|
|
function diffFromJSON(json) {
|
|
|
|
if (json.pure != null) {
|
|
|
|
return new Diff({
|
|
|
|
pure: new Patch(json.pure)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (json.roll != null) {
|
|
|
|
return new Diff({
|
|
|
|
roll: {
|
|
|
|
extract: json.roll.extract,
|
|
|
|
unwrap: new Syntax(json.roll.unwrap, function(x) {
|
|
|
|
return diffFromJSON(x);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function Diff(object) {
|
|
|
|
if (object.pure != null) {
|
|
|
|
this.pure = object.pure;
|
|
|
|
}
|
|
|
|
if (object.roll != null) {
|
|
|
|
this.roll = object.roll;
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// forall a b. Diff a -> (a -> b) -> Diff b
|
|
|
|
Diff.prototype.map = function(transform) {
|
|
|
|
if (this.pure != null) {
|
|
|
|
return new Diff({
|
|
|
|
pure: transform(this.pure)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (this.roll != null) {
|
|
|
|
return new Diff({
|
|
|
|
roll: {
|
|
|
|
extract: this.roll.extract,
|
|
|
|
unwrap: this.roll.unwrap.map(function(x) {
|
|
|
|
return x.map(transform);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// forall a. Diff a -> (Syntax a -> a) -> a
|
|
|
|
Diff.prototype.cata = function(transform) {
|
|
|
|
if (this.pure != null) {
|
|
|
|
return this.pure;
|
|
|
|
}
|
|
|
|
if (this.roll != null) {
|
|
|
|
return transform(this.roll.unwrap.map(function(diff) {
|
|
|
|
return diff.cata(transform);
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-29 22:11:18 +03:00
|
|
|
function wrap(tagName, element) {
|
|
|
|
if (element == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
var node = document.createElement(tagName);
|
|
|
|
node.appendChild(element);
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// String -> Syntax a -> Range -> (a -> Range) -> (a -> DOM) -> DOM
|
2015-11-03 23:26:32 +03:00
|
|
|
function termToDOM(source, syntax, extract, getRange, recur) {
|
2015-10-29 22:11:18 +03:00
|
|
|
recur = recur || function(term) {
|
2015-11-03 23:26:32 +03:00
|
|
|
return termToDOM(source, term.unwrap, term.extract, getRange);
|
2015-10-29 22:11:18 +03:00
|
|
|
}
|
2015-11-03 23:16:43 +03:00
|
|
|
var categories = extract.categories;
|
2015-11-03 23:16:36 +03:00
|
|
|
var range = extract.range;
|
2015-10-29 22:11:18 +03:00
|
|
|
var element;
|
|
|
|
if (syntax.leaf != null) {
|
|
|
|
element = document.createElement("span");
|
|
|
|
element.textContent = source.substr(range[0], range[1]);
|
|
|
|
} else if (syntax.indexed != null || syntax.fixed != null) {
|
|
|
|
var values = syntax.indexed || syntax.fixed;
|
2015-10-29 23:27:41 +03:00
|
|
|
element = document.createElement("ul");
|
2015-11-03 21:04:48 +03:00
|
|
|
var previous = range[0];
|
2015-10-29 22:11:18 +03:00
|
|
|
for (i in values) {
|
|
|
|
var child = values[i];
|
|
|
|
if (child.pure == "") continue;
|
|
|
|
var childRange = getRange(child);
|
2015-11-03 23:25:52 +03:00
|
|
|
element.appendChild(document.createTextNode(source.substr(previous, childRange[0] - previous)));
|
|
|
|
element.appendChild(wrap("li", recur(child)));
|
|
|
|
previous = childRange[0] + childRange[1];
|
2015-10-29 22:11:18 +03:00
|
|
|
}
|
|
|
|
element.appendChild(document.createTextNode(source.substr(previous, range[0] + range[1] - previous)));
|
|
|
|
} else if (syntax.keyed != null) {
|
|
|
|
element = document.createElement("dl");
|
|
|
|
var values = [];
|
|
|
|
for (k in syntax.keyed.values) {
|
|
|
|
if (syntax.keyed.values[k].pure == "") continue;
|
|
|
|
values.push([k, syntax.keyed.values[k]]);
|
|
|
|
}
|
|
|
|
values.sort(function(a, b) {
|
|
|
|
if (getRange(a[1])[0] < getRange(b[1])[0]) {
|
|
|
|
return -1;
|
|
|
|
} else if (getRange(a[1])[0] > getRange(b[1])[0]) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
|
|
|
|
var previous = range[0];
|
|
|
|
for (i in values) {
|
|
|
|
var pair = values[i];
|
|
|
|
var key = pair[0];
|
|
|
|
var child = pair[1];
|
|
|
|
var childRange = getRange(child);
|
|
|
|
element.appendChild(document.createTextNode(source.substr(previous, childRange[0] - previous)));
|
|
|
|
element.appendChild(wrap("dt", document.createTextNode(key)));
|
|
|
|
element.appendChild(wrap("dd", recur(child)));
|
|
|
|
previous = childRange[0] + childRange[1];
|
|
|
|
}
|
|
|
|
element.appendChild(document.createTextNode(source.substr(previous, range[0] + range[1] - previous)));
|
|
|
|
}
|
|
|
|
element["data-range"] = range;
|
2015-11-03 23:16:43 +03:00
|
|
|
|
|
|
|
for (index in categories) {
|
|
|
|
element.classList.add('category-'+categories[index]);
|
|
|
|
}
|
|
|
|
|
2015-10-29 22:11:18 +03:00
|
|
|
return element;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Diff -> String -> DOM
|
2015-11-09 20:06:45 +03:00
|
|
|
function diffToDOM(diff, sources, lineNumbers) {
|
2015-10-29 23:27:41 +03:00
|
|
|
|
|
|
|
function getRange(diffOrTerm) {
|
|
|
|
if (diffOrTerm.pure != null) {
|
|
|
|
var beforeRange, afterRange;
|
|
|
|
if (diffOrTerm.pure.before != null) {
|
2015-11-03 21:58:59 +03:00
|
|
|
beforeRange = diffOrTerm.pure.before.extract.range
|
2015-10-29 23:27:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (diffOrTerm.pure.after != null) {
|
2015-11-03 21:58:59 +03:00
|
|
|
afterRange = diffOrTerm.pure.after.extract.range
|
2015-10-29 23:27:41 +03:00
|
|
|
}
|
2015-11-02 23:39:04 +03:00
|
|
|
|
|
|
|
if (beforeRange == null) {
|
|
|
|
beforeRange = afterRange;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (afterRange == null) {
|
|
|
|
afterRange = beforeRange;
|
|
|
|
}
|
2015-10-29 23:27:41 +03:00
|
|
|
return { "before": beforeRange, "after": afterRange };
|
|
|
|
}
|
|
|
|
if (diffOrTerm.roll != null) {
|
2015-11-03 21:58:59 +03:00
|
|
|
return { before: diffOrTerm.roll.extract.before.range, after: diffOrTerm.roll.extract.after.range };
|
2015-10-29 23:27:41 +03:00
|
|
|
}
|
|
|
|
if (diffOrTerm.extract != null) {
|
2015-11-03 21:58:59 +03:00
|
|
|
return diffOrTerm.extract.range;
|
2015-10-29 23:27:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-27 18:11:45 +03:00
|
|
|
if (diff.pure != null) {
|
2015-11-09 20:06:45 +03:00
|
|
|
return pureToDOM(sources, diff.pure, lineNumbers, getRange, function(diff) {
|
|
|
|
return diffToDOM(diff, sources, lineNumbers);
|
2015-10-29 23:27:41 +03:00
|
|
|
})
|
2015-10-27 18:11:45 +03:00
|
|
|
}
|
|
|
|
|
2015-11-09 20:06:45 +03:00
|
|
|
return rollToDOM(sources, diff.roll, lineNumbers, getRange, function(diff) {
|
|
|
|
return diffToDOM(diff, sources, lineNumbers);
|
2015-10-29 23:27:41 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-11-09 20:06:45 +03:00
|
|
|
function pureToDOM(sources, patch, lineNumbers, getRangeFun, diffToDOMFun) {
|
2015-10-29 23:27:41 +03:00
|
|
|
var elementA, elementB;
|
|
|
|
if (patch.before != null) {
|
2015-11-03 23:26:32 +03:00
|
|
|
elementA = termToDOM(sources.before, patch.before.unwrap, patch.before.extract, getRangeFun);
|
2015-10-29 23:27:41 +03:00
|
|
|
elementA.classList.add("delete");
|
|
|
|
if (patch.after != null) {
|
|
|
|
elementA.classList.add("replace");
|
2015-10-27 18:11:45 +03:00
|
|
|
}
|
2015-11-09 20:06:45 +03:00
|
|
|
|
2015-11-09 21:54:09 +03:00
|
|
|
elementA.setAttribute("data-line-number", patch.before.extract.lines[0])
|
2015-10-29 23:27:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (patch.after != null) {
|
2015-11-03 23:26:32 +03:00
|
|
|
elementB = termToDOM(sources.after, patch.after.unwrap, patch.after.extract, getRangeFun);
|
2015-10-29 23:27:41 +03:00
|
|
|
elementB.classList.add("insert");
|
|
|
|
if (patch.before != null) {
|
|
|
|
elementB.classList.add("replace");
|
2015-10-27 18:11:45 +03:00
|
|
|
}
|
2015-11-09 20:06:45 +03:00
|
|
|
|
2015-11-09 21:54:09 +03:00
|
|
|
elementB.setAttribute("data-line-number", patch.after.extract.lines[0])
|
2015-10-27 18:11:45 +03:00
|
|
|
}
|
2015-10-29 23:27:41 +03:00
|
|
|
|
2015-11-02 23:39:04 +03:00
|
|
|
if (elementA == null) {
|
2015-11-09 20:06:45 +03:00
|
|
|
elementA = elementB.cloneNode(true);
|
|
|
|
elementA.classList.add("invisible");
|
|
|
|
|
2015-11-09 22:13:47 +03:00
|
|
|
elementA.setAttribute("data-line-number", '\u00A0')
|
2015-11-02 23:39:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (elementB == null) {
|
2015-11-09 20:06:45 +03:00
|
|
|
elementB = elementA.cloneNode(true);
|
|
|
|
elementB.classList.add("invisible");
|
|
|
|
|
2015-11-09 22:13:47 +03:00
|
|
|
elementB.setAttribute("data-line-number", '\u00A0')
|
2015-11-02 23:39:04 +03:00
|
|
|
}
|
|
|
|
|
2015-10-29 23:27:41 +03:00
|
|
|
return { "before": elementA || "", "after": elementB || "" };
|
2015-10-27 18:11:45 +03:00
|
|
|
}
|
2015-10-29 22:11:18 +03:00
|
|
|
|
2015-11-09 20:06:45 +03:00
|
|
|
function rollToDOM(sources, rollOrTerm, lineNumbers, getRangeFun, diffToDOMFun) {
|
2015-10-29 23:27:41 +03:00
|
|
|
var syntax = rollOrTerm.unwrap
|
2015-11-03 21:20:01 +03:00
|
|
|
var categories = {
|
|
|
|
before: rollOrTerm.extract.before.categories,
|
|
|
|
after: rollOrTerm.extract.after.categories
|
|
|
|
}
|
|
|
|
var range = {
|
|
|
|
before: rollOrTerm.extract.before.range,
|
|
|
|
after: rollOrTerm.extract.after.range
|
|
|
|
}
|
2015-10-29 22:11:18 +03:00
|
|
|
|
2015-11-09 20:06:45 +03:00
|
|
|
var lines = {
|
|
|
|
before: rollOrTerm.extract.before.lines[0],
|
|
|
|
after: rollOrTerm.extract.after.lines[0]
|
|
|
|
}
|
|
|
|
|
2015-10-29 22:11:18 +03:00
|
|
|
var elementA;
|
|
|
|
var elementB;
|
|
|
|
if (syntax.leaf != null) {
|
|
|
|
elementA = document.createElement("span");
|
2015-10-29 23:27:41 +03:00
|
|
|
elementA.textContent = sources.before.substr(range.before[0], range.before[1]);
|
2015-10-29 22:11:18 +03:00
|
|
|
elementB = document.createElement("span");
|
2015-10-29 23:27:41 +03:00
|
|
|
elementB.textContent = sources.after.substr(range.after[0], range.after[1]);
|
2015-11-09 20:06:45 +03:00
|
|
|
|
2015-11-09 21:54:09 +03:00
|
|
|
elementA.setAttribute("data-line-number", lines.before)
|
|
|
|
elementB.setAttribute("data-line-number", lines.after)
|
2015-10-29 22:11:18 +03:00
|
|
|
} else if (syntax.indexed != null || syntax.fixed != null) {
|
|
|
|
var values = syntax.indexed || syntax.fixed;
|
|
|
|
elementA = document.createElement("ul");
|
|
|
|
elementB = document.createElement("ul");
|
|
|
|
var previousBefore = range.before[0];
|
2015-10-30 01:24:29 +03:00
|
|
|
var previousAfter = range.after[0];
|
2015-11-09 21:54:09 +03:00
|
|
|
|
|
|
|
var lineNumbers = { "before": [], "after": [] };
|
2015-10-29 22:11:18 +03:00
|
|
|
for (i in values) {
|
|
|
|
var child = values[i];
|
|
|
|
if (child.pure == "") continue;
|
|
|
|
|
|
|
|
var childRange = getRangeFun(child);
|
2015-10-29 23:27:41 +03:00
|
|
|
var beforeAfterChild = diffToDOMFun(child)
|
2015-10-29 22:11:18 +03:00
|
|
|
if (childRange.before != null) {
|
|
|
|
var beforeRange = childRange.before;
|
|
|
|
|
2015-11-06 18:31:27 +03:00
|
|
|
var li = wrap("li", beforeAfterChild.before);
|
|
|
|
if (beforeAfterChild.before.classList.contains("invisible")) {
|
|
|
|
li.classList.add("invisible");
|
2015-11-06 18:31:53 +03:00
|
|
|
} else {
|
2015-11-06 18:32:14 +03:00
|
|
|
var beforeSeparator = sources.before.substr(previousBefore, beforeRange[0] - previousBefore);
|
|
|
|
elementA.appendChild(document.createTextNode(beforeSeparator));
|
2015-11-06 18:31:53 +03:00
|
|
|
previousBefore = beforeRange[0] + beforeRange[1];
|
2015-11-06 18:31:27 +03:00
|
|
|
}
|
|
|
|
elementA.appendChild(li);
|
2015-11-09 21:54:09 +03:00
|
|
|
|
|
|
|
var lineNumber = beforeAfterChild.before.getAttribute("data-line-number")
|
|
|
|
if (lineNumber != null) {
|
|
|
|
lineNumbers.before.push(lineNumber)
|
|
|
|
}
|
2015-10-29 22:11:18 +03:00
|
|
|
}
|
|
|
|
if (childRange.after != null) {
|
|
|
|
var afterRange = childRange.after;
|
|
|
|
|
2015-11-06 18:31:27 +03:00
|
|
|
var li = wrap("li", beforeAfterChild.after);
|
|
|
|
if (beforeAfterChild.after.classList.contains("invisible")) {
|
|
|
|
li.classList.add("invisible");
|
2015-11-06 18:31:53 +03:00
|
|
|
} else {
|
2015-11-06 18:32:14 +03:00
|
|
|
var afterSeparator = sources.after.substr(previousAfter, afterRange[0] - previousAfter);
|
|
|
|
elementB.appendChild(document.createTextNode(afterSeparator));
|
2015-11-06 18:31:53 +03:00
|
|
|
previousAfter = afterRange[0] + afterRange[1];
|
2015-11-06 18:31:27 +03:00
|
|
|
}
|
|
|
|
elementB.appendChild(li);
|
2015-11-09 21:54:09 +03:00
|
|
|
|
|
|
|
var lineNumber = beforeAfterChild.after.getAttribute("data-line-number");
|
|
|
|
if (lineNumber != null) {
|
|
|
|
lineNumbers.after.push(lineNumber)
|
|
|
|
}
|
2015-10-29 22:11:18 +03:00
|
|
|
}
|
|
|
|
}
|
2015-10-29 23:27:41 +03:00
|
|
|
var beforeText = sources.before.substr(previousBefore, range.before[0] + range.before[1] - previousBefore);
|
|
|
|
var afterText = sources.after.substr(previousAfter, range.after[0] + range.after[1] - previousAfter);
|
2015-10-29 22:11:18 +03:00
|
|
|
|
|
|
|
elementA.appendChild(document.createTextNode(beforeText));
|
|
|
|
elementB.appendChild(document.createTextNode(afterText));
|
2015-11-09 20:06:45 +03:00
|
|
|
|
2015-11-09 21:54:09 +03:00
|
|
|
elementA.setAttribute("data-line-number", lineNumbers.before)
|
|
|
|
elementB.setAttribute("data-line-number", lineNumbers.after)
|
2015-10-29 22:11:18 +03:00
|
|
|
} else if (syntax.keyed != null) {
|
2015-10-30 01:24:29 +03:00
|
|
|
elementA = document.createElement("dl");
|
|
|
|
elementB = document.createElement("dl");
|
2015-10-29 22:11:18 +03:00
|
|
|
|
2015-10-30 01:24:29 +03:00
|
|
|
var befores = [];
|
|
|
|
var afters = [];
|
|
|
|
for (k in syntax.keyed.values) {
|
|
|
|
if (syntax.keyed.values[k].pure == "") continue;
|
|
|
|
var child = syntax.keyed.values[k];
|
|
|
|
|
|
|
|
var ranges = getRangeFun(child);
|
|
|
|
var doms = diffToDOMFun(child)
|
|
|
|
if (ranges.before != null) {
|
|
|
|
befores.push({ "key": k, "range": ranges.before, "child": doms.before });
|
|
|
|
}
|
|
|
|
if (ranges.after != null) {
|
|
|
|
afters.push({ "key": k, "range": ranges.after, "child": doms.after });
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-02 23:39:04 +03:00
|
|
|
var sortByRange = function (array) {
|
|
|
|
return function(a, b) {
|
|
|
|
// if a is invisible, sort it based on its range vs. bs opposite range
|
|
|
|
function sortByOppositeRange(el) {
|
|
|
|
var opArray = array === befores ? afters : befores;
|
|
|
|
var opEl = opArray.find(function (e) { return el.key === e.key; });
|
|
|
|
var otherEl = el === a ? b : a;
|
|
|
|
var opOtherEl = opArray.find(function (e) { return otherEl.key === e.key; });
|
2015-10-30 01:24:29 +03:00
|
|
|
|
|
|
|
|
2015-11-02 23:39:04 +03:00
|
|
|
if (el.child.classList.contains("delete") && otherEl.child.classList.contains("insert")) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (el.child.classList.contains("insert") && otherEl.child.classList.contains("delete")) {
|
|
|
|
return 1;
|
|
|
|
}
|
2015-10-30 01:24:29 +03:00
|
|
|
|
2015-11-03 00:27:14 +03:00
|
|
|
if (!el.child.classList.contains("invisible") && !otherEl.child.classList.contains("invisible")) {
|
|
|
|
if (el.range[0] < otherEl.range[0]) {
|
|
|
|
return -1;
|
|
|
|
} else if (el.range[0] > otherEl.range[0]) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-02 23:39:04 +03:00
|
|
|
if (opEl.range[0] < opOtherEl.range[0]) {
|
|
|
|
return -1;
|
|
|
|
} else if (opEl.range[0] > opOtherEl.range[0]) {
|
|
|
|
return 1;
|
|
|
|
}
|
2015-10-30 01:24:29 +03:00
|
|
|
|
|
|
|
|
2015-11-02 23:39:04 +03:00
|
|
|
return 0;
|
|
|
|
}
|
2015-10-30 01:24:29 +03:00
|
|
|
|
2015-11-02 23:39:04 +03:00
|
|
|
return sortByOppositeRange(a);
|
|
|
|
}
|
2015-10-30 01:24:29 +03:00
|
|
|
}
|
|
|
|
|
2015-11-02 23:39:04 +03:00
|
|
|
befores.sort(sortByRange(befores));
|
|
|
|
afters.sort(sortByRange(afters));
|
|
|
|
|
|
|
|
var previousA = range.before[0];
|
|
|
|
var previousB = range.after[0];
|
|
|
|
|
2015-11-09 22:13:47 +03:00
|
|
|
var lineNumbers = { "before": [ lines.before ], "after": [ lines.after ] };
|
|
|
|
|
2015-11-02 23:39:04 +03:00
|
|
|
zip(befores, afters, function (a, b) {
|
|
|
|
var key = a.key
|
|
|
|
var childElA = a.child
|
|
|
|
var childRangeA = a.range
|
|
|
|
var childElB = b.child
|
|
|
|
var childRangeB = b.range
|
|
|
|
|
|
|
|
var isFirst = elementA.childNodes.length == 0;
|
|
|
|
|
|
|
|
function penultimateNode(el) {
|
|
|
|
var nodes = Array.prototype.slice.call(el.childNodes).reverse();
|
|
|
|
|
|
|
|
var lastVisibleNode;
|
|
|
|
for (i in nodes) {
|
|
|
|
var node = nodes[i];
|
|
|
|
if (node.nodeType != 3 && node.classList.contains("invisible")) {
|
|
|
|
var nextIndex = parseInt(i, 10) + 1;
|
|
|
|
if (nextIndex < nodes.length) {
|
|
|
|
var nextNode = nodes[nextIndex];
|
|
|
|
if (nextNode.nodeType != 3 && !nextNode.classList.contains("invisible")) {
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isFirst || !childElA.classList.contains("invisible")) {
|
|
|
|
var text = sources.before.substr(previousA, childRangeA[0] - previousA);
|
|
|
|
|
|
|
|
var beforeNode = penultimateNode(elementA)
|
|
|
|
if (beforeNode != null) {
|
|
|
|
elementA.insertBefore(document.createTextNode(text), beforeNode);
|
|
|
|
} else {
|
|
|
|
elementA.appendChild(document.createTextNode(text));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isFirst || !childElB.classList.contains("invisible")) {
|
|
|
|
var text = sources.after.substr(previousB, childRangeB[0] - previousB);
|
|
|
|
|
|
|
|
var beforeNode = penultimateNode(elementB)
|
|
|
|
if (beforeNode != null) {
|
|
|
|
elementB.insertBefore(document.createTextNode(text), beforeNode);
|
|
|
|
} else {
|
|
|
|
elementB.appendChild(document.createTextNode(text));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var dtA = wrap("dt", document.createTextNode(key));
|
|
|
|
elementA.appendChild(dtA);
|
|
|
|
var ddA = wrap("dd", childElA)
|
|
|
|
elementA.appendChild(ddA);
|
|
|
|
if (childElA.classList.contains("invisible")) {
|
|
|
|
dtA.classList.add("invisible");
|
|
|
|
ddA.classList.add("invisible");
|
|
|
|
}
|
|
|
|
|
2015-11-09 22:13:47 +03:00
|
|
|
var lineNumberA = childElA.getAttribute("data-line-number");
|
|
|
|
if (lineNumberA != null) {
|
|
|
|
lineNumbers.before.push(lineNumberA)
|
|
|
|
}
|
|
|
|
|
2015-11-02 23:39:04 +03:00
|
|
|
var dtB = wrap("dt", document.createTextNode(key));
|
|
|
|
elementB.appendChild(dtB);
|
|
|
|
var ddB = wrap("dd", childElB);
|
|
|
|
elementB.appendChild(ddB);
|
|
|
|
if (childElB.classList.contains("invisible")) {
|
|
|
|
dtB.classList.add("invisible");
|
|
|
|
ddB.classList.add("invisible");
|
|
|
|
}
|
|
|
|
|
2015-11-09 22:13:47 +03:00
|
|
|
var lineNumberB = childElB.getAttribute("data-line-number");
|
|
|
|
if (lineNumberB != null) {
|
|
|
|
lineNumbers.after.push(lineNumberB)
|
|
|
|
}
|
2015-11-02 23:39:04 +03:00
|
|
|
|
|
|
|
if (isFirst || !childElA.classList.contains("invisible")) {
|
|
|
|
previousA = childRangeA[0] + childRangeA[1]
|
|
|
|
}
|
|
|
|
if (isFirst || !childElB.classList.contains("invisible")) {
|
|
|
|
previousB = childRangeB[0] + childRangeB[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
var nextIndex = befores.indexOf(a) + 1;
|
|
|
|
if (childElA.classList.contains("invisible") && nextIndex < afters.length) {
|
2015-11-02 23:56:12 +03:00
|
|
|
var nextVisibleElement = afters[nextIndex]
|
2015-11-03 18:45:32 +03:00
|
|
|
while (nextVisibleElement.child.classList.contains("invisible") && ++nextIndex < afters.length) {
|
2015-11-02 23:56:12 +03:00
|
|
|
nextVisibleElement = afters[nextIndex]
|
|
|
|
}
|
2015-11-03 01:03:59 +03:00
|
|
|
if (nextVisibleElement == null) return;
|
2015-11-02 23:39:04 +03:00
|
|
|
|
2015-11-02 23:56:12 +03:00
|
|
|
var beginningOfNextElement = nextVisibleElement.range[0];
|
2015-11-02 23:39:04 +03:00
|
|
|
var text = sources.after.substr(previousB, beginningOfNextElement - previousB);
|
|
|
|
var node = wrap("span", document.createTextNode(text));
|
|
|
|
node.classList.add("invisible");
|
|
|
|
elementA.appendChild(node);
|
|
|
|
}
|
|
|
|
|
2015-11-03 18:45:32 +03:00
|
|
|
var nextIndex = afters.indexOf(b) + 1;
|
2015-11-03 01:03:59 +03:00
|
|
|
if (childElB.classList.contains("invisible") && nextIndex < befores.length) {
|
|
|
|
var nextVisibleElement = befores[nextIndex]
|
2015-11-03 18:45:32 +03:00
|
|
|
while (nextVisibleElement.child.classList.contains("invisible") && ++nextIndex < befores.length) {
|
2015-11-03 01:03:59 +03:00
|
|
|
nextVisibleElement = befores[nextIndex]
|
|
|
|
}
|
|
|
|
if (nextVisibleElement == null) return;
|
2015-11-02 23:39:04 +03:00
|
|
|
|
2015-11-02 23:56:12 +03:00
|
|
|
var beginningOfNextElement = nextVisibleElement.range[0];
|
2015-11-02 23:39:04 +03:00
|
|
|
var text = sources.before.substr(previousA, beginningOfNextElement - previousA);
|
|
|
|
var node = wrap("span", document.createTextNode(text));
|
|
|
|
node.classList.add("invisible");
|
|
|
|
elementB.appendChild(node);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var textA = sources.before.substr(previousA, range.before[0] + range.before[1] - previousA);
|
|
|
|
elementA.appendChild(document.createTextNode(textA));
|
|
|
|
var textB = sources.after.substr(previousB, range.after[0] + range.after[1] - previousB);
|
|
|
|
elementB.appendChild(document.createTextNode(textB));
|
2015-11-09 20:06:45 +03:00
|
|
|
|
2015-11-09 22:13:47 +03:00
|
|
|
|
|
|
|
lineNumbers.before.push(rollOrTerm.extract.before.lines[1])
|
|
|
|
lineNumbers.after.push(rollOrTerm.extract.after.lines[1])
|
|
|
|
elementA.setAttribute("data-line-number", lineNumbers.before)
|
|
|
|
elementB.setAttribute("data-line-number", lineNumbers.after)
|
2015-10-29 22:11:18 +03:00
|
|
|
}
|
|
|
|
|
2015-11-03 22:08:32 +03:00
|
|
|
for (index in categories.before) {
|
|
|
|
elementA.classList.add('category-'+categories.before[index]);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (index in categories.after) {
|
|
|
|
elementB.classList.add('category-'+categories.after[index]);
|
|
|
|
}
|
|
|
|
|
2015-10-29 22:11:18 +03:00
|
|
|
return { "before": elementA, "after": elementB }
|
|
|
|
}
|
2015-11-02 23:39:04 +03:00
|
|
|
|
|
|
|
|
|
|
|
/// ([a], [b]) -> [(a, b)]
|
|
|
|
function zip (a, b, callback) {
|
|
|
|
if (a.length > b.length) {
|
|
|
|
return b.map(function(b, index) {
|
|
|
|
return callback(a[index], b);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return a.map(function(a, index) {
|
|
|
|
return callback(a, b[index]) ;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|