2012-06-06 05:19:33 -07:00
|
|
|
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- /
|
|
|
|
/* 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 05:19:33 -07:00
|
|
|
"use strict";
|
2012-06-06 05:19:33 -07:00
|
|
|
|
2012-10-04 23:21:10 -07:00
|
|
|
dump("###################################### forms.js loaded\n");
|
|
|
|
|
2012-06-06 05:19:33 -07:00
|
|
|
let Ci = Components.interfaces;
|
|
|
|
let Cc = Components.classes;
|
|
|
|
let Cu = Components.utils;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
2012-06-26 07:01:53 -07:00
|
|
|
Cu.import('resource://gre/modules/XPCOMUtils.jsm');
|
2012-06-06 05:19:33 -07:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(Services, "fm",
|
|
|
|
"@mozilla.org/focus-manager;1",
|
|
|
|
"nsIFocusManager");
|
2012-06-06 05:19:33 -07:00
|
|
|
|
2012-11-07 03:53:24 -08:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "domWindowUtils", function () {
|
|
|
|
return content.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIDOMWindowUtils);
|
|
|
|
});
|
|
|
|
|
2012-11-09 09:39:23 -08:00
|
|
|
const RESIZE_SCROLL_DELAY = 20;
|
2012-11-07 03:53:24 -08:00
|
|
|
|
2013-01-04 07:00:21 -08:00
|
|
|
let HTMLDocument = Ci.nsIDOMHTMLDocument;
|
|
|
|
let HTMLHtmlElement = Ci.nsIDOMHTMLHtmlElement;
|
|
|
|
let HTMLBodyElement = Ci.nsIDOMHTMLBodyElement;
|
|
|
|
let HTMLIFrameElement = Ci.nsIDOMHTMLIFrameElement;
|
2012-06-06 05:19:33 -07:00
|
|
|
let HTMLInputElement = Ci.nsIDOMHTMLInputElement;
|
|
|
|
let HTMLTextAreaElement = Ci.nsIDOMHTMLTextAreaElement;
|
2012-06-06 05:19:33 -07:00
|
|
|
let HTMLSelectElement = Ci.nsIDOMHTMLSelectElement;
|
|
|
|
let HTMLOptGroupElement = Ci.nsIDOMHTMLOptGroupElement;
|
|
|
|
let HTMLOptionElement = Ci.nsIDOMHTMLOptionElement;
|
|
|
|
|
2013-01-04 07:00:21 -08: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 05:19:33 -07:00
|
|
|
let FormAssistant = {
|
|
|
|
init: function fa_init() {
|
2012-06-06 05:19:33 -07:00
|
|
|
addEventListener("focus", this, true, false);
|
2012-06-26 07:01:53 -07:00
|
|
|
addEventListener("blur", this, true, false);
|
2012-06-06 05:19:33 -07:00
|
|
|
addEventListener("resize", this, true, false);
|
2013-01-07 03:09:50 -08:00
|
|
|
addEventListener("submit", this, true, false);
|
|
|
|
addEventListener("pagehide", this, true, false);
|
2013-03-15 05:28:51 -07:00
|
|
|
addEventListener("input", this, true, false);
|
|
|
|
addEventListener("keydown", this, true, false);
|
2012-06-06 05:19:33 -07:00
|
|
|
addMessageListener("Forms:Select:Choice", this);
|
2012-06-06 05:19:33 -07:00
|
|
|
addMessageListener("Forms:Input:Value", this);
|
2012-10-17 04:18:14 -07:00
|
|
|
addMessageListener("Forms:Select:Blur", this);
|
2013-03-15 05:28:51 -07:00
|
|
|
addMessageListener("Forms:SetSelectionRange", this);
|
2012-06-06 05:19:33 -07:00
|
|
|
},
|
|
|
|
|
2012-11-07 03:53:24 -08:00
|
|
|
ignoredInputTypes: new Set([
|
|
|
|
'button', 'file', 'checkbox', 'radio', 'reset', 'submit', 'image'
|
|
|
|
]),
|
|
|
|
|
2012-06-06 05:19:33 -07:00
|
|
|
isKeyboardOpened: false,
|
2013-03-15 05:28:51 -07:00
|
|
|
selectionStart: -1,
|
|
|
|
selectionEnd: -1,
|
2012-11-09 09:39:23 -08:00
|
|
|
scrollIntoViewTimeout: null,
|
2012-10-24 11:09:12 -07:00
|
|
|
_focusedElement: null,
|
2013-01-01 03:26:49 -08:00
|
|
|
_documentEncoder: null,
|
2012-10-24 11:09:12 -07:00
|
|
|
|
|
|
|
get focusedElement() {
|
2012-10-24 16:30:14 -07:00
|
|
|
if (this._focusedElement && Cu.isDeadWrapper(this._focusedElement))
|
2012-10-24 11:09:12 -07:00
|
|
|
this._focusedElement = null;
|
2012-10-24 16:30:14 -07:00
|
|
|
|
2012-10-24 11:09:12 -07:00
|
|
|
return this._focusedElement;
|
|
|
|
},
|
|
|
|
|
|
|
|
set focusedElement(val) {
|
|
|
|
this._focusedElement = val;
|
|
|
|
},
|
|
|
|
|
2012-10-04 23:21:10 -07:00
|
|
|
setFocusedElement: function fa_setFocusedElement(element) {
|
|
|
|
if (element === this.focusedElement)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (this.focusedElement) {
|
|
|
|
this.focusedElement.removeEventListener('mousedown', this);
|
|
|
|
this.focusedElement.removeEventListener('mouseup', this);
|
2012-10-17 04:18:14 -07:00
|
|
|
if (!element) {
|
|
|
|
this.focusedElement.blur();
|
|
|
|
}
|
2012-10-04 23:21:10 -07:00
|
|
|
}
|
|
|
|
|
2013-01-01 03:26:49 -08:00
|
|
|
this._documentEncoder = null;
|
|
|
|
|
2012-10-04 23:21:10 -07:00
|
|
|
if (element) {
|
|
|
|
element.addEventListener('mousedown', this);
|
|
|
|
element.addEventListener('mouseup', this);
|
2013-01-01 03:26:49 -08:00
|
|
|
if (isContentEditable(element)) {
|
|
|
|
this._documentEncoder = getDocumentEncoder(element);
|
|
|
|
}
|
2012-10-04 23:21:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
this.focusedElement = element;
|
|
|
|
},
|
|
|
|
|
2013-01-01 03:26:49 -08:00
|
|
|
get documentEncoder() {
|
|
|
|
return this._documentEncoder;
|
|
|
|
},
|
|
|
|
|
2012-06-06 05:19:33 -07:00
|
|
|
handleEvent: function fa_handleEvent(evt) {
|
2012-06-06 05:19:33 -07:00
|
|
|
let target = evt.target;
|
|
|
|
|
2013-01-01 03:26:49 -08:00
|
|
|
let range = null;
|
2012-06-06 05:19:33 -07:00
|
|
|
switch (evt.type) {
|
2012-06-06 05:19:33 -07:00
|
|
|
case "focus":
|
2013-02-12 16:31:08 -08:00
|
|
|
if (!target) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (target instanceof HTMLDocument || target == content) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isContentEditable(target)) {
|
2013-01-04 07:00:21 -08:00
|
|
|
this.showKeyboard(this.getTopLevelEditable(target));
|
2013-03-15 05:28:51 -07:00
|
|
|
this.updateSelection();
|
2013-01-04 07:00:21 -08:00
|
|
|
break;
|
|
|
|
}
|
2012-06-06 05:19:33 -07:00
|
|
|
|
2013-03-15 05:28:51 -07:00
|
|
|
if (this.isFocusableElement(target)) {
|
2012-12-21 11:08:56 -08:00
|
|
|
this.showKeyboard(target);
|
2013-03-15 05:28:51 -07:00
|
|
|
this.updateSelection();
|
|
|
|
}
|
2012-06-26 07:01:53 -07:00
|
|
|
break;
|
|
|
|
|
2013-03-07 05:35:38 -08:00
|
|
|
case "pagehide":
|
|
|
|
// We are only interested to the pagehide event from the root document.
|
|
|
|
if (target && target != content.document) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// fall through
|
2012-06-26 07:01:53 -07:00
|
|
|
case "blur":
|
2013-01-07 03:09:50 -08:00
|
|
|
case "submit":
|
2013-03-15 05:28:51 -07:00
|
|
|
if (this.focusedElement) {
|
2012-12-18 15:10:18 -08:00
|
|
|
this.hideKeyboard();
|
2013-03-15 05:28:51 -07:00
|
|
|
this.selectionStart = -1;
|
|
|
|
this.selectionEnd = -1;
|
|
|
|
}
|
2012-10-04 23:21:10 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'mousedown':
|
|
|
|
// We only listen for this event on the currently focused element.
|
|
|
|
// When the mouse goes down, note the cursor/selection position
|
2013-03-15 05:28:51 -07:00
|
|
|
this.updateSelection();
|
2012-10-04 23:21:10 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'mouseup':
|
|
|
|
// 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 03:26:49 -08:00
|
|
|
range = getSelectionRange(this.focusedElement);
|
|
|
|
if (range[0] !== this.selectionStart ||
|
|
|
|
range[1] !== this.selectionEnd) {
|
2012-12-18 15:10:18 -08:00
|
|
|
this.sendKeyboardState(this.focusedElement);
|
2013-03-15 05:28:51 -07:00
|
|
|
this.updateSelection();
|
2012-06-25 23:42:45 -07:00
|
|
|
}
|
2012-06-26 07:01:53 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "resize":
|
|
|
|
if (!this.isKeyboardOpened)
|
|
|
|
return;
|
2012-06-06 05:19:33 -07:00
|
|
|
|
2012-11-09 09:39:23 -08: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-04 23:21:10 -07:00
|
|
|
if (this.focusedElement) {
|
2012-11-09 09:39:23 -08:00
|
|
|
this.scrollIntoViewTimeout = content.setTimeout(function () {
|
|
|
|
this.scrollIntoViewTimeout = null;
|
2013-01-04 07:00:21 -08:00
|
|
|
if (this.focusedElement && !FormVisibility.isVisible(this.focusedElement)) {
|
2012-11-09 09:39:23 -08:00
|
|
|
this.focusedElement.scrollIntoView(false);
|
|
|
|
}
|
|
|
|
}.bind(this), RESIZE_SCROLL_DELAY);
|
2012-06-26 07:01:53 -07:00
|
|
|
}
|
2012-06-06 05:19:33 -07:00
|
|
|
break;
|
2013-03-15 05:28:51 -07:00
|
|
|
|
|
|
|
case "input":
|
|
|
|
// When the text content changes, notify the keyboard
|
|
|
|
this.updateSelection();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "keydown":
|
|
|
|
// We use 'setTimeout' to wait until the input element accomplishes the
|
|
|
|
// change in selection range
|
|
|
|
content.setTimeout(function() {
|
|
|
|
this.updateSelection();
|
|
|
|
}.bind(this), 0);
|
|
|
|
break;
|
2012-06-06 05:19:33 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
receiveMessage: function fa_receiveMessage(msg) {
|
2012-10-04 23:21:10 -07:00
|
|
|
let target = this.focusedElement;
|
2012-06-26 07:01:53 -07:00
|
|
|
if (!target) {
|
2012-06-06 05:19:33 -07:00
|
|
|
return;
|
2012-06-26 07:01:53 -07:00
|
|
|
}
|
2012-06-06 05:19:33 -07:00
|
|
|
|
2012-06-06 05:19:33 -07:00
|
|
|
let json = msg.json;
|
|
|
|
switch (msg.name) {
|
2012-09-26 04:08:50 -07:00
|
|
|
case "Forms:Input:Value": {
|
2012-06-06 05:19:33 -07:00
|
|
|
target.value = json.value;
|
2012-09-26 04:08:50 -07:00
|
|
|
|
|
|
|
let event = content.document.createEvent('HTMLEvents');
|
|
|
|
event.initEvent('input', true, false);
|
|
|
|
target.dispatchEvent(event);
|
2012-06-06 05:19:33 -07:00
|
|
|
break;
|
2012-09-26 04:08:50 -07:00
|
|
|
}
|
2012-06-06 05:19:33 -07:00
|
|
|
|
2012-06-06 05:19:33 -07:00
|
|
|
case "Forms:Select:Choice":
|
|
|
|
let options = target.options;
|
2012-09-04 23:10:55 -07:00
|
|
|
let valueChanged = false;
|
2012-06-06 05:19:33 -07:00
|
|
|
if ("index" in json) {
|
2012-09-04 23:10:55 -07:00
|
|
|
if (options.selectedIndex != json.index) {
|
|
|
|
options.selectedIndex = json.index;
|
|
|
|
valueChanged = true;
|
|
|
|
}
|
2012-06-06 05:19:33 -07:00
|
|
|
} else if ("indexes" in json) {
|
|
|
|
for (let i = 0; i < options.length; i++) {
|
2012-09-04 23:10:55 -07:00
|
|
|
let newValue = (json.indexes.indexOf(i) != -1);
|
|
|
|
if (options.item(i).selected != newValue) {
|
|
|
|
options.item(i).selected = newValue;
|
|
|
|
valueChanged = true;
|
|
|
|
}
|
2012-06-06 05:19:33 -07:00
|
|
|
}
|
|
|
|
}
|
2012-09-04 23:10:55 -07:00
|
|
|
|
|
|
|
// only fire onchange event if any selected option is changed
|
|
|
|
if (valueChanged) {
|
|
|
|
let event = content.document.createEvent('HTMLEvents');
|
|
|
|
event.initEvent('change', true, true);
|
|
|
|
target.dispatchEvent(event);
|
|
|
|
}
|
2012-06-06 05:19:33 -07:00
|
|
|
break;
|
2012-10-17 04:18:14 -07:00
|
|
|
|
|
|
|
case "Forms:Select:Blur": {
|
|
|
|
this.setFocusedElement(null);
|
|
|
|
break;
|
|
|
|
}
|
2013-03-15 05:28:51 -07:00
|
|
|
|
|
|
|
case "Forms:SetSelectionRange": {
|
|
|
|
let start = json.selectionStart;
|
|
|
|
let end = json.selectionEnd;
|
|
|
|
setSelectionRange(target, start, end);
|
|
|
|
this.updateSelection();
|
|
|
|
break;
|
|
|
|
}
|
2012-06-06 05:19:33 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-12-21 11:08:56 -08:00
|
|
|
showKeyboard: function fa_showKeyboard(target) {
|
2012-11-07 03:53:24 -08:00
|
|
|
if (this.isKeyboardOpened)
|
2012-07-12 19:28:19 -07:00
|
|
|
return;
|
|
|
|
|
2012-12-21 11:08:56 -08:00
|
|
|
if (target instanceof HTMLOptionElement)
|
|
|
|
target = target.parentNode;
|
|
|
|
|
2013-01-01 03:26:49 -08:00
|
|
|
this.setFocusedElement(target);
|
|
|
|
|
2012-12-18 15:10:18 -08:00
|
|
|
let kbOpened = this.sendKeyboardState(target);
|
2012-11-07 08:32:09 -08:00
|
|
|
if (this.isTextInputElement(target))
|
2012-11-07 03:53:24 -08:00
|
|
|
this.isKeyboardOpened = kbOpened;
|
|
|
|
},
|
|
|
|
|
2012-12-18 15:10:18 -08:00
|
|
|
hideKeyboard: function fa_hideKeyboard() {
|
2012-11-07 03:53:24 -08:00
|
|
|
sendAsyncMessage("Forms:Input", { "type": "blur" });
|
|
|
|
this.isKeyboardOpened = false;
|
2012-12-21 11:08:56 -08:00
|
|
|
this.setFocusedElement(null);
|
2012-11-07 03:53:24 -08: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 08:32:09 -08:00
|
|
|
isTextInputElement: function fa_isTextInputElement(element) {
|
|
|
|
return element instanceof HTMLInputElement ||
|
2012-11-12 00:52:24 -08:00
|
|
|
element instanceof HTMLTextAreaElement ||
|
2013-01-04 07:00:21 -08:00
|
|
|
isContentEditable(element);
|
|
|
|
},
|
|
|
|
|
|
|
|
getTopLevelEditable: function fa_getTopLevelEditable(element) {
|
|
|
|
function retrieveTopLevelEditable(element) {
|
|
|
|
// Retrieve the top element that is editable
|
|
|
|
if (element instanceof HTMLHtmlElement)
|
|
|
|
element = element.ownerDocument.body;
|
|
|
|
else if (element instanceof HTMLDocument)
|
|
|
|
element = element.body;
|
|
|
|
|
|
|
|
while (element && !isContentEditable(element))
|
|
|
|
element = element.parentNode;
|
|
|
|
|
|
|
|
// Return the container frame if we are into a nested editable frame
|
|
|
|
if (element &&
|
|
|
|
element instanceof HTMLBodyElement &&
|
|
|
|
element.ownerDocument.defaultView != content.document.defaultView)
|
|
|
|
return element.ownerDocument.defaultView.frameElement;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (element instanceof HTMLIFrameElement)
|
|
|
|
return element;
|
|
|
|
|
|
|
|
return retrieveTopLevelEditable(element) || element;
|
2012-11-07 08:32:09 -08:00
|
|
|
},
|
|
|
|
|
2012-12-18 15:10:18 -08:00
|
|
|
sendKeyboardState: function(element) {
|
2012-06-06 05:19:33 -07:00
|
|
|
// FIXME/bug 729623: work around apparent bug in the IME manager
|
|
|
|
// in gecko.
|
|
|
|
let readonly = element.getAttribute("readonly");
|
2012-06-26 07:01:53 -07:00
|
|
|
if (readonly) {
|
2012-06-06 05:19:33 -07:00
|
|
|
return false;
|
2012-06-26 07:01:53 -07:00
|
|
|
}
|
2012-06-06 05:19:33 -07:00
|
|
|
|
|
|
|
sendAsyncMessage("Forms:Input", getJSON(element));
|
|
|
|
return true;
|
2013-03-15 05:28:51 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
// Notify when the selection range changes
|
|
|
|
updateSelection: function fa_updateSelection() {
|
|
|
|
let range = getSelectionRange(this.focusedElement);
|
|
|
|
if (range[0] != this.selectionStart || range[1] != this.selectionEnd) {
|
|
|
|
this.selectionStart = range[0];
|
|
|
|
this.selectionEnd = range[1];
|
|
|
|
sendAsyncMessage("Forms:SelectionChange", {
|
|
|
|
selectionStart: range[0],
|
|
|
|
selectionEnd: range[1]
|
|
|
|
});
|
|
|
|
}
|
2012-06-06 05:19:33 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
FormAssistant.init();
|
|
|
|
|
2013-01-04 07:00:21 -08:00
|
|
|
function isContentEditable(element) {
|
2013-04-13 23:50:53 -07:00
|
|
|
if (!element) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-01-04 07:00:21 -08:00
|
|
|
if (element.isContentEditable || element.designMode == "on")
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// If a body element is editable and the body is the child of an
|
|
|
|
// iframe we can assume this is an advanced HTML editor
|
|
|
|
if (element instanceof HTMLIFrameElement &&
|
|
|
|
element.contentDocument &&
|
|
|
|
(element.contentDocument.body.isContentEditable ||
|
|
|
|
element.contentDocument.designMode == "on"))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return element.ownerDocument && element.ownerDocument.designMode == "on";
|
|
|
|
}
|
|
|
|
|
2012-06-06 05:19:33 -07:00
|
|
|
function getJSON(element) {
|
2012-06-26 07:01:53 -07:00
|
|
|
let type = element.type || "";
|
2013-01-01 03:26:49 -08:00
|
|
|
let value = element.value || "";
|
2013-03-19 06:08:28 -07:00
|
|
|
let max = element.max || "";
|
|
|
|
let min = element.min || "";
|
2012-11-12 00:52:24 -08:00
|
|
|
|
2013-01-01 03:26:49 -08:00
|
|
|
// Treat contenteditble element as a special text area field
|
2013-01-04 07:00:21 -08:00
|
|
|
if (isContentEditable(element)) {
|
2013-01-01 03:26:49 -08:00
|
|
|
type = "textarea";
|
|
|
|
value = getContentEditableText(element);
|
2012-11-12 00:52:24 -08:00
|
|
|
}
|
2012-06-26 07:01:53 -07:00
|
|
|
|
2013-01-10 04:25:30 -08:00
|
|
|
// Until the input type=date/datetime/range have been implemented
|
2012-08-08 12:46:38 -07:00
|
|
|
// let's return their real type even if the platform returns 'text'
|
2012-06-06 05:19:33 -07:00
|
|
|
let attributeType = element.getAttribute("type") || "";
|
2012-08-07 09:09:38 -07:00
|
|
|
|
|
|
|
if (attributeType) {
|
|
|
|
var typeLowerCase = attributeType.toLowerCase();
|
|
|
|
switch (typeLowerCase) {
|
|
|
|
case "datetime":
|
|
|
|
case "datetime-local":
|
2013-01-10 04:25:30 -08:00
|
|
|
case "range":
|
2012-08-07 09:09:38 -07:00
|
|
|
type = typeLowerCase;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-06-06 05:19:33 -07:00
|
|
|
|
2012-11-15 06:38:47 -08: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-04 23:21:10 -07:00
|
|
|
if (inputmode) {
|
|
|
|
inputmode = inputmode.toLowerCase();
|
2012-10-12 14:51:13 -07:00
|
|
|
} else {
|
|
|
|
inputmode = '';
|
2012-10-04 23:21:10 -07:00
|
|
|
}
|
|
|
|
|
2013-01-01 03:26:49 -08:00
|
|
|
let range = getSelectionRange(element);
|
|
|
|
|
2012-06-06 05:19:33 -07:00
|
|
|
return {
|
|
|
|
"type": type.toLowerCase(),
|
2012-09-29 09:32:57 -07:00
|
|
|
"choices": getListForElement(element),
|
2012-11-12 00:52:24 -08:00
|
|
|
"value": value,
|
2012-10-04 23:21:10 -07:00
|
|
|
"inputmode": inputmode,
|
2013-01-01 03:26:49 -08:00
|
|
|
"selectionStart": range[0],
|
2013-03-19 06:08:28 -07:00
|
|
|
"selectionEnd": range[1],
|
|
|
|
"max": max,
|
|
|
|
"min": min
|
2012-06-06 05:19:33 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2012-06-06 05:19:33 -07: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 03:26:49 -08: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 |
|
|
|
|
Ci.nsIDocumentEncoder.OutputLFLineBreak |
|
|
|
|
Ci.nsIDocumentEncoder.OutputDropInvisibleBreak;
|
|
|
|
encoder.init(element.ownerDocument, "text/plain", flags);
|
|
|
|
return encoder;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the visible content text of a content editable element
|
|
|
|
function getContentEditableText(element) {
|
|
|
|
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;
|
|
|
|
if (element instanceof HTMLInputElement ||
|
|
|
|
element instanceof HTMLTextAreaElement) {
|
|
|
|
// Get the selection range of <input> and <textarea> elements
|
|
|
|
start = element.selectionStart;
|
|
|
|
end = element.selectionEnd;
|
2013-03-08 20:14:16 -08:00
|
|
|
} else if (isContentEditable(element)){
|
2013-01-01 03:26:49 -08:00
|
|
|
// Get the selection range of contenteditable elements
|
|
|
|
let win = element.ownerDocument.defaultView;
|
|
|
|
let sel = win.getSelection();
|
2013-03-15 05:28:51 -07:00
|
|
|
start = getContentEditableSelectionStart(element, sel);
|
|
|
|
end = start + getContentEditableSelectionLength(element, sel);
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
let isPlainTextField = element instanceof HTMLInputElement ||
|
|
|
|
element instanceof HTMLTextAreaElement;
|
|
|
|
|
|
|
|
// Check the parameters
|
|
|
|
|
|
|
|
if (!isPlainTextField && !isContentEditable(element)) {
|
|
|
|
// Skip HTMLOptionElement and HTMLSelectElement elements, as they don't
|
|
|
|
// support the operation of setSelectionRange
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let text = isPlainTextField ? element.value : getContentEditableText(element);
|
|
|
|
let length = text.length;
|
|
|
|
if (start < 0) {
|
|
|
|
start = 0;
|
|
|
|
}
|
|
|
|
if (end > length) {
|
|
|
|
end = length;
|
|
|
|
}
|
|
|
|
if (start > end) {
|
|
|
|
start = end;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isPlainTextField) {
|
|
|
|
// Set the selection range of <input> and <textarea> elements
|
|
|
|
element.setSelectionRange(start, end, "forward");
|
|
|
|
} 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 03:26:49 -08:00
|
|
|
|
2013-03-15 05:28:51 -07:00
|
|
|
while (getContentEditableSelectionStart(element, sel) < start) {
|
|
|
|
sel.modify("move", "forward", "character");
|
|
|
|
}
|
2013-01-01 03:26:49 -08:00
|
|
|
|
2013-03-15 05:28:51 -07:00
|
|
|
// Extend the selection to the end position
|
|
|
|
for (let i = start; i < end; i++) {
|
|
|
|
sel.modify("extend", "forward", "character");
|
|
|
|
}
|
2013-01-01 03:26:49 -08:00
|
|
|
|
2013-03-15 05:28:51 -07:00
|
|
|
let selectionLength = end - start;
|
|
|
|
while (getContentEditableSelectionLength(element, sel) < selectionLength) {
|
|
|
|
sel.modify("extend", "forward", "character");
|
|
|
|
}
|
2013-01-01 03:26:49 -08:00
|
|
|
}
|
|
|
|
}
|
2013-03-15 05:28:51 -07:00
|
|
|
|