From 84d31a1fae66345651def9cf4ef9daf8d9cecf2d Mon Sep 17 00:00:00 2001 From: Sanjay Dhavanam Date: Sun, 17 May 2026 16:26:48 +0530 Subject: [PATCH] fix: prevent invalid rich text payload submissions --- client/src/pages/AskQuestionPage.jsx | 78 ++++++++++++++++++++++------ 1 file changed, 61 insertions(+), 17 deletions(-) diff --git a/client/src/pages/AskQuestionPage.jsx b/client/src/pages/AskQuestionPage.jsx index 150c765..07f3d21 100644 --- a/client/src/pages/AskQuestionPage.jsx +++ b/client/src/pages/AskQuestionPage.jsx @@ -14,6 +14,7 @@ const AskQuestionPage = () => { const [tagInput, setTagInput] = useState('') const [content, setContent] = useState('') const [isSubmitting, setIsSubmitting] = useState(false) + const navigate = useNavigate() const queryClient = useQueryClient() @@ -50,6 +51,7 @@ const AskQuestionPage = () => { const addTag = () => { const tag = tagInput.trim().toLowerCase() + if (tag && !tags.includes(tag) && tags.length < 5) { setTags([...tags, tag]) setTagInput('') @@ -57,21 +59,28 @@ const AskQuestionPage = () => { } const removeTag = (tagToRemove) => { - setTags(tags.filter(tag => tag !== tagToRemove)) + setTags(tags.filter((tag) => tag !== tagToRemove)) + } + + const getPlainTextContent = () => { + return content.replace(/<[^>]*>/g, '').trim() } const onSubmit = async (data) => { + const plainTextContent = getPlainTextContent() + if (tags.length === 0) { toast.error('Please add at least one tag') return } - if (content.trim().length < 20) { + if (plainTextContent.length < 20) { toast.error('Question content must be at least 20 characters') return } setIsSubmitting(true) + try { await createQuestionMutation.mutateAsync({ title: data.title, @@ -85,23 +94,30 @@ const AskQuestionPage = () => { const quillModules = { toolbar: [ - [{ 'header': [1, 2, 3, false] }], + [{ header: [1, 2, 3, false] }], ['bold', 'italic', 'underline', 'strike'], - [{ 'list': 'ordered'}, { 'list': 'bullet' }], - [{ 'color': [] }, { 'background': [] }], - [{ 'align': [] }], + [{ list: 'ordered' }, { list: 'bullet' }], + [{ color: [] }, { background: [] }], + [{ align: [] }], ['link', 'image', 'code-block'], - ['clean'] + ['clean'], ], } const quillFormats = [ 'header', - 'bold', 'italic', 'underline', 'strike', - 'list', 'bullet', - 'color', 'background', + 'bold', + 'italic', + 'underline', + 'strike', + 'list', + 'bullet', + 'color', + 'background', 'align', - 'link', 'image', 'code-block' + 'link', + 'image', + 'code-block', ] return ( @@ -116,6 +132,7 @@ const AskQuestionPage = () => {

Ask a Question

+

Share your knowledge and help others learn

@@ -124,9 +141,13 @@ const AskQuestionPage = () => {
{/* Title */}
-
@@ -182,6 +210,7 @@ const AskQuestionPage = () => { +
{ className="input-field flex-1" maxLength={20} /> +
@@ -240,6 +278,7 @@ const AskQuestionPage = () => {

Writing a good question

+
  • • Be specific and provide context
  • • Include code examples if relevant
  • @@ -258,9 +297,14 @@ const AskQuestionPage = () => { > Cancel +