diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/app/js/set_dom_strings.js | 76 |
1 files changed, 62 insertions, 14 deletions
diff --git a/src/app/js/set_dom_strings.js b/src/app/js/set_dom_strings.js index 000aba4..e039ebd 100644 --- a/src/app/js/set_dom_strings.js +++ b/src/app/js/set_dom_strings.js @@ -1,19 +1,67 @@ -// replaces strings in the HTML will language strings properly. This -// searches for %%<string>%%, aka, %%gui.exit%% will be replaced with -// "Exit", this works without issues. -module.exports = () => { - // finds %%%% strings - html = document.body.innerHTML.split("%%"); +function format_string(string) { + let parts = string.split("%%"); + + // basic checks to make sure `string` has lang strings + if (parts.length == 0 + || string.trim() == "" || ! string.match("%%")) { + return string; + } - for (let i = 0; i < html.length; i++) { + for (let i = 0; i < parts.length; i++) { // simply checks to make sure it is actually a lang string. - if (html[i][0] != " " && - html[i][html[i].length - 1] != " ") { - // Replaces it with it's string - html[i] = lang(html[i]); + if (parts[i][0] != " " && + parts[i][parts[i].length - 1] != " ") { + + // get string + let lang_str = lang(parts[i]); + + // make sure we got a string back, and if not, do nothing + if (typeof lang_str !== "string") { + continue; + } + + // replace this part with the lang string + parts[i] = lang_str; } } - - // replaces the original HTML with the translated/replaced HTML - document.body.innerHTML = html.join(""); + + // return finalized formatted string + return parts.join(""); +} + +// runs `format_string()` on `el`'s attributes, text nodes and children +function replace_in_el(el) { + // we don't want to mess with script tags + if (el.tagName == "SCRIPT") {return} + + let attributes = el.getAttributeNames(); + + // run through child nodes + for (let i = 0; i < el.childNodes.length; i++) { + // if the node isn't a text node, we do nothing + if (el.childNodes[i].nodeType != Node.TEXT_NODE) { + continue; + } + + // the node is a text node, so we set its `.textContent` by + // running `format_string()` on it + el.childNodes[i].textContent = + format_string(el.childNodes[i].textContent) + } + + // run through attributes and run `format_string()` on their values + for (let i = 0; i < attributes.length; i++) { + let attr = el.getAttribute(attributes[i]); + el.setAttribute(attributes[i], format_string(attr)) + } + + // run `replace_in_el()` on `el`'s children + for (let i = 0; i < el.children.length; i++) { + replace_in_el(el.children[i]); + } +} + +// replaces lang strings on (almost) all the elements inside `<body>` +module.exports = () => { + replace_in_el(document.body); } |