Exclude .notranslate and [contenteditable] elements from translation (#478)

* Exclude `.notranslate` and `[contenteditable]` elements from translation

The `.notranslate` class is used by Google Translate to skip certain elements. And for obvious reasons we should not be changing the contents of elements with `contenteditable` enabled. This will fix the issue with composing tweets on Twitter.

* fix linter

Co-authored-by: Jelmer van der Linde <jelmer@ikhoefgeen.nl>
This commit is contained in:
Andre Natal 2022-07-28 12:09:13 -07:00 коммит произвёл GitHub
Родитель 4f908f4a27
Коммит 831d5ad5f4
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
1 изменённых файлов: 13 добавлений и 1 удалений

Просмотреть файл

@ -228,7 +228,7 @@ class InPageTranslation {
* pre-construct the excluded node selector. Doing it here since it
* needs to know `language`. See `containsExcludedNode()`.
*/
this.excludedNodeSelector = `[lang]:not([lang|="${this.language}"]),[translate=no],${Array.from(this.excludedTags).join(",")},#OTapp`;
this.excludedNodeSelector = `[lang]:not([lang|="${this.language}"]),[translate=no],.notranslate,[contenteditable],${Array.from(this.excludedTags).join(",")},#OTapp`;
for (let node of this.targetNodes) this.startTreeWalker(node);
@ -537,6 +537,18 @@ class InPageTranslation {
// we should explicitly exclude the outbound translations widget
if (node.id === "OTapp") return true;
/*
* exclude elements with the notranslate class which is also honoured
* by Google Translate
*/
if (node.classList.contains("notranslate")) return true;
/*
* exclude editable elements for the same reason we don't translate the
* contents of form input fields.
*/
if (node.contenteditable) return true;
return false;
}