hterm: push selection logic down into copySelectionToClipboard
In preparation for using newer permission & clipboard APIs (which are
all Promise based), rework this API to accept the content we want to
copy directly. If we have to use the document.execCommand method to
copy the data, we'll create the required nodes on the fly. This helps
break up the changes into separate commits: converting existing APIs
before supporting newer APIs.
This step is particularly important because the newer APIs are both
asynchronous and take the state to be copied directly. The current
execCommand API operates on the implicit state of the document and
whatever selection it has active (which is why we had to generate the
random nodes in the first place).
This also allows for more directed unittest coverage.
Bug: chromium:758466
Change-Id: I9a384d790b2ef0d73d30819ab03a16bbedf2d656
Reviewed-on: https://chromium-review.googlesource.com/c/1393864
Reviewed-by: Vitaliy Shipitsyn <vsh@google.com>
Tested-by: Mike Frysinger <vapier@chromium.org>
diff --git a/hterm/js/hterm_terminal.js b/hterm/js/hterm_terminal.js
index 8f72c9c..ea8db00 100644
--- a/hterm/js/hterm_terminal.js
+++ b/hterm/js/hterm_terminal.js
@@ -3060,40 +3060,7 @@
if (this.prefs_.get('enable-clipboard-notice'))
setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
- var copySource = this.document_.createElement('pre');
- copySource.id = 'hterm:copy-to-clipboard-source';
- copySource.textContent = str;
- copySource.style.cssText = (
- '-webkit-user-select: text;' +
- '-moz-user-select: text;' +
- 'position: absolute;' +
- 'top: -99px');
-
- this.document_.body.appendChild(copySource);
-
- var selection = this.document_.getSelection();
- var anchorNode = selection.anchorNode;
- var anchorOffset = selection.anchorOffset;
- var focusNode = selection.focusNode;
- var focusOffset = selection.focusOffset;
-
- // FF sometimes throws NS_ERROR_FAILURE exceptions when we make this call.
- // Catch it because a failure here leaks the copySource node.
- // https://bugzilla.mozilla.org/show_bug.cgi?id=1178676
- try {
- selection.selectAllChildren(copySource);
- } catch (ex) {}
-
- hterm.copySelectionToClipboard(this.document_);
-
- // IE doesn't support selection.extend. This means that the selection
- // won't return on IE.
- if (selection.extend) {
- selection.collapse(anchorNode, anchorOffset);
- selection.extend(focusNode, focusOffset);
- }
-
- copySource.parentNode.removeChild(copySource);
+ hterm.copySelectionToClipboard(this.document_, str);
};
/**