hterm: Update the selection when accessibility is enabled even if it's collapsed

This updates the html cursor (selection) to be in the same location
as the terminal cursor. We generally only do this if the selection
is collapsed to prevent clearing a selection that a user has set.
However when a screen reader is enabled, it is expected that
input should cause the selection to move to the cursor. So in this
case we always update selection, even if it's collapsed.

Bug: 822490, 646690
Change-Id: I455b29e18dbdc0365baef0f14aad9400bd348c79
Reviewed-on: https://chromium-review.googlesource.com/1133604
Reviewed-by: Mike Frysinger <vapier@chromium.org>
Tested-by: Raymes Khoury <raymes@chromium.org>
diff --git a/hterm/js/hterm_terminal.js b/hterm/js/hterm_terminal.js
index 12fb3ed..140d225 100644
--- a/hterm/js/hterm_terminal.js
+++ b/hterm/js/hterm_terminal.js
@@ -2757,11 +2757,19 @@
   var cursorRowIndex = this.scrollbackRows_.length +
       this.screen_.cursorPosition.row;
 
+  let forceSyncSelection = false;
   if (this.accessibilityReader_.accessibilityEnabled) {
     // Report the new position of the cursor for accessibility purposes.
     const cursorColumnIndex = this.screen_.cursorPosition.column;
     const cursorLineText =
         this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
+    // This will force the selection to be sync'd to the cursor position if the
+    // user has pressed a key. Generally we would only sync the cursor position
+    // when selection is collapsed so that if the user has selected something
+    // we don't clear the selection by moving the selection. However when a
+    // screen reader is used, it's intuitive for entering a key to move the
+    // selection to the cursor.
+    forceSyncSelection = this.accessibilityReader_.hasUserGesture;
     this.accessibilityReader_.afterCursorChange(
         cursorLineText, cursorRowIndex, cursorColumnIndex);
   }
@@ -2794,8 +2802,9 @@
 
   // Update the caret for a11y purposes.
   var selection = this.document_.getSelection();
-  if (selection && selection.isCollapsed)
+  if (selection && (selection.isCollapsed || forceSyncSelection)) {
     this.screen_.syncSelectionCaret(selection);
+  }
 };
 
 /**