From cdf85edad99e8139da11cfda265fa21586ca0207 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Fri, 16 Jul 2021 13:01:33 -0700 Subject: [PATCH] Move pill parsing into deserialize and avoid intermediate objects. --- .../session/room/timeline/deserialize.js | 24 ++++++++++++++----- src/domain/session/room/timeline/pills.js | 12 ---------- 2 files changed, 18 insertions(+), 18 deletions(-) delete mode 100644 src/domain/session/room/timeline/pills.js diff --git a/src/domain/session/room/timeline/deserialize.js b/src/domain/session/room/timeline/deserialize.js index d987332b..ad3eb941 100644 --- a/src/domain/session/room/timeline/deserialize.js +++ b/src/domain/session/room/timeline/deserialize.js @@ -1,6 +1,5 @@ import { MessageBody, HeaderBlock, TableBlock, ListBlock, CodeBlock, PillPart, FormatPart, NewLinePart, RulePart, TextPart, LinkPart, ImagePart } from "./MessageBody.js" import { linkify } from "./linkify/linkify.js"; -import { parsePillLink } from "./pills.js" /* At the time of writing (Jul 1 2021), Matrix Spec recommends * allowing the following HTML tags: @@ -15,6 +14,8 @@ import { parsePillLink } from "./pills.js" const basicInline = ["EM", "STRONG", "CODE", "DEL", "SPAN" ]; const basicBlock = ["DIV", "BLOCKQUOTE"]; const safeSchemas = ["https", "http", "ftp", "mailto", "magnet"].map(name => `${name}://`); +const baseUrl = 'https://matrix.to'; +const linkPrefix = `${baseUrl}/#/`; class Deserializer { constructor(result, mediaRepository) { @@ -22,6 +23,17 @@ class Deserializer { this.mediaRepository = mediaRepository; } + parsePillLink(link) { + if (!link.startsWith(linkPrefix)) { + return null; + } + const contents = link.substring(linkPrefix.length); + if (contents[0] === '@') { + return contents; + } + return null; + } + parseLink(node, children) { const href = this.result.getAttributeValue(node, "href"); const lcUrl = href?.toLowerCase(); @@ -29,9 +41,9 @@ class Deserializer { if (!lcUrl || !safeSchemas.some(schema => lcUrl.startsWith(schema))) { return new FormatPart("span", children); } - const pillData = parsePillLink(href); - if (pillData && pillData.userId) { - return new PillPart(pillData.userId, href, children); + const pillId = this.parsePillLink(href); + if (pillId) { + return new PillPart(pillId, href, children); } return new LinkPart(href, children); } @@ -43,13 +55,13 @@ class Deserializer { // Will return 1 for, say, '1A', which may not be intended? start = parseInt(result.getAttributeValue(node, "start")) || 1; } - const nodes = []; + const items = []; for (const child of result.getChildNodes(node)) { if (result.getNodeElementName(child) !== "LI") { continue; } const item = this.parseAnyNodes(result.getChildNodes(child)); - nodes.push(item); + items.push(item); } return new ListBlock(start, nodes); } diff --git a/src/domain/session/room/timeline/pills.js b/src/domain/session/room/timeline/pills.js deleted file mode 100644 index a638ea58..00000000 --- a/src/domain/session/room/timeline/pills.js +++ /dev/null @@ -1,12 +0,0 @@ -const baseUrl = 'https://matrix.to'; -const linkPrefix = `${baseUrl}/#/`; - -export function parsePillLink(link) { - if (!link.startsWith(linkPrefix)) { - return null; - } - const contents = link.substring(linkPrefix.length); - if (contents[0] === '@') { - return { userId: contents } - } -}