summaryrefslogtreecommitdiffstats
path: root/dom/l10n/tests/mochitest/l10n_overlays/test_attributes.html
blob: 3d1f6048b2126d4526b44b42b3885680884ce41a (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
83
84
85
86
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Test L10nOverlays Top-level attributes</title>
  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
  <script type="application/javascript">
  /* global L10nOverlays */
  "use strict";

  function elem(name) {
    return function(str) {
      const element = document.createElement(name);
      // eslint-disable-next-line no-unsanitized/property
      element.innerHTML = str;
      return element;
    };
  }

  const { translateElement } = L10nOverlays;

  {
    // Allowed attribute
    const element = elem("div")``;
    const translation = {
      value: null,
      attributes: [
        {name: "title", value: "FOO"},
      ],
    };
    translateElement(element, translation);
    is(element.outerHTML, '<div title="FOO"></div>');
  }

  {
    // Forbidden attribute
    const element = elem("input")``;
    const translation = {
      value: null,
      attributes: [
        {name: "disabled", value: "DISABLED"},
      ],
    };
    translateElement(element, translation);
    is(element.outerHTML, "<input>");
  }

  {
    // Attributes do not leak on first translation
    const element = elem("div")`Foo`;
    element.setAttribute("title", "Title");

    const translation = {
      value: "FOO",
      attributes: null,
    };
    translateElement(element, translation);
    is(element.outerHTML, "<div>FOO</div>");
  }

  {
    // Attributes do not leak on retranslation
    const element = elem("div")`Foo`;

    const translationA = {
      value: "FOO A",
      attributes: [
        {name: "title", value: "TITLE A"},
      ],
    };

    const translationB = {
      value: "FOO B",
      attributes: null,
    };
    translateElement(element, translationA);
    is(element.outerHTML, '<div title="TITLE A">FOO A</div>');
    translateElement(element, translationB);
    is(element.outerHTML, "<div>FOO B</div>");
  }
  </script>
</head>
<body>
</body>
</html>