Skip to content

Commit

Permalink
Merge pull request #4770 from thematters/fix/trim-moment-empty-content
Browse files Browse the repository at this point in the history
fix(Moment): sanitize moment content
  • Loading branch information
wlliaml authored Aug 23, 2024
2 parents 1c9f4a5 + 4894fef commit 514545c
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 24 deletions.
13 changes: 0 additions & 13 deletions src/common/utils/comment/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,6 @@ export function filterResponses<T extends Response | Comment>(
})
}

export function trimCommentContent(content: string) {
// clear empty p tag
let trimContent = content.replace(/^(<p>\s*<\/p>)+|(<p>\s*<\/p>)+$/g, '')
// clear empty line and space at the beginning
trimContent = trimContent.replace(/^(<p>(<br class="smart">|\s)+)/g, '<p>')
// clear empty line and space at the end
trimContent = trimContent.replace(
/((<br class="smart">|\s)+<\/p>)+$/g,
'</p>'
)
return trimContent
}

export const highlightComment = (
targetElement: HTMLElement,
isParentComment?: boolean,
Expand Down
16 changes: 16 additions & 0 deletions src/common/utils/text/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,19 @@ export const truncate = (
str.length - suffixLen
)}`
}

export function sanitizeContent(content: string) {
// clear empty p tag
let sanitizedContent = content.replace(/^(<p>\s*<\/p>)+|(<p>\s*<\/p>)+$/g, '')
// clear empty line and space at the beginning
sanitizedContent = sanitizedContent.replace(
/^(<p>(<br class="smart">|\s)+)/g,
'<p>'
)
// clear empty line and space at the end
sanitizedContent = sanitizedContent.replace(
/((<br class="smart">|\s)+<\/p>)+$/g,
'</p>'
)
return sanitizedContent
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useContext, useEffect, useRef, useState } from 'react'
import { FormattedMessage } from 'react-intl'

import { MAX_ARTICLE_COMMENT_LENGTH } from '~/common/enums'
import { dom, formStorage, stripHtml, trimCommentContent } from '~/common/utils'
import { dom, formStorage, sanitizeContent, stripHtml } from '~/common/utils'
import {
Dialog,
SpinnerBlock,
Expand Down Expand Up @@ -78,10 +78,10 @@ const CommentForm: React.FC<CommentFormProps> = ({

const handleSubmit = async (event?: React.FormEvent<HTMLFormElement>) => {
const mentions = dom.getAttributes('data-id', content)
const trimContent = trimCommentContent(content)
const sanitizedContent = sanitizeContent(content)
const input = {
comment: {
content: trimContent,
content: sanitizedContent,
replyTo: replyToId,
articleId,
parentId,
Expand Down
6 changes: 3 additions & 3 deletions src/components/Forms/ArticleCommentForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
OPEN_UNIVERSAL_AUTH_DIALOG,
UNIVERSAL_AUTH_TRIGGER,
} from '~/common/enums'
import { dom, formStorage, stripHtml, trimCommentContent } from '~/common/utils'
import { dom, formStorage, sanitizeContent, stripHtml } from '~/common/utils'
import {
Button,
SpinnerBlock,
Expand Down Expand Up @@ -93,11 +93,11 @@ export const ArticleCommentForm: React.FC<ArticleCommentFormProps> = ({

const handleSubmit = async (event?: React.FormEvent<HTMLFormElement>) => {
const mentions = dom.getAttributes('data-id', content)
const trimContent = trimCommentContent(content)
const sanitizedContent = sanitizeContent(content)
const input = {
id: commentId,
comment: {
content: trimContent,
content: sanitizedContent,
replyTo: replyToId,
articleId,
parentId,
Expand Down
6 changes: 3 additions & 3 deletions src/components/Forms/MomentCommentForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
UNIVERSAL_AUTH_TRIGGER,
UPDATE_NEWEST_MOMENT_COMMENT,
} from '~/common/enums'
import { formStorage, stripHtml, trimCommentContent } from '~/common/utils'
import { formStorage, sanitizeContent, stripHtml } from '~/common/utils'
import {
Button,
SpinnerBlock,
Expand Down Expand Up @@ -107,10 +107,10 @@ const MomentCommentForm = ({
const isValid = contentCount > 0 && contentCount <= MAX_MOMENT_COMMENT_LENGTH

const handleSubmit = async (event?: React.FormEvent<HTMLFormElement>) => {
const trimContent = trimCommentContent(content)
const sanitizedContent = sanitizeContent(content)
const input = {
comment: {
content: trimContent,
content: sanitizedContent,
momentId,
type: 'moment',
},
Expand Down
9 changes: 7 additions & 2 deletions src/components/Forms/MomentForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ import {
MAX_MOMENT_CONTENT_LENGTH,
OPEN_MOMENT_FORM,
} from '~/common/enums'
import { formStorage, parseFormSubmitErrors, stripHtml } from '~/common/utils'
import {
formStorage,
parseFormSubmitErrors,
sanitizeContent,
stripHtml,
} from '~/common/utils'
import {
Button,
ClearMomentDialog,
Expand Down Expand Up @@ -165,7 +170,7 @@ const MomentForm = ({ setFirstRendered }: MomentFormProps) => {
const { data } = await putMoment({
variables: {
input: {
content,
content: sanitizeContent(content),
assets: assets.map(({ assetId }) => assetId),
},
},
Expand Down

0 comments on commit 514545c

Please sign in to comment.