summaryrefslogtreecommitdiffstats
path: root/browser/components/newtab/components/CustomElements/paragraph.js
blob: 570183667e32d32169180523b2aabe58506f5b5b (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
/* 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 is loaded into all XUL windows. Wrap in a block to prevent
// leaking to window scope.
{
  const { RemoteL10n } = ChromeUtils.importESModule(
    "resource://activity-stream/lib/RemoteL10n.sys.mjs"
  );
  class MozTextParagraph extends HTMLElement {
    constructor() {
      super();

      this._content = null;
    }

    get fluentAttributeValues() {
      const attributes = {};
      for (let name of this.getAttributeNames()) {
        if (name.startsWith("fluent-variable-")) {
          let value = this.getAttribute(name);
          // Attribute value is a string, in some cases that is not useful
          // for example instantiating a Date object will fail. We try to
          // convert all possible integers back.
          if (value.match(/^\d+/)) {
            value = parseInt(value, 10);
          }
          attributes[name.replace(/^fluent-variable-/, "")] = value;
        }
      }

      return attributes;
    }

    render() {
      if (this.getAttribute("fluent-remote-id") && this._content) {
        RemoteL10n.l10n.setAttributes(
          this._content,
          this.getAttribute("fluent-remote-id"),
          this.fluentAttributeValues
        );
      }
    }

    static get observedAttributes() {
      return ["fluent-remote-id"];
    }

    attributeChangedCallback(name, oldValue, newValue) {
      this.render();
    }

    connectedCallback() {
      if (this.shadowRoot) {
        this.render();
        return;
      }

      const shadowRoot = this.attachShadow({ mode: "open" });
      this._content = document.createElement("span");
      shadowRoot.appendChild(this._content);

      this.render();
      RemoteL10n.l10n.translateFragment(this._content);
    }
  }

  customElements.define("remote-text", MozTextParagraph);
}