2019-07-23 10:12:24 +03:00
|
|
|
/*
|
2021-06-22 19:22:38 +03:00
|
|
|
Copyright 2019 - 2021 The Matrix.org Foundation C.I.C.
|
2019-07-23 10:12:24 +03:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-06-22 19:22:38 +03:00
|
|
|
import React, { ReactNode } from 'react';
|
2019-07-23 10:12:24 +03:00
|
|
|
import classNames from 'classnames';
|
2021-06-22 19:22:38 +03:00
|
|
|
import { diff_match_patch as DiffMatchPatch } from 'diff-match-patch';
|
2021-06-24 15:28:16 +03:00
|
|
|
import { DiffDOM, IDiff } from "diff-dom";
|
2021-06-22 19:22:38 +03:00
|
|
|
import { IContent } from "matrix-js-sdk/src/models/event";
|
|
|
|
|
|
|
|
import { bodyToHtml, checkBlockNode, IOptsReturnString } from "../HtmlUtils";
|
2019-07-23 10:12:24 +03:00
|
|
|
|
2021-10-15 17:31:29 +03:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
|
|
|
|
2019-07-23 10:12:24 +03:00
|
|
|
const decodeEntities = (function() {
|
|
|
|
let textarea = null;
|
2021-06-22 19:22:38 +03:00
|
|
|
return function(str: string): string {
|
2019-07-23 10:12:24 +03:00
|
|
|
if (!textarea) {
|
|
|
|
textarea = document.createElement("textarea");
|
|
|
|
}
|
2021-06-22 19:22:38 +03:00
|
|
|
textarea.innerHTML = str;
|
2019-07-23 10:12:24 +03:00
|
|
|
return textarea.value;
|
|
|
|
};
|
|
|
|
})();
|
|
|
|
|
2021-06-22 19:22:38 +03:00
|
|
|
function textToHtml(text: string): string {
|
2019-07-23 10:12:24 +03:00
|
|
|
const container = document.createElement("div");
|
|
|
|
container.textContent = text;
|
|
|
|
return container.innerHTML;
|
|
|
|
}
|
|
|
|
|
2021-06-22 19:22:38 +03:00
|
|
|
function getSanitizedHtmlBody(content: IContent): string {
|
|
|
|
const opts: IOptsReturnString = {
|
2019-07-23 10:12:24 +03:00
|
|
|
stripReplyFallback: true,
|
|
|
|
returnString: true,
|
|
|
|
};
|
|
|
|
if (content.format === "org.matrix.custom.html") {
|
|
|
|
return bodyToHtml(content, null, opts);
|
|
|
|
} else {
|
|
|
|
// convert the string to something that can be safely
|
|
|
|
// embedded in an html document, e.g. use html entities where needed
|
|
|
|
// This is also needed so that DiffDOM wouldn't interpret something
|
|
|
|
// as a tag when somebody types e.g. "</sarcasm>"
|
|
|
|
|
|
|
|
// as opposed to bodyToHtml, here we also render
|
|
|
|
// text messages with dangerouslySetInnerHTML, to unify
|
|
|
|
// the code paths and because we need html to show differences
|
|
|
|
return textToHtml(bodyToHtml(content, null, opts));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-22 19:22:38 +03:00
|
|
|
function wrapInsertion(child: Node): HTMLElement {
|
2019-07-23 10:12:24 +03:00
|
|
|
const wrapper = document.createElement(checkBlockNode(child) ? "div" : "span");
|
|
|
|
wrapper.className = "mx_EditHistoryMessage_insertion";
|
|
|
|
wrapper.appendChild(child);
|
|
|
|
return wrapper;
|
|
|
|
}
|
|
|
|
|
2021-06-22 19:22:38 +03:00
|
|
|
function wrapDeletion(child: Node): HTMLElement {
|
2019-07-23 10:12:24 +03:00
|
|
|
const wrapper = document.createElement(checkBlockNode(child) ? "div" : "span");
|
|
|
|
wrapper.className = "mx_EditHistoryMessage_deletion";
|
|
|
|
wrapper.appendChild(child);
|
|
|
|
return wrapper;
|
|
|
|
}
|
|
|
|
|
2021-06-22 19:22:38 +03:00
|
|
|
function findRefNodes(root: Node, route: number[], isAddition = false) {
|
2019-07-23 10:12:24 +03:00
|
|
|
let refNode = root;
|
|
|
|
let refParentNode;
|
|
|
|
const end = isAddition ? route.length - 1 : route.length;
|
|
|
|
for (let i = 0; i < end; ++i) {
|
|
|
|
refParentNode = refNode;
|
|
|
|
refNode = refNode.childNodes[route[i]];
|
|
|
|
}
|
2021-06-22 19:22:38 +03:00
|
|
|
return { refNode, refParentNode };
|
2019-07-23 10:12:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function diffTreeToDOM(desc) {
|
|
|
|
if (desc.nodeName === "#text") {
|
|
|
|
return stringAsTextNode(desc.data);
|
|
|
|
} else {
|
|
|
|
const node = document.createElement(desc.nodeName);
|
|
|
|
if (desc.attributes) {
|
|
|
|
for (const [key, value] of Object.entries(desc.attributes)) {
|
|
|
|
node.setAttribute(key, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (desc.childNodes) {
|
|
|
|
for (const childDesc of desc.childNodes) {
|
|
|
|
node.appendChild(diffTreeToDOM(childDesc));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-22 19:22:38 +03:00
|
|
|
function insertBefore(parent: Node, nextSibling: Node | null, child: Node): void {
|
2019-07-23 10:12:24 +03:00
|
|
|
if (nextSibling) {
|
|
|
|
parent.insertBefore(child, nextSibling);
|
|
|
|
} else {
|
|
|
|
parent.appendChild(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-22 19:22:38 +03:00
|
|
|
function isRouteOfNextSibling(route1: number[], route2: number[]): boolean {
|
2019-07-23 10:12:24 +03:00
|
|
|
// routes are arrays with indices,
|
|
|
|
// to be interpreted as a path in the dom tree
|
|
|
|
|
|
|
|
// ensure same parent
|
|
|
|
for (let i = 0; i < route1.length - 1; ++i) {
|
|
|
|
if (route1[i] !== route2[i]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// the route2 is only affected by the diff of route1
|
|
|
|
// inserting an element if the index at the level of the
|
|
|
|
// last element of route1 being larger
|
|
|
|
// (e.g. coming behind route1 at that level)
|
|
|
|
const lastD1Idx = route1.length - 1;
|
|
|
|
return route2[lastD1Idx] >= route1[lastD1Idx];
|
|
|
|
}
|
|
|
|
|
2021-06-22 19:22:38 +03:00
|
|
|
function adjustRoutes(diff: IDiff, remainingDiffs: IDiff[]): void {
|
2019-07-23 10:12:24 +03:00
|
|
|
if (diff.action === "removeTextElement" || diff.action === "removeElement") {
|
|
|
|
// as removed text is not removed from the html, but marked as deleted,
|
|
|
|
// we need to readjust indices that assume the current node has been removed.
|
|
|
|
const advance = 1;
|
|
|
|
for (const rd of remainingDiffs) {
|
|
|
|
if (isRouteOfNextSibling(diff.route, rd.route)) {
|
|
|
|
rd.route[diff.route.length - 1] += advance;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-22 19:22:38 +03:00
|
|
|
function stringAsTextNode(string: string): Text {
|
2019-07-23 10:12:24 +03:00
|
|
|
return document.createTextNode(decodeEntities(string));
|
|
|
|
}
|
|
|
|
|
2021-06-22 19:22:38 +03:00
|
|
|
function renderDifferenceInDOM(originalRootNode: Node, diff: IDiff, diffMathPatch: DiffMatchPatch): void {
|
2021-06-29 15:11:58 +03:00
|
|
|
const { refNode, refParentNode } = findRefNodes(originalRootNode, diff.route);
|
2019-07-23 10:12:24 +03:00
|
|
|
switch (diff.action) {
|
2021-06-24 13:16:13 +03:00
|
|
|
case "replaceElement": {
|
2019-07-23 10:12:24 +03:00
|
|
|
const container = document.createElement("span");
|
|
|
|
const delNode = wrapDeletion(diffTreeToDOM(diff.oldValue));
|
|
|
|
const insNode = wrapInsertion(diffTreeToDOM(diff.newValue));
|
|
|
|
container.appendChild(delNode);
|
|
|
|
container.appendChild(insNode);
|
|
|
|
refNode.parentNode.replaceChild(container, refNode);
|
|
|
|
break;
|
|
|
|
}
|
2021-06-24 13:16:13 +03:00
|
|
|
case "removeTextElement": {
|
2019-07-23 10:12:24 +03:00
|
|
|
const delNode = wrapDeletion(stringAsTextNode(diff.value));
|
|
|
|
refNode.parentNode.replaceChild(delNode, refNode);
|
|
|
|
break;
|
|
|
|
}
|
2021-06-24 13:16:13 +03:00
|
|
|
case "removeElement": {
|
2019-07-23 10:12:24 +03:00
|
|
|
const delNode = wrapDeletion(diffTreeToDOM(diff.element));
|
|
|
|
refNode.parentNode.replaceChild(delNode, refNode);
|
|
|
|
break;
|
|
|
|
}
|
2021-06-24 13:16:13 +03:00
|
|
|
case "modifyTextElement": {
|
2019-07-23 10:12:24 +03:00
|
|
|
const textDiffs = diffMathPatch.diff_main(diff.oldValue, diff.newValue);
|
|
|
|
diffMathPatch.diff_cleanupSemantic(textDiffs);
|
|
|
|
const container = document.createElement("span");
|
|
|
|
for (const [modifier, text] of textDiffs) {
|
2021-06-22 19:22:38 +03:00
|
|
|
let textDiffNode: Node = stringAsTextNode(text);
|
2019-07-23 10:12:24 +03:00
|
|
|
if (modifier < 0) {
|
|
|
|
textDiffNode = wrapDeletion(textDiffNode);
|
|
|
|
} else if (modifier > 0) {
|
|
|
|
textDiffNode = wrapInsertion(textDiffNode);
|
|
|
|
}
|
|
|
|
container.appendChild(textDiffNode);
|
|
|
|
}
|
|
|
|
refNode.parentNode.replaceChild(container, refNode);
|
|
|
|
break;
|
|
|
|
}
|
2021-06-24 13:16:13 +03:00
|
|
|
case "addElement": {
|
2019-07-23 10:12:24 +03:00
|
|
|
const insNode = wrapInsertion(diffTreeToDOM(diff.element));
|
|
|
|
insertBefore(refParentNode, refNode, insNode);
|
|
|
|
break;
|
|
|
|
}
|
2021-06-24 13:16:13 +03:00
|
|
|
case "addTextElement": {
|
2020-05-14 17:35:38 +03:00
|
|
|
// XXX: sometimes diffDOM says insert a newline when there shouldn't be one
|
|
|
|
// but we must insert the node anyway so that we don't break the route child IDs.
|
|
|
|
// See https://github.com/fiduswriter/diffDOM/issues/100
|
|
|
|
const insNode = wrapInsertion(stringAsTextNode(diff.value !== "\n" ? diff.value : ""));
|
|
|
|
insertBefore(refParentNode, refNode, insNode);
|
2019-07-23 10:12:24 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
// e.g. when changing a the href of a link,
|
|
|
|
// show the link with old href as removed and with the new href as added
|
2021-06-24 13:16:13 +03:00
|
|
|
case "removeAttribute":
|
|
|
|
case "addAttribute":
|
|
|
|
case "modifyAttribute": {
|
2019-07-23 10:12:24 +03:00
|
|
|
const delNode = wrapDeletion(refNode.cloneNode(true));
|
2021-06-22 19:22:38 +03:00
|
|
|
const updatedNode = refNode.cloneNode(true) as HTMLElement;
|
2019-07-23 10:12:24 +03:00
|
|
|
if (diff.action === "addAttribute" || diff.action === "modifyAttribute") {
|
|
|
|
updatedNode.setAttribute(diff.name, diff.newValue);
|
|
|
|
} else {
|
|
|
|
updatedNode.removeAttribute(diff.name);
|
|
|
|
}
|
|
|
|
const insNode = wrapInsertion(updatedNode);
|
|
|
|
const container = document.createElement(checkBlockNode(refNode) ? "div" : "span");
|
|
|
|
container.appendChild(delNode);
|
|
|
|
container.appendChild(insNode);
|
|
|
|
refNode.parentNode.replaceChild(container, refNode);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
// Should not happen (modifyComment, ???)
|
2021-10-15 17:31:29 +03:00
|
|
|
logger.warn("MessageDiffUtils::editBodyDiffToHtml: diff action not supported atm", diff);
|
2019-07-23 10:12:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-22 19:22:38 +03:00
|
|
|
function routeIsEqual(r1: number[], r2: number[]): boolean {
|
2019-07-31 13:25:04 +03:00
|
|
|
return r1.length === r2.length && !r1.some((e, i) => e !== r2[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// workaround for https://github.com/fiduswriter/diffDOM/issues/90
|
2021-06-22 19:22:38 +03:00
|
|
|
function filterCancelingOutDiffs(originalDiffActions: IDiff[]): IDiff[] {
|
2019-07-31 13:25:04 +03:00
|
|
|
const diffActions = originalDiffActions.slice();
|
|
|
|
|
|
|
|
for (let i = 0; i < diffActions.length; ++i) {
|
|
|
|
const diff = diffActions[i];
|
|
|
|
if (diff.action === "removeTextElement") {
|
|
|
|
const nextDiff = diffActions[i + 1];
|
|
|
|
const cancelsOut = nextDiff &&
|
|
|
|
nextDiff.action === "addTextElement" &&
|
|
|
|
nextDiff.text === diff.text &&
|
|
|
|
routeIsEqual(nextDiff.route, diff.route);
|
|
|
|
|
|
|
|
if (cancelsOut) {
|
|
|
|
diffActions.splice(i, 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return diffActions;
|
|
|
|
}
|
|
|
|
|
2019-07-23 10:12:24 +03:00
|
|
|
/**
|
|
|
|
* Renders a message with the changes made in an edit shown visually.
|
|
|
|
* @param {object} originalContent the content for the base message
|
|
|
|
* @param {object} editContent the content for the edit message
|
|
|
|
* @return {object} a react element similar to what `bodyToHtml` returns
|
|
|
|
*/
|
2021-06-22 19:22:38 +03:00
|
|
|
export function editBodyDiffToHtml(originalContent: IContent, editContent: IContent): ReactNode {
|
2019-07-23 10:12:24 +03:00
|
|
|
// wrap the body in a div, DiffDOM needs a root element
|
|
|
|
const originalBody = `<div>${getSanitizedHtmlBody(originalContent)}</div>`;
|
|
|
|
const editBody = `<div>${getSanitizedHtmlBody(editContent)}</div>`;
|
|
|
|
const dd = new DiffDOM();
|
|
|
|
// diffActions is an array of objects with at least a `action` and `route`
|
|
|
|
// property. `action` tells us what the diff object changes, and `route` where.
|
|
|
|
// `route` is a path on the DOM tree expressed as an array of indices.
|
2019-07-31 13:25:04 +03:00
|
|
|
const originaldiffActions = dd.diff(originalBody, editBody);
|
|
|
|
// work around https://github.com/fiduswriter/diffDOM/issues/90
|
|
|
|
const diffActions = filterCancelingOutDiffs(originaldiffActions);
|
2019-07-23 10:12:24 +03:00
|
|
|
// for diffing text fragments
|
|
|
|
const diffMathPatch = new DiffMatchPatch();
|
|
|
|
// parse the base html message as a DOM tree, to which we'll apply the differences found.
|
|
|
|
// fish out the div in which we wrapped the messages above with children[0].
|
|
|
|
const originalRootNode = new DOMParser().parseFromString(originalBody, "text/html").body.children[0];
|
|
|
|
for (let i = 0; i < diffActions.length; ++i) {
|
|
|
|
const diff = diffActions[i];
|
|
|
|
renderDifferenceInDOM(originalRootNode, diff, diffMathPatch);
|
|
|
|
// DiffDOM assumes in subsequent diffs route path that
|
|
|
|
// the action was applied (e.g. that a removeElement action removed the element).
|
|
|
|
// This is not the case for us. We render differences in the DOM tree, and don't apply them.
|
|
|
|
// So we need to adjust the routes of the remaining diffs to account for this.
|
|
|
|
adjustRoutes(diff, diffActions.slice(i + 1));
|
|
|
|
}
|
|
|
|
// take the html out of the modified DOM tree again
|
|
|
|
const safeBody = originalRootNode.innerHTML;
|
|
|
|
const className = classNames({
|
|
|
|
'mx_EventTile_body': true,
|
|
|
|
'markdown-body': true,
|
|
|
|
});
|
|
|
|
return <span key="body" className={className} dangerouslySetInnerHTML={{ __html: safeBody }} dir="auto" />;
|
|
|
|
}
|