home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / xulrunner-1.9.0.14 / chrome / toolkit.jar / content / global / bindings / autocomplete.xml next >
Encoding:
Extensible Markup Language  |  2008-04-22  |  54.8 KB  |  1,530 lines

  1. <?xml version="1.0"?>
  2.  
  3.  
  4. <bindings id="autocompleteBindings"
  5.           xmlns="http://www.mozilla.org/xbl"
  6.           xmlns:html="http://www.w3.org/1999/xhtml"
  7.           xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
  8.           xmlns:xbl="http://www.mozilla.org/xbl">
  9.  
  10.   <binding id="autocomplete"
  11.            extends="chrome://global/content/bindings/textbox.xml#textbox">
  12.     <resources>
  13.       <stylesheet src="chrome://global/skin/autocomplete.css"/>
  14.     </resources>
  15.  
  16.     <content sizetopopup="pref">
  17.       <xul:hbox class="autocomplete-textbox-container" flex="1" xbl:inherits="focused">
  18.         <children includes="image|deck|stack|box">
  19.           <xul:image class="autocomplete-icon" allowevents="true"/>
  20.         </children>
  21.  
  22.         <xul:hbox anonid="textbox-input-box" class="textbox-input-box" flex="1" xbl:inherits="tooltiptext=inputtooltiptext">
  23.           <children/>
  24.           <html:input anonid="input" class="autocomplete-textbox textbox-input"
  25.                       flex="1" allowevents="true"
  26.                       xbl:inherits="tooltiptext=inputtooltiptext,onfocus,onblur,value,type,maxlength,disabled,size,readonly,userAction"/>
  27.         </xul:hbox>
  28.         <children includes="hbox"/>
  29.       </xul:hbox>
  30.  
  31.       <xul:dropmarker anonid="historydropmarker" class="autocomplete-history-dropmarker"
  32.                       allowevents="true"
  33.                       xbl:inherits="open,enablehistory,parentfocused=focused"/>
  34.  
  35.       <xul:popupset anonid="popupset" class="autocomplete-result-popupset"/>
  36.     </content>
  37.  
  38.     <implementation implements="nsIAccessibleProvider, nsIAutoCompleteInput, nsIDOMXULMenuListElement">
  39.       <field name="mController">null</field>
  40.       <field name="mSearchNames">null</field>
  41.       <field name="mIgnoreInput">false</field>
  42.       <field name="mEnterEvent">null</field>
  43.       <field name="mConsumeRollupEvent">false</field>
  44.  
  45.       <constructor><![CDATA[
  46.         mController = Components.classes["@mozilla.org/autocomplete/controller;1"].
  47.                         getService(Components.interfaces.nsIAutoCompleteController);
  48.       ]]></constructor>
  49.  
  50.       <!-- =================== nsIAccessibleProvider =================== -->
  51.  
  52.       <property name="accessibleType" readonly="true">
  53.         <getter>
  54.           <![CDATA[
  55.             // Will be exposed as nsIAccessible::ROLE_AUTOCOMPLETE
  56.             return Components.interfaces.nsIAccessibleProvider.XULCombobox;
  57.           ]]>
  58.         </getter>
  59.       </property>
  60.  
  61.       <!-- =================== nsIAutoCompleteInput =================== -->
  62.  
  63.       <field name="popup"><![CDATA[
  64.         var popup = null;
  65.         var popupId = this.getAttribute("autocompletepopup");
  66.         if (popupId)
  67.           popup = document.getElementById(popupId);
  68.         if (!popup) {
  69.           popup = document.createElement("panel");
  70.           popup.setAttribute("type", "autocomplete");
  71.           popup.setAttribute("noautofocus", "true");
  72.           
  73.           var popupset = document.getAnonymousElementByAttribute(this, "anonid", "popupset");
  74.           popupset.appendChild(popup);
  75.         }
  76.         popup.mInput = this;
  77.         popup;
  78.       ]]></field>
  79.  
  80.       <property name="controller" onget="return this.mController;" readonly="true"/>
  81.  
  82.       <property name="popupOpen"
  83.                 onget="return this.popup.popupOpen;"
  84.                 onset="if (val) this.openPopup(); else this.closePopup();"/>
  85.  
  86.       <property name="disableAutoComplete"
  87.                 onset="this.setAttribute('disableautocomplete', val); return val;"
  88.                 onget="return this.getAttribute('disableautocomplete') == 'true';"/>
  89.  
  90.       <property name="completeDefaultIndex"
  91.                 onset="this.setAttribute('completedefaultindex', val); return val;"
  92.                 onget="return this.getAttribute('completedefaultindex') == 'true';"/>
  93.  
  94.       <property name="completeSelectedIndex"
  95.                 onset="this.setAttribute('completeselectedindex', val); return val;"
  96.                 onget="return this.getAttribute('completeselectedindex') == 'true';"/>
  97.  
  98.       <property name="forceComplete"
  99.                 onset="this.setAttribute('forcecomplete', val); return val;"
  100.                 onget="return this.getAttribute('forcecomplete') == 'true';"/>
  101.  
  102.       <property name="minResultsForPopup"
  103.                 onset="this.setAttribute('minresultsforpopup', val); return val;"
  104.                 onget="return parseInt(this.getAttribute('minresultsforpopup')) || 0;"/>
  105.  
  106.       <property name="showCommentColumn"
  107.                 onset="this.setAttribute('showcommentcolumn', val); return val;"
  108.                 onget="return this.getAttribute('showcommentcolumn') == 'true';"/>
  109.  
  110.       <property name="showImageColumn"
  111.                 onset="this.setAttribute('showimagecolumn', val); return val;"
  112.                 onget="return this.getAttribute('showimagecolumn') == 'true';"/>
  113.  
  114.       <property name="timeout"
  115.                 onset="this.setAttribute('timeout', val); return val;"
  116.                 onget="return parseInt(this.getAttribute('timeout')) || 50;"/>
  117.  
  118.       <property name="searchParam"
  119.                 onget="return this.getAttribute('autocompletesearchparam');"
  120.                 onset="this.setAttribute('autocompletesearchparam', val); return val;"/>
  121.  
  122.       <property name="searchCount" readonly="true"
  123.                 onget="this.initSearchNames(); return this.mSearchNames.length;"/>
  124.  
  125.       <property name="consumeRollupEvent" readonly="true"
  126.                 onget="return this.mConsumeRollupEvent;"/>
  127.  
  128.       <!-- This is the maximum number of drop-down rows we get when we
  129.             hit the drop marker beside fields that have it (like the URLbar).-->
  130.       <field name="maxDropMarkerRows" readonly="true">14</field>
  131.  
  132.       <method name="getSearchAt">
  133.         <parameter name="aIndex"/>
  134.         <body><![CDATA[
  135.           this.initSearchNames();
  136.           return this.mSearchNames[aIndex];
  137.         ]]></body>
  138.       </method>
  139.  
  140.       <property name="textValue"
  141.                 onget="return this.value;">
  142.         <setter><![CDATA[
  143.           // Completing a result should simulate the user typing the result,
  144.           // so fire an input event.
  145.           this.value = val;
  146.           var evt = document.createEvent("UIEvents");
  147.           evt.initUIEvent("input", true, false, window, 0);
  148.           this.mIgnoreInput = true;
  149.           this.dispatchEvent(evt);
  150.           this.mIgnoreInput = false;
  151.           return this.value;
  152.         ]]></setter>
  153.       </property>
  154.  
  155.       <method name="selectTextRange">
  156.         <parameter name="aStartIndex"/>
  157.         <parameter name="aEndIndex"/>
  158.         <body><![CDATA[
  159.           this.inputField.setSelectionRange(aStartIndex, aEndIndex);
  160.         ]]></body>
  161.       </method>
  162.  
  163.       <method name="onSearchBegin">
  164.         <body><![CDATA[
  165.           this.fireEvent("searchbegin");
  166.         ]]></body>
  167.       </method>
  168.  
  169.       <method name="onSearchComplete">
  170.         <body><![CDATA[
  171.           if (this.mController.matchCount == 0)
  172.             this.popup.setAttribute("nomatch", "true");
  173.           else
  174.             this.popup.removeAttribute("nomatch");
  175.  
  176.           this.fireEvent("searchcomplete");
  177.         ]]></body>
  178.       </method>
  179.  
  180.       <method name="onTextEntered">
  181.         <body><![CDATA[
  182.           var rv = this.fireEvent("textentered", this.mEnterEvent);
  183.           this.mEnterEvent = null;
  184.           return rv;
  185.         ]]></body>
  186.       </method>
  187.  
  188.       <method name="onTextReverted">
  189.         <body><![CDATA[
  190.           return this.fireEvent("textreverted");
  191.         ]]></body>
  192.       </method>
  193.  
  194.       <!-- =================== nsIDOMXULMenuListElement =================== -->
  195.  
  196.       <property name="editable" readonly="true"
  197.                 onget="return true;" />
  198.  
  199.       <property name="crop"
  200.                 onset="this.setAttribute('crop',val); return val;"
  201.                 onget="return this.getAttribute('crop');"/>
  202.  
  203.       <property name="open"
  204.                 onget="return this.getAttribute('open') == 'true';">
  205.         <setter><![CDATA[
  206.           if (val)
  207.             this.showHistoryPopup();
  208.           else
  209.             this.closePopup();
  210.         ]]></setter>
  211.       </property>
  212.  
  213.       <!-- =================== PUBLIC MEMBERS =================== -->
  214.  
  215.       <property name="value"
  216.                 onget="return this.hasAttribute('empty') ? '' : this.inputField.value;">
  217.         <setter><![CDATA[
  218.           this.mIgnoreInput = true;
  219.           if (val) {
  220.             // clear the emptyText _before_ setting a new non-empty value
  221.             this._clearEmptyText();
  222.             this.inputField.value = val;
  223.           } else {
  224.             // display the emptyText _after_ setting a value that's an empty string
  225.             this.inputField.value = val;
  226.             this._updateVisibleText();
  227.           }
  228.           this.mIgnoreInput = false;
  229.           var event = document.createEvent('Events');
  230.           event.initEvent('ValueChange', true, true);
  231.           this.inputField.dispatchEvent(event);
  232.           return val;
  233.         ]]></setter>
  234.       </property>
  235.  
  236.       <property name="focused" readonly="true"
  237.                 onget="return this.getAttribute('focused') == 'true';"/>
  238.  
  239.       <!-- maximum number of rows to display at a time -->
  240.       <property name="maxRows"
  241.                 onset="this.setAttribute('maxrows', val); return val;"
  242.                 onget="return parseInt(this.getAttribute('maxrows')) || 0;"/>
  243.  
  244.       <!-- option to allow scrolling through the list via the tab key, rather than
  245.            tab moving focus out of the textbox -->
  246.       <property name="tabScrolling"
  247.                 onset="return this.setAttribute('tabscrolling', val); return val;"
  248.                 onget="return this.getAttribute('tabscrolling') == 'true';"/>
  249.  
  250.       <!-- disable key navigation handling in the popup results -->
  251.       <property name="disableKeyNavigation"
  252.                 onset="this.setAttribute('disablekeynavigation', val); return val;"
  253.                 onget="return this.getAttribute('disablekeynavigation') == 'true';"/>
  254.  
  255.       <!-- option to completely ignore any blur events while  
  256.            searches are still going on.  This is useful so that nothing
  257.            gets autopicked if the window is required to lose focus for
  258.            some reason (eg in LDAP autocomplete, another window may be
  259.            brought up so that the user can enter a password to authenticate
  260.            to an LDAP server).  -->
  261.       <property name="ignoreBlurWhileSearching"
  262.                 onset="this.setAttribute('ignoreblurwhilesearching', val); return val;"
  263.                 onget="return this.getAttribute('ignoreblurwhilesearching') == 'true';"/>
  264.  
  265.       <!-- =================== PRIVATE MEMBERS =================== -->
  266.  
  267.       <!-- ::::::::::::: autocomplete controller ::::::::::::: -->
  268.  
  269.       <method name="attachController">
  270.         <body><![CDATA[
  271.           this.mController.input = this;
  272.         ]]></body>
  273.       </method>
  274.  
  275.       <method name="detachController">
  276.         <body><![CDATA[
  277.           try { 
  278.             if  (this.mController.input == this)
  279.             this.mController.input = null;
  280.           } catch (ex) {
  281.             // nothing really to do.
  282.           }       
  283.         ]]></body>
  284.       </method>
  285.  
  286.       <!-- ::::::::::::: popup opening ::::::::::::: -->
  287.  
  288.       <method name="openPopup">
  289.         <body><![CDATA[
  290.           this.popup.openAutocompletePopup(this, this);
  291.         ]]></body>
  292.       </method>
  293.  
  294.       <method name="closePopup">
  295.         <body><![CDATA[
  296.           this.mConsumeRollupEvent = false;
  297.           this.popup.closePopup();
  298.         ]]></body>
  299.       </method>
  300.  
  301.       <method name="showHistoryPopup">
  302.         <body><![CDATA[
  303.           // history dropmarker pushed state
  304.           function cleanup(popup) {
  305.             popup.removeEventListener("popupshowing", onShow, false);
  306.           }
  307.           function onShow(event) {
  308.             var popup = event.target, input = popup.input;
  309.             cleanup(popup);
  310.             input.setAttribute("open", "true");
  311.             function onHide() {
  312.               input.removeAttribute("open");
  313.               input.mConsumeRollupEvent = false;
  314.               popup.removeEventListener("popuphiding", onHide, false);
  315.             }
  316.             popup.addEventListener("popuphiding", onHide, false);
  317.           }
  318.           this.popup.addEventListener("popupshowing", onShow, false);
  319.           setTimeout(cleanup, 1000, this.popup);
  320.  
  321.           // Store our "normal" maxRows on the popup, so that it can reset the
  322.           // value when the popup is hidden.
  323.           this.popup._normalMaxRows = this.maxRows;
  324.  
  325.           // Increase our maxRows temporarily, since we want the dropdown to
  326.           // be bigger in this case. The popup's popupshowing/popuphiding
  327.           // handlers will take care of resetting this.
  328.           this.maxRows = this.maxDropMarkerRows;
  329.  
  330.           // Ensure that we have focus.
  331.           if (!this.focused)
  332.             this.focus();
  333.           this.mConsumeRollupEvent = true;
  334.           this.attachController();
  335.           this.mController.startSearch("");
  336.         ]]></body>
  337.       </method>
  338.  
  339.       <method name="toggleHistoryPopup">
  340.         <body><![CDATA[
  341.           if (!this.popup.mPopupOpen)
  342.             this.showHistoryPopup();
  343.           else
  344.             this.closePopup();
  345.         ]]></body>
  346.       </method>
  347.  
  348.       <!-- ::::::::::::: event dispatching ::::::::::::: -->
  349.  
  350.       <method name="fireEvent">
  351.         <parameter name="aEventType"/>
  352.         <body><![CDATA[
  353.           var cancel = false;
  354.           // handle any xml attribute event handlers
  355.           var handler = this.getAttribute("on"+aEventType);
  356.           if (handler) {
  357.             var fn = new Function("eventType", "param", handler);
  358.             cancel = fn.apply(this, arguments);
  359.           }
  360.  
  361.           return cancel;
  362.         ]]></body>
  363.       </method>
  364.  
  365.       <!-- ::::::::::::: key handling ::::::::::::: -->
  366.  
  367.       <method name="onKeyPress">
  368.         <parameter name="aEvent"/>
  369.         <body><![CDATA[
  370.           if (aEvent.target.localName != "textbox")
  371.             return;  // Let child buttons of autocomplete take input
  372.  
  373.           //XXXpch this is so bogus...
  374.           if (aEvent.getPreventDefault())
  375.             return false;
  376.  
  377.           var cancel = false;
  378.  
  379.           // Catch any keys that could potentially move the caret. Ctrl can be
  380.           // used in combination with these keys on Windows and Linux; and Alt
  381.           // can be used on OS X, so make sure the unused one isn't used.
  382.           if (!this.disableKeyNavigation &&
  383.               !aEvent.altKey) {
  384.             switch (aEvent.keyCode) {
  385.               case KeyEvent.DOM_VK_LEFT:
  386.               case KeyEvent.DOM_VK_RIGHT:
  387.               case KeyEvent.DOM_VK_HOME:
  388.                 cancel = this.mController.handleKeyNavigation(aEvent.keyCode);
  389.                 break;
  390.             }
  391.           }
  392.  
  393.           // Handle keys that are not part of a keyboard shortcut (no Ctrl or Alt)
  394.           if (!this.disableKeyNavigation && !aEvent.ctrlKey && !aEvent.altKey) {
  395.             switch (aEvent.keyCode) {
  396.               case KeyEvent.DOM_VK_TAB:
  397.                 if (this.tabScrolling && this.popup.mPopupOpen)
  398.                   cancel = this.mController.handleKeyNavigation(aEvent.shiftKey ?
  399.                                                                 KeyEvent.DOM_VK_UP :
  400.                                                                 KeyEvent.DOM_VK_DOWN);
  401.                 break;
  402.               case KeyEvent.DOM_VK_UP:
  403.               case KeyEvent.DOM_VK_DOWN:
  404.               case KeyEvent.DOM_VK_PAGE_UP:
  405.               case KeyEvent.DOM_VK_PAGE_DOWN:
  406.                 cancel = this.mController.handleKeyNavigation(aEvent.keyCode);
  407.                 break;
  408.             }
  409.           }
  410.  
  411.           // Handle keys we know aren't part of a shortcut, even with Alt or
  412.           // Ctrl.
  413.           switch (aEvent.keyCode) {
  414.             case KeyEvent.DOM_VK_ESCAPE:
  415.               cancel = this.mController.handleEscape();
  416.               break;
  417.             case KeyEvent.DOM_VK_RETURN:
  418.               this.mEnterEvent = aEvent;
  419.               cancel = this.mController.handleEnter(false);
  420.               break;
  421.             case KeyEvent.DOM_VK_DELETE:
  422.               cancel = this.mController.handleDelete();
  423.               break;
  424.             case KeyEvent.DOM_VK_DOWN:
  425.             case KeyEvent.DOM_VK_UP:
  426.               if (aEvent.altKey)
  427.                 this.toggleHistoryPopup();
  428.               break;
  429.             case KeyEvent.DOM_VK_F4:
  430.               this.toggleHistoryPopup();
  431.               break;
  432.           }
  433.  
  434.           if (cancel) {
  435.             aEvent.stopPropagation();
  436.             aEvent.preventDefault();
  437.           }
  438.           
  439.           return true;
  440.         ]]></body>
  441.       </method>
  442.  
  443.       <!-- ::::::::::::: miscellaneous ::::::::::::: -->
  444.  
  445.       <method name="initSearchNames">
  446.         <body><![CDATA[
  447.           if (!this.mSearchNames) {
  448.             var names = this.getAttribute("autocompletesearch");
  449.             if (!names)
  450.               this.mSearchNames = [];
  451.             else
  452.               this.mSearchNames = names.split(" ");
  453.           }
  454.         ]]></body>
  455.       </method>
  456.  
  457.       <method name="ifSetAttribute">
  458.         <parameter name="aAttr"/>
  459.         <parameter name="aVal"/>
  460.         <body><![CDATA[
  461.           if (!this.hasAttribute(aAttr))
  462.             this.setAttribute(aAttr, aVal);
  463.         ]]></body>
  464.       </method>
  465.  
  466.       <method name="_focus">
  467.         <!-- doesn't reset this.mController -->
  468.         <body><![CDATA[
  469.           this._dontBlur = true;
  470.           this.focus();
  471.           this._dontBlur = false;
  472.         ]]></body>
  473.       </method>
  474.  
  475.     </implementation>
  476.  
  477.     <handlers>
  478.       <handler event="input"
  479.                action="if (!this.mIgnoreInput && this.mController.input == this) this.mController.handleText(false);"/>
  480.  
  481.       <handler event="keypress" phase="capturing"
  482.                action="return this.onKeyPress(event);"/>
  483.  
  484.       <handler event="compositionstart" phase="capturing"
  485.                action="if (this.mController.input == this) this.mController.handleStartComposition();"/>
  486.  
  487.       <handler event="compositionend" phase="capturing"
  488.                action="if (this.mController.input == this) this.mController.handleEndComposition();"/>
  489.  
  490.       <handler event="focus" phase="capturing"
  491.                action="this.attachController();"/>
  492.  
  493.       <handler event="blur" phase="capturing"
  494.                action="if (!this._dontBlur) this.detachController();"/>
  495.     </handlers>
  496.   </binding>
  497.  
  498.   <binding id="autocomplete-result-popup" extends="chrome://global/content/bindings/autocomplete.xml#autocomplete-base-popup">
  499.     <resources>
  500.       <stylesheet src="chrome://global/skin/tree.css"/>
  501.       <stylesheet src="chrome://global/skin/autocomplete.css"/>
  502.     </resources>
  503.  
  504.     <content ignorekeys="true">
  505.       <xul:tree anonid="tree" class="autocomplete-tree plain" hidecolumnpicker="true" flex="1" seltype="single">
  506.         <xul:treecols anonid="treecols">
  507.           <xul:treecol id="treecolAutoCompleteValue" class="autocomplete-treecol" flex="1" overflow="true"/>
  508.         </xul:treecols>
  509.         <xul:treechildren class="autocomplete-treebody"/>
  510.       </xul:tree>
  511.     </content>
  512.  
  513.     <implementation>
  514.       <field name="mShowCommentColumn">false</field>
  515.       <field name="mShowImageColumn">false</field>
  516.  
  517.       <property name="showCommentColumn"
  518.                    onget="return this.mShowCommentColumn;">
  519.         <setter>
  520.           <![CDATA[
  521.           if (!val && this.mShowCommentColumn) {
  522.             // reset the flex on the value column and remove the comment column
  523.             document.getElementById("treecolAutoCompleteValue").setAttribute("flex", 1);
  524.             this.removeColumn("treecolAutoCompleteComment");
  525.           } else if (val && !this.mShowCommentColumn) {
  526.             // reset the flex on the value column and add the comment column
  527.             document.getElementById("treecolAutoCompleteValue").setAttribute("flex", 2);
  528.             this.addColumn({id: "treecolAutoCompleteComment", flex: 1});
  529.           }
  530.           this.mShowCommentColumn = val;
  531.           return val;
  532.         ]]>
  533.         </setter>
  534.       </property>
  535.  
  536.       <property name="showImageColumn"
  537.                 onget="return this.mShowImageColumn;">
  538.         <setter>
  539.           <![CDATA[
  540.           if (!val && this.mShowImageColumn) {
  541.             // remove the image column
  542.             this.removeColumn("treecolAutoCompleteImage");
  543.           } else if (val && !this.mShowImageColumn) {
  544.             // add the image column
  545.             this.addColumn({id: "treecolAutoCompleteImage", flex: 1});
  546.           }
  547.           this.mShowImageColumn = val;
  548.           return val;
  549.         ]]>
  550.         </setter>
  551.       </property>
  552.  
  553.  
  554.       <method name="addColumn">
  555.         <parameter name="aAttrs"/>
  556.         <body>
  557.           <![CDATA[
  558.           var col = document.createElement("treecol");
  559.           col.setAttribute("class", "autocomplete-treecol");
  560.           for (var name in aAttrs)
  561.             col.setAttribute(name, aAttrs[name]);
  562.           this.treecols.appendChild(col);
  563.           return col;
  564.         ]]>
  565.         </body>
  566.       </method>
  567.  
  568.       <method name="removeColumn">
  569.         <parameter name="aColId"/>
  570.         <body>
  571.           <![CDATA[
  572.           return this.treecols.removeChild(document.getElementById(aColId));
  573.         ]]>
  574.         </body>
  575.       </method>
  576.  
  577.       <property name="selectedIndex"
  578.                 onget="return this.tree.currentIndex;">
  579.         <setter>
  580.           <![CDATA[
  581.           this.tree.view.selection.select(val);
  582.           if (this.tree.treeBoxObject.height > 0)
  583.             this.tree.treeBoxObject.ensureRowIsVisible(val < 0 ? 0 : val);
  584.           // Fire select event on xul:tree so that accessibility API
  585.           // support layer can fire appropriate accessibility events.
  586.           var event = document.createEvent('Events');
  587.           event.initEvent("select", true, true);
  588.           this.tree.dispatchEvent(event);
  589.           return val;
  590.         ]]></setter>
  591.       </property>
  592.  
  593.       <method name="adjustHeight">
  594.         <body>
  595.           <![CDATA[
  596.           // detect the desired height of the tree
  597.           var bx = this.tree.treeBoxObject;
  598.           var view = this.tree.view;
  599.           if (!view)
  600.             return;
  601.           var rows = this.maxRows;
  602.           if (!view.rowCount || (rows && view.rowCount < rows))
  603.             rows = view.rowCount;
  604.           
  605.           var height = rows * bx.rowHeight;
  606.           
  607.           if (height == 0)
  608.             this.tree.setAttribute("collapsed", "true");
  609.           else {
  610.             if (this.tree.hasAttribute("collapsed"))
  611.               this.tree.removeAttribute("collapsed");
  612.  
  613.             this.tree.setAttribute("height", height);
  614.           }
  615.           this.tree.setAttribute("hidescrollbar", view.rowCount <= rows);
  616.         ]]>
  617.         </body>
  618.       </method>
  619.  
  620.       <method name="openAutocompletePopup">
  621.         <parameter name="aInput"/>
  622.         <parameter name="aElement"/>
  623.         <body><![CDATA[
  624.           // until we have "baseBinding", (see bug #373652) this allows
  625.           // us to override openAutocompletePopup(), but still call
  626.           // the method on the base class
  627.           this._openAutocompletePopup(aInput, aElement);
  628.         ]]></body>
  629.       </method>
  630.  
  631.       <method name="_openAutocompletePopup">
  632.         <parameter name="aInput"/>
  633.         <parameter name="aElement"/>
  634.         <body><![CDATA[
  635.           if (!this.mPopupOpen) {
  636.             this.mInput = aInput;
  637.             this.view = aInput.controller.QueryInterface(Components.interfaces.nsITreeView);
  638.             this.invalidate();
  639.  
  640.             this.showCommentColumn = this.mInput.showCommentColumn;
  641.             this.showImageColumn = this.mInput.showImageColumn;
  642.  
  643.             document.popupNode = null;
  644.  
  645.             var rect = aElement.getBoundingClientRect();
  646.             var nav = aElement.ownerDocument.defaultView.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
  647.                               .getInterface(Components.interfaces.nsIWebNavigation);
  648.             var docShell = nav.QueryInterface(Components.interfaces.nsIDocShell);
  649.             var docViewer = docShell.contentViewer.QueryInterface(Components.interfaces.nsIMarkupDocumentViewer);
  650.             var width = (rect.right - rect.left) * docViewer.fullZoom;
  651.             this.setAttribute("width", width > 100 ? width : 100);
  652.  
  653.             // setConsumeRollupEvent() before we call openPopup()
  654.             var nsIPopupBO = Components.interfaces.nsIPopupBoxObject;
  655.             this.popupBoxObject.setConsumeRollupEvent(
  656.               this.mInput.consumeRollupEvent ? 
  657.                 nsIPopupBO.ROLLUP_CONSUME : 
  658.                 nsIPopupBO.ROLLUP_NO_CONSUME);
  659.             this.openPopup(aElement, "after_start", 0, 0, false, false);
  660.           }
  661.         ]]></body>
  662.       </method>
  663.  
  664.       <method name="invalidate">
  665.         <body><![CDATA[
  666.           this.adjustHeight();
  667.           this.tree.treeBoxObject.invalidate();
  668.         ]]></body>
  669.       </method>
  670.  
  671.       <method name="selectBy">
  672.         <parameter name="aReverse"/>
  673.         <parameter name="aPage"/>
  674.         <body><![CDATA[
  675.           try {
  676.             var amount = aPage ? 5 : 1;
  677.             this.selectedIndex = this.getNextIndex(aReverse, amount, this.selectedIndex, this.tree.view.rowCount-1);
  678.             if (this.selectedIndex == -1) {
  679.               this.input._focus();
  680.             }
  681.           } catch (ex) {
  682.             // do nothing - occasionally timer-related js errors happen here
  683.             // e.g. "this.selectedIndex has no properties", when you type fast and hit a
  684.             // navigation key before this popup has opened
  685.           }
  686.         ]]></body>
  687.       </method>
  688.  
  689.       <!-- =================== PUBLIC MEMBERS =================== -->
  690.  
  691.       <field name="tree">
  692.         document.getAnonymousElementByAttribute(this, "anonid", "tree");
  693.       </field>
  694.  
  695.       <field name="treecols">
  696.         document.getAnonymousElementByAttribute(this, "anonid", "treecols");
  697.       </field>
  698.  
  699.       <property name="view" 
  700.                 onget="return this.mView;">
  701.         <setter><![CDATA[
  702.           // We must do this by hand because the tree binding may not be ready yet
  703.           this.mView = val;
  704.           var bx = this.tree.boxObject;
  705.           bx = bx.QueryInterface(Components.interfaces.nsITreeBoxObject);
  706.           bx.view = val;
  707.         ]]></setter>
  708.       </property>
  709.  
  710.     </implementation>
  711.   </binding>
  712.  
  713.   <binding id="autocomplete-base-popup" extends="chrome://global/content/bindings/popup.xml#popup">
  714.     <implementation implements="nsIAutoCompletePopup">
  715.       <!-- nsIAccessible from #popup -->
  716.       <property name="accessibleType" readonly="true"
  717.                 onget="return Components.interfaces.nsIAccessibleProvider.NoAccessible;"/>
  718.  
  719.       <field name="mInput">null</field>
  720.       <field name="mPopupOpen">false</field>
  721.  
  722.       <!-- =================== nsIAutoCompletePopup =================== -->
  723.  
  724.       <property name="input" readonly="true"
  725.                 onget="return this.mInput"/>
  726.  
  727.       <property name="overrideValue" readonly="true"
  728.                 onget="return null;"/>
  729.  
  730.       <property name="popupOpen" readonly="true"
  731.                 onget="return this.mPopupOpen;"/>
  732.  
  733.       <method name="closePopup">
  734.         <body>
  735.           <![CDATA[
  736.           if (this.mPopupOpen) {
  737.             this.hidePopup();
  738.             document.popupNode = null;
  739.             this.removeAttribute("width");
  740.           }
  741.         ]]>
  742.         </body>
  743.       </method>
  744.  
  745.       <!-- This is the default number of rows that we give the autocomplete
  746.            popup when the textbox doesn't have a "maxrows" attribute
  747.            for us to use. -->
  748.       <field name="defaultMaxRows" readonly="true">6</field>
  749.  
  750.       <!-- In some cases (e.g. when the input's dropmarker button is clicked),
  751.            the input wants to display a popup with more rows. In that case, it
  752.            should increase its maxRows property and store the "normal" maxRows
  753.            in this field. When the popup is hidden, we restore the input's
  754.            maxRows to the value stored in this field.
  755.  
  756.            This field is set to -1 between uses so that we can tell when it's
  757.            been set by the input and when we need to set it in the popupshowing
  758.            handler. -->
  759.       <field name="_normalMaxRows">-1</field>
  760.  
  761.       <property name="maxRows" readonly="true">
  762.         <getter>
  763.           <![CDATA[
  764.           return (this.mInput && this.mInput.maxRows) || this.defaultMaxRows;
  765.         ]]>
  766.         </getter>
  767.       </property>
  768.  
  769.       <method name="getNextIndex">
  770.         <parameter name="aReverse"/>
  771.         <parameter name="aAmount"/>
  772.         <parameter name="aIndex"/>
  773.         <parameter name="aMaxRow"/>
  774.         <body><![CDATA[
  775.           if (aMaxRow < 0)
  776.             return -1;
  777.           
  778.           var newIdx = aIndex + (aReverse?-1:1)*aAmount;
  779.           if (aReverse && aIndex == -1 || newIdx > aMaxRow && aIndex != aMaxRow)
  780.             newIdx = aMaxRow;
  781.           else if (!aReverse && aIndex == -1 || newIdx < 0 && aIndex != 0)
  782.             newIdx = 0;
  783.           
  784.           if (newIdx < 0 && aIndex == 0 || newIdx > aMaxRow && aIndex == aMaxRow)
  785.             aIndex = -1;
  786.           else
  787.             aIndex = newIdx;
  788.           
  789.           return aIndex;
  790.         ]]></body>
  791.       </method>
  792.  
  793.       <method name="onPopupClick">
  794.         <parameter name="aEvent"/>
  795.         <body><![CDATA[
  796.           var controller = this.view.QueryInterface(Components.interfaces.nsIAutoCompleteController);
  797.           controller.handleEnter(true);
  798.         ]]></body>
  799.       </method>
  800.     </implementation>
  801.  
  802.     <handlers>
  803.       <handler event="popupshowing"><![CDATA[
  804.         // If normalMaxRows wasn't already set by the input, then set it here
  805.         // so that we restore the correct number when the popup is hidden.
  806.         if (this._normalMaxRows < 0)
  807.           this._normalMaxRows = this.mInput.maxRows;
  808.  
  809.         this.mPopupOpen = true;
  810.       ]]></handler>
  811.  
  812.       <handler event="popuphiding"><![CDATA[
  813.         var isListActive = true;
  814.         if (this.selectedIndex == -1)
  815.           isListActive = false;
  816.         var controller = this.view.QueryInterface(Components.interfaces.nsIAutoCompleteController);
  817.         controller.stopSearch();
  818.  
  819.         // when the popup hides, we need to clear the selection
  820.         // otherwise we will use the value of the selected index when the
  821.         // user hits enter.
  822.         // see bug #400671 for details
  823.         this.selectedIndex = -1;
  824.         this.mPopupOpen = false;
  825.  
  826.         // Reset the maxRows property to the cached "normal" value, and reset
  827.         // _normalMaxRows so that we can detect whether it was set by the input
  828.         // when the popupshowing handler runs.
  829.         this.mInput.maxRows = this._normalMaxRows;
  830.         this._normalMaxRows = -1;
  831.         // If the list was being navigated and then closed, make sure
  832.         // we fire accessible focus event back to textbox
  833.         if (isListActive) {
  834.           this.mInput.mIgnoreFocus = true;
  835.           this.mInput._focus();
  836.           this.mInput.mIgnoreFocus = false;
  837.         }
  838.       ]]></handler>
  839.     </handlers>
  840.   </binding>
  841.  
  842.   <binding id="autocomplete-rich-result-popup" extends="chrome://global/content/bindings/autocomplete.xml#autocomplete-base-popup">
  843.     <resources>
  844.       <stylesheet src="chrome://global/skin/autocomplete.css"/>
  845.     </resources>
  846.  
  847.     <content ignorekeys="true">
  848.       <xul:richlistbox anonid="richlistbox" class="autocomplete-richlistbox" flex="1"/>
  849.     </content>
  850.  
  851.     <implementation implements="nsIAutoCompletePopup">
  852.       <field name="_currentIndex">0</field>
  853.  
  854.       <!-- =================== nsIAutoCompletePopup =================== -->
  855.  
  856.       <property name="selectedIndex"
  857.                 onget="return this.richlistbox.selectedIndex;">
  858.         <setter>
  859.           <![CDATA[
  860.           this.richlistbox.selectedIndex = val;
  861.  
  862.           // when clearing the selection (val == -1, so selectedItem will be
  863.           // null), we want to scroll back to the top.  see bug #406194
  864.           this.richlistbox.ensureElementIsVisible(
  865.             this.richlistbox.selectedItem || this.richlistbox.firstChild);
  866.  
  867.           return val;
  868.         ]]>
  869.         </setter>
  870.       </property>
  871.  
  872.       <method name="openAutocompletePopup">
  873.         <parameter name="aInput"/>
  874.         <parameter name="aElement"/>
  875.         <body>
  876.           <![CDATA[
  877.           // until we have "baseBinding", (see bug #373652) this allows
  878.           // us to override openAutocompletePopup(), but still call
  879.           // the method on the base class
  880.           this._openAutocompletePopup(aInput, aElement);
  881.         ]]>
  882.         </body>
  883.       </method>
  884.  
  885.       <method name="_openAutocompletePopup">
  886.         <parameter name="aInput"/>
  887.         <parameter name="aElement"/>
  888.         <body>
  889.           <![CDATA[
  890.           if (!this.mPopupOpen) {
  891.             this.mInput = aInput;
  892.             document.popupNode = null;
  893.  
  894.             var rect = aElement.getBoundingClientRect();
  895.             var width = rect.right - rect.left;
  896.             this.setAttribute("width", width > 100 ? width : 100);
  897.             // invalidate() depends on the width attribute
  898.             this._invalidate();
  899.  
  900.             // setConsumeRollupEvent() before we call openPopup()
  901.             var nsIPopupBO = Components.interfaces.nsIPopupBoxObject;
  902.             this.popupBoxObject.setConsumeRollupEvent(
  903.               this.mInput.consumeRollupEvent ? 
  904.                 nsIPopupBO.ROLLUP_CONSUME : 
  905.                 nsIPopupBO.ROLLUP_NO_CONSUME);
  906.             this.openPopup(aElement, "after_start", 0, 0, false, false);
  907.           }
  908.         ]]>
  909.         </body>
  910.       </method>
  911.  
  912.       <method name="invalidate">
  913.         <body>
  914.           <![CDATA[
  915.           // Don't bother doing work if we're not even showing
  916.           if (!this.mPopupOpen)
  917.             return;
  918.  
  919.           this._invalidate();
  920.           ]]>
  921.         </body>
  922.       </method>
  923.  
  924.       <method name="_invalidate">
  925.         <body>
  926.           <![CDATA[
  927.           // collapsed if no matches
  928.           this.richlistbox.collapsed = (this._matchCount == 0);
  929.  
  930.           // Dynamically update height until richlistbox.rows works (bug 401939)
  931.           // Adjust the height immediately and after the row contents update
  932.           this.adjustHeight();
  933.           setTimeout(function(self) self.adjustHeight(), 0, this);
  934.  
  935.           // make sure to collapse any existing richlistitems
  936.           // that aren't going to be used
  937.           var existingItemsCount = this.richlistbox.childNodes.length;
  938.           for (var i = this._matchCount; i < existingItemsCount; i++)
  939.             this.richlistbox.childNodes[i].collapsed = true;
  940.  
  941.           this._currentIndex = 0;
  942.           this._appendCurrentResult();
  943.         ]]>
  944.         </body>
  945.       </method>
  946.  
  947.       <property name="maxResults" readonly="true">
  948.         <getter>
  949.           <![CDATA[
  950.             // this is how many richlistitems will be kept around
  951.             // (note, this getter may be overridden)
  952.             return 20;
  953.           ]]>
  954.         </getter>
  955.       </property>
  956.  
  957.       <property name="_matchCount" readonly="true">
  958.         <getter>
  959.           <![CDATA[
  960.           return Math.min(this.mInput.controller.matchCount, this.maxResults);
  961.           ]]>
  962.         </getter>
  963.       </property>
  964.  
  965.       <method name="adjustHeight">
  966.         <body>
  967.           <![CDATA[
  968.           // Figure out how many rows to show
  969.           let rows = this.richlistbox.childNodes;
  970.           let numRows = Math.min(this._matchCount, this.maxRows, rows.length);
  971.  
  972.           // Default the height to 0 if we have no rows to show
  973.           let height = 0;
  974.           if (numRows) {
  975.             let lastRowShown = rows[numRows - 1];
  976.  
  977.             // Calculate the height to have the first row to last row shown
  978.             height = lastRowShown.boxObject.y + lastRowShown.boxObject.height -
  979.               rows[0].boxObject.y;
  980.           }
  981.  
  982.           // Only update the height if we have a non-zero height and if it
  983.           // changed (the richlistbox is collapsed if there are no results)
  984.           if (height && height != this.richlistbox.height)
  985.             this.richlistbox.height = height;
  986.           ]]>
  987.         </body>
  988.       </method>
  989.  
  990.       <method name="_appendCurrentResult">
  991.         <body>
  992.           <![CDATA[
  993.           var controller = this.mInput.controller;
  994.  
  995.           // Process maxRows per chunk to improve performance and user experience
  996.           for (let i = 0; i < this.maxRows; i++) {
  997.             if (this._currentIndex >= this._matchCount)
  998.               return;
  999.  
  1000.             var existingItemsCount = this.richlistbox.childNodes.length;
  1001.             var item;
  1002.  
  1003.             // trim the leading/trailing whitespace
  1004.             var trimmedSearchString = controller.searchString.replace(/^\s+/, "").replace(/\s+$/, "");  
  1005.  
  1006.             // Unescape the URI spec for showing as an entry in the popup
  1007.             let url = Components.classes["@mozilla.org/intl/texttosuburi;1"].
  1008.               getService(Components.interfaces.nsITextToSubURI).
  1009.               unEscapeURIForUI("UTF-8", controller.getValueAt(this._currentIndex));
  1010.  
  1011.             if (this._currentIndex < existingItemsCount) {
  1012.               // re-use the existing item
  1013.               item = this.richlistbox.childNodes[this._currentIndex];
  1014.  
  1015.               // Completely re-use the existing richlistitem if it's the same
  1016.               if (item.getAttribute("text") == trimmedSearchString &&
  1017.                   item.getAttribute("url") == url) {
  1018.                 item.collapsed = false;
  1019.                 this._currentIndex++;
  1020.                 continue;
  1021.               }
  1022.             }
  1023.             else {
  1024.               // need to create a new item
  1025.               item = document.createElementNS("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul", "richlistitem");
  1026.             }
  1027.  
  1028.             // set these attributes before we set the class 
  1029.             // so that we can use them from the contructor         
  1030.             item.setAttribute("image", controller.getImageAt(this._currentIndex));
  1031.             item.setAttribute("url", url);
  1032.             item.setAttribute("title", controller.getCommentAt(this._currentIndex));
  1033.             item.setAttribute("type", controller.getStyleAt(this._currentIndex));
  1034.             item.setAttribute("text", trimmedSearchString);
  1035.         
  1036.             if (this._currentIndex < existingItemsCount) {
  1037.               // re-use the existing item
  1038.               item._adjustAcItem();
  1039.               item.collapsed = false;
  1040.             }
  1041.             else {
  1042.               // set the class at the end so we can use the attributes 
  1043.               // in the xbl constructor
  1044.               item.className = "autocomplete-richlistitem";
  1045.               this.richlistbox.appendChild(item);
  1046.             }
  1047.  
  1048.             this._currentIndex++;
  1049.           }
  1050.  
  1051.           // yield after each batch of items so that typing the url bar is responsive
  1052.           setTimeout(function (self) { self._appendCurrentResult(); }, 0, this);
  1053.         ]]>
  1054.         </body>
  1055.       </method>
  1056.  
  1057.       <method name="selectBy">
  1058.         <parameter name="aReverse"/>
  1059.         <parameter name="aPage"/>
  1060.         <body>
  1061.           <![CDATA[
  1062.           try {
  1063.             var amount = aPage ? 5 : 1;
  1064.             
  1065.             // because we collapsed unused items, we can't use this.richlistbox.getRowCount(), we need to use the matchCount
  1066.             this.selectedIndex = this.getNextIndex(aReverse, amount, this.selectedIndex, this._matchCount - 1);
  1067.             if (this.selectedIndex == -1) {
  1068.               this.input._focus();
  1069.             }
  1070.           } catch (ex) {
  1071.             // do nothing - occasionally timer-related js errors happen here
  1072.             // e.g. "this.selectedIndex has no properties", when you type fast and hit a
  1073.             // navigation key before this popup has opened
  1074.           }
  1075.             ]]>
  1076.         </body>
  1077.       </method>
  1078.  
  1079.       <field name="richlistbox">
  1080.         document.getAnonymousElementByAttribute(this, "anonid", "richlistbox");
  1081.       </field>
  1082.  
  1083.       <property name="view" 
  1084.                 onget="return this.mInput.controller;"
  1085.                 onset="return val;"/>
  1086.  
  1087.     </implementation>
  1088.   </binding>
  1089.  
  1090.   <binding id="autocomplete-richlistitem" extends="chrome://global/content/bindings/richlistbox.xml#richlistitem">
  1091.     <content>
  1092.       <xul:hbox align="center">
  1093.         <xul:image xbl:inherits="src=image" class="ac-site-icon"/>
  1094.         <xul:hbox anonid="title-box" class="ac-title" flex="1"
  1095.                   onunderflow="_doUnderflow('_title');">
  1096.           <xul:description anonid="title" class="ac-normal-text ac-comment" xbl:inherits="selected"/>
  1097.           <xul:hbox anonid="extra-box" class="ac-extra" align="center" hidden="true">
  1098.             <xul:image class="ac-result-type-tag"/>
  1099.             <xul:description anonid="extra" class="ac-normal-text ac-comment" xbl:inherits="selected"/>
  1100.           </xul:hbox>
  1101.         </xul:hbox>
  1102.         <xul:label anonid="title-overflow-ellipsis" xbl:inherits="selected"
  1103.                    class="ac-ellipsis-after ac-comment" hidden="true"/>
  1104.         <xul:image anonid="type-image" class="ac-type-icon"/>
  1105.       </xul:hbox>
  1106.       <xul:hbox align="center">
  1107.         <xul:spacer class="ac-site-icon"/>
  1108.         <xul:hbox anonid="url-box" class="ac-url" flex="1"
  1109.                   onunderflow="_doUnderflow('_url');">
  1110.           <xul:description anonid="url" class="ac-normal-text ac-url-text" xbl:inherits="selected"/>
  1111.         </xul:hbox>
  1112.         <xul:label anonid="url-overflow-ellipsis" xbl:inherits="selected" 
  1113.                    class="ac-ellipsis-after ac-url-text" hidden="true"/>
  1114.         <xul:spacer class="ac-type-icon"/>
  1115.       </xul:hbox>
  1116.     </content>
  1117.     <implementation implements="nsIDOMXULSelectControlItemElement">
  1118.       <constructor>
  1119.         <![CDATA[
  1120.             let ellipsis = "\u2026";
  1121.             try {
  1122.               ellipsis = Components.classes["@mozilla.org/preferences-service;1"].
  1123.                 getService(Components.interfaces.nsIPrefBranch).
  1124.                 getComplexValue("intl.ellipsis",
  1125.                   Components.interfaces.nsIPrefLocalizedString).data;
  1126.             } catch (ex) {
  1127.               // Do nothing.. we already have a default
  1128.             }
  1129.  
  1130.             this._urlOverflowEllipsis = document.getAnonymousElementByAttribute(this, "anonid", "url-overflow-ellipsis");  
  1131.             this._titleOverflowEllipsis = document.getAnonymousElementByAttribute(this, "anonid", "title-overflow-ellipsis");
  1132.  
  1133.             this._urlOverflowEllipsis.value = ellipsis;
  1134.             this._titleOverflowEllipsis.value = ellipsis;
  1135.  
  1136.             this._typeImage = document.getAnonymousElementByAttribute(this, "anonid", "type-image");
  1137.  
  1138.             this._urlBox = document.getAnonymousElementByAttribute(this, "anonid", "url-box");
  1139.             this._url = document.getAnonymousElementByAttribute(this, "anonid", "url");
  1140.  
  1141.             this._titleBox = document.getAnonymousElementByAttribute(this, "anonid", "title-box");
  1142.             this._title = document.getAnonymousElementByAttribute(this, "anonid", "title");
  1143.  
  1144.             this._extraBox = document.getAnonymousElementByAttribute(this, "anonid", "extra-box");
  1145.             this._extra = document.getAnonymousElementByAttribute(this, "anonid", "extra");
  1146.  
  1147.             this._adjustAcItem();
  1148.           ]]>
  1149.       </constructor>
  1150.  
  1151.       <property name="label" readonly="true">
  1152.         <getter>
  1153.           <![CDATA[
  1154.             var title = this.getAttribute("title");
  1155.             var url = this.getAttribute("url");
  1156.             var panel = this.parentNode.parentNode;
  1157.  
  1158.             // allow consumers that have extended popups to override 
  1159.             // the label values for the richlistitems
  1160.             if (panel.createResultLabel)
  1161.               return panel.createResultLabel(title, url, this.getAttribute("type"));
  1162.  
  1163.             // aType (ex: "ac-result-type-<aType>") is related to the class of the image, 
  1164.             // and is not "visible" text so don't use it for the label (for accessibility).
  1165.             return title + " " + url;
  1166.           ]]>
  1167.         </getter>
  1168.       </property>
  1169.  
  1170.       <field name="_boundaryCutoff">null</field>
  1171.  
  1172.       <property name="boundaryCutoff" readonly="true">
  1173.         <getter>
  1174.           <![CDATA[
  1175.           if (!this._boundaryCutoff) {
  1176.             this._boundaryCutoff =
  1177.               Components.classes["@mozilla.org/preferences-service;1"].
  1178.               getService(Components.interfaces.nsIPrefBranch).
  1179.               getIntPref("toolkit.autocomplete.richBoundaryCutoff");
  1180.           }
  1181.           return this._boundaryCutoff;
  1182.           ]]>
  1183.         </getter>
  1184.       </property>
  1185.  
  1186.       <method name="_getBoundaryIndices">
  1187.         <parameter name="aText"/>
  1188.         <parameter name="aSearchTokens"/>
  1189.         <body>
  1190.           <![CDATA[
  1191.           // Short circuit for empty search ([""] == "")
  1192.           if (aSearchTokens == "")
  1193.             return [0, aText.length];
  1194.  
  1195.           // Find which regions of text match the search terms
  1196.           let regions = [];
  1197.           for each (let search in aSearchTokens) {
  1198.             let matchIndex;
  1199.             let startIndex = 0;
  1200.             let searchLen = search.length;
  1201.  
  1202.             // Find all matches of the search terms, but stop early for perf
  1203.             let lowerText = aText.toLowerCase().substr(0, this.boundaryCutoff);
  1204.             while ((matchIndex = lowerText.indexOf(search, startIndex)) >= 0) {
  1205.               // Start the next search from where this one finished
  1206.               startIndex = matchIndex + searchLen;
  1207.               regions.push([matchIndex, startIndex]);
  1208.             }
  1209.           }
  1210.  
  1211.           // Sort the regions by start position then end position
  1212.           regions = regions.sort(function(a, b) let (start = a[0] - b[0])
  1213.             start == 0 ? a[1] - b[1] : start);
  1214.  
  1215.           // Generate the boundary indices from each region
  1216.           let start = 0;
  1217.           let end = 0;
  1218.           let boundaries = [];
  1219.           let len = regions.length;
  1220.           for (let i = 0; i < len; i++) {
  1221.             // We have a new boundary if the start of the next is past the end
  1222.             let region = regions[i];
  1223.             if (region[0] > end) {
  1224.               // First index is the beginning of match
  1225.               boundaries.push(start);
  1226.               // Second index is the beginning of non-match
  1227.               boundaries.push(end);
  1228.  
  1229.               // Track the new region now that we've stored the previous one
  1230.               start = region[0];
  1231.             }
  1232.  
  1233.             // Push back the end index for the current or new region
  1234.             end = Math.max(end, region[1]);
  1235.           }
  1236.  
  1237.           // Add the last region
  1238.           boundaries.push(start);
  1239.           boundaries.push(end);
  1240.  
  1241.           // Put on the end boundary if necessary
  1242.           if (end < aText.length)
  1243.             boundaries.push(aText.length);
  1244.  
  1245.           // Skip the first item because it's always 0
  1246.           return boundaries.slice(1);
  1247.           ]]>
  1248.         </body>
  1249.       </method>
  1250.  
  1251.       <method name="_getSearchTokens">
  1252.         <parameter name="aSearch"/>
  1253.         <body>
  1254.           <![CDATA[
  1255.           let search = aSearch.toLowerCase();
  1256.           return search.split(/\s+/);
  1257.           ]]>
  1258.         </body>
  1259.       </method>
  1260.  
  1261.       <method name="_needsAlternateEmphasis">
  1262.         <parameter name="aText"/>
  1263.         <body>
  1264.           <![CDATA[
  1265.           for (let i = aText.length; --i >= 0; ) {
  1266.             let charCode = aText.charCodeAt(i);
  1267.             // Arabic, Syriac, Indic languages are likely to have ligatures
  1268.             // that are broken when using the main emphasis styling
  1269.             if (0x0600 <= charCode && charCode <= 0x109F)
  1270.               return true;
  1271.           }
  1272.  
  1273.           return false;
  1274.           ]]>
  1275.         </body>
  1276.       </method>
  1277.  
  1278.       <method name="_setUpDescription">
  1279.         <parameter name="aDescriptionElement"/>
  1280.         <parameter name="aText"/>
  1281.         <body>
  1282.           <![CDATA[
  1283.           // Get rid of all previous text
  1284.           while (aDescriptionElement.hasChildNodes())
  1285.             aDescriptionElement.removeChild(aDescriptionElement.firstChild);
  1286.  
  1287.           // Get the indices that separate match and non-match text
  1288.           let search = this.getAttribute("text");
  1289.           let tokens = this._getSearchTokens(search);
  1290.           let indices = this._getBoundaryIndices(aText, tokens);
  1291.  
  1292.           // If we're searching for something that needs alternate emphasis,
  1293.           // we'll need to check the text that we match
  1294.           let checkAlt = this._needsAlternateEmphasis(search);
  1295.  
  1296.           let next;
  1297.           let start = 0;
  1298.           let len = indices.length;
  1299.           // Even indexed boundaries are matches, so skip the 0th if it's empty
  1300.           for (let i = indices[0] == 0 ? 1 : 0; i < len; i++) {
  1301.             next = indices[i];
  1302.             let text = aText.substr(start, next - start);
  1303.             start = next;
  1304.  
  1305.             if (i % 2 == 0) {
  1306.               // Emphasize the text for even indices
  1307.               let span = aDescriptionElement.appendChild(
  1308.                 document.createElementNS("http://www.w3.org/1999/xhtml", "span"));
  1309.               span.className = checkAlt && this._needsAlternateEmphasis(text) ?
  1310.                 "ac-emphasize-alt" : "ac-emphasize-text";
  1311.               span.textContent = text;
  1312.             } else {
  1313.               // Otherwise, it's plain text
  1314.               aDescriptionElement.appendChild(document.createTextNode(text));
  1315.             }
  1316.           }
  1317.           ]]>
  1318.         </body>
  1319.       </method>
  1320.  
  1321.       <method name="_adjustAcItem">
  1322.         <body>
  1323.           <![CDATA[
  1324.           var url = this.getAttribute("url");
  1325.           var title = this.getAttribute("title");
  1326.           var type = this.getAttribute("type");
  1327.  
  1328.           // If we have a tag match, show the tags and icon
  1329.           if (type == "tag") {
  1330.             // Configure the extra box for tags display
  1331.             this._extraBox.hidden = false;
  1332.             this._extraBox.flex = 1;
  1333.             this._extraBox.pack = "end";
  1334.  
  1335.             // The title is separated from the tags by an endash
  1336.             let tags;
  1337.             [, title, tags] = title.match(/^(.+) \u2013 (.+)$/);
  1338.  
  1339.             // Each tag is split by a comma in an undefined order, so sort it
  1340.             let sortedTags = tags.split(",").sort().join(", ");
  1341.  
  1342.             // Emphasize the matching text in the tags
  1343.             this._setUpDescription(this._extra, sortedTags);
  1344.  
  1345.             // Treat tagged matches as bookmarks for the star
  1346.             type = "bookmark";
  1347.           } else {
  1348.             // Hide the title's extra box if we don't need extra stuff
  1349.             this._extraBox.hidden = true;
  1350.           }
  1351.  
  1352.           // Give the image the icon style and a special one for the type
  1353.           this._typeImage.className = "ac-type-icon" +
  1354.             (type ? " ac-result-type-" + type : "");
  1355.  
  1356.           // Show the url as the title if we don't have a title
  1357.           if (title == "")
  1358.             title = url;
  1359.  
  1360.           // Emphasize the matching search terms for the description
  1361.           this._setUpDescription(this._title, title);
  1362.           this._setUpDescription(this._url, url);
  1363.  
  1364.           // Set up overflow on a timeout because the contents of the box
  1365.           // might not have a width yet even though we just changed them
  1366.           setTimeout(this._setUpOverflow, 0, this._titleBox, this._titleOverflowEllipsis);
  1367.           setTimeout(this._setUpOverflow, 0, this._urlBox, this._urlOverflowEllipsis);
  1368.           ]]>
  1369.         </body>
  1370.       </method>
  1371.  
  1372.       <method name="_setUpOverflow">
  1373.         <parameter name="aParentBox"/>
  1374.         <parameter name="aEllipsis"/>
  1375.         <body>
  1376.           <![CDATA[
  1377.           // Hide the ellipsis incase there's just enough to not underflow
  1378.           aEllipsis.hidden = true;
  1379.  
  1380.           // Start with the parent's width and subtract off its children
  1381.           let tooltip = [];
  1382.           let children = aParentBox.childNodes;
  1383.           let widthDiff = aParentBox.boxObject.width;
  1384.  
  1385.           for (let i = 0; i < children.length; i++) {
  1386.             // Only consider a child if it actually takes up space
  1387.             let childWidth = children[i].boxObject.width;
  1388.             if (childWidth > 0) {
  1389.               // Subtract a little less to account for subpixel rounding
  1390.               widthDiff -= childWidth - .5;
  1391.  
  1392.               // Add to the tooltip if it's not hidden and has text
  1393.               let childText = children[i].textContent;
  1394.               if (childText)
  1395.                 tooltip.push(childText);
  1396.             }
  1397.           }
  1398.  
  1399.           // If the children take up more space than the parent.. overflow!
  1400.           if (widthDiff < 0) {
  1401.             // Re-show the ellipsis now that we know it's needed
  1402.             aEllipsis.hidden = false;
  1403.  
  1404.             // Separate text components with a ndash --
  1405.             aParentBox.tooltipText = tooltip.join(" \u2013 ");
  1406.           }
  1407.           ]]>
  1408.         </body>
  1409.       </method>
  1410.  
  1411.       <method name="_doUnderflow">
  1412.         <parameter name="aName"/>
  1413.         <body>
  1414.           <![CDATA[
  1415.           // Hide the ellipsis right when we know we're underflowing instead of
  1416.           // waiting for the timeout to trigger the _setUpOverflow calculations
  1417.           this[aName + "Box"].tooltipText = "";
  1418.           this[aName + "OverflowEllipsis"].hidden = true;
  1419.           ]]>
  1420.         </body>
  1421.       </method>
  1422.  
  1423.     </implementation>
  1424.   </binding>
  1425.  
  1426.   <binding id="autocomplete-tree" extends="chrome://global/content/bindings/tree.xml#tree">
  1427.     <content>
  1428.       <children includes="treecols"/>
  1429.       <xul:treerows class="autocomplete-treerows tree-rows" xbl:inherits="hidescrollbar" flex="1">
  1430.         <children/>
  1431.       </xul:treerows>
  1432.     </content>
  1433.   </binding>
  1434.  
  1435.   <binding id="autocomplete-richlistbox" extends="chrome://global/content/bindings/richlistbox.xml#richlistbox">
  1436.     <implementation>
  1437.       <field name="mLastMoveTime">Date.now()</field>
  1438.     </implementation>
  1439.     <handlers>
  1440.       <handler event="mouseup">
  1441.         <![CDATA[
  1442.         // don't call onPopupClick for the scrollbar buttons, thumb, slider, etc.
  1443.         var item = event.originalTarget;
  1444.  
  1445.         while (item && item.localName != "richlistitem")
  1446.           item = item.parentNode;
  1447.  
  1448.         if (!item)
  1449.           return;
  1450.         
  1451.         this.parentNode.onPopupClick(event);
  1452.       ]]>
  1453.       </handler>
  1454.  
  1455.       <handler event="mousemove">
  1456.         <![CDATA[
  1457.         if (Date.now() - this.mLastMoveTime > 30) {
  1458.          var item = event.target;
  1459.  
  1460.          while (item && item.localName != "richlistitem")
  1461.            item = item.parentNode;
  1462.  
  1463.          if (!item)
  1464.            return;
  1465.  
  1466.          var rc = this.getIndexOfItem(item);
  1467.          if (rc != this.selectedIndex)
  1468.             this.selectedIndex = rc;
  1469.  
  1470.          this.mLastMoveTime = Date.now();
  1471.         }
  1472.       ]]>
  1473.       </handler>
  1474.     </handlers>
  1475.   </binding>
  1476.  
  1477.   <binding id="autocomplete-treebody">
  1478.     <implementation>
  1479.       <field name="mLastMoveTime">Date.now()</field>
  1480.     </implementation>
  1481.  
  1482.     <handlers>
  1483.       <handler event="mouseup" action="this.parentNode.parentNode.onPopupClick(event);"/>
  1484.  
  1485.       <handler event="mousedown"><![CDATA[
  1486.          var rc = this.parentNode.treeBoxObject.getRowAt(event.clientX, event.clientY);
  1487.          if (rc != this.parentNode.currentIndex)
  1488.             this.parentNode.view.selection.select(rc);
  1489.       ]]></handler>
  1490.  
  1491.       <handler event="mousemove"><![CDATA[
  1492.         if (Date.now() - this.mLastMoveTime > 30) {
  1493.          var rc = this.parentNode.treeBoxObject.getRowAt(event.clientX, event.clientY);
  1494.          if (rc != this.parentNode.currentIndex)
  1495.             this.parentNode.view.selection.select(rc);
  1496.          this.mLastMoveTime = Date.now();
  1497.         }
  1498.       ]]></handler>
  1499.     </handlers>
  1500.   </binding>
  1501.  
  1502.   <binding id="autocomplete-treerows">
  1503.     <content>
  1504.       <xul:hbox flex="1" class="tree-bodybox">
  1505.         <children/>
  1506.       </xul:hbox>
  1507.       <xul:scrollbar xbl:inherits="collapsed=hidescrollbar" orient="vertical" class="tree-scrollbar"/>
  1508.     </content>
  1509.   </binding>
  1510.  
  1511.   <binding id="history-dropmarker" extends="chrome://global/content/bindings/general.xml#dropmarker">
  1512.     <implementation>
  1513.       <method name="showPopup">
  1514.         <body><![CDATA[
  1515.           var textbox = document.getBindingParent(this);
  1516.           textbox.showHistoryPopup();
  1517.         ]]></body>
  1518.       </method>
  1519.     </implementation>
  1520.  
  1521.     <handlers>
  1522.       <handler event="mousedown" button="0"><![CDATA[
  1523.         this.showPopup();
  1524.       ]]></handler>
  1525.     </handlers>
  1526.   </binding>
  1527.  
  1528. </bindings>
  1529.  
  1530.