fix: ignore altText to avoid replace " with "

- ignore altText to avoid replacing " with "(double quotes) in alt text value
- modify unit tests to cover this new code
This commit is contained in:
Muhammad Faraz Maqsood
2025-04-08 16:35:33 +05:00
committed by Muhammad Faraz Maqsood
parent 7ceeb32820
commit 0ddcbbb7a5
2 changed files with 40 additions and 1 deletions

View File

@@ -24,9 +24,41 @@ export const prepareShowBtnEscapeHTML = () => {
};
export const cleanHTML = ({ initialText }) => {
const altRegex = /(alt="(.*?)")/g;
const altTextMap = {};
let altTextIndex = 0;
// Replace alt attributes with ALT_TEXT_0/1/2...
const initialTextWithoutAltAttr = initialText.replace(altRegex, (match) => {
const altText = `ALT_TEXT_${altTextIndex++}`;
altTextMap[altText] = match;
return altText;
});
// Replace HTML entities in the rest of the text, excluding altTexts
const translateRegex = new RegExp(`&(${Object.keys(alphanumericMap).join('|')});`, 'g');
const translator = ($0, $1) => alphanumericMap[$1];
return initialText.replace(translateRegex, translator);
const cleanedTextWithoutAltAttr = initialTextWithoutAltAttr.replace(translateRegex, translator);
// Clean altText matches in altTextMap, ignore "
const alphanumericMapForAltText = { ...alphanumericMap };
delete alphanumericMapForAltText.quot;
Object.keys(altTextMap).forEach((key) => {
const altTextValue = altTextMap[key];
const cleanedAltTextValue = altTextValue.replace(
/&(\w+);/g,
($0, $1) => alphanumericMapForAltText[$1] || $0,
);
altTextMap[key] = cleanedAltTextValue;
});
// Restore the original alt attributes from altTextMap
const finalCleanedText = Object.keys(altTextMap).reduce(
(text, altText) => text.replace(altText, altTextMap[altText]),
cleanedTextWithoutAltAttr,
);
return finalCleanedText;
};
export const syntaxChecker = ({ textArr, lang }) => {

View File

@@ -64,10 +64,17 @@ describe('CodeEditor', () => {
describe('cleanHTML', () => {
const dirtyText = `&${Object.keys(alphanumericMap).join('; , &')};`;
const cleanText = `${Object.values(alphanumericMap).join(' , ')}`;
const dirtyTextWithAlt = '<img src="image.png" alt="Description &le; and &ge; &quot;do not convert these to double quotes&quot; 1" /> and &le; and &ge;';
const cleanTextWithAlt = '<img src="image.png" alt="Description ≤ and ≥ &quot;do not convert these to double quotes&quot; 1" /> and ≤ and ≥';
it('escapes alphanumerics and sets them to be literals', () => {
expect(hooks.cleanHTML({ initialText: dirtyText })).toEqual(cleanText);
});
it('replaces alt attributes with placeholders and restores them', () => {
const result = hooks.cleanHTML({ initialText: dirtyTextWithAlt });
expect(result).toEqual(cleanTextWithAlt);
});
});
describe('escapeHTMLSpecialChars', () => {