2014-06-25 05:12:07 +00:00
|
|
|
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- /
|
2012-06-06 12:19:33 +00:00
|
|
|
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
|
|
|
|
/* 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/. */
|
|
|
|
|
2012-06-06 12:19:33 +00:00
|
|
|
"use strict";
|
2012-06-06 12:19:33 +00:00
|
|
|
|
2012-10-05 06:21:10 +00:00
|
|
|
dump("###################################### forms.js loaded\n");
|
|
|
|
|
2012-06-06 12:19:33 +00:00
|
|
|
let Ci = Components.interfaces;
|
|
|
|
let Cc = Components.classes;
|
|
|
|
let Cu = Components.utils;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
2012-06-26 14:01:53 +00:00
|
|
|
Cu.import('resource://gre/modules/XPCOMUtils.jsm');
|
2012-06-06 12:19:33 +00:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(Services, "fm",
|
|
|
|
"@mozilla.org/focus-manager;1",
|
|
|
|
"nsIFocusManager");
|
2012-06-06 12:19:33 +00:00
|
|
|
|
2012-11-07 11:53:24 +00:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "domWindowUtils", function () {
|
|
|
|
return content.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIDOMWindowUtils);
|
|
|
|
});
|
|
|
|
|
2012-11-09 17:39:23 +00:00
|
|
|
const RESIZE_SCROLL_DELAY = 20;
|
2014-03-20 10:37:00 +00:00
|
|
|
// In content editable node, when there are hidden elements such as <br>, it
|
|
|
|
// may need more than one (usually less than 3 times) move/extend operations
|
|
|
|
// to change the selection range. If we cannot change the selection range
|
|
|
|
// with more than 20 opertations, we are likely being blocked and cannot change
|
|
|
|
// the selection range any more.
|
|
|
|
const MAX_BLOCKED_COUNT = 20;
|
2012-11-07 11:53:24 +00:00
|
|
|
|
2013-01-04 15:00:21 +00:00
|
|
|
let HTMLDocument = Ci.nsIDOMHTMLDocument;
|
|
|
|
let HTMLHtmlElement = Ci.nsIDOMHTMLHtmlElement;
|
|
|
|
let HTMLBodyElement = Ci.nsIDOMHTMLBodyElement;
|
|
|
|
let HTMLIFrameElement = Ci.nsIDOMHTMLIFrameElement;
|
2012-06-06 12:19:33 +00:00
|
|
|
let HTMLInputElement = Ci.nsIDOMHTMLInputElement;
|
|
|
|
let HTMLTextAreaElement = Ci.nsIDOMHTMLTextAreaElement;
|
2012-06-06 12:19:33 +00:00
|
|
|
let HTMLSelectElement = Ci.nsIDOMHTMLSelectElement;
|
|
|
|
let HTMLOptGroupElement = Ci.nsIDOMHTMLOptGroupElement;
|
|
|
|
let HTMLOptionElement = Ci.nsIDOMHTMLOptionElement;
|
|
|
|
|
2013-01-04 15:00:21 +00:00
|
|
|
let FormVisibility = {
|
|
|
|
/**
|
|
|
|
* Searches upwards in the DOM for an element that has been scrolled.
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} node element to start search at.
|
|
|
|
* @return {Window|HTMLElement|Null} null when none are found window/element otherwise.
|
|
|
|
*/
|
|
|
|
findScrolled: function fv_findScrolled(node) {
|
|
|
|
let win = node.ownerDocument.defaultView;
|
|
|
|
|
|
|
|
while (!(node instanceof HTMLBodyElement)) {
|
|
|
|
|
|
|
|
// We can skip elements that have not been scrolled.
|
|
|
|
// We only care about top now remember to add the scrollLeft
|
|
|
|
// check if we decide to care about the X axis.
|
|
|
|
if (node.scrollTop !== 0) {
|
|
|
|
// the element has been scrolled so we may need to adjust
|
|
|
|
// where we think the root element is located.
|
|
|
|
//
|
|
|
|
// Otherwise it may seem visible but be scrolled out of the viewport
|
|
|
|
// inside this scrollable node.
|
|
|
|
return node;
|
|
|
|
} else {
|
|
|
|
// this node does not effect where we think
|
|
|
|
// the node is even if it is scrollable it has not hidden
|
|
|
|
// the element we are looking for.
|
|
|
|
node = node.parentNode;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// we also care about the window this is the more
|
|
|
|
// common case where the content is larger then
|
|
|
|
// the viewport/screen.
|
|
|
|
if (win.scrollMaxX || win.scrollMaxY) {
|
|
|
|
return win;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if "top and "bottom" points of the position is visible.
|
|
|
|
*
|
|
|
|
* @param {Number} top position.
|
|
|
|
* @param {Number} height of the element.
|
|
|
|
* @param {Number} maxHeight of the window.
|
|
|
|
* @return {Boolean} true when visible.
|
|
|
|
*/
|
|
|
|
yAxisVisible: function fv_yAxisVisible(top, height, maxHeight) {
|
|
|
|
return (top > 0 && (top + height) < maxHeight);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Searches up through the dom for scrollable elements
|
|
|
|
* which are not currently visible (relative to the viewport).
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} element to start search at.
|
|
|
|
* @param {Object} pos .top, .height and .width of element.
|
|
|
|
*/
|
|
|
|
scrollablesVisible: function fv_scrollablesVisible(element, pos) {
|
|
|
|
while ((element = this.findScrolled(element))) {
|
|
|
|
if (element.window && element.self === element)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// remember getBoundingClientRect does not care
|
|
|
|
// about scrolling only where the element starts
|
|
|
|
// in the document.
|
|
|
|
let offset = element.getBoundingClientRect();
|
|
|
|
|
|
|
|
// the top of both the scrollable area and
|
|
|
|
// the form element itself are in the same document.
|
|
|
|
// We adjust the "top" so if the elements coordinates
|
|
|
|
// are relative to the viewport in the current document.
|
|
|
|
let adjustedTop = pos.top - offset.top;
|
|
|
|
|
|
|
|
let visible = this.yAxisVisible(
|
|
|
|
adjustedTop,
|
|
|
|
pos.height,
|
|
|
|
pos.width
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!visible)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
element = element.parentNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Verifies the element is visible in the viewport.
|
|
|
|
* Handles scrollable areas, frames and scrollable viewport(s) (windows).
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} element to verify.
|
|
|
|
* @return {Boolean} true when visible.
|
|
|
|
*/
|
|
|
|
isVisible: function fv_isVisible(element) {
|
|
|
|
// scrollable frames can be ignored we just care about iframes...
|
|
|
|
let rect = element.getBoundingClientRect();
|
|
|
|
let parent = element.ownerDocument.defaultView;
|
|
|
|
|
|
|
|
// used to calculate the inner position of frames / scrollables.
|
|
|
|
// The intent was to use this information to scroll either up or down.
|
|
|
|
// scrollIntoView(true) will _break_ some web content so we can't do
|
|
|
|
// this today. If we want that functionality we need to manually scroll
|
|
|
|
// the individual elements.
|
|
|
|
let pos = {
|
|
|
|
top: rect.top,
|
|
|
|
height: rect.height,
|
|
|
|
width: rect.width
|
|
|
|
};
|
|
|
|
|
|
|
|
let visible = true;
|
|
|
|
|
|
|
|
do {
|
|
|
|
let frame = parent.frameElement;
|
|
|
|
visible = visible &&
|
|
|
|
this.yAxisVisible(pos.top, pos.height, parent.innerHeight) &&
|
|
|
|
this.scrollablesVisible(element, pos);
|
|
|
|
|
|
|
|
// nothing we can do about this now...
|
|
|
|
// In the future we can use this information to scroll
|
|
|
|
// only the elements we need to at this point as we should
|
|
|
|
// have all the details we need to figure out how to scroll.
|
|
|
|
if (!visible)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (frame) {
|
|
|
|
let frameRect = frame.getBoundingClientRect();
|
|
|
|
|
|
|
|
pos.top += frameRect.top + frame.clientTop;
|
|
|
|
}
|
|
|
|
} while (
|
|
|
|
(parent !== parent.parent) &&
|
|
|
|
(parent = parent.parent)
|
|
|
|
);
|
|
|
|
|
|
|
|
return visible;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-06-06 12:19:33 +00:00
|
|
|
let FormAssistant = {
|
|
|
|
init: function fa_init() {
|
2012-06-06 12:19:33 +00:00
|
|
|
addEventListener("focus", this, true, false);
|
2012-06-26 14:01:53 +00:00
|
|
|
addEventListener("blur", this, true, false);
|
2012-06-06 12:19:33 +00:00
|
|
|
addEventListener("resize", this, true, false);
|
2013-01-07 11:09:50 +00:00
|
|
|
addEventListener("submit", this, true, false);
|
|
|
|
addEventListener("pagehide", this, true, false);
|
2013-05-08 16:27:59 +00:00
|
|
|
addEventListener("beforeunload", this, true, false);
|
2013-03-15 12:28:51 +00:00
|
|
|
addEventListener("input", this, true, false);
|
|
|
|
addEventListener("keydown", this, true, false);
|
2013-05-16 12:21:07 +00:00
|
|
|
addEventListener("keyup", this, true, false);
|
2012-06-06 12:19:33 +00:00
|
|
|
addMessageListener("Forms:Select:Choice", this);
|
2012-06-06 12:19:33 +00:00
|
|
|
addMessageListener("Forms:Input:Value", this);
|
2012-10-17 11:18:14 +00:00
|
|
|
addMessageListener("Forms:Select:Blur", this);
|
2013-03-15 12:28:51 +00:00
|
|
|
addMessageListener("Forms:SetSelectionRange", this);
|
2013-04-13 14:53:41 +00:00
|
|
|
addMessageListener("Forms:ReplaceSurroundingText", this);
|
2013-08-08 00:07:41 +00:00
|
|
|
addMessageListener("Forms:GetText", this);
|
|
|
|
addMessageListener("Forms:Input:SendKey", this);
|
|
|
|
addMessageListener("Forms:GetContext", this);
|
2013-08-30 12:07:16 +00:00
|
|
|
addMessageListener("Forms:SetComposition", this);
|
|
|
|
addMessageListener("Forms:EndComposition", this);
|
2012-06-06 12:19:33 +00:00
|
|
|
},
|
|
|
|
|
2012-11-07 11:53:24 +00:00
|
|
|
ignoredInputTypes: new Set([
|
2013-08-12 17:17:29 +00:00
|
|
|
'button', 'file', 'checkbox', 'radio', 'reset', 'submit', 'image',
|
|
|
|
'range'
|
2012-11-07 11:53:24 +00:00
|
|
|
]),
|
|
|
|
|
2012-06-06 12:19:33 +00:00
|
|
|
isKeyboardOpened: false,
|
2013-03-15 12:28:51 +00:00
|
|
|
selectionStart: -1,
|
|
|
|
selectionEnd: -1,
|
2013-08-08 00:07:41 +00:00
|
|
|
textBeforeCursor: "",
|
|
|
|
textAfterCursor: "",
|
2012-11-09 17:39:23 +00:00
|
|
|
scrollIntoViewTimeout: null,
|
2012-10-24 18:09:12 +00:00
|
|
|
_focusedElement: null,
|
2013-08-08 00:07:41 +00:00
|
|
|
_focusCounter: 0, // up one for every time we focus a new element
|
2013-08-21 12:24:25 +00:00
|
|
|
_observer: null,
|
2013-01-01 11:26:49 +00:00
|
|
|
_documentEncoder: null,
|
2013-05-03 13:02:30 +00:00
|
|
|
_editor: null,
|
|
|
|
_editing: false,
|
2012-10-24 18:09:12 +00:00
|
|
|
|
|
|
|
get focusedElement() {
|
2012-10-24 23:30:14 +00:00
|
|
|
if (this._focusedElement && Cu.isDeadWrapper(this._focusedElement))
|
2012-10-24 18:09:12 +00:00
|
|
|
this._focusedElement = null;
|
2012-10-24 23:30:14 +00:00
|
|
|
|
2012-10-24 18:09:12 +00:00
|
|
|
return this._focusedElement;
|
|
|
|
},
|
|
|
|
|
|
|
|
set focusedElement(val) {
|
2013-08-08 00:07:41 +00:00
|
|
|
this._focusCounter++;
|
2012-10-24 18:09:12 +00:00
|
|
|
this._focusedElement = val;
|
|
|
|
},
|
|
|
|
|
2012-10-05 06:21:10 +00:00
|
|
|
setFocusedElement: function fa_setFocusedElement(element) {
|
2013-08-21 12:24:25 +00:00
|
|
|
let self = this;
|
|
|
|
|
2012-10-05 06:21:10 +00:00
|
|
|
if (element === this.focusedElement)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (this.focusedElement) {
|
|
|
|
this.focusedElement.removeEventListener('mousedown', this);
|
|
|
|
this.focusedElement.removeEventListener('mouseup', this);
|
2013-08-30 12:07:16 +00:00
|
|
|
this.focusedElement.removeEventListener('compositionend', this);
|
2013-08-21 12:24:25 +00:00
|
|
|
if (this._observer) {
|
|
|
|
this._observer.disconnect();
|
|
|
|
this._observer = null;
|
|
|
|
}
|
2012-10-17 11:18:14 +00:00
|
|
|
if (!element) {
|
|
|
|
this.focusedElement.blur();
|
|
|
|
}
|
2012-10-05 06:21:10 +00:00
|
|
|
}
|
|
|
|
|
2013-01-01 11:26:49 +00:00
|
|
|
this._documentEncoder = null;
|
2013-05-03 13:02:30 +00:00
|
|
|
if (this._editor) {
|
2013-07-16 10:00:58 +00:00
|
|
|
// When the nsIFrame of the input element is reconstructed by
|
|
|
|
// CSS restyling, the editor observers are removed. Catch
|
|
|
|
// [nsIEditor.removeEditorObserver] failure exception if that
|
|
|
|
// happens.
|
|
|
|
try {
|
|
|
|
this._editor.removeEditorObserver(this);
|
|
|
|
} catch (e) {}
|
2013-05-03 13:02:30 +00:00
|
|
|
this._editor = null;
|
|
|
|
}
|
2013-01-01 11:26:49 +00:00
|
|
|
|
2012-10-05 06:21:10 +00:00
|
|
|
if (element) {
|
|
|
|
element.addEventListener('mousedown', this);
|
|
|
|
element.addEventListener('mouseup', this);
|
2013-08-30 12:07:16 +00:00
|
|
|
element.addEventListener('compositionend', this);
|
2013-01-01 11:26:49 +00:00
|
|
|
if (isContentEditable(element)) {
|
|
|
|
this._documentEncoder = getDocumentEncoder(element);
|
|
|
|
}
|
2013-05-03 13:02:30 +00:00
|
|
|
this._editor = getPlaintextEditor(element);
|
|
|
|
if (this._editor) {
|
|
|
|
// Add a nsIEditorObserver to monitor the text content of the focused
|
|
|
|
// element.
|
|
|
|
this._editor.addEditorObserver(this);
|
|
|
|
}
|
2013-08-21 12:24:25 +00:00
|
|
|
|
|
|
|
// If our focusedElement is removed from DOM we want to handle it properly
|
|
|
|
let MutationObserver = element.ownerDocument.defaultView.MutationObserver;
|
|
|
|
this._observer = new MutationObserver(function(mutations) {
|
|
|
|
var del = [].some.call(mutations, function(m) {
|
|
|
|
return [].some.call(m.removedNodes, function(n) {
|
2013-12-03 13:37:38 +00:00
|
|
|
return n.contains(element);
|
2013-08-21 12:24:25 +00:00
|
|
|
});
|
|
|
|
});
|
2013-08-28 15:55:57 +00:00
|
|
|
if (del && element === self.focusedElement) {
|
2013-08-21 12:24:25 +00:00
|
|
|
// item was deleted, fake a blur so all state gets set correctly
|
|
|
|
self.handleEvent({ target: element, type: "blur" });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-12-03 13:37:38 +00:00
|
|
|
this._observer.observe(element.ownerDocument.body, {
|
|
|
|
childList: true,
|
|
|
|
subtree: true
|
2013-08-21 12:24:25 +00:00
|
|
|
});
|
2012-10-05 06:21:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.focusedElement = element;
|
|
|
|
},
|
|
|
|
|
2013-01-01 11:26:49 +00:00
|
|
|
get documentEncoder() {
|
|
|
|
return this._documentEncoder;
|
|
|
|
},
|
|
|
|
|
2013-04-13 14:53:41 +00:00
|
|
|
// Get the nsIPlaintextEditor object of current input field.
|
|
|
|
get editor() {
|
|
|
|
return this._editor;
|
|
|
|
},
|
|
|
|
|
2013-05-03 13:02:30 +00:00
|
|
|
// Implements nsIEditorObserver get notification when the text content of
|
|
|
|
// current input field has changed.
|
|
|
|
EditAction: function fa_editAction() {
|
|
|
|
if (this._editing) {
|
2013-06-24 00:43:40 +00:00
|
|
|
return;
|
2013-05-03 13:02:30 +00:00
|
|
|
}
|
|
|
|
this.sendKeyboardState(this.focusedElement);
|
|
|
|
},
|
|
|
|
|
2012-06-06 12:19:33 +00:00
|
|
|
handleEvent: function fa_handleEvent(evt) {
|
2012-06-06 12:19:33 +00:00
|
|
|
let target = evt.target;
|
|
|
|
|
2013-01-01 11:26:49 +00:00
|
|
|
let range = null;
|
2012-06-06 12:19:33 +00:00
|
|
|
switch (evt.type) {
|
2012-06-06 12:19:33 +00:00
|
|
|
case "focus":
|
2013-02-13 00:31:08 +00:00
|
|
|
if (!target) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-08-29 13:57:12 +00:00
|
|
|
// Focusing on Window, Document or iFrame should focus body
|
2013-09-03 01:56:14 +00:00
|
|
|
if (target instanceof HTMLHtmlElement) {
|
2013-08-29 13:57:12 +00:00
|
|
|
target = target.document.body;
|
2013-09-03 01:56:14 +00:00
|
|
|
} else if (target instanceof HTMLDocument) {
|
2013-08-29 13:57:12 +00:00
|
|
|
target = target.body;
|
2013-09-03 01:56:14 +00:00
|
|
|
} else if (target instanceof HTMLIFrameElement) {
|
|
|
|
target = target.contentDocument ? target.contentDocument.body
|
|
|
|
: null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!target) {
|
|
|
|
break;
|
|
|
|
}
|
2013-02-13 00:31:08 +00:00
|
|
|
|
|
|
|
if (isContentEditable(target)) {
|
2013-01-04 15:00:21 +00:00
|
|
|
this.showKeyboard(this.getTopLevelEditable(target));
|
2013-03-15 12:28:51 +00:00
|
|
|
this.updateSelection();
|
2013-01-04 15:00:21 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-06-06 12:19:33 +00:00
|
|
|
|
2013-03-15 12:28:51 +00:00
|
|
|
if (this.isFocusableElement(target)) {
|
2012-12-21 19:08:56 +00:00
|
|
|
this.showKeyboard(target);
|
2013-03-15 12:28:51 +00:00
|
|
|
this.updateSelection();
|
|
|
|
}
|
2012-06-26 14:01:53 +00:00
|
|
|
break;
|
|
|
|
|
2013-03-07 13:35:38 +00:00
|
|
|
case "pagehide":
|
2013-05-08 16:27:59 +00:00
|
|
|
case "beforeunload":
|
|
|
|
// We are only interested to the pagehide and beforeunload events from
|
|
|
|
// the root document.
|
2013-03-07 13:35:38 +00:00
|
|
|
if (target && target != content.document) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// fall through
|
2012-06-26 14:01:53 +00:00
|
|
|
case "blur":
|
2013-01-07 11:09:50 +00:00
|
|
|
case "submit":
|
2013-03-15 12:28:51 +00:00
|
|
|
if (this.focusedElement) {
|
2012-12-18 23:10:18 +00:00
|
|
|
this.hideKeyboard();
|
2013-03-15 12:28:51 +00:00
|
|
|
this.selectionStart = -1;
|
|
|
|
this.selectionEnd = -1;
|
|
|
|
}
|
2012-10-05 06:21:10 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'mousedown':
|
2013-08-14 17:54:36 +00:00
|
|
|
if (!this.focusedElement) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-10-05 06:21:10 +00:00
|
|
|
// We only listen for this event on the currently focused element.
|
|
|
|
// When the mouse goes down, note the cursor/selection position
|
2013-03-15 12:28:51 +00:00
|
|
|
this.updateSelection();
|
2012-10-05 06:21:10 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'mouseup':
|
2013-08-14 17:54:36 +00:00
|
|
|
if (!this.focusedElement) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-10-05 06:21:10 +00:00
|
|
|
// We only listen for this event on the currently focused element.
|
|
|
|
// When the mouse goes up, see if the cursor has moved (or the
|
|
|
|
// selection changed) since the mouse went down. If it has, we
|
|
|
|
// need to tell the keyboard about it
|
2013-01-01 11:26:49 +00:00
|
|
|
range = getSelectionRange(this.focusedElement);
|
|
|
|
if (range[0] !== this.selectionStart ||
|
|
|
|
range[1] !== this.selectionEnd) {
|
2013-03-15 12:28:51 +00:00
|
|
|
this.updateSelection();
|
2012-06-26 06:42:45 +00:00
|
|
|
}
|
2012-06-26 14:01:53 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "resize":
|
|
|
|
if (!this.isKeyboardOpened)
|
|
|
|
return;
|
2012-06-06 12:19:33 +00:00
|
|
|
|
2012-11-09 17:39:23 +00:00
|
|
|
if (this.scrollIntoViewTimeout) {
|
|
|
|
content.clearTimeout(this.scrollIntoViewTimeout);
|
|
|
|
this.scrollIntoViewTimeout = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We may receive multiple resize events in quick succession, so wait
|
|
|
|
// a bit before scrolling the input element into view.
|
2012-10-05 06:21:10 +00:00
|
|
|
if (this.focusedElement) {
|
2012-11-09 17:39:23 +00:00
|
|
|
this.scrollIntoViewTimeout = content.setTimeout(function () {
|
|
|
|
this.scrollIntoViewTimeout = null;
|
2013-01-04 15:00:21 +00:00
|
|
|
if (this.focusedElement && !FormVisibility.isVisible(this.focusedElement)) {
|
2013-12-13 21:10:56 +00:00
|
|
|
scrollSelectionOrElementIntoView(this.focusedElement);
|
2012-11-09 17:39:23 +00:00
|
|
|
}
|
|
|
|
}.bind(this), RESIZE_SCROLL_DELAY);
|
2012-06-26 14:01:53 +00:00
|
|
|
}
|
2012-06-06 12:19:33 +00:00
|
|
|
break;
|
2013-03-15 12:28:51 +00:00
|
|
|
|
|
|
|
case "input":
|
2013-08-12 17:17:29 +00:00
|
|
|
if (this.focusedElement) {
|
|
|
|
// When the text content changes, notify the keyboard
|
|
|
|
this.updateSelection();
|
|
|
|
}
|
2013-03-15 12:28:51 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "keydown":
|
2013-08-13 12:40:54 +00:00
|
|
|
if (!this.focusedElement) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-08-30 12:07:16 +00:00
|
|
|
CompositionManager.endComposition('');
|
|
|
|
|
2013-03-15 12:28:51 +00:00
|
|
|
// We use 'setTimeout' to wait until the input element accomplishes the
|
2013-05-16 12:21:07 +00:00
|
|
|
// change in selection range.
|
2013-03-15 12:28:51 +00:00
|
|
|
content.setTimeout(function() {
|
|
|
|
this.updateSelection();
|
|
|
|
}.bind(this), 0);
|
|
|
|
break;
|
2013-05-16 12:21:07 +00:00
|
|
|
|
|
|
|
case "keyup":
|
2013-08-13 12:40:54 +00:00
|
|
|
if (!this.focusedElement) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-08-30 12:07:16 +00:00
|
|
|
CompositionManager.endComposition('');
|
|
|
|
|
2013-05-16 12:21:07 +00:00
|
|
|
break;
|
2013-08-30 12:07:16 +00:00
|
|
|
|
|
|
|
case "compositionend":
|
|
|
|
if (!this.focusedElement) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
CompositionManager.onCompositionEnd();
|
|
|
|
break;
|
2012-06-06 12:19:33 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
receiveMessage: function fa_receiveMessage(msg) {
|
2012-10-05 06:21:10 +00:00
|
|
|
let target = this.focusedElement;
|
2013-08-08 00:07:41 +00:00
|
|
|
let json = msg.json;
|
|
|
|
|
|
|
|
// To not break mozKeyboard contextId is optional
|
|
|
|
if ('contextId' in json &&
|
|
|
|
json.contextId !== this._focusCounter &&
|
|
|
|
json.requestId) {
|
|
|
|
// Ignore messages that are meant for a previously focused element
|
|
|
|
sendAsyncMessage("Forms:SequenceError", {
|
|
|
|
requestId: json.requestId,
|
|
|
|
error: "Expected contextId " + this._focusCounter +
|
|
|
|
" but was " + json.contextId
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-26 14:01:53 +00:00
|
|
|
if (!target) {
|
2013-08-08 00:07:41 +00:00
|
|
|
switch (msg.name) {
|
|
|
|
case "Forms:GetText":
|
|
|
|
sendAsyncMessage("Forms:GetText:Result:Error", {
|
|
|
|
requestId: json.requestId,
|
|
|
|
error: "No focused element"
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
2012-06-06 12:19:33 +00:00
|
|
|
return;
|
2012-06-26 14:01:53 +00:00
|
|
|
}
|
2012-06-06 12:19:33 +00:00
|
|
|
|
2013-05-03 13:02:30 +00:00
|
|
|
this._editing = true;
|
2012-06-06 12:19:33 +00:00
|
|
|
switch (msg.name) {
|
2012-09-26 11:08:50 +00:00
|
|
|
case "Forms:Input:Value": {
|
2013-08-30 12:07:16 +00:00
|
|
|
CompositionManager.endComposition('');
|
|
|
|
|
2012-06-06 12:19:33 +00:00
|
|
|
target.value = json.value;
|
2012-09-26 11:08:50 +00:00
|
|
|
|
2013-04-26 10:17:25 +00:00
|
|
|
let event = target.ownerDocument.createEvent('HTMLEvents');
|
2012-09-26 11:08:50 +00:00
|
|
|
event.initEvent('input', true, false);
|
|
|
|
target.dispatchEvent(event);
|
2012-06-06 12:19:33 +00:00
|
|
|
break;
|
2012-09-26 11:08:50 +00:00
|
|
|
}
|
2012-06-06 12:19:33 +00:00
|
|
|
|
2013-08-08 00:07:41 +00:00
|
|
|
case "Forms:Input:SendKey":
|
2013-08-30 12:07:16 +00:00
|
|
|
CompositionManager.endComposition('');
|
|
|
|
|
2014-07-10 23:46:28 +00:00
|
|
|
let flags = domWindowUtils.KEY_FLAG_NOT_SYNTHESIZED_FOR_TESTS;
|
2013-12-03 13:37:38 +00:00
|
|
|
this._editing = true;
|
|
|
|
let doKeypress = domWindowUtils.sendKeyEvent('keydown', json.keyCode,
|
2014-07-10 23:46:28 +00:00
|
|
|
json.charCode, json.modifiers, flags);
|
2013-12-03 13:37:38 +00:00
|
|
|
if (doKeypress) {
|
|
|
|
domWindowUtils.sendKeyEvent('keypress', json.keyCode,
|
2014-07-10 23:46:28 +00:00
|
|
|
json.charCode, json.modifiers, flags);
|
2013-12-03 13:37:38 +00:00
|
|
|
}
|
2014-03-25 07:46:52 +00:00
|
|
|
|
|
|
|
if(!json.repeat) {
|
|
|
|
domWindowUtils.sendKeyEvent('keyup', json.keyCode,
|
2014-07-10 23:46:28 +00:00
|
|
|
json.charCode, json.modifiers, flags);
|
2014-03-25 07:46:52 +00:00
|
|
|
}
|
|
|
|
|
2013-12-03 13:37:38 +00:00
|
|
|
this._editing = false;
|
2013-08-08 00:07:41 +00:00
|
|
|
|
2014-01-02 15:39:26 +00:00
|
|
|
if (json.requestId && doKeypress) {
|
2013-08-08 00:07:41 +00:00
|
|
|
sendAsyncMessage("Forms:SendKey:Result:OK", {
|
|
|
|
requestId: json.requestId
|
|
|
|
});
|
|
|
|
}
|
2014-01-02 15:39:26 +00:00
|
|
|
else if (json.requestId && !doKeypress) {
|
|
|
|
sendAsyncMessage("Forms:SendKey:Result:Error", {
|
|
|
|
requestId: json.requestId,
|
|
|
|
error: "Keydown event got canceled"
|
|
|
|
});
|
|
|
|
}
|
2013-08-08 00:07:41 +00:00
|
|
|
break;
|
|
|
|
|
2012-06-06 12:19:33 +00:00
|
|
|
case "Forms:Select:Choice":
|
|
|
|
let options = target.options;
|
2012-09-05 06:10:55 +00:00
|
|
|
let valueChanged = false;
|
2012-06-06 12:19:33 +00:00
|
|
|
if ("index" in json) {
|
2012-09-05 06:10:55 +00:00
|
|
|
if (options.selectedIndex != json.index) {
|
|
|
|
options.selectedIndex = json.index;
|
|
|
|
valueChanged = true;
|
|
|
|
}
|
2012-06-06 12:19:33 +00:00
|
|
|
} else if ("indexes" in json) {
|
|
|
|
for (let i = 0; i < options.length; i++) {
|
2012-09-05 06:10:55 +00:00
|
|
|
let newValue = (json.indexes.indexOf(i) != -1);
|
|
|
|
if (options.item(i).selected != newValue) {
|
|
|
|
options.item(i).selected = newValue;
|
|
|
|
valueChanged = true;
|
|
|
|
}
|
2012-06-06 12:19:33 +00:00
|
|
|
}
|
|
|
|
}
|
2012-09-05 06:10:55 +00:00
|
|
|
|
|
|
|
// only fire onchange event if any selected option is changed
|
|
|
|
if (valueChanged) {
|
2013-04-26 10:17:25 +00:00
|
|
|
let event = target.ownerDocument.createEvent('HTMLEvents');
|
2012-09-05 06:10:55 +00:00
|
|
|
event.initEvent('change', true, true);
|
|
|
|
target.dispatchEvent(event);
|
|
|
|
}
|
2012-06-06 12:19:33 +00:00
|
|
|
break;
|
2012-10-17 11:18:14 +00:00
|
|
|
|
|
|
|
case "Forms:Select:Blur": {
|
|
|
|
this.setFocusedElement(null);
|
|
|
|
break;
|
|
|
|
}
|
2013-03-15 12:28:51 +00:00
|
|
|
|
|
|
|
case "Forms:SetSelectionRange": {
|
2013-08-30 12:07:16 +00:00
|
|
|
CompositionManager.endComposition('');
|
|
|
|
|
2013-03-15 12:28:51 +00:00
|
|
|
let start = json.selectionStart;
|
|
|
|
let end = json.selectionEnd;
|
2014-03-20 10:37:00 +00:00
|
|
|
|
|
|
|
if (!setSelectionRange(target, start, end)) {
|
|
|
|
if (json.requestId) {
|
|
|
|
sendAsyncMessage("Forms:SetSelectionRange:Result:Error", {
|
|
|
|
requestId: json.requestId,
|
|
|
|
error: "failed"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-03-15 12:28:51 +00:00
|
|
|
this.updateSelection();
|
2013-08-08 00:07:41 +00:00
|
|
|
|
|
|
|
if (json.requestId) {
|
|
|
|
sendAsyncMessage("Forms:SetSelectionRange:Result:OK", {
|
|
|
|
requestId: json.requestId,
|
|
|
|
selectioninfo: this.getSelectionInfo()
|
|
|
|
});
|
|
|
|
}
|
2013-03-15 12:28:51 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-04-13 14:53:41 +00:00
|
|
|
|
|
|
|
case "Forms:ReplaceSurroundingText": {
|
2013-08-30 12:07:16 +00:00
|
|
|
CompositionManager.endComposition('');
|
|
|
|
|
2013-08-08 00:07:41 +00:00
|
|
|
let selectionRange = getSelectionRange(target);
|
2014-03-20 10:37:00 +00:00
|
|
|
if (!replaceSurroundingText(target,
|
|
|
|
json.text,
|
|
|
|
selectionRange[0],
|
|
|
|
selectionRange[1],
|
|
|
|
json.offset,
|
|
|
|
json.length)) {
|
|
|
|
if (json.requestId) {
|
|
|
|
sendAsyncMessage("Forms:ReplaceSurroundingText:Result:Error", {
|
|
|
|
requestId: json.requestId,
|
|
|
|
error: "failed"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2013-08-08 00:07:41 +00:00
|
|
|
|
|
|
|
if (json.requestId) {
|
|
|
|
sendAsyncMessage("Forms:ReplaceSurroundingText:Result:OK", {
|
|
|
|
requestId: json.requestId,
|
|
|
|
selectioninfo: this.getSelectionInfo()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case "Forms:GetText": {
|
2013-08-14 17:54:36 +00:00
|
|
|
let value = isContentEditable(target) ? getContentEditableText(target)
|
|
|
|
: target.value;
|
2013-08-08 00:07:41 +00:00
|
|
|
|
|
|
|
if (json.offset && json.length) {
|
|
|
|
value = value.substr(json.offset, json.length);
|
|
|
|
}
|
|
|
|
else if (json.offset) {
|
|
|
|
value = value.substr(json.offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
sendAsyncMessage("Forms:GetText:Result:OK", {
|
|
|
|
requestId: json.requestId,
|
|
|
|
text: value
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case "Forms:GetContext": {
|
|
|
|
let obj = getJSON(target, this._focusCounter);
|
|
|
|
sendAsyncMessage("Forms:GetContext:Result:OK", obj);
|
2013-04-13 14:53:41 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-08-30 12:07:16 +00:00
|
|
|
|
|
|
|
case "Forms:SetComposition": {
|
|
|
|
CompositionManager.setComposition(target, json.text, json.cursor,
|
|
|
|
json.clauses);
|
|
|
|
sendAsyncMessage("Forms:SetComposition:Result:OK", {
|
|
|
|
requestId: json.requestId,
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case "Forms:EndComposition": {
|
|
|
|
CompositionManager.endComposition(json.text);
|
|
|
|
sendAsyncMessage("Forms:EndComposition:Result:OK", {
|
|
|
|
requestId: json.requestId,
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
2012-06-06 12:19:33 +00:00
|
|
|
}
|
2013-05-03 13:02:30 +00:00
|
|
|
this._editing = false;
|
|
|
|
|
2012-06-06 12:19:33 +00:00
|
|
|
},
|
|
|
|
|
2012-12-21 19:08:56 +00:00
|
|
|
showKeyboard: function fa_showKeyboard(target) {
|
2013-10-30 19:21:02 +00:00
|
|
|
if (this.focusedElement === target)
|
2012-07-13 02:28:19 +00:00
|
|
|
return;
|
|
|
|
|
2012-12-21 19:08:56 +00:00
|
|
|
if (target instanceof HTMLOptionElement)
|
|
|
|
target = target.parentNode;
|
|
|
|
|
2013-01-01 11:26:49 +00:00
|
|
|
this.setFocusedElement(target);
|
|
|
|
|
2012-12-18 23:10:18 +00:00
|
|
|
let kbOpened = this.sendKeyboardState(target);
|
2012-11-07 16:32:09 +00:00
|
|
|
if (this.isTextInputElement(target))
|
2012-11-07 11:53:24 +00:00
|
|
|
this.isKeyboardOpened = kbOpened;
|
|
|
|
},
|
|
|
|
|
2012-12-18 23:10:18 +00:00
|
|
|
hideKeyboard: function fa_hideKeyboard() {
|
2012-11-07 11:53:24 +00:00
|
|
|
sendAsyncMessage("Forms:Input", { "type": "blur" });
|
|
|
|
this.isKeyboardOpened = false;
|
2012-12-21 19:08:56 +00:00
|
|
|
this.setFocusedElement(null);
|
2012-11-07 11:53:24 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
isFocusableElement: function fa_isFocusableElement(element) {
|
|
|
|
if (element instanceof HTMLSelectElement ||
|
|
|
|
element instanceof HTMLTextAreaElement)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (element instanceof HTMLOptionElement &&
|
|
|
|
element.parentNode instanceof HTMLSelectElement)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return (element instanceof HTMLInputElement &&
|
|
|
|
!this.ignoredInputTypes.has(element.type));
|
|
|
|
},
|
|
|
|
|
2012-11-07 16:32:09 +00:00
|
|
|
isTextInputElement: function fa_isTextInputElement(element) {
|
|
|
|
return element instanceof HTMLInputElement ||
|
2012-11-12 08:52:24 +00:00
|
|
|
element instanceof HTMLTextAreaElement ||
|
2013-01-04 15:00:21 +00:00
|
|
|
isContentEditable(element);
|
|
|
|
},
|
|
|
|
|
|
|
|
getTopLevelEditable: function fa_getTopLevelEditable(element) {
|
|
|
|
function retrieveTopLevelEditable(element) {
|
|
|
|
while (element && !isContentEditable(element))
|
|
|
|
element = element.parentNode;
|
|
|
|
|
|
|
|
return element;
|
2013-08-29 13:57:12 +00:00
|
|
|
}
|
2013-01-04 15:00:21 +00:00
|
|
|
|
|
|
|
return retrieveTopLevelEditable(element) || element;
|
2012-11-07 16:32:09 +00:00
|
|
|
},
|
|
|
|
|
2012-12-18 23:10:18 +00:00
|
|
|
sendKeyboardState: function(element) {
|
2012-06-06 12:19:33 +00:00
|
|
|
// FIXME/bug 729623: work around apparent bug in the IME manager
|
|
|
|
// in gecko.
|
|
|
|
let readonly = element.getAttribute("readonly");
|
2012-06-26 14:01:53 +00:00
|
|
|
if (readonly) {
|
2012-06-06 12:19:33 +00:00
|
|
|
return false;
|
2012-06-26 14:01:53 +00:00
|
|
|
}
|
2012-06-06 12:19:33 +00:00
|
|
|
|
2013-08-08 00:07:41 +00:00
|
|
|
sendAsyncMessage("Forms:Input", getJSON(element, this._focusCounter));
|
2012-06-06 12:19:33 +00:00
|
|
|
return true;
|
2013-03-15 12:28:51 +00:00
|
|
|
},
|
|
|
|
|
2013-08-08 00:07:41 +00:00
|
|
|
getSelectionInfo: function fa_getSelectionInfo() {
|
|
|
|
let element = this.focusedElement;
|
|
|
|
let range = getSelectionRange(element);
|
|
|
|
|
2013-08-14 17:54:36 +00:00
|
|
|
let text = isContentEditable(element) ? getContentEditableText(element)
|
|
|
|
: element.value;
|
2013-08-08 00:07:41 +00:00
|
|
|
|
|
|
|
let textAround = getTextAroundCursor(text, range);
|
|
|
|
|
|
|
|
let changed = this.selectionStart !== range[0] ||
|
|
|
|
this.selectionEnd !== range[1] ||
|
|
|
|
this.textBeforeCursor !== textAround.before ||
|
|
|
|
this.textAfterCursor !== textAround.after;
|
|
|
|
|
|
|
|
this.selectionStart = range[0];
|
|
|
|
this.selectionEnd = range[1];
|
|
|
|
this.textBeforeCursor = textAround.before;
|
|
|
|
this.textAfterCursor = textAround.after;
|
|
|
|
|
|
|
|
return {
|
|
|
|
selectionStart: range[0],
|
|
|
|
selectionEnd: range[1],
|
|
|
|
textBeforeCursor: textAround.before,
|
|
|
|
textAfterCursor: textAround.after,
|
|
|
|
changed: changed
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2013-03-15 12:28:51 +00:00
|
|
|
// Notify when the selection range changes
|
|
|
|
updateSelection: function fa_updateSelection() {
|
2013-08-15 16:59:28 +00:00
|
|
|
if (!this.focusedElement) {
|
|
|
|
return;
|
|
|
|
}
|
2013-08-08 00:07:41 +00:00
|
|
|
let selectionInfo = this.getSelectionInfo();
|
|
|
|
if (selectionInfo.changed) {
|
|
|
|
sendAsyncMessage("Forms:SelectionChange", this.getSelectionInfo());
|
2013-03-15 12:28:51 +00:00
|
|
|
}
|
2012-06-06 12:19:33 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
FormAssistant.init();
|
|
|
|
|
2013-01-04 15:00:21 +00:00
|
|
|
function isContentEditable(element) {
|
2013-04-14 06:50:53 +00:00
|
|
|
if (!element) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-01-04 15:00:21 +00:00
|
|
|
if (element.isContentEditable || element.designMode == "on")
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return element.ownerDocument && element.ownerDocument.designMode == "on";
|
|
|
|
}
|
|
|
|
|
2013-08-14 17:54:36 +00:00
|
|
|
function isPlainTextField(element) {
|
|
|
|
if (!element) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-11-21 12:54:48 +00:00
|
|
|
return element instanceof HTMLTextAreaElement ||
|
|
|
|
(element instanceof HTMLInputElement &&
|
|
|
|
element.mozIsTextField(false));
|
2013-08-14 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2013-08-08 00:07:41 +00:00
|
|
|
function getJSON(element, focusCounter) {
|
2013-11-20 09:32:46 +00:00
|
|
|
// <input type=number> has a nested anonymous <input type=text> element that
|
|
|
|
// takes focus on behalf of the number control when someone tries to focus
|
|
|
|
// the number control. If |element| is such an anonymous text control then we
|
|
|
|
// need it's number control here in order to get the correct 'type' etc.:
|
|
|
|
element = element.ownerNumberControl || element;
|
|
|
|
|
2012-06-26 14:01:53 +00:00
|
|
|
let type = element.type || "";
|
2013-01-01 11:26:49 +00:00
|
|
|
let value = element.value || "";
|
2013-03-19 13:08:28 +00:00
|
|
|
let max = element.max || "";
|
|
|
|
let min = element.min || "";
|
2012-11-12 08:52:24 +00:00
|
|
|
|
2013-01-01 11:26:49 +00:00
|
|
|
// Treat contenteditble element as a special text area field
|
2013-01-04 15:00:21 +00:00
|
|
|
if (isContentEditable(element)) {
|
2013-01-01 11:26:49 +00:00
|
|
|
type = "textarea";
|
|
|
|
value = getContentEditableText(element);
|
2012-11-12 08:52:24 +00:00
|
|
|
}
|
2012-06-26 14:01:53 +00:00
|
|
|
|
2013-01-10 12:25:30 +00:00
|
|
|
// Until the input type=date/datetime/range have been implemented
|
2012-08-08 19:46:38 +00:00
|
|
|
// let's return their real type even if the platform returns 'text'
|
2012-06-06 12:19:33 +00:00
|
|
|
let attributeType = element.getAttribute("type") || "";
|
2012-08-07 16:09:38 +00:00
|
|
|
|
|
|
|
if (attributeType) {
|
2013-05-03 13:02:30 +00:00
|
|
|
var typeLowerCase = attributeType.toLowerCase();
|
2012-08-07 16:09:38 +00:00
|
|
|
switch (typeLowerCase) {
|
|
|
|
case "datetime":
|
|
|
|
case "datetime-local":
|
2013-01-10 12:25:30 +00:00
|
|
|
case "range":
|
2012-08-07 16:09:38 +00:00
|
|
|
type = typeLowerCase;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-06-06 12:19:33 +00:00
|
|
|
|
2012-11-15 14:38:47 +00:00
|
|
|
// Gecko has some support for @inputmode but behind a preference and
|
|
|
|
// it is disabled by default.
|
|
|
|
// Gaia is then using @x-inputmode has its proprietary way to set
|
|
|
|
// inputmode for fields. This shouldn't be used outside of pre-installed
|
|
|
|
// apps because the attribute is going to disappear as soon as a definitive
|
|
|
|
// solution will be find.
|
|
|
|
let inputmode = element.getAttribute('x-inputmode');
|
2012-10-05 06:21:10 +00:00
|
|
|
if (inputmode) {
|
|
|
|
inputmode = inputmode.toLowerCase();
|
2012-10-12 21:51:13 +00:00
|
|
|
} else {
|
|
|
|
inputmode = '';
|
2012-10-05 06:21:10 +00:00
|
|
|
}
|
|
|
|
|
2013-01-01 11:26:49 +00:00
|
|
|
let range = getSelectionRange(element);
|
2013-08-08 00:07:41 +00:00
|
|
|
let textAround = getTextAroundCursor(value, range);
|
2013-01-01 11:26:49 +00:00
|
|
|
|
2012-06-06 12:19:33 +00:00
|
|
|
return {
|
2013-08-08 00:07:41 +00:00
|
|
|
"contextId": focusCounter,
|
|
|
|
|
2012-06-06 12:19:33 +00:00
|
|
|
"type": type.toLowerCase(),
|
2012-09-29 16:32:57 +00:00
|
|
|
"choices": getListForElement(element),
|
2012-11-12 08:52:24 +00:00
|
|
|
"value": value,
|
2012-10-05 06:21:10 +00:00
|
|
|
"inputmode": inputmode,
|
2013-01-01 11:26:49 +00:00
|
|
|
"selectionStart": range[0],
|
2013-03-19 13:08:28 +00:00
|
|
|
"selectionEnd": range[1],
|
|
|
|
"max": max,
|
2013-08-08 00:07:41 +00:00
|
|
|
"min": min,
|
|
|
|
"lang": element.lang || "",
|
|
|
|
"textBeforeCursor": textAround.before,
|
|
|
|
"textAfterCursor": textAround.after
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function getTextAroundCursor(value, range) {
|
|
|
|
let textBeforeCursor = range[0] < 100 ?
|
|
|
|
value.substr(0, range[0]) :
|
|
|
|
value.substr(range[0] - 100, 100);
|
|
|
|
|
|
|
|
let textAfterCursor = range[1] + 100 > value.length ?
|
|
|
|
value.substr(range[0], value.length) :
|
|
|
|
value.substr(range[0], range[1] - range[0] + 100);
|
|
|
|
|
|
|
|
return {
|
|
|
|
before: textBeforeCursor,
|
|
|
|
after: textAfterCursor
|
2012-06-06 12:19:33 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2012-06-06 12:19:33 +00:00
|
|
|
function getListForElement(element) {
|
|
|
|
if (!(element instanceof HTMLSelectElement))
|
|
|
|
return null;
|
|
|
|
|
|
|
|
let optionIndex = 0;
|
|
|
|
let result = {
|
|
|
|
"multiple": element.multiple,
|
|
|
|
"choices": []
|
|
|
|
};
|
|
|
|
|
|
|
|
// Build up a flat JSON array of the choices.
|
|
|
|
// In HTML, it's possible for select element choices to be under a
|
|
|
|
// group header (but not recursively). We distinguish between headers
|
|
|
|
// and entries using the boolean "list.group".
|
|
|
|
let children = element.children;
|
|
|
|
for (let i = 0; i < children.length; i++) {
|
|
|
|
let child = children[i];
|
|
|
|
|
|
|
|
if (child instanceof HTMLOptGroupElement) {
|
|
|
|
result.choices.push({
|
|
|
|
"group": true,
|
|
|
|
"text": child.label || child.firstChild.data,
|
|
|
|
"disabled": child.disabled
|
|
|
|
});
|
|
|
|
|
|
|
|
let subchildren = child.children;
|
|
|
|
for (let j = 0; j < subchildren.length; j++) {
|
|
|
|
let subchild = subchildren[j];
|
|
|
|
result.choices.push({
|
|
|
|
"group": false,
|
|
|
|
"inGroup": true,
|
|
|
|
"text": subchild.text,
|
|
|
|
"disabled": child.disabled || subchild.disabled,
|
|
|
|
"selected": subchild.selected,
|
|
|
|
"optionIndex": optionIndex++
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else if (child instanceof HTMLOptionElement) {
|
|
|
|
result.choices.push({
|
|
|
|
"group": false,
|
|
|
|
"inGroup": false,
|
|
|
|
"text": child.text,
|
|
|
|
"disabled": child.disabled,
|
|
|
|
"selected": child.selected,
|
|
|
|
"optionIndex": optionIndex++
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
2013-01-01 11:26:49 +00:00
|
|
|
// Create a plain text document encode from the focused element.
|
|
|
|
function getDocumentEncoder(element) {
|
|
|
|
let encoder = Cc["@mozilla.org/layout/documentEncoder;1?type=text/plain"]
|
|
|
|
.createInstance(Ci.nsIDocumentEncoder);
|
|
|
|
let flags = Ci.nsIDocumentEncoder.SkipInvisibleContent |
|
|
|
|
Ci.nsIDocumentEncoder.OutputRaw |
|
2014-03-20 10:37:00 +00:00
|
|
|
Ci.nsIDocumentEncoder.OutputDropInvisibleBreak |
|
2013-09-05 20:10:04 +00:00
|
|
|
// Bug 902847. Don't trim trailing spaces of a line.
|
|
|
|
Ci.nsIDocumentEncoder.OutputDontRemoveLineEndingSpaces |
|
2013-01-01 11:26:49 +00:00
|
|
|
Ci.nsIDocumentEncoder.OutputLFLineBreak |
|
2013-07-12 13:04:05 +00:00
|
|
|
Ci.nsIDocumentEncoder.OutputNonTextContentAsPlaceholder;
|
2013-01-01 11:26:49 +00:00
|
|
|
encoder.init(element.ownerDocument, "text/plain", flags);
|
|
|
|
return encoder;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the visible content text of a content editable element
|
|
|
|
function getContentEditableText(element) {
|
2013-08-14 17:54:36 +00:00
|
|
|
if (!element || !isContentEditable(element)) {
|
2013-08-13 04:02:11 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-01-01 11:26:49 +00:00
|
|
|
let doc = element.ownerDocument;
|
|
|
|
let range = doc.createRange();
|
|
|
|
range.selectNodeContents(element);
|
|
|
|
let encoder = FormAssistant.documentEncoder;
|
|
|
|
encoder.setRange(range);
|
|
|
|
return encoder.encodeToString();
|
|
|
|
}
|
|
|
|
|
|
|
|
function getSelectionRange(element) {
|
|
|
|
let start = 0;
|
|
|
|
let end = 0;
|
2013-08-14 17:54:36 +00:00
|
|
|
if (isPlainTextField(element)) {
|
2013-01-01 11:26:49 +00:00
|
|
|
// Get the selection range of <input> and <textarea> elements
|
|
|
|
start = element.selectionStart;
|
|
|
|
end = element.selectionEnd;
|
2013-03-09 04:14:16 +00:00
|
|
|
} else if (isContentEditable(element)){
|
2013-01-01 11:26:49 +00:00
|
|
|
// Get the selection range of contenteditable elements
|
|
|
|
let win = element.ownerDocument.defaultView;
|
|
|
|
let sel = win.getSelection();
|
2013-09-30 11:53:40 +00:00
|
|
|
if (sel && sel.rangeCount > 0) {
|
2013-08-14 17:53:53 +00:00
|
|
|
start = getContentEditableSelectionStart(element, sel);
|
|
|
|
end = start + getContentEditableSelectionLength(element, sel);
|
|
|
|
} else {
|
|
|
|
dump("Failed to get window.getSelection()\n");
|
|
|
|
}
|
2013-03-15 12:28:51 +00:00
|
|
|
}
|
|
|
|
return [start, end];
|
|
|
|
}
|
|
|
|
|
|
|
|
function getContentEditableSelectionStart(element, selection) {
|
|
|
|
let doc = element.ownerDocument;
|
|
|
|
let range = doc.createRange();
|
|
|
|
range.setStart(element, 0);
|
|
|
|
range.setEnd(selection.anchorNode, selection.anchorOffset);
|
|
|
|
let encoder = FormAssistant.documentEncoder;
|
|
|
|
encoder.setRange(range);
|
|
|
|
return encoder.encodeToString().length;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getContentEditableSelectionLength(element, selection) {
|
|
|
|
let encoder = FormAssistant.documentEncoder;
|
|
|
|
encoder.setRange(selection.getRangeAt(0));
|
|
|
|
return encoder.encodeToString().length;
|
|
|
|
}
|
|
|
|
|
|
|
|
function setSelectionRange(element, start, end) {
|
2013-08-14 17:54:36 +00:00
|
|
|
let isTextField = isPlainTextField(element);
|
2013-03-15 12:28:51 +00:00
|
|
|
|
|
|
|
// Check the parameters
|
|
|
|
|
2013-08-14 17:54:36 +00:00
|
|
|
if (!isTextField && !isContentEditable(element)) {
|
2013-03-15 12:28:51 +00:00
|
|
|
// Skip HTMLOptionElement and HTMLSelectElement elements, as they don't
|
|
|
|
// support the operation of setSelectionRange
|
2014-03-20 10:37:00 +00:00
|
|
|
return false;
|
2013-03-15 12:28:51 +00:00
|
|
|
}
|
|
|
|
|
2013-08-14 17:54:36 +00:00
|
|
|
let text = isTextField ? element.value : getContentEditableText(element);
|
2013-03-15 12:28:51 +00:00
|
|
|
let length = text.length;
|
|
|
|
if (start < 0) {
|
|
|
|
start = 0;
|
|
|
|
}
|
|
|
|
if (end > length) {
|
|
|
|
end = length;
|
|
|
|
}
|
|
|
|
if (start > end) {
|
|
|
|
start = end;
|
|
|
|
}
|
|
|
|
|
2013-08-14 17:54:36 +00:00
|
|
|
if (isTextField) {
|
2013-03-15 12:28:51 +00:00
|
|
|
// Set the selection range of <input> and <textarea> elements
|
|
|
|
element.setSelectionRange(start, end, "forward");
|
2014-03-20 10:37:00 +00:00
|
|
|
return true;
|
2013-03-15 12:28:51 +00:00
|
|
|
} else {
|
|
|
|
// set the selection range of contenteditable elements
|
|
|
|
let win = element.ownerDocument.defaultView;
|
|
|
|
let sel = win.getSelection();
|
|
|
|
|
|
|
|
// Move the caret to the start position
|
|
|
|
sel.collapse(element, 0);
|
|
|
|
for (let i = 0; i < start; i++) {
|
|
|
|
sel.modify("move", "forward", "character");
|
|
|
|
}
|
2013-01-01 11:26:49 +00:00
|
|
|
|
2014-03-20 10:37:00 +00:00
|
|
|
// Avoid entering infinite loop in case we cannot change the selection
|
|
|
|
// range. See bug https://bugzilla.mozilla.org/show_bug.cgi?id=978918
|
|
|
|
let oldStart = getContentEditableSelectionStart(element, sel);
|
|
|
|
let counter = 0;
|
|
|
|
while (oldStart < start) {
|
2013-03-15 12:28:51 +00:00
|
|
|
sel.modify("move", "forward", "character");
|
2014-03-20 10:37:00 +00:00
|
|
|
let newStart = getContentEditableSelectionStart(element, sel);
|
|
|
|
if (oldStart == newStart) {
|
|
|
|
counter++;
|
|
|
|
if (counter > MAX_BLOCKED_COUNT) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
counter = 0;
|
|
|
|
oldStart = newStart;
|
|
|
|
}
|
2013-03-15 12:28:51 +00:00
|
|
|
}
|
2013-01-01 11:26:49 +00:00
|
|
|
|
2013-03-15 12:28:51 +00:00
|
|
|
// Extend the selection to the end position
|
|
|
|
for (let i = start; i < end; i++) {
|
|
|
|
sel.modify("extend", "forward", "character");
|
|
|
|
}
|
2013-01-01 11:26:49 +00:00
|
|
|
|
2014-03-20 10:37:00 +00:00
|
|
|
// Avoid entering infinite loop in case we cannot change the selection
|
|
|
|
// range. See bug https://bugzilla.mozilla.org/show_bug.cgi?id=978918
|
|
|
|
counter = 0;
|
2013-03-15 12:28:51 +00:00
|
|
|
let selectionLength = end - start;
|
2014-03-20 10:37:00 +00:00
|
|
|
let oldSelectionLength = getContentEditableSelectionLength(element, sel);
|
|
|
|
while (oldSelectionLength < selectionLength) {
|
2013-03-15 12:28:51 +00:00
|
|
|
sel.modify("extend", "forward", "character");
|
2014-03-20 10:37:00 +00:00
|
|
|
let newSelectionLength = getContentEditableSelectionLength(element, sel);
|
|
|
|
if (oldSelectionLength == newSelectionLength ) {
|
|
|
|
counter++;
|
|
|
|
if (counter > MAX_BLOCKED_COUNT) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
counter = 0;
|
|
|
|
oldSelectionLength = newSelectionLength;
|
|
|
|
}
|
2013-03-15 12:28:51 +00:00
|
|
|
}
|
2014-03-20 10:37:00 +00:00
|
|
|
return true;
|
2013-01-01 11:26:49 +00:00
|
|
|
}
|
|
|
|
}
|
2013-03-15 12:28:51 +00:00
|
|
|
|
2013-12-13 21:10:56 +00:00
|
|
|
/**
|
|
|
|
* Scroll the given element into view.
|
|
|
|
*
|
|
|
|
* Calls scrollSelectionIntoView for contentEditable elements.
|
|
|
|
*/
|
|
|
|
function scrollSelectionOrElementIntoView(element) {
|
|
|
|
let editor = getPlaintextEditor(element);
|
|
|
|
if (editor) {
|
|
|
|
editor.selectionController.scrollSelectionIntoView(
|
|
|
|
Ci.nsISelectionController.SELECTION_NORMAL,
|
|
|
|
Ci.nsISelectionController.SELECTION_FOCUS_REGION,
|
|
|
|
Ci.nsISelectionController.SCROLL_SYNCHRONOUS);
|
|
|
|
} else {
|
|
|
|
element.scrollIntoView(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-03 13:02:30 +00:00
|
|
|
// Get nsIPlaintextEditor object from an input field
|
|
|
|
function getPlaintextEditor(element) {
|
|
|
|
let editor = null;
|
|
|
|
// Get nsIEditor
|
2013-08-14 17:54:36 +00:00
|
|
|
if (isPlainTextField(element)) {
|
2013-05-03 13:02:30 +00:00
|
|
|
// Get from the <input> and <textarea> elements
|
|
|
|
editor = element.QueryInterface(Ci.nsIDOMNSEditableElement).editor;
|
|
|
|
} else if (isContentEditable(element)) {
|
|
|
|
// Get from content editable element
|
|
|
|
let win = element.ownerDocument.defaultView;
|
|
|
|
let editingSession = win.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIWebNavigation)
|
|
|
|
.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIEditingSession);
|
|
|
|
if (editingSession) {
|
|
|
|
editor = editingSession.getEditorForWindow(win);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (editor) {
|
|
|
|
editor.QueryInterface(Ci.nsIPlaintextEditor);
|
|
|
|
}
|
|
|
|
return editor;
|
|
|
|
}
|
2013-04-13 14:53:41 +00:00
|
|
|
|
2014-01-03 14:17:28 +00:00
|
|
|
function replaceSurroundingText(element, text, selectionStart, selectionEnd,
|
|
|
|
offset, length) {
|
2013-04-13 14:53:41 +00:00
|
|
|
let editor = FormAssistant.editor;
|
|
|
|
if (!editor) {
|
2014-03-20 10:37:00 +00:00
|
|
|
return false;
|
2013-04-13 14:53:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check the parameters.
|
2014-01-03 14:17:28 +00:00
|
|
|
let start = selectionStart + offset;
|
|
|
|
if (start < 0) {
|
|
|
|
start = 0;
|
2013-04-13 14:53:41 +00:00
|
|
|
}
|
2014-01-03 14:17:28 +00:00
|
|
|
if (length < 0) {
|
|
|
|
length = 0;
|
2013-04-13 14:53:41 +00:00
|
|
|
}
|
2014-01-03 14:17:28 +00:00
|
|
|
let end = start + length;
|
2013-04-13 14:53:41 +00:00
|
|
|
|
2014-01-03 14:17:28 +00:00
|
|
|
if (selectionStart != start || selectionEnd != end) {
|
2013-04-13 14:53:41 +00:00
|
|
|
// Change selection range before replacing.
|
2014-03-20 10:37:00 +00:00
|
|
|
if (!setSelectionRange(element, start, end)) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-04-13 14:53:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (start != end) {
|
|
|
|
// Delete the selected text.
|
|
|
|
editor.deleteSelection(Ci.nsIEditor.ePrevious, Ci.nsIEditor.eStrip);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (text) {
|
2013-08-22 13:53:47 +00:00
|
|
|
// We don't use CR but LF
|
|
|
|
// see https://bugzilla.mozilla.org/show_bug.cgi?id=902847
|
|
|
|
text = text.replace(/\r/g, '\n');
|
2013-04-13 14:53:41 +00:00
|
|
|
// Insert the text to be replaced with.
|
|
|
|
editor.insertText(text);
|
|
|
|
}
|
2014-03-20 10:37:00 +00:00
|
|
|
return true;
|
2013-04-13 14:53:41 +00:00
|
|
|
}
|
2013-08-30 12:07:16 +00:00
|
|
|
|
|
|
|
let CompositionManager = {
|
|
|
|
_isStarted: false,
|
|
|
|
_text: '',
|
|
|
|
_clauseAttrMap: {
|
2013-09-12 15:19:00 +00:00
|
|
|
'raw-input':
|
|
|
|
Ci.nsICompositionStringSynthesizer.ATTR_RAWINPUT,
|
|
|
|
'selected-raw-text':
|
|
|
|
Ci.nsICompositionStringSynthesizer.ATTR_SELECTEDRAWTEXT,
|
|
|
|
'converted-text':
|
|
|
|
Ci.nsICompositionStringSynthesizer.ATTR_CONVERTEDTEXT,
|
|
|
|
'selected-converted-text':
|
|
|
|
Ci.nsICompositionStringSynthesizer.ATTR_SELECTEDCONVERTEDTEXT
|
2013-08-30 12:07:16 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
setComposition: function cm_setComposition(element, text, cursor, clauses) {
|
|
|
|
// Check parameters.
|
|
|
|
if (!element) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let len = text.length;
|
2013-09-12 15:19:00 +00:00
|
|
|
if (cursor > len) {
|
2013-08-30 12:07:16 +00:00
|
|
|
cursor = len;
|
|
|
|
}
|
2013-09-12 15:19:00 +00:00
|
|
|
let clauseLens = [];
|
|
|
|
let clauseAttrs = [];
|
2013-08-30 12:07:16 +00:00
|
|
|
if (clauses) {
|
|
|
|
let remainingLength = len;
|
2013-09-12 15:19:00 +00:00
|
|
|
for (let i = 0; i < clauses.length; i++) {
|
2013-08-30 12:07:16 +00:00
|
|
|
if (clauses[i]) {
|
|
|
|
let clauseLength = clauses[i].length || 0;
|
|
|
|
// Make sure the total clauses length is not bigger than that of the
|
|
|
|
// composition string.
|
|
|
|
if (clauseLength > remainingLength) {
|
|
|
|
clauseLength = remainingLength;
|
|
|
|
}
|
|
|
|
remainingLength -= clauseLength;
|
2013-09-12 15:19:00 +00:00
|
|
|
clauseLens.push(clauseLength);
|
|
|
|
clauseAttrs.push(this._clauseAttrMap[clauses[i].selectionType] ||
|
|
|
|
Ci.nsICompositionStringSynthesizer.ATTR_RAWINPUT);
|
2013-08-30 12:07:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// If the total clauses length is less than that of the composition
|
|
|
|
// string, extend the last clause to the end of the composition string.
|
|
|
|
if (remainingLength > 0) {
|
2013-09-12 15:19:00 +00:00
|
|
|
clauseLens[clauseLens.length - 1] += remainingLength;
|
2013-08-30 12:07:16 +00:00
|
|
|
}
|
2013-09-12 15:19:00 +00:00
|
|
|
} else {
|
|
|
|
clauseLens.push(len);
|
|
|
|
clauseAttrs.push(Ci.nsICompositionStringSynthesizer.ATTR_RAWINPUT);
|
2013-08-30 12:07:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start composition if need to.
|
|
|
|
if (!this._isStarted) {
|
|
|
|
this._isStarted = true;
|
|
|
|
domWindowUtils.sendCompositionEvent('compositionstart', '', '');
|
|
|
|
this._text = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the composing text.
|
|
|
|
if (this._text !== text) {
|
|
|
|
this._text = text;
|
|
|
|
domWindowUtils.sendCompositionEvent('compositionupdate', text, '');
|
|
|
|
}
|
2013-09-12 15:19:00 +00:00
|
|
|
let compositionString = domWindowUtils.createCompositionStringSynthesizer();
|
|
|
|
compositionString.setString(text);
|
|
|
|
for (var i = 0; i < clauseLens.length; i++) {
|
|
|
|
compositionString.appendClause(clauseLens[i], clauseAttrs[i]);
|
|
|
|
}
|
|
|
|
if (cursor >= 0) {
|
|
|
|
compositionString.setCaret(cursor, 0);
|
|
|
|
}
|
|
|
|
compositionString.dispatchEvent();
|
2013-08-30 12:07:16 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
endComposition: function cm_endComposition(text) {
|
|
|
|
if (!this._isStarted) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Update the composing text.
|
|
|
|
if (this._text !== text) {
|
|
|
|
domWindowUtils.sendCompositionEvent('compositionupdate', text, '');
|
|
|
|
}
|
2013-09-12 15:19:00 +00:00
|
|
|
let compositionString = domWindowUtils.createCompositionStringSynthesizer();
|
|
|
|
compositionString.setString(text);
|
2013-09-05 12:20:25 +00:00
|
|
|
// Set the cursor position to |text.length| so that the text will be
|
|
|
|
// committed before the cursor position.
|
2013-09-12 15:19:00 +00:00
|
|
|
compositionString.setCaret(text.length, 0);
|
|
|
|
compositionString.dispatchEvent();
|
2013-08-30 12:07:16 +00:00
|
|
|
domWindowUtils.sendCompositionEvent('compositionend', text, '');
|
|
|
|
this._text = '';
|
|
|
|
this._isStarted = false;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Composition ends due to external actions.
|
|
|
|
onCompositionEnd: function cm_onCompositionEnd() {
|
|
|
|
if (!this._isStarted) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._text = '';
|
|
|
|
this._isStarted = false;
|
|
|
|
}
|
|
|
|
};
|