diff --git a/src/shared/components/post/metadata-card.tsx b/src/shared/components/post/metadata-card.tsx index 1d8e345b..1269c0b7 100644 --- a/src/shared/components/post/metadata-card.tsx +++ b/src/shared/components/post/metadata-card.tsx @@ -1,7 +1,6 @@ import { Component } from "inferno"; import { Post } from "lemmy-js-client"; import * as sanitizeHtml from "sanitize-html"; -import { unescapeHTML } from "@utils/helpers"; import { relTags } from "../../config"; import { Icon } from "../common/icon"; @@ -26,21 +25,17 @@ export class MetadataCard extends Component { {post.name !== post.embed_title && ( <>
- - {unescapeHTML(post.embed_title)} + + {post.embed_title}
- {new URL(unescapeHTML(post.url)).hostname} + {new URL(post.url).hostname} diff --git a/src/shared/components/post/post-listing.tsx b/src/shared/components/post/post-listing.tsx index 64b9e993..afd5daa5 100644 --- a/src/shared/components/post/post-listing.tsx +++ b/src/shared/components/post/post-listing.tsx @@ -5,7 +5,6 @@ import { capitalizeFirstLetter, futureDaysToUnixTime, hostname, - unescapeHTML, } from "@utils/helpers"; import { isImage, isVideo } from "@utils/media"; import { @@ -207,9 +206,9 @@ export class PostListing extends Component { <> {this.listing()} {this.state.imageExpanded && !this.props.hideImage && this.img} - {this.showBody && - unescapeHTML(post.url) && - unescapeHTML(post.embed_title) && } + {this.showBody && post.url && post.embed_title && ( + + )} {this.showBody && this.body()} ) : ( @@ -286,8 +285,8 @@ export class PostListing extends Component { ); @@ -310,7 +309,7 @@ export class PostListing extends Component { get imageSrc(): string | undefined { const post = this.postView.post; - const url = unescapeHTML(post.url); + const url = post.url; const thumbnail = post.thumbnail_url; if (thumbnail) { @@ -324,7 +323,7 @@ export class PostListing extends Component { thumbnail() { const post = this.postView.post; - const url = unescapeHTML(post.url); + const url = post.url; const thumbnail = post.thumbnail_url; if (!this.props.hideImage && url && isImage(url) && this.imageSrc) { @@ -450,7 +449,7 @@ export class PostListing extends Component { > ); @@ -458,7 +457,7 @@ export class PostListing extends Component { postTitleLine() { const post = this.postView.post; - const url = unescapeHTML(post.url); + const url = post.url; return ( <> @@ -474,9 +473,7 @@ export class PostListing extends Component { href={url} title={url} rel={relTags} - dangerouslySetInnerHTML={mdToHtmlInline( - unescapeHTML(post.name), - )} + dangerouslySetInnerHTML={mdToHtmlInline(post.name)} > ) : ( this.postLink @@ -489,7 +486,7 @@ export class PostListing extends Component { * MetadataCard/body toggle. */} {!this.props.showBody && - (unescapeHTML(post.url && post.embed_title) || post.body) && + ((post.url && post.embed_title) || post.body) && this.showPreviewButton()} {post.removed && ( @@ -551,7 +548,7 @@ export class PostListing extends Component { urlLine() { const post = this.postView.post; - const url = unescapeHTML(post.url); + const url = post.url; return (

diff --git a/src/shared/components/post/post.tsx b/src/shared/components/post/post.tsx index c624f076..1b18c9c4 100644 --- a/src/shared/components/post/post.tsx +++ b/src/shared/components/post/post.tsx @@ -19,7 +19,7 @@ import { restoreScrollPosition, saveScrollPosition, } from "@utils/browser"; -import { debounce, randomStr, unescapeHTML } from "@utils/helpers"; +import { debounce, randomStr } from "@utils/helpers"; import { isImage } from "@utils/media"; import { RouteDataResponse } from "@utils/types"; import autosize from "autosize"; @@ -325,9 +325,8 @@ export class Post extends Component { get documentTitle(): string { const siteName = this.state.siteRes.site_view.site.name; - const postTitle = unescapeHTML(this.state.postRes.data.post_view.post.name); return this.state.postRes.state === "success" - ? `${postTitle} - ${siteName}` + ? `${this.state.postRes.data.post_view.post.name} - ${siteName}` : siteName; } diff --git a/src/shared/utils/helpers/html-entities.ts b/src/shared/utils/helpers/html-entities.ts deleted file mode 100644 index 3cfe95d3..00000000 --- a/src/shared/utils/helpers/html-entities.ts +++ /dev/null @@ -1,60 +0,0 @@ -const matchEscHtmlRx = /["'&<>]/; -const matchUnEscRx = /&(?:amp|#38|lt|#60|gt|#62|apos|#39|quot|#34);/g; - -export function escapeHTML(str: string): string { - const matchEscHtml = matchEscHtmlRx.exec(str); - if (!matchEscHtml) { - return str; - } - let escape; - let html = ""; - let index = 0; - let lastIndex = 0; - for (index = matchEscHtml.index; index < str.length; index++) { - switch (str.charCodeAt(index)) { - case 34: // " - escape = """; - break; - case 38: // & - escape = "&"; - break; - case 39: // ' - escape = "'"; - break; - case 60: // < - escape = "<"; - break; - case 62: // > - escape = ">"; - break; - default: - continue; - } - - if (lastIndex !== index) { - html += str.substring(lastIndex, index); - } - - lastIndex = index + 1; - html += escape; - } - - return lastIndex !== index ? html + str.substring(lastIndex, index) : html; -} - -export function unescapeHTML(str: string): string { - const matchUnEsc = matchUnEscRx.exec(str); - if (!matchUnEsc) { - return str; - } - - const res = str - .replace(/"/g, '"') - .replace(/'/g, "'") - .replace(/:/g, ":") - .replace(/</g, "<") - .replace(/>/g, ">") - .replace(/&/g, "&"); - - return unescapeHTML(res); -} diff --git a/src/shared/utils/helpers/index.ts b/src/shared/utils/helpers/index.ts index aedfef0c..c42f9109 100644 --- a/src/shared/utils/helpers/index.ts +++ b/src/shared/utils/helpers/index.ts @@ -18,19 +18,16 @@ import numToSI from "./num-to-si"; import poll from "./poll"; import randomStr from "./random-str"; import removeAuthParam from "./remove-auth-param"; -import returnStringFromString from "./return-str"; import sleep from "./sleep"; import validEmail from "./valid-email"; import validInstanceTLD from "./valid-instance-tld"; import validTitle from "./valid-title"; import validURL from "./valid-url"; -import { escapeHTML, unescapeHTML } from "./html-entities"; export { capitalizeFirstLetter, debounce, editListImmutable, - escapeHTML, formatPastDate, futureDaysToUnixTime, getIdFromString, @@ -48,9 +45,7 @@ export { poll, randomStr, removeAuthParam, - returnStringFromString, sleep, - unescapeHTML, validEmail, validInstanceTLD, validTitle,