From c261b9fb2367026832737545477483e626bdc93e Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Mon, 12 Jul 2021 12:47:22 -0700 Subject: [PATCH] Avoid intermediate objects while parsing TextTiles' bodies. --- .../room/timeline/tiles/BaseTextTile.js | 11 +++++++--- .../session/room/timeline/tiles/TextTile.js | 21 +++++++++++-------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/domain/session/room/timeline/tiles/BaseTextTile.js b/src/domain/session/room/timeline/tiles/BaseTextTile.js index f566f6e1..414ecd5d 100644 --- a/src/domain/session/room/timeline/tiles/BaseTextTile.js +++ b/src/domain/session/room/timeline/tiles/BaseTextTile.js @@ -28,22 +28,27 @@ export class BaseTextTile extends BaseMessageTile { return "message"; } - _parseBody(body) { + _parseBody(body, _) { return stringAsBody(body.string); } + _getBodyFormat() { + return "plain"; + } + get body() { const body = this._getBody(); + const format = this._getBodyFormat(); // body.string is a string, so we can check for difference by just // doing an equality check // Even if the body hasn't changed, but the format has, we need // to re-fill our cache. - if (!this._messageBody || this._messageBody.sourceString !== body.string || this._messageFormat !== body.format) { + if (!this._messageBody || this._messageBody.sourceString !== body || this._messageFormat !== format) { // body with markup is an array of parts, // so we should not recreate it for the same body string, // or else the equality check in the binding will always fail. // So cache it here. - this._messageBody = this._parseBody(body); + this._messageBody = this._parseBody(body, format); this._messageFormat = body.format; } return this._messageBody; diff --git a/src/domain/session/room/timeline/tiles/TextTile.js b/src/domain/session/room/timeline/tiles/TextTile.js index 0d25cc14..0c8684dc 100644 --- a/src/domain/session/room/timeline/tiles/TextTile.js +++ b/src/domain/session/room/timeline/tiles/TextTile.js @@ -19,7 +19,7 @@ import {parsePlainBody} from "../MessageBody.js"; import {parseHTMLBody} from "../deserialize.js"; export class TextTile extends BaseTextTile { - _getContentString(key, format, fallback = null) { + _getContentString(key, fallback = null) { const content = this._getContent(); let val = content?.[key] || fallback; if (!val && val !== "") { // empty string is falsy, but OK here. @@ -28,27 +28,30 @@ export class TextTile extends BaseTextTile { if (content.msgtype === "m.emote") { val = `* ${this.displayName} ${body}`; } - return { string: val, format }; + return val; } _getPlainBody() { - return this._getContentString("body", "plain", ""); + return this._getContentString("body", ""); } _getFormattedBody() { - return this._getContentString("formatted_body", "html"); + return this._getContentString("formatted_body"); } _getBody() { return this._getFormattedBody() || this._getPlainBody(); } - _parseBody(body) { - const string = body.string; - if (body.format === "html") { - return parseHTMLBody({ mediaRepository: this._mediaRepository, platform: this.platform }, string); + _getBodyFormat() { + return this._getContent()?.["formatted_body"] ? "html" : "plain" + } + + _parseBody(body, format) { + if (format === "html") { + return parseHTMLBody({ mediaRepository: this._mediaRepository, platform: this.platform }, body); } else { - return parsePlainBody(string); + return parsePlainBody(body); } } }