anchor.diff:            controller.add(e);
           };
   
  -        // Handle cases where the mouse is down on one element, dragged, and
  -        // then released on another element. This catches clicks too, in real
  -        // browsers.
  -        mouseUpListener = document.onMouseUp.listen(listener);
  -
  -        // In tests, we generally only see click events and not mouseups. So
  -        // listen to those too.
  +        // If new trigger logic is enabled listen to mouse down events. When it
  +        // is not enabled old logic will be used because the lastMouseDown event
  +        // will always be null.
  +        if (enableNewTriggerLogic) {
  +          // Keep track of mousedown events so that we can filter mouseup events
  +          // that occurred on a different element than the mousedown.
  +          mouseDownListener = document.onMouseDown.listen((MouseEvent e) {
  +            lastDownEvent = e;
  +          });
  +        }
  +
  +        // Listen to mouseup to prevent scenarios where a single click event
  +        // both opens and closes an element.
  +        mouseUpListener = document.onMouseUp.listen((MouseEvent e) {
  +          // Allow for the event to be listened to if there was no down event
  +          // for example if it was canceled or if the target is the same as
  +          // where the 'click' started.
  +          if (lastDownEvent == null || e.target == lastDownEvent.target) {
  +            listener(e);
  +          }
  +          lastEvent = e;
  +        });
  +
           clickListener = document.onClick.listen((MouseEvent e) {
             // Ignore the click if we just saw a mouseup on the same element... it
  -          // probably means that that mouseup was part of this same click
  -          if (lastEvent?.type == 'mouseup' && e.target == lastEvent?.target)
  +          // probably means that the mouseup was part of this same click.
  +          //
  +          // This prevents scenarios where clicking an element that displays
  +          // another element (e.g. a button to open a popup) inadvertently
  +          // triggers an "outside" event, immediately hiding the just-displayed
  +          // element.
  +          if (lastEvent?.type == 'mouseup' && e.target == lastEvent?.target) {
               return;
  -          listener(e);
  +          }
  +          // Allow for the event to be listened to if there was no down event
  +          // for example if it was canceled or if the target is the same as
  +          // where the 'click' started.
  +          if (lastDownEvent == null || e.target == lastDownEvent.target) {
  +            listener(e);
  +          }
  +          lastDownEvent = null;
           });
   
           // Since 'focusin' event is not supported in Firefox, listen to 'focus'
  
positive.code:   return triggersOutsideAny((node) => node == element);
  }
  
  /// A stream of click, mouseup or focus events of any node none of whose parents
  /// pass the check inside function.
  Stream<Event> triggersOutsideAny(Predicate<Node> checkNodeInside) {
    StreamController<Event> controller;
    StreamSubscription<MouseEvent> clickListener;
    StreamSubscription<MouseEvent> mouseUpListener;
    EventListener listener;
  
    controller = StreamController.broadcast(
        sync: true,
        onListen: () {
          assert(clickListener == null);
          assert(mouseUpListener == null);
  
          Event lastEvent;
  
          listener = (Event e) {
            lastEvent = e;
  
negative.code:       onCancel: () {
          clickListener.cancel();
          clickListener = null;
          mouseDownListener?.cancel();
          mouseDownListener = null;
          mouseUpListener.cancel();
          mouseUpListener = null;
          document.removeEventListener('focus', listener, true);
  