fix: html sensitization function (#2552)

This commit is contained in:
Dakshesh Jain 2023-10-30 13:59:00 +05:30 committed by GitHub
parent fc82d6fc23
commit 8cc61bc427
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 7 deletions

View File

@ -11,7 +11,7 @@ import { ArchiveIcon, CustomMenu, Tooltip } from "@plane/ui";
import { ArchiveRestore, Clock, MessageSquare, User2 } from "lucide-react"; import { ArchiveRestore, Clock, MessageSquare, User2 } from "lucide-react";
// helper // helper
import { stripHTML, replaceUnderscoreIfSnakeCase, truncateText } from "helpers/string.helper"; import { replaceUnderscoreIfSnakeCase, truncateText, stripAndTruncateHTML } from "helpers/string.helper";
import { import {
formatDateDistance, formatDateDistance,
render12HourFormatTime, render12HourFormatTime,
@ -115,10 +115,10 @@ export const NotificationCard: React.FC<NotificationCardProps> = (props) => {
renderShortDateWithYearFormat(notification.data.issue_activity.new_value) renderShortDateWithYearFormat(notification.data.issue_activity.new_value)
) : notification.data.issue_activity.field === "attachment" ? ( ) : notification.data.issue_activity.field === "attachment" ? (
"the issue" "the issue"
) : stripHTML(notification.data.issue_activity.new_value).length > 55 ? ( ) : notification.data.issue_activity.field === "description" ? (
stripHTML(notification.data.issue_activity.new_value).slice(0, 50) + "..." stripAndTruncateHTML(notification.data.issue_activity.new_value, 55)
) : ( ) : (
stripHTML(notification.data.issue_activity.new_value) notification.data.issue_activity.new_value
) )
) : ( ) : (
<span> <span>

View File

@ -111,11 +111,20 @@ export const getFirstCharacters = (str: string) => {
*/ */
export const stripHTML = (html: string) => { export const stripHTML = (html: string) => {
const tmp = document.createElement("DIV"); const strippedText = html.replace(/<script[^>]*>[\s\S]*?<\/script>/gi, ""); // Remove script tags
tmp.innerHTML = html; return strippedText.replace(/<[^>]*>/g, ""); // Remove all other HTML tags
return tmp.textContent || tmp.innerText || "";
}; };
/**
*
* @example:
* const html = "<p>Some text</p>";
* const text = stripAndTruncateHTML(html);
* console.log(text); // Some text
*/
export const stripAndTruncateHTML = (html: string, length: number = 55) => truncateText(stripHTML(html), length);
/** /**
* @description: This function return number count in string if number is more than 100 then it will return 99+ * @description: This function return number count in string if number is more than 100 then it will return 99+
* @param {number} number * @param {number} number