B2G/gecko/browser/components/sessionstore/src/DocumentUtils.jsm
/**
   * Low-level function to restore a form value to a DOMNode.
   *
   * If you want a higher-level interface, see mergeFormData().
   *
   * When the value is changed, the function will fire the appropriate DOM
   * events.
   *
   * @param  aNode
   *         DOMNode to set form value on.
   * @param  aValue
   *         Value to set form element to.
   * @param  aDocument [optional]
   *         DOMDocument node belongs to. If not defined, node.ownerDocument
   *         is used.
   */
Low-level function to restore a form value to a DOMNode.
If you want a higher-level interface, see mergeFormData().

When the value is changed, the function will fire the appropriate DOM
events.

@param  aNode
DOMNode to set form value on.
@param  aValue
Value to set form element to.
@param  aDocument [optional]
DOMDocument node belongs to. If not defined, node.ownerDocument
is used.
Low - level function to restore a form value to a DOMNode . If you want a higher - level interface see mergeFormData () . When the value is changed the function will fire the appropriate DOM events . @param aNode DOMNode to set form value on . @param aValue Value to set form element to . @param aDocument [ optional ] DOMDocument node belongs to . If not defined node . ownerDocument is used .
function DocumentUtils_restoreFormValue(aNode, aValue, aDocument) {
    aDocument = aDocument || aNode.ownerDocument;
    let eventType;
    if (typeof aValue == "string" && aNode.type != "file") {
      if (aNode.value == aValue) {
        return;
      }
      aNode.value = aValue;
      eventType = "input";
    } else if (typeof aValue == "boolean") {
      if (aNode.checked == aValue) {
        return;
      }
      aNode.checked = aValue;
      eventType = "change";
    } else if (typeof aValue == "number") {
      if (aNode.selectedIndex == aValue) {
        return;
      }
      if (aValue < aNode.options.length) {
        aNode.selectedIndex = aValue;
        eventType = "change";
      }
    } else if (aValue && aValue.selectedIndex >= 0 && aValue.value) {
      if (aNode.options[aNode.selectedIndex].value == aValue.value) {
        return;
      }
      for (let i = 0; i < aNode.options.length; i++) {
        if (aNode.options[i].value == aValue.value) {
          aNode.selectedIndex = i;
          break;
        }
      }
      eventType = "change";
    } else if (aValue && aValue.fileList && aValue.type == "file" &&
      aNode.type == "file") {
      aNode.mozSetFileNameArray(aValue.fileList, aValue.fileList.length);
      eventType = "input";
    } else if (aValue && typeof aValue.indexOf == "function" && aNode.options) {
      Array.forEach(aNode.options, function(opt, index) {
        opt.selected = aValue.indexOf(opt.value) > -1;
        if (!opt.defaultSelected) {
          eventType = "change";
        }
      });
    }
    if (eventType) {
      let event = aDocument.createEvent("UIEvents");
      event.initUIEvent(eventType, true, true, aDocument.defaultView, 0);
      aNode.dispatchEvent(event);
    }
  }
function DocumentUtils_restoreFormValue ( aNode , aValue , aDocument ) { aDocument = aDocument || aNode . ownerDocument ; let eventType ; if ( typeof aValue == "string" && aNode . type != "file" ) { if ( aNode . value == aValue ) { return ; } aNode . value = aValue ; eventType = "input" ; } else if ( typeof aValue == "boolean" ) { if ( aNode . checked == aValue ) { return ; } aNode . checked = aValue ; eventType = "change" ; } else if ( typeof aValue == "number" ) { if ( aNode . selectedIndex == aValue ) { return ; } if ( aValue < aNode . options . length ) { aNode . selectedIndex = aValue ; eventType = "change" ; } } else if ( aValue && aValue . selectedIndex >= 0 && aValue . value ) { if ( aNode . options [ aNode . selectedIndex ] . value == aValue . value ) { return ; } for ( let i = 0 ; i < aNode . options . length ; i ++ ) { if ( aNode . options [ i ] . value == aValue . value ) { aNode . selectedIndex = i ; break ; } } eventType = "change" ; } else if ( aValue && aValue . fileList && aValue . type == "file" && aNode . type == "file" ) { aNode . mozSetFileNameArray ( aValue . fileList , aValue . fileList . length ) ; eventType = "input" ; } else if ( aValue && typeof aValue . indexOf == "function" && aNode . options ) { Array . forEach ( aNode . options , function ( opt , index ) { opt . selected = aValue . indexOf ( opt . value ) > - 1 ; if ( ! opt . defaultSelected ) { eventType = "change" ; } } ) ; } if ( eventType ) { let event = aDocument . createEvent ( "UIEvents" ) ; event . initUIEvent ( eventType , true , true , aDocument . defaultView , 0 ) ; aNode . dispatchEvent ( event ) ; } }
Low-level function to restore a form value to a DOMNode.
Low - level function to restore a form value to a DOMNode .
// Don't dispatch an input event if there is no change. // Don't dispatch a change event for no change. // handle select backwards compatibility, example { "#id" : index } // We saved the value blindly since selects take more work to determine // default values. So now we should check to avoid unnecessary events. // handle select new format // Don't dispatch a change event for no change // find first option with matching aValue if possible // don't worry about malformed options with same values // Only fire the event here if this wasn't selected by default // Fire events for this node if applicable