summaryrefslogtreecommitdiffstats
path: root/editor/libeditor/tests/test_abs_positioner_appearance.html
blob: c516b9c511613df7dac814400234e5d9da58e73b (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<!DOCTYPE html>
<html>
<head>
  <title>Test for absolute positioner appearance</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script src="/tests/SimpleTest/EventUtils.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<p id="display"></p>
<div id="content" style="display: none;">

</div>

<div id="editor" contenteditable></div>
<div id="clickaway" style="width: 3px; height: 3px;"></div>
<img src="green.png"><!-- for ensuring to load the image at first test of <img> case -->
<pre id="test">

<script class="testbody" type="application/javascript">
"use strict";

SimpleTest.waitForExplicitFinish();
SimpleTest.waitForFocus(async function() {
  async function waitForSelectionChange() {
    return new Promise(resolve => {
      document.addEventListener("selectionchange", () => {
        resolve();
      }, {once: true});
    });
  }

  let editor = document.getElementById("editor");
  let outOfEditor = document.getElementById("clickaway");

  async function testIfAppears() {
    const kTests = [
      { description: "absolute positioned <div>",
        innerHTML: "<div id=\"target\" style=\"position: absolute; top: 50px; left: 50px;\">positioned</div>",
        movable: true,
      },
      { description: "fixed positioned <div>",
        innerHTML: "<div id=\"target\" style=\"position: fixed; top: 50px; left: 50px;\">positioned</div>",
        movable: false,
      },
      { description: "relative positioned <div>",
        innerHTML: "<div id=\"target\" style=\"position: relative; top: 50px; left: 50px;\">positioned</div>",
        movable: false,
      },
    ];

    for (const kTest of kTests) {
      const kDescription = "testIfAppears, " + kTest.description + ": ";
      editor.innerHTML = kTest.innerHTML;
      let target = document.getElementById("target");

      document.execCommand("enableAbsolutePositionEditing", false, false);
      ok(!document.queryCommandState("enableAbsolutePositionEditing"),
         kDescription + "Absolute positioned element editor should be disabled by the call of execCommand");

      synthesizeMouseAtCenter(outOfEditor, {});
      let promiseSelectionChangeEvent1 = waitForSelectionChange();
      synthesizeMouseAtCenter(target, {});
      await promiseSelectionChangeEvent1;

      ok(!target.hasAttribute("_moz_abspos"),
         kDescription + "While enableAbsolutePositioner is disabled, positioner shouldn't appear");

      document.execCommand("enableAbsolutePositionEditing", false, true);
      ok(document.queryCommandState("enableAbsolutePositionEditing"),
         kDescription + "Absolute positioned element editor should be enabled by the call of execCommand");

      synthesizeMouseAtCenter(outOfEditor, {});
      let promiseSelectionChangeEvent2 = waitForSelectionChange();
      synthesizeMouseAtCenter(target, {});
      await promiseSelectionChangeEvent2;

      is(target.hasAttribute("_moz_abspos"), kTest.movable,
         kDescription + (kTest.movable ? "While enableAbsolutePositionEditing is enabled, positioner should appear" :
                                         "Even while enableAbsolutePositionEditing is enabled, positioner shouldn't appear"));

      document.execCommand("enableAbsolutePositionEditing", false, false);
      ok(!target.hasAttribute("_moz_abspos"),
         kDescription + "When enableAbsolutePositionEditing is disabled even while positioner is visible, positioner should disappear");

      document.execCommand("enableAbsolutePositionEditing", false, true);
      is(target.hasAttribute("_moz_abspos"), kTest.movable,
         kDescription + (kTest.movable ?
                           "When enableAbsolutePositionEditing is enabled when absolute positioned element is selected, positioner should appear" :
                           "Even if enableAbsolutePositionEditing is enabled when static positioned element is selected, positioner shouldn't appear"));
    }
  }

  async function testStyle() {
    // See HTMLEditor::GetTemporaryStyleForFocusedPositionedElement().
    const kTests = [
      { description: "background-color: transparent; color: white;",
        innerHTML: "<div id=\"target\" style=\"position: absolute; " +
                                              "top: 50%; left: 50%; " +
                                              "background-color: transparent; " +
                                              "color: white;\">positioned</div>",
        value: "black",
      },
      { description: "background-color: transparent; color: black;",
        innerHTML: "<div id=\"target\" style=\"position: absolute; " +
                                              "top: 50%; left: 50%; " +
                                              "background-color: transparent; " +
                                              "color: black;\">positioned</div>",
        value: "white",
      },
      { description: "background-color: black; color: white;",
        innerHTML: "<div id=\"target\" style=\"position: absolute; " +
                                              "top: 50%; left: 50%; " +
                                              "background-color: black; " +
                                              "color: white;\">positioned</div>",
        value: "",
      },
      { description: "background-color: white; color: black;",
        innerHTML: "<div id=\"target\" style=\"position: absolute; " +
                                              "top: 50%; left: 50%; " +
                                              "background-color: white; " +
                                              "color: black;\">positioned</div>",
        value: "",
      },
      { description: "background-image: green.png; background-color: black; color: white;",
        innerHTML: "<div id=\"target\" style=\"position: absolute; " +
                                              "top: 50%; left: 50%; " +
                                              "background-image: green.png; " +
                                              "background-color: black; " +
                                              "color: white;\">positioned</div>",
        value: "",
      },
      { description: "background-image: green.png; background-color: white; color: black;",
        innerHTML: "<div id=\"target\" style=\"position: absolute; " +
                                              "top: 50%; left: 50%; " +
                                              "background-image: green.png; " +
                                              "background-color: white; " +
                                              "color: black;\">positioned</div>",
        value: "",
      },
      { description: "background-image: green.png;",
        innerHTML: "<div id=\"target\" style=\"position: absolute; " +
                                              "top: 50%; left: 50%; " +
                                              "background-image: green.png;\">positioned</div>",
        value: "white", // XXX Why? background-image is not "none"...
      },
    ];

    document.execCommand("enableAbsolutePositionEditing", false, true);
    ok(document.queryCommandState("enableAbsolutePositionEditing"),
       "testStyle, Absolute positioned element editor should be enabled by the call of execCommand");

    for (const kTest of kTests) {
      const kDescription = "testStyle, " + kTest.description + ": ";

      editor.innerHTML = kTest.innerHTML;
      let target = document.getElementById("target");

      synthesizeMouseAtCenter(outOfEditor, {});
      let promiseSelectionChangeEvent = waitForSelectionChange();
      synthesizeMouseAtCenter(target, {});
      await promiseSelectionChangeEvent;

      is(target.getAttribute("_moz_abspos"), kTest.value,
         kDescription + "The value of _moz_abspos attribute is unexpected");
    }
  }

  await testIfAppears();
  await testStyle();

  SimpleTest.finish();
});
</script>
</pre>
</body>
</html>