1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 const Cc = Components.classes;
8 const Ci = Components.interfaces;
9 const Cu = Components.utils;
11 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
13 XPCOMUtils.defineLazyModuleGetter(this, "BrowserUtils",
14 "resource://gre/modules/BrowserUtils.jsm");
16 this.EXPORTED_SYMBOLS = [
20 this.SelectContentHelper = function (aElement, aGlobal) {
21 this.element = aElement;
22 this.global = aGlobal;
27 this.SelectContentHelper.prototype = {
29 this.global.addMessageListener("Forms:SelectDropDownItem", this);
30 this.global.addMessageListener("Forms:DismissedDropDown", this);
31 this.global.addEventListener("pagehide", this);
35 this.global.removeMessageListener("Forms:SelectDropDownItem", this);
36 this.global.removeMessageListener("Forms:DismissedDropDown", this);
37 this.global.removeEventListener("pagehide", this);
42 showDropDown: function() {
43 let rect = this._getBoundingContentRect();
45 this.global.sendAsyncMessage("Forms:ShowDropDown", {
47 options: this._buildOptionList(),
48 selectedIndex: this.element.selectedIndex,
52 _getBoundingContentRect: function() {
53 return BrowserUtils.getElementBoundingScreenRect(this.element);
56 _buildOptionList: function() {
57 return buildOptionListForChildren(this.element);
60 receiveMessage: function(message) {
61 switch (message.name) {
62 case "Forms:SelectDropDownItem":
63 if (this.element.selectedIndex != message.data.value) {
64 this.element.selectedIndex = message.data.value;
66 let event = this.element.ownerDocument.createEvent("Events");
67 event.initEvent("change", true, true);
68 this.element.dispatchEvent(event);
71 //intentional fall-through
72 case "Forms:DismissedDropDown":
78 handleEvent: function(event) {
81 this.global.sendAsyncMessage("Forms:HideDropDown", {});
89 function buildOptionListForChildren(node) {
91 for (let child = node.firstChild; child; child = child.nextSibling) {
92 if (child.tagName == 'OPTION' || child.tagName == 'OPTGROUP') {
94 tagName: child.tagName,
95 textContent: child.tagName == 'OPTGROUP' ? child.getAttribute("label")
97 // XXX this uses a highlight color when this is the selected element.
98 // We need to suppress such highlighting in the content process to get
99 // the option's correct unhighlighted color here.
100 // We also need to detect default color vs. custom so that a standard
101 // color does not override color: menutext in the parent.
102 // backgroundColor: computedStyle.backgroundColor,
103 // color: computedStyle.color,
104 children: child.tagName == 'OPTGROUP' ? buildOptionListForChildren(child) : []