2019-05-07 15:31:37 +00:00
|
|
|
/*
|
|
|
|
Copyright 2019 New Vector Ltd
|
2019-05-22 14:16:32 +00:00
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
2019-05-07 15:31:37 +00: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 12:46:15 +00:00
|
|
|
import { walkDOMDepthFirst } from "./dom";
|
2019-05-07 15:31:37 +00:00
|
|
|
|
2019-05-22 11:00:39 +00:00
|
|
|
const REGEX_MATRIXTO = new RegExp(MATRIXTO_URL_PATTERN);
|
|
|
|
|
2019-06-14 16:25:02 +00: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 06:40:58 +00:00
|
|
|
// it's safe to never append @room after the last text
|
|
|
|
// as split will report an empty string at the end if
|
|
|
|
// `text` ended in @room.
|
2019-06-14 16:25:02 +00:00
|
|
|
const isLast = i === arr.length - 1;
|
|
|
|
if (!isLast) {
|
|
|
|
parts.push(partCreator.atRoomPill(ATROOM));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return parts;
|
|
|
|
}
|
|
|
|
|
2019-06-14 10:16:34 +00:00
|
|
|
function parseLink(a, partCreator) {
|
2019-05-22 11:00:39 +00: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 10:16:34 +00:00
|
|
|
return partCreator.userPill(a.textContent, resourceId);
|
2019-05-22 11:00:39 +00:00
|
|
|
case "#":
|
2019-06-14 10:16:34 +00:00
|
|
|
return partCreator.roomPill(resourceId);
|
2019-05-22 11:00:39 +00:00
|
|
|
default: {
|
|
|
|
if (href === a.textContent) {
|
2019-06-14 10:16:34 +00:00
|
|
|
return partCreator.plain(a.textContent);
|
2019-05-22 11:00:39 +00:00
|
|
|
} else {
|
2019-06-14 10:16:34 +00:00
|
|
|
return partCreator.plain(`[${a.textContent}](${href})`);
|
2019-05-22 11:00:39 +00:00
|
|
|
}
|
2019-05-29 12:46:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 10:16:34 +00:00
|
|
|
function parseCodeBlock(n, partCreator) {
|
2019-05-29 12:46:15 +00:00
|
|
|
const parts = [];
|
2019-05-31 09:06:15 +00:00
|
|
|
const preLines = ("```\n" + n.textContent + "```").split("\n");
|
2019-05-29 12:46:15 +00:00
|
|
|
preLines.forEach((l, i) => {
|
2019-06-14 10:16:34 +00:00
|
|
|
parts.push(partCreator.plain(l));
|
2019-05-29 12:46:15 +00:00
|
|
|
if (i < preLines.length - 1) {
|
2019-06-14 10:16:34 +00:00
|
|
|
parts.push(partCreator.newline());
|
2019-05-29 12:46:15 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return parts;
|
|
|
|
}
|
|
|
|
|
2019-06-14 10:16:34 +00:00
|
|
|
function parseElement(n, partCreator) {
|
2019-05-29 12:46:15 +00:00
|
|
|
switch (n.nodeName) {
|
|
|
|
case "A":
|
2019-06-14 10:16:34 +00:00
|
|
|
return parseLink(n, partCreator);
|
2019-05-29 12:46:15 +00:00
|
|
|
case "BR":
|
2019-06-14 10:16:34 +00:00
|
|
|
return partCreator.newline();
|
2019-05-29 12:46:15 +00:00
|
|
|
case "EM":
|
2019-06-14 10:16:34 +00:00
|
|
|
return partCreator.plain(`*${n.textContent}*`);
|
2019-05-29 12:46:15 +00:00
|
|
|
case "STRONG":
|
2019-06-14 10:16:34 +00:00
|
|
|
return partCreator.plain(`**${n.textContent}**`);
|
2019-05-29 12:46:15 +00:00
|
|
|
case "PRE":
|
2019-06-14 10:16:34 +00:00
|
|
|
return parseCodeBlock(n, partCreator);
|
2019-05-29 12:46:15 +00:00
|
|
|
case "CODE":
|
2019-06-14 10:16:34 +00:00
|
|
|
return partCreator.plain(`\`${n.textContent}\``);
|
2019-05-29 12:46:15 +00:00
|
|
|
case "DEL":
|
2019-06-14 10:16:34 +00:00
|
|
|
return partCreator.plain(`<del>${n.textContent}</del>`);
|
2019-05-29 12:46:15 +00:00
|
|
|
case "LI":
|
|
|
|
if (n.parentElement.nodeName === "OL") {
|
2019-06-14 10:16:34 +00:00
|
|
|
return partCreator.plain(` 1. `);
|
2019-05-29 12:46:15 +00:00
|
|
|
} else {
|
2019-06-14 10:16:34 +00:00
|
|
|
return partCreator.plain(` - `);
|
2019-05-29 12:46:15 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
// don't textify block nodes we'll decend into
|
|
|
|
if (!checkDecendInto(n)) {
|
2019-06-14 10:16:34 +00:00
|
|
|
return partCreator.plain(n.textContent);
|
2019-05-29 12:46:15 +00: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 checkBlockNode(node) {
|
|
|
|
switch (node.nodeName) {
|
|
|
|
case "PRE":
|
|
|
|
case "BLOCKQUOTE":
|
|
|
|
case "DIV":
|
|
|
|
case "P":
|
|
|
|
case "UL":
|
|
|
|
case "OL":
|
|
|
|
case "LI":
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-06-14 10:16:34 +00:00
|
|
|
function prefixQuoteLines(isFirstNode, parts, partCreator) {
|
2019-05-29 12:46:15 +00:00
|
|
|
const PREFIX = "> ";
|
|
|
|
// 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-06-14 10:16:34 +00:00
|
|
|
parts.splice(0, 0, partCreator.plain(PREFIX));
|
2019-05-29 12:46:15 +00:00
|
|
|
}
|
|
|
|
for (let i = 0; i < parts.length; i += 1) {
|
|
|
|
if (parts[i].type === "newline") {
|
2019-06-14 10:16:34 +00:00
|
|
|
parts.splice(i + 1, 0, partCreator.plain(PREFIX));
|
2019-05-29 12:46:15 +00:00
|
|
|
i += 1;
|
2019-05-22 11:00:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 10:16:34 +00:00
|
|
|
function parseHtmlMessage(html, partCreator) {
|
2019-05-08 09:13:36 +00: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 12:46:15 +00:00
|
|
|
const rootNode = new DOMParser().parseFromString(html, "text/html").body;
|
2019-05-22 11:00:39 +00:00
|
|
|
const parts = [];
|
2019-05-29 12:46:15 +00:00
|
|
|
let lastNode;
|
|
|
|
let inQuote = false;
|
|
|
|
|
|
|
|
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 10:16:34 +00:00
|
|
|
newParts.push(partCreator.newline());
|
2019-05-29 12:46:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (n.nodeType === Node.TEXT_NODE) {
|
2019-06-14 16:25:02 +00:00
|
|
|
newParts.push(...parseAtRoomMentions(n.nodeValue, partCreator));
|
2019-05-29 12:46:15 +00:00
|
|
|
} else if (n.nodeType === Node.ELEMENT_NODE) {
|
2019-06-14 10:16:34 +00:00
|
|
|
const parseResult = parseElement(n, partCreator);
|
2019-05-29 12:46:15 +00:00
|
|
|
if (parseResult) {
|
|
|
|
if (Array.isArray(parseResult)) {
|
|
|
|
newParts.push(...parseResult);
|
|
|
|
} else {
|
|
|
|
newParts.push(parseResult);
|
2019-05-07 15:31:37 +00:00
|
|
|
}
|
2019-05-29 12:46:15 +00:00
|
|
|
}
|
2019-05-07 15:31:37 +00:00
|
|
|
}
|
2019-05-29 12:46:15 +00:00
|
|
|
|
|
|
|
if (newParts.length && inQuote) {
|
|
|
|
const isFirstPart = parts.length === 0;
|
2019-06-14 10:16:34 +00:00
|
|
|
prefixQuoteLines(isFirstPart, newParts, partCreator);
|
2019-05-29 12:46:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
parts.push(...newParts);
|
|
|
|
|
|
|
|
// extra newline after quote, only if there something behind it...
|
|
|
|
if (lastNode && lastNode.nodeName === "BLOCKQUOTE") {
|
2019-06-14 10:16:34 +00:00
|
|
|
parts.push(partCreator.newline());
|
2019-05-29 12:46:15 +00:00
|
|
|
}
|
|
|
|
lastNode = null;
|
|
|
|
return checkDecendInto(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
function onNodeLeave(n) {
|
|
|
|
if (checkIgnored(n)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (n.nodeName === "BLOCKQUOTE") {
|
|
|
|
inQuote = false;
|
2019-05-22 11:00:39 +00:00
|
|
|
}
|
2019-05-29 12:46:15 +00:00
|
|
|
lastNode = n;
|
2019-05-22 11:00:39 +00:00
|
|
|
}
|
2019-05-29 12:46:15 +00:00
|
|
|
|
|
|
|
walkDOMDepthFirst(rootNode, onNodeEnter, onNodeLeave);
|
|
|
|
|
2019-05-07 15:31:37 +00:00
|
|
|
return parts;
|
|
|
|
}
|
|
|
|
|
2019-06-14 10:16:34 +00:00
|
|
|
export function parseEvent(event, partCreator) {
|
2019-05-07 15:31:37 +00:00
|
|
|
const content = event.getContent();
|
2019-06-14 09:01:52 +00:00
|
|
|
let parts;
|
2019-05-07 15:31:37 +00:00
|
|
|
if (content.format === "org.matrix.custom.html") {
|
2019-06-14 10:16:34 +00:00
|
|
|
parts = parseHtmlMessage(content.formatted_body || "", partCreator);
|
2019-05-07 15:31:37 +00:00
|
|
|
} else {
|
2019-05-15 10:49:43 +00:00
|
|
|
const body = content.body || "";
|
|
|
|
const lines = body.split("\n");
|
2019-06-14 09:01:52 +00:00
|
|
|
parts = lines.reduce((parts, line, i) => {
|
2019-05-14 09:37:40 +00:00
|
|
|
const isLast = i === lines.length - 1;
|
2019-06-14 16:25:02 +00:00
|
|
|
const newParts = parseAtRoomMentions(line, partCreator);
|
|
|
|
if (!isLast) {
|
|
|
|
newParts.push(partCreator.newline());
|
2019-05-14 09:37:40 +00:00
|
|
|
}
|
2019-06-14 16:25:02 +00:00
|
|
|
return parts.concat(newParts);
|
2019-05-14 09:37:40 +00:00
|
|
|
}, []);
|
2019-05-07 15:31:37 +00:00
|
|
|
}
|
2019-06-14 09:01:52 +00:00
|
|
|
if (content.msgtype === "m.emote") {
|
2019-06-14 10:16:34 +00:00
|
|
|
parts.unshift(partCreator.plain("/me "));
|
2019-06-14 09:01:52 +00:00
|
|
|
}
|
|
|
|
return parts;
|
2019-05-07 15:31:37 +00:00
|
|
|
}
|