Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Remove reliance on DOM API to generated message preview
Browse files Browse the repository at this point in the history
  • Loading branch information
germain-gg committed Apr 22, 2021
1 parent d9dac7b commit 2b6551d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
13 changes: 8 additions & 5 deletions src/HtmlUtils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,14 @@ export function sanitizedHtmlNode(insaneHtml: string) {
return <div dangerouslySetInnerHTML={{ __html: saneHtml }} dir="auto" />;
}

export function sanitizedHtmlNodeInnerText(insaneHtml: string) {
const saneHtml = sanitizeHtml(insaneHtml, sanitizeHtmlParams);
const contentDiv = document.createElement("div");
contentDiv.innerHTML = saneHtml;
return contentDiv.innerText;
export function getHtmlText(insaneHtml: string) {
return sanitizeHtml(insaneHtml, {
allowedTags: [],
allowedAttributes: {},
selfClosing: [],
allowedSchemes: [],
disallowedTagsMode: 'discard',
})
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/stores/room-list/previews/MessageEventPreview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { MatrixEvent } from "matrix-js-sdk/src/models/event";
import { _t } from "../../../languageHandler";
import { getSenderName, isSelf, shouldPrefixMessagesIn } from "./utils";
import ReplyThread from "../../../components/views/elements/ReplyThread";
import { sanitizedHtmlNodeInnerText } from "../../../HtmlUtils";
import { getHtmlText } from "../../../HtmlUtils";

export class MessageEventPreview implements IPreview {
public getTextFor(event: MatrixEvent, tagId?: TagID): string {
Expand Down Expand Up @@ -55,7 +55,7 @@ export class MessageEventPreview implements IPreview {
}

if (hasHtml) {
body = sanitizedHtmlNodeInnerText(body);
body = getHtmlText(body);
}

if (msgtype === 'm.emote') {
Expand Down

0 comments on commit 2b6551d

Please sign in to comment.