2019-05-07 17:31:37 +02:00
|
|
|
/*
|
|
|
|
Copyright 2019 New Vector Ltd
|
2019-05-22 16:16:32 +02:00
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
2019-05-07 17:31:37 +02: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { MATRIXTO_URL_PATTERN } from '../linkify-matrix';
|
2019-05-29 14:46:15 +02:00
|
|
|
import { walkDOMDepthFirst } from "./dom";
|
2019-07-23 09:12:24 +02:00
|
|
|
import { checkBlockNode } from "../HtmlUtils";
|
2019-05-07 17:31:37 +02:00
|
|
|
|
2019-05-22 13:00:39 +02:00
|
|
|
const REGEX_MATRIXTO = new RegExp(MATRIXTO_URL_PATTERN);
|
|
|
|
|
2019-06-14 18:25:02 +02:00
|
|
|
function parseAtRoomMentions(text, partCreator) {
|
|
|
|
const ATROOM = "@room";
|
|
|
|
const parts = [];
|
|
|
|
text.split(ATROOM).forEach((textPart, i, arr) => {
|
|
|
|
if (textPart.length) {
|
|
|
|
parts.push(partCreator.plain(textPart));
|
|
|
|
}
|
2019-06-18 09:50:31 +02:00
|
|
|
// it's safe to never append @room after the last textPart
|
2019-06-18 08:40:58 +02:00
|
|
|
// as split will report an empty string at the end if
|
|
|
|
// `text` ended in @room.
|
2019-06-14 18:25:02 +02:00
|
|
|
const isLast = i === arr.length - 1;
|
|
|
|
if (!isLast) {
|
|
|
|
parts.push(partCreator.atRoomPill(ATROOM));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return parts;
|
|
|
|
}
|
|
|
|
|
2019-06-14 12:16:34 +02:00
|
|
|
function parseLink(a, partCreator) {
|
2019-05-22 13:00:39 +02:00
|
|
|
const {href} = a;
|
|
|
|
const pillMatch = REGEX_MATRIXTO.exec(href) || [];
|
|
|
|
const resourceId = pillMatch[1]; // The room/user ID
|
|
|
|
const prefix = pillMatch[2]; // The first character of prefix
|
|
|
|
switch (prefix) {
|
|
|
|
case "@":
|
2019-06-14 12:16:34 +02:00
|
|
|
return partCreator.userPill(a.textContent, resourceId);
|
2019-05-22 13:00:39 +02:00
|
|
|
case "#":
|
2019-06-14 12:16:34 +02:00
|
|
|
return partCreator.roomPill(resourceId);
|
2019-05-22 13:00:39 +02:00
|
|
|
default: {
|
|
|
|
if (href === a.textContent) {
|
2019-06-14 12:16:34 +02:00
|
|
|
return partCreator.plain(a.textContent);
|
2019-05-22 13:00:39 +02:00
|
|
|
} else {
|
2019-06-14 12:16:34 +02:00
|
|
|
return partCreator.plain(`[${a.textContent}](${href})`);
|
2019-05-22 13:00:39 +02:00
|
|
|
}
|
2019-05-29 14:46:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 12:16:34 +02:00
|
|
|
function parseCodeBlock(n, partCreator) {
|
2019-05-29 14:46:15 +02:00
|
|
|
const parts = [];
|
2019-05-31 11:06:15 +02:00
|
|
|
const preLines = ("```\n" + n.textContent + "```").split("\n");
|
2019-05-29 14:46:15 +02:00
|
|
|
preLines.forEach((l, i) => {
|
2019-06-14 12:16:34 +02:00
|
|
|
parts.push(partCreator.plain(l));
|
2019-05-29 14:46:15 +02:00
|
|
|
if (i < preLines.length - 1) {
|
2019-06-14 12:16:34 +02:00
|
|
|
parts.push(partCreator.newline());
|
2019-05-29 14:46:15 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return parts;
|
|
|
|
}
|
|
|
|
|
2019-06-21 11:40:27 +02:00
|
|
|
function parseElement(n, partCreator, state) {
|
2019-05-29 14:46:15 +02:00
|
|
|
switch (n.nodeName) {
|
|
|
|
case "A":
|
2019-06-14 12:16:34 +02:00
|
|
|
return parseLink(n, partCreator);
|
2019-05-29 14:46:15 +02:00
|
|
|
case "BR":
|
2019-06-14 12:16:34 +02:00
|
|
|
return partCreator.newline();
|
2019-05-29 14:46:15 +02:00
|
|
|
case "EM":
|
2019-06-14 12:16:34 +02:00
|
|
|
return partCreator.plain(`*${n.textContent}*`);
|
2019-05-29 14:46:15 +02:00
|
|
|
case "STRONG":
|
2019-06-14 12:16:34 +02:00
|
|
|
return partCreator.plain(`**${n.textContent}**`);
|
2019-05-29 14:46:15 +02:00
|
|
|
case "PRE":
|
2019-06-14 12:16:34 +02:00
|
|
|
return parseCodeBlock(n, partCreator);
|
2019-05-29 14:46:15 +02:00
|
|
|
case "CODE":
|
2019-06-14 12:16:34 +02:00
|
|
|
return partCreator.plain(`\`${n.textContent}\``);
|
2019-05-29 14:46:15 +02:00
|
|
|
case "DEL":
|
2019-06-14 12:16:34 +02:00
|
|
|
return partCreator.plain(`<del>${n.textContent}</del>`);
|
2019-06-21 11:40:27 +02:00
|
|
|
case "LI": {
|
|
|
|
const indent = " ".repeat(state.listDepth - 1);
|
2019-05-29 14:46:15 +02:00
|
|
|
if (n.parentElement.nodeName === "OL") {
|
2019-06-21 11:40:27 +02:00
|
|
|
return partCreator.plain(`${indent}1. `);
|
2019-05-29 14:46:15 +02:00
|
|
|
} else {
|
2019-06-21 11:40:27 +02:00
|
|
|
return partCreator.plain(`${indent}- `);
|
2019-05-29 14:46:15 +02:00
|
|
|
}
|
2019-06-21 11:40:27 +02:00
|
|
|
}
|
|
|
|
case "OL":
|
|
|
|
case "UL":
|
|
|
|
state.listDepth = (state.listDepth || 0) + 1;
|
|
|
|
// es-lint-disable-next-line no-fallthrough
|
2019-05-29 14:46:15 +02:00
|
|
|
default:
|
|
|
|
// don't textify block nodes we'll decend into
|
|
|
|
if (!checkDecendInto(n)) {
|
2019-06-14 12:16:34 +02:00
|
|
|
return partCreator.plain(n.textContent);
|
2019-05-29 14:46:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkDecendInto(node) {
|
|
|
|
switch (node.nodeName) {
|
|
|
|
case "PRE":
|
|
|
|
// a code block is textified in parseCodeBlock
|
|
|
|
// as we don't want to preserve markup in it,
|
|
|
|
// so no need to decend into it
|
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
return checkBlockNode(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkIgnored(n) {
|
|
|
|
if (n.nodeType === Node.TEXT_NODE) {
|
|
|
|
// riot adds \n text nodes in a lot of places,
|
|
|
|
// which should be ignored
|
|
|
|
return n.nodeValue === "\n";
|
|
|
|
} else if (n.nodeType === Node.ELEMENT_NODE) {
|
|
|
|
return n.nodeName === "MX-REPLY";
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-08-20 12:34:35 +02:00
|
|
|
const QUOTE_LINE_PREFIX = "> ";
|
2019-06-14 12:16:34 +02:00
|
|
|
function prefixQuoteLines(isFirstNode, parts, partCreator) {
|
2019-05-29 14:46:15 +02:00
|
|
|
// a newline (to append a > to) wouldn't be added to parts for the first line
|
|
|
|
// if there was no content before the BLOCKQUOTE, so handle that
|
|
|
|
if (isFirstNode) {
|
2019-08-20 12:34:35 +02:00
|
|
|
parts.splice(0, 0, partCreator.plain(QUOTE_LINE_PREFIX));
|
2019-05-29 14:46:15 +02:00
|
|
|
}
|
|
|
|
for (let i = 0; i < parts.length; i += 1) {
|
|
|
|
if (parts[i].type === "newline") {
|
2019-08-20 12:34:35 +02:00
|
|
|
parts.splice(i + 1, 0, partCreator.plain(QUOTE_LINE_PREFIX));
|
2019-05-29 14:46:15 +02:00
|
|
|
i += 1;
|
2019-05-22 13:00:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-20 12:34:35 +02:00
|
|
|
function parseHtmlMessage(html, partCreator, isQuotedMessage) {
|
2019-05-08 11:13:36 +02:00
|
|
|
// no nodes from parsing here should be inserted in the document,
|
|
|
|
// as scripts in event handlers, etc would be executed then.
|
|
|
|
// we're only taking text, so that is fine
|
2019-05-29 14:46:15 +02:00
|
|
|
const rootNode = new DOMParser().parseFromString(html, "text/html").body;
|
2019-05-22 13:00:39 +02:00
|
|
|
const parts = [];
|
2019-05-29 14:46:15 +02:00
|
|
|
let lastNode;
|
2019-08-20 12:34:35 +02:00
|
|
|
let inQuote = isQuotedMessage;
|
2019-06-21 11:40:27 +02:00
|
|
|
const state = {};
|
2019-05-29 14:46:15 +02:00
|
|
|
|
|
|
|
function onNodeEnter(n) {
|
|
|
|
if (checkIgnored(n)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (n.nodeName === "BLOCKQUOTE") {
|
|
|
|
inQuote = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const newParts = [];
|
|
|
|
if (lastNode && (checkBlockNode(lastNode) || checkBlockNode(n))) {
|
2019-06-14 12:16:34 +02:00
|
|
|
newParts.push(partCreator.newline());
|
2019-05-29 14:46:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (n.nodeType === Node.TEXT_NODE) {
|
2019-06-14 18:25:02 +02:00
|
|
|
newParts.push(...parseAtRoomMentions(n.nodeValue, partCreator));
|
2019-05-29 14:46:15 +02:00
|
|
|
} else if (n.nodeType === Node.ELEMENT_NODE) {
|
2019-06-21 11:40:27 +02:00
|
|
|
const parseResult = parseElement(n, partCreator, state);
|
2019-05-29 14:46:15 +02:00
|
|
|
if (parseResult) {
|
|
|
|
if (Array.isArray(parseResult)) {
|
|
|
|
newParts.push(...parseResult);
|
|
|
|
} else {
|
|
|
|
newParts.push(parseResult);
|
2019-05-07 17:31:37 +02:00
|
|
|
}
|
2019-05-29 14:46:15 +02:00
|
|
|
}
|
2019-05-07 17:31:37 +02:00
|
|
|
}
|
2019-05-29 14:46:15 +02:00
|
|
|
|
|
|
|
if (newParts.length && inQuote) {
|
|
|
|
const isFirstPart = parts.length === 0;
|
2019-06-14 12:16:34 +02:00
|
|
|
prefixQuoteLines(isFirstPart, newParts, partCreator);
|
2019-05-29 14:46:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
parts.push(...newParts);
|
|
|
|
|
|
|
|
// extra newline after quote, only if there something behind it...
|
|
|
|
if (lastNode && lastNode.nodeName === "BLOCKQUOTE") {
|
2019-06-14 12:16:34 +02:00
|
|
|
parts.push(partCreator.newline());
|
2019-05-29 14:46:15 +02:00
|
|
|
}
|
2019-08-02 16:36:09 +02:00
|
|
|
const decend = checkDecendInto(n);
|
|
|
|
// when not decending (like for PRE), onNodeLeave won't be called to set lastNode
|
|
|
|
// so do that here.
|
|
|
|
lastNode = decend ? null : n;
|
|
|
|
return decend;
|
2019-05-29 14:46:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function onNodeLeave(n) {
|
|
|
|
if (checkIgnored(n)) {
|
|
|
|
return;
|
|
|
|
}
|
2019-06-21 11:40:27 +02:00
|
|
|
switch (n.nodeName) {
|
|
|
|
case "BLOCKQUOTE":
|
|
|
|
inQuote = false;
|
|
|
|
break;
|
|
|
|
case "OL":
|
|
|
|
case "UL":
|
|
|
|
state.listDepth -= 1;
|
|
|
|
break;
|
2019-05-22 13:00:39 +02:00
|
|
|
}
|
2019-05-29 14:46:15 +02:00
|
|
|
lastNode = n;
|
2019-05-22 13:00:39 +02:00
|
|
|
}
|
2019-05-29 14:46:15 +02:00
|
|
|
|
|
|
|
walkDOMDepthFirst(rootNode, onNodeEnter, onNodeLeave);
|
|
|
|
|
2019-05-07 17:31:37 +02:00
|
|
|
return parts;
|
|
|
|
}
|
|
|
|
|
2019-08-20 12:34:35 +02:00
|
|
|
function parsePlainTextMessage(body, partCreator, isQuotedMessage) {
|
|
|
|
const lines = body.split("\n");
|
|
|
|
const parts = lines.reduce((parts, line, i) => {
|
|
|
|
if (isQuotedMessage) {
|
|
|
|
parts.push(partCreator.plain(QUOTE_LINE_PREFIX));
|
|
|
|
}
|
|
|
|
parts.push(...parseAtRoomMentions(line, partCreator));
|
2019-08-22 15:41:40 +02:00
|
|
|
const isLast = i === lines.length - 1;
|
|
|
|
if (!isLast) {
|
|
|
|
parts.push(partCreator.newline());
|
|
|
|
}
|
2019-08-20 12:34:35 +02:00
|
|
|
return parts;
|
|
|
|
}, []);
|
|
|
|
return parts;
|
|
|
|
}
|
|
|
|
|
2019-08-20 17:15:12 +02:00
|
|
|
export function parseEvent(event, partCreator, {isQuotedMessage = false} = {}) {
|
2019-05-07 17:31:37 +02:00
|
|
|
const content = event.getContent();
|
2019-06-14 11:01:52 +02:00
|
|
|
let parts;
|
2019-05-07 17:31:37 +02:00
|
|
|
if (content.format === "org.matrix.custom.html") {
|
2019-08-20 12:34:35 +02:00
|
|
|
parts = parseHtmlMessage(content.formatted_body || "", partCreator, isQuotedMessage);
|
2019-05-07 17:31:37 +02:00
|
|
|
} else {
|
2019-08-20 12:34:35 +02:00
|
|
|
parts = parsePlainTextMessage(content.body || "", partCreator, isQuotedMessage);
|
2019-05-07 17:31:37 +02:00
|
|
|
}
|
2019-06-14 11:01:52 +02:00
|
|
|
if (content.msgtype === "m.emote") {
|
2019-06-14 12:16:34 +02:00
|
|
|
parts.unshift(partCreator.plain("/me "));
|
2019-06-14 11:01:52 +02:00
|
|
|
}
|
|
|
|
return parts;
|
2019-05-07 17:31:37 +02:00
|
|
|
}
|