Skip to content

Commit

Permalink
decode html entities
Browse files Browse the repository at this point in the history
  • Loading branch information
jemikanegara committed Jul 24, 2024
1 parent 9edfc7c commit e6229de
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,23 @@ function getTagName(node) {
}
}

function decodeHTMLEntities(text = "") {
let textArea = document.createElement('textarea');
textArea.innerHTML = text;
let decodedText = textArea.value;
textArea = null; // Nullify the reference, so garbage collection can take care of it
return decodedText;
}


function updateNode(node, language, type = "text", debugSource) {
// console.log("update node", debugSource, node, node.textContent, language);

// update title
if (node == document) {
const newText = window.translationCache?.[window.location.pathname]?.[language]?.[document.title] || "";
if (newText && !newText.includes(DEFAULT_UNTRANSLATED_VALUE)) {
document.title = newText;
document.title = decodeHTMLEntities(newText);
}
return;
}
Expand All @@ -116,7 +125,7 @@ function updateNode(node, language, type = "text", debugSource) {
if (node.tagName == "META") {
const newText = window.translationCache?.[window.location.pathname]?.[language]?.[node.content] || "";
if (newText && !newText.includes(DEFAULT_UNTRANSLATED_VALUE)) {
node.content = newText;
node.content = decodeHTMLEntities(newText);
}
return;
}
Expand All @@ -127,11 +136,11 @@ function updateNode(node, language, type = "text", debugSource) {
const newTitle = window.translationCache?.[window.location.pathname]?.[language]?.[node.title] || "";

if (newAlt && !newAlt.includes(DEFAULT_UNTRANSLATED_VALUE)) {
node.alt = newAlt;
node.alt = decodeHTMLEntities(newAlt);
}

if (newTitle && !newTitle.includes(DEFAULT_UNTRANSLATED_VALUE)) {
node.title = newTitle;
node.title = decodeHTMLEntities(newTitle);
}
return;
}
Expand All @@ -140,15 +149,15 @@ function updateNode(node, language, type = "text", debugSource) {
if (type == "seo" && node.tagName == "A") {
const newTitle = window.translationCache?.[window.location.pathname]?.[language]?.[node.title] || "";
if (newTitle && !newTitle.includes(DEFAULT_UNTRANSLATED_VALUE)) {
node.title = newTitle;
node.title = decodeHTMLEntities(newTitle);
}
return;
}

if (type == "form" && (node.tagName == "TEXTAREA" || node.tagName == "INPUT")) {
const newPlaceholder = window.translationCache?.[window.location.pathname]?.[language]?.[node.placeholder] || "";
if (newPlaceholder && !newPlaceholder.includes(DEFAULT_UNTRANSLATED_VALUE)) {
node.placeholder = newPlaceholder;
node.placeholder = decodeHTMLEntities(newPlaceholder);
}
return;
}
Expand Down Expand Up @@ -295,7 +304,7 @@ function updateNode(node, language, type = "text", debugSource) {
// make sure text is still the same before replacing
if (node.textContent == text && newValue != text) {
// console.log("node.textContent replace", node.textContent, text, newValue)
node.textContent = newValue; // TODO: right now we only replace based on translation position, later we should swap the node position to preserve the styles
node.textContent = decodeHTMLEntities(newValue); // TODO: right now we only replace based on translation position, later we should swap the node position to preserve the styles
}
}
} catch(err) {
Expand All @@ -315,7 +324,7 @@ function updateNode(node, language, type = "text", debugSource) {
// console.log("isTextStillTheSame", node.textContent == text)
// make sure text is still the same before replacing
if(node.textContent == text && newText != text) {
node.textContent = newText;
node.textContent = decodeHTMLEntities(newText);
}
}
}
Expand Down

0 comments on commit e6229de

Please sign in to comment.