Skip to content

Commit b53d715

Browse files
fix(client): fix speaking-modal showing span tags (freeCodeCamp#64759)
Co-authored-by: Huyen Nguyen <[email protected]>
1 parent c147f72 commit b53d715

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed

client/src/templates/Challenges/components/multiple-choice-questions.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { SuperBlocks } from '../../../../../shared/config/curriculum';
1111
import SpeakingModal from './speaking-modal';
1212
import ChallengeHeading from './challenge-heading';
1313
import PrismFormatted from './prism-formatted';
14+
import { stripHtmlTags } from './speaking-modal-helpers';
1415

1516
type MultipleChoiceQuestionsProps = {
1617
questions: Question[];
@@ -41,16 +42,12 @@ function MultipleChoiceQuestions({
4142
const [modalAnswerIndex, setModalAnswerIndex] = useState<number>(0);
4243
const [modalQuestionIndex, setModalQuestionIndex] = useState<number>(0);
4344

44-
function stripCodeTags(text: string): string {
45-
return text.replace(/<code>(.*?)<\/code>/g, '$1');
46-
}
47-
4845
const handleSpeakingButtonClick = (
4946
answer: string,
5047
answerIndex: number,
5148
questionIndex: number
5249
) => {
53-
setModalText(stripCodeTags(removeParagraphTags(answer)));
50+
setModalText(stripHtmlTags(answer));
5451
setModalAnswerIndex(answerIndex);
5552
setModalQuestionIndex(questionIndex);
5653
openSpeakingModal();

client/src/templates/Challenges/components/speaking-modal-helpers.test.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { describe, it, expect } from 'vitest';
2-
import { normalizeText, compareTexts } from './speaking-modal-helpers';
2+
import {
3+
normalizeText,
4+
compareTexts,
5+
stripHtmlTags
6+
} from './speaking-modal-helpers';
37

48
describe('speaking-modal-helpers', () => {
59
describe('normalizeText', () => {
@@ -231,4 +235,38 @@ describe('speaking-modal-helpers', () => {
231235
});
232236
});
233237
});
238+
239+
describe('stripHtmlTags', () => {
240+
it('should remove HTML tags and attributes', () => {
241+
expect(stripHtmlTags('<code>hello</code>')).toBe('hello');
242+
expect(stripHtmlTags('<span>world</span>')).toBe('world');
243+
expect(stripHtmlTags('<p>Text content</p>')).toBe('Text content');
244+
expect(
245+
stripHtmlTags('<span id="text" class="highlighted-text">Nǐ hǎo</span>')
246+
).toBe('Nǐ hǎo');
247+
});
248+
249+
it('should handle nested tags', () => {
250+
expect(stripHtmlTags('<p>Hello <span>world</span></p>')).toBe(
251+
'Hello world'
252+
);
253+
expect(stripHtmlTags('<p><span class="highlight">text</span></p>')).toBe(
254+
'text'
255+
);
256+
});
257+
258+
it('should handle text without tags', () => {
259+
expect(stripHtmlTags('plain text')).toBe('plain text');
260+
});
261+
262+
it('should handle empty string', () => {
263+
expect(stripHtmlTags('')).toBe('');
264+
});
265+
266+
it('should remove tags but preserve spacing', () => {
267+
expect(stripHtmlTags('Hello <code>world</code> today')).toBe(
268+
'Hello world today'
269+
);
270+
});
271+
});
234272
});

client/src/templates/Challenges/components/speaking-modal-helpers.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,7 @@ function alignWords(
181181
}));
182182
}
183183
}
184+
185+
export const stripHtmlTags = (text: string): string => {
186+
return text.replace(/<[^>]*>/g, '');
187+
};

0 commit comments

Comments
 (0)