private boolean matchesSelectionTextNodes(Text nodelet, int affectedAfterOffset) {
   if (savedSelection == null) {
     return false;
   }
   if (savedSelection.isOrdered()) {
     if (nodelet == savedSelectionAnchorTextNodelet) {
       return savedSelectionAnchorOffset > affectedAfterOffset;
     } else if (nodelet == savedSelectionFocusTextNodelet) {
       return true;
     }
   } else {
     // The inverse of the above
     if (nodelet == savedSelectionFocusTextNodelet) {
       return savedSelectionFocusOffset > affectedAfterOffset;
     } else if (nodelet == savedSelectionAnchorTextNodelet) {
       return true;
     }
   }
   return false;
 }
  /**
   * Same as {@link #restoreSelection()}, but if the selection is actually required to be explicitly
   * set, transform its saved location with the given modifier.
   */
  public void restoreSelection(DocOp modifier) {
    savedSelectionDepth--;

    if (editingConcerns != null && savedSelectionDepth == 0) {
      try {
        if (savedSelection != null) {
          // selectionChangedInappropriately() only deals with selection boundaries. If we had
          // a ranged selection, and we're in a browser where changing it internally matters,
          // then just always restore the selection. This is safe to do w.r.t. IMEs because it's
          // unlikely to have a ranged selection during an uncommitted IME state.
          needToRestoreSelection |=
              QuirksConstants.RANGED_SELECTION_AFFECTED_BY_INTERNAL_CHANGED
                  && !savedSelection.isCollapsed();

          if (needToRestoreSelection || selectionChangedInappropriately()) {
            if (modifier != null) {
              savedSelection = RangeHelper.applyModifier(savedSelection, modifier);
            }

            EditorStaticDeps.logger.trace().log("Restoring selection");
            if (document.size() >= 4) {
              editingConcerns.getSelectionHelper().setSelectionRange(savedSelection);
            }
          } else {
            EditorStaticDeps.logger.trace().log("Not restoring selection");
          }
        }
      } finally {
        savedSelection = null;
        savedSelectionAnchor = null;
        savedSelectionAnchorTextNodelet = null;
        savedSelectionAnchorOffset = 0;
        savedSelectionFocus = null;
        savedSelectionFocusTextNodelet = null;
        savedSelectionFocusOffset = 0;
      }
    }
  }