summaryrefslogtreecommitdiffstats
path: root/toolkit/components/translations/tests/browser/browser_translations_shadow_dom.js
blob: 86c8d8b33f957e9f7a4000cb9fbb8d21def247af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const URL =
  "https://example.com/browser/toolkit/components/translations/tests/browser/translations-tester-shadow-dom-es.html";

const URL_SLOT =
  "https://example.com/browser/toolkit/components/translations/tests/browser/translations-tester-shadow-dom-slot-es.html";

/**
 * Check that the translation feature works with ShadowDOM.
 */
add_task(async function test_shadow_dom_translation() {
  await autoTranslatePage({
    page: URL,
    languagePairs: [
      { fromLang: "es", toLang: "en" },
      { fromLang: "en", toLang: "es" },
    ],
    runInPage: async TranslationsTest => {
      await TranslationsTest.assertTranslationResult(
        "Text outside of the Shadow DOM is translated",
        function () {
          return content.document.querySelector("h1");
        },
        "ESTO SE CONTENTA EN LUZ DOM [es to en, html]"
      );

      await TranslationsTest.assertTranslationResult(
        "The content in the Shadow DOM is translated.",
        function () {
          const root = content.document.getElementById("host").shadowRoot;
          return root.querySelector("p");
        },
        "ESTO SE CONTENTO EN SHADOW DOM [es to en, html]"
      );

      await TranslationsTest.assertTranslationResult(
        "Content in the interior root of a Shadow DOM is translated.",
        function () {
          const outerRoot = content.document.getElementById("host").shadowRoot;
          const innerRoot = outerRoot.querySelector("div").shadowRoot;
          return innerRoot.querySelector("p");
        },
        "ESTO SE CONTENTA EN RAÍZ INTERIOR [es to en, html]"
      );

      await TranslationsTest.assertTranslationResult(
        "Content in the Shaodw DOM where the host element is inside an empty textContent element is translated.",
        function () {
          const root = content.document.getElementById("host2").shadowRoot;
          return root.querySelector("p");
        },
        "ESTO SE CONTENTO EN SHADOW DOM 2 [es to en, html]"
      );
    },
  });
});

/**
 * Check that the translation feature works with ShadowDOM with slotted text node.
 */
add_task(async function test_shadow_dom_translation_slotted() {
  await autoTranslatePage({
    page: URL_SLOT,
    languagePairs: [
      { fromLang: "es", toLang: "en" },
      { fromLang: "en", toLang: "es" },
    ],
    runInPage: async TranslationsTest => {
      await TranslationsTest.assertTranslationResult(
        "Slotted text node is translated",
        function () {
          return content.document.getElementById("host");
        },
        "ESTO SE CONTENTA EN LUZ DOM [es to en]"
      );
    },
  });
});