summaryrefslogtreecommitdiffstats
path: root/browser/extensions/formautofill/content/l10n.js
blob: 85346d41ee7513e07bfb47b2cec252cbfd7b6e4d (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

/**
 * This file will be replaced by Fluent but it's a middle ground so we can share
 * the edit dialog code with the unprivileged PaymentRequest dialog before the
 * Fluent conversion
 */

const { FormAutofillUtils } = ChromeUtils.import(
  "resource://formautofill/FormAutofillUtils.jsm"
);

const CONTENT_WIN = typeof window != "undefined" ? window : this;

const L10N_ATTRIBUTES = ["data-localization", "data-localization-region"];

// eslint-disable-next-line mozilla/balanced-listeners
CONTENT_WIN.addEventListener("DOMContentLoaded", function onDCL(evt) {
  let doc = evt.target;
  FormAutofillUtils.localizeMarkup(doc);

  let mutationObserver = new doc.ownerGlobal.MutationObserver(
    function onMutation(mutations) {
      for (let mutation of mutations) {
        switch (mutation.type) {
          case "attributes": {
            if (!mutation.target.hasAttribute(mutation.attributeName)) {
              // The attribute was removed in the meantime.
              continue;
            }
            FormAutofillUtils.localizeAttributeForElement(
              mutation.target,
              mutation.attributeName
            );
            break;
          }

          case "childList": {
            // We really only care about elements appending inside pages.
            if (!mutation.addedNodes || !mutation.target.closest(".page")) {
              break;
            }
            FormAutofillUtils.localizeMarkup(mutation.target);
            break;
          }
        }
      }
    }
  );

  mutationObserver.observe(doc, {
    attributes: true,
    attributeFilter: L10N_ATTRIBUTES,
    childList: true,
    subtree: true,
  });
});