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-13 17:20:21 +00:00
|
|
|
import { PlainPart, UserPillPart, RoomPillPart, NewlinePart } from "./parts";
|
2019-05-07 15:31:37 +00:00
|
|
|
|
2019-05-22 11:00:39 +00:00
|
|
|
const REGEX_MATRIXTO = new RegExp(MATRIXTO_URL_PATTERN);
|
|
|
|
|
|
|
|
function parseLink(a, parts, room) {
|
|
|
|
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 "@":
|
|
|
|
parts.push(new UserPillPart(
|
|
|
|
resourceId,
|
|
|
|
a.textContent,
|
|
|
|
room.getMember(resourceId),
|
|
|
|
));
|
|
|
|
break;
|
|
|
|
case "#":
|
|
|
|
parts.push(new RoomPillPart(resourceId));
|
|
|
|
break;
|
|
|
|
default: {
|
|
|
|
if (href === a.textContent) {
|
|
|
|
parts.push(new PlainPart(a.textContent));
|
|
|
|
} else {
|
|
|
|
parts.push(new PlainPart(`[${a.textContent}](${href})`));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-17 18:48:05 +00:00
|
|
|
function parseHtmlMessage(html, room) {
|
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-22 11:00:39 +00:00
|
|
|
const root = new DOMParser().parseFromString(html, "text/html").body;
|
|
|
|
let n = root.firstChild;
|
|
|
|
const parts = [];
|
|
|
|
let isFirstNode = true;
|
|
|
|
while (n && n !== root) {
|
2019-05-07 15:31:37 +00:00
|
|
|
switch (n.nodeType) {
|
|
|
|
case Node.TEXT_NODE:
|
2019-05-22 11:00:39 +00:00
|
|
|
// the plainpart doesn't accept \n and will cause
|
|
|
|
// a newlinepart to be created.
|
|
|
|
if (n.nodeValue !== "\n") {
|
|
|
|
parts.push(new PlainPart(n.nodeValue));
|
|
|
|
}
|
|
|
|
break;
|
2019-05-07 15:31:37 +00:00
|
|
|
case Node.ELEMENT_NODE:
|
|
|
|
switch (n.nodeName) {
|
2019-05-27 15:04:26 +00:00
|
|
|
case "MX-REPLY":
|
|
|
|
break;
|
2019-05-22 11:00:39 +00:00
|
|
|
case "DIV":
|
|
|
|
case "P": {
|
|
|
|
// block element should cause line break if not first
|
|
|
|
if (!isFirstNode) {
|
|
|
|
parts.push(new NewlinePart("\n"));
|
|
|
|
}
|
|
|
|
// decend into paragraph or div
|
|
|
|
if (n.firstChild) {
|
|
|
|
n = n.firstChild;
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
break;
|
2019-05-07 15:31:37 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-22 11:00:39 +00:00
|
|
|
case "A": {
|
|
|
|
parseLink(n, parts, room);
|
|
|
|
break;
|
|
|
|
}
|
2019-05-13 17:20:21 +00:00
|
|
|
case "BR":
|
2019-05-22 11:00:39 +00:00
|
|
|
parts.push(new NewlinePart("\n"));
|
|
|
|
break;
|
2019-05-21 15:34:18 +00:00
|
|
|
case "EM":
|
2019-05-22 11:00:39 +00:00
|
|
|
parts.push(new PlainPart(`*${n.textContent}*`));
|
|
|
|
break;
|
2019-05-21 15:34:18 +00:00
|
|
|
case "STRONG":
|
2019-05-22 11:00:39 +00:00
|
|
|
parts.push(new PlainPart(`**${n.textContent}**`));
|
|
|
|
break;
|
|
|
|
case "PRE": {
|
|
|
|
// block element should cause line break if not first
|
|
|
|
if (!isFirstNode) {
|
|
|
|
parts.push(new NewlinePart("\n"));
|
|
|
|
}
|
|
|
|
const preLines = `\`\`\`\n${n.textContent}\`\`\``.split("\n");
|
|
|
|
preLines.forEach((l, i) => {
|
|
|
|
parts.push(new PlainPart(l));
|
|
|
|
if (i < preLines.length - 1) {
|
|
|
|
parts.push(new NewlinePart("\n"));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
2019-05-21 15:34:18 +00:00
|
|
|
case "CODE":
|
2019-05-22 11:00:39 +00:00
|
|
|
parts.push(new PlainPart(`\`${n.textContent}\``));
|
|
|
|
break;
|
2019-05-24 18:57:03 +00:00
|
|
|
case "DEL":
|
|
|
|
parts.push(new PlainPart(`<del>${n.textContent}</del>`));
|
|
|
|
break;
|
2019-05-07 15:31:37 +00:00
|
|
|
default:
|
2019-05-22 11:00:39 +00:00
|
|
|
parts.push(new PlainPart(n.textContent));
|
|
|
|
break;
|
2019-05-07 15:31:37 +00:00
|
|
|
}
|
2019-05-22 11:00:39 +00:00
|
|
|
break;
|
2019-05-07 15:31:37 +00:00
|
|
|
}
|
2019-05-22 11:00:39 +00:00
|
|
|
// go up if we can't go next
|
|
|
|
if (!n.nextSibling) {
|
|
|
|
n = n.parentElement;
|
|
|
|
}
|
|
|
|
n = n.nextSibling;
|
|
|
|
isFirstNode = false;
|
|
|
|
}
|
2019-05-07 15:31:37 +00:00
|
|
|
return parts;
|
|
|
|
}
|
|
|
|
|
2019-05-17 18:48:05 +00:00
|
|
|
export function parseEvent(event, room) {
|
2019-05-07 15:31:37 +00:00
|
|
|
const content = event.getContent();
|
|
|
|
if (content.format === "org.matrix.custom.html") {
|
2019-05-17 18:48:05 +00:00
|
|
|
return parseHtmlMessage(content.formatted_body || "", room);
|
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-05-14 09:37:40 +00:00
|
|
|
const parts = lines.reduce((parts, line, i) => {
|
|
|
|
const isLast = i === lines.length - 1;
|
|
|
|
const text = new PlainPart(line);
|
|
|
|
const newLine = !isLast && new NewlinePart("\n");
|
|
|
|
if (newLine) {
|
|
|
|
return parts.concat(text, newLine);
|
|
|
|
} else {
|
|
|
|
return parts.concat(text);
|
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
return parts;
|
2019-05-07 15:31:37 +00:00
|
|
|
}
|
|
|
|
}
|