Skip to content

Commit

Permalink
fix: don't rend the HTML tags
Browse files Browse the repository at this point in the history
  • Loading branch information
zephyrxvxx7 committed Oct 4, 2022
1 parent fac1ccc commit e242a44
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions PolyglotSafariExtension/Sources/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,28 @@ function translationErrorHandler(message: UpstreamError) {
showError(message.error);
}

function convertHTMLToDisplayText(htmlString: string): string {
let encodedString = htmlString.replace(/&/g, "&");
encodedString = encodedString.replace(/</g, "&lt;");
encodedString = encodedString.replace(/>/g, "&gt;");
encodedString = encodedString.replace(/"/g, "&quot;");

return convertNewlineToHTML(encodedString);
}

function convertNewlineToHTML(text: string): string {
return text.replace(/\n/g, "<br/>");
}

function translationHandler(message: ReceivedTranslation): void {
if (message.id !== window.location.href) return;

const args = {
sourceLanguage: message.sourceLanguage || null,
translation: message.translation.replace(/\n/g, "<br/>"),
transliteration: message.transliteration.replace(/\n/g, "<br/>"),
sourceTransliteration: message.sourceTransliteration.replace(
/\n/g,
"<br/>"
translation: convertHTMLToDisplayText(message.translation),
transliteration: convertHTMLToDisplayText(message.transliteration),
sourceTransliteration: convertHTMLToDisplayText(
message.sourceTransliteration
),
synonyms: message.synonyms
? message.synonyms.map((synonym) => ({
Expand Down

0 comments on commit e242a44

Please sign in to comment.