summaryrefslogtreecommitdiffstats
path: root/widget/tests/file_ime_state_test_helper.js
blob: 0cee5c036f16ef09e272c09fea452cf4eb78f94f (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
"use strict";

function IsIMEOpenStateSupported() {
  // We support to control IME open state on Windows and Mac actually.  However,
  // we cannot test it on Mac if the current keyboard layout is not CJK. And also
  // we cannot test it on Win32 if the system didn't be installed IME. So,
  // currently we should not run the open state testing.
  return false;
}

/**
 * @param {Node} aNode
 */
function nodeIsInShadowDOM(aNode) {
  for (let node = aNode; node; node = node.parentNode) {
    if (node instanceof ShadowRoot) {
      return true;
    }
    if (node == node.parentNode) {
      break;
    }
  }
  return false;
}

/**
 * @param {Node} aNode
 */
function nodeIsInDesignMode(aNode) {
  return (
    aNode.isConnected &&
    !nodeIsInShadowDOM(aNode) &&
    aNode.ownerDocument.designMode == "on"
  );
}

/**
 * param {Node} aNode
 */
function getEditingHost(aNode) {
  if (nodeIsInDesignMode(aNode)) {
    return aNode.ownerDocument.documentElement;
  }
  for (
    let element =
      aNode.nodeType == Node.ELEMENT_NODE ? aNode : aNode.parentElement;
    element;
    element = element.parentElement
  ) {
    const contenteditable = element.getAttribute("contenteditable");
    if (contenteditable === "true" || contenteditable === "") {
      return element;
    }
    if (contenteditable === "false") {
      return null;
    }
  }
  return null;
}

/**
 * @param {Node} aNode
 */
function nodeIsEditable(aNode) {
  if (nodeIsInDesignMode(aNode)) {
    return true;
  }
  if (!aNode.isConnected) {
    return false;
  }
  return getEditingHost(aNode) != null;
}

/**
 * @param {Element} aElement
 */
function elementIsEditingHost(aElement) {
  return (
    nodeIsEditable(aElement) &&
    (!aElement.parentElement || !getEditingHost(aElement) == aElement)
  );
}

/**
 * @returns {Element} Retrieve focused element.  If focused element is a element
 *                    in UA widget, this returns its host element.  E.g., when
 *                    a button in the controls of <audio> or <video> has focus,
 *                    this returns the <video> or <audio>.
 */
function getFocusedElementOrUAWidgetHost() {
  const focusedElement = SpecialPowers.focusManager.focusedElement;
  if (SpecialPowers.wrap(focusedElement)?.containingShadowRoot?.isUAWidget()) {
    return focusedElement.containingShadowRoot.host;
  }
  return focusedElement;
}

class TIPWrapper {
  #mTIP = null;
  #mFocusBlurNotifications = [];
  #mFocusBlurListener;
  #mWindow;

  constructor(aWindow) {
    this.#mWindow = aWindow;
    this.#mTIP = Cc["@mozilla.org/text-input-processor;1"].createInstance(
      Ci.nsITextInputProcessor
    );
    if (!this.beginInputTransactionForTests()) {
      this.#mTIP = null;
    }
  }

  beginInputTransactionForTests() {
    return this.#mTIP.beginInputTransactionForTests(
      this.#mWindow,
      this.#observer.bind(this)
    );
  }

  typeA() {
    const AKey = new this.#mWindow.KeyboardEvent("", {
      key: "a",
      code: "KeyA",
      keyCode: this.#mWindow.KeyboardEvent.DOM_VK_A,
    });
    this.#mTIP.keydown(AKey);
    this.#mTIP.keyup(AKey);
  }

  isAvailable() {
    return this.#mTIP != null;
  }

  #observer(aTIP, aNotification) {
    if (aTIP != this.#mTIP) {
      return false;
    }
    switch (aNotification.type) {
      case "request-to-commit":
        this.#mTIP.commitComposition();
        break;
      case "request-to-cancel":
        this.#mTIP.cancelComposition();
        break;
      case "notify-focus":
      case "notify-blur":
        this.#mFocusBlurNotifications.push(aNotification.type);
        if (this.#mFocusBlurListener) {
          this.#mFocusBlurListener(aNotification.type);
        }
        break;
    }
    return true;
  }

  get TIP() {
    return this.#mTIP;
  }

  /**
   * @param {Function} aListener
   */
  set onIMEFocusBlur(aListener) {
    this.#mFocusBlurListener = aListener;
  }

  get focusBlurNotifications() {
    return this.#mFocusBlurNotifications.concat();
  }

  get numberOfFocusNotifications() {
    return this.#mFocusBlurNotifications.filter(t => t == "notify-focus")
      .length;
  }
  get numberOfBlurNotifications() {
    return this.#mFocusBlurNotifications.filter(t => t == "notify-blur").length;
  }

  get IMEHasFocus() {
    return (
      !!this.#mFocusBlurNotifications.length &&
      this.#mFocusBlurNotifications[this.#mFocusBlurNotifications.length - 1] ==
        "notify-focus"
    );
  }

  clearFocusBlurNotifications() {
    this.#mFocusBlurNotifications = [];
  }

  destroy() {
    this.#mTIP = null;
    this.#mFocusBlurListener = null;
    this.#mFocusBlurNotifications = [];
  }
}