rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
rginda | cbbd748 | 2012-06-13 15:06:16 -0700 | [diff] [blame] | 5 | 'use strict'; |
| 6 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 7 | /** |
| 8 | * @fileoverview This class represents a single terminal screen full of text. |
| 9 | * |
| 10 | * It maintains the current cursor position and has basic methods for text |
| 11 | * insert and overwrite, and adding or removing rows from the screen. |
| 12 | * |
| 13 | * This class has no knowledge of the scrollback buffer. |
| 14 | * |
| 15 | * The number of rows on the screen is determined only by the number of rows |
| 16 | * that the caller inserts into the screen. If a caller wants to ensure a |
| 17 | * constant number of rows on the screen, it's their responsibility to remove a |
| 18 | * row for each row inserted. |
| 19 | * |
| 20 | * The screen width, in contrast, is enforced locally. |
| 21 | * |
| 22 | * |
| 23 | * In practice... |
| 24 | * - The hterm.Terminal class holds two hterm.Screen instances. One for the |
| 25 | * primary screen and one for the alternate screen. |
| 26 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 27 | * - The html.Screen class only cares that rows are HTML Elements. In the |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 28 | * larger context of hterm, however, the rows happen to be displayed by an |
| 29 | * hterm.ScrollPort and have to follow a few rules as a result. Each |
| 30 | * row must be rooted by the custom HTML tag 'x-row', and each must have a |
| 31 | * rowIndex property that corresponds to the index of the row in the context |
| 32 | * of the scrollback buffer. These invariants are enforced by hterm.Terminal |
| 33 | * because that is the class using the hterm.Screen in the context of an |
| 34 | * hterm.ScrollPort. |
| 35 | */ |
| 36 | |
| 37 | /** |
| 38 | * Create a new screen instance. |
| 39 | * |
| 40 | * The screen initially has no rows and a maximum column count of 0. |
| 41 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 42 | * @param {number=} columnCount The maximum number of columns for this |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 43 | * screen. See insertString() and overwriteString() for information about |
| 44 | * what happens when too many characters are added too a row. Defaults to |
| 45 | * 0 if not provided. |
Joel Hockey | add2f7e | 2019-09-20 16:37:35 -0700 | [diff] [blame] | 46 | * @constructor |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 47 | */ |
Mike Frysinger | 1adc26e | 2020-04-08 00:17:30 -0400 | [diff] [blame] | 48 | hterm.Screen = function(columnCount = 0) { |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 49 | /** |
| 50 | * Public, read-only access to the rows in this screen. |
Mike Frysinger | 23b5b83 | 2019-10-01 17:05:29 -0400 | [diff] [blame] | 51 | * |
Joel Hockey | add2f7e | 2019-09-20 16:37:35 -0700 | [diff] [blame] | 52 | * @type {!Array<!Element>} |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 53 | */ |
| 54 | this.rowsArray = []; |
| 55 | |
| 56 | // The max column width for this screen. |
Mike Frysinger | 60a156d | 2019-06-13 10:15:45 -0400 | [diff] [blame] | 57 | this.columnCount_ = columnCount; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 58 | |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 59 | // The current color, bold, underline and blink attributes. |
| 60 | this.textAttributes = new hterm.TextAttributes(window.document); |
| 61 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 62 | // Current zero-based cursor coordinates. |
| 63 | this.cursorPosition = new hterm.RowCol(0, 0); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 64 | |
Mike Frysinger | a2cacaa | 2017-11-29 13:51:09 -0800 | [diff] [blame] | 65 | // Saved state used by DECSC and related settings. This is only for saving |
| 66 | // and restoring specific state, not for the current/active state. |
| 67 | this.cursorState_ = new hterm.Screen.CursorState(this); |
| 68 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 69 | // The node containing the row that the cursor is positioned on. |
| 70 | this.cursorRowNode_ = null; |
| 71 | |
| 72 | // The node containing the span of text that the cursor is positioned on. |
| 73 | this.cursorNode_ = null; |
| 74 | |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 75 | // The offset in column width into cursorNode_ where the cursor is positioned. |
Joel Hockey | add2f7e | 2019-09-20 16:37:35 -0700 | [diff] [blame] | 76 | this.cursorOffset_ = 0; |
Mike Frysinger | 664e999 | 2017-05-19 01:24:24 -0400 | [diff] [blame] | 77 | |
| 78 | // Regexes for expanding word selections. |
Joel Hockey | add2f7e | 2019-09-20 16:37:35 -0700 | [diff] [blame] | 79 | /** @type {?string} */ |
Mike Frysinger | 664e999 | 2017-05-19 01:24:24 -0400 | [diff] [blame] | 80 | this.wordBreakMatchLeft = null; |
Joel Hockey | add2f7e | 2019-09-20 16:37:35 -0700 | [diff] [blame] | 81 | /** @type {?string} */ |
Mike Frysinger | 664e999 | 2017-05-19 01:24:24 -0400 | [diff] [blame] | 82 | this.wordBreakMatchRight = null; |
Joel Hockey | add2f7e | 2019-09-20 16:37:35 -0700 | [diff] [blame] | 83 | /** @type {?string} */ |
Mike Frysinger | 664e999 | 2017-05-19 01:24:24 -0400 | [diff] [blame] | 84 | this.wordBreakMatchMiddle = null; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | /** |
| 88 | * Return the screen size as an hterm.Size object. |
| 89 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 90 | * @return {!hterm.Size} hterm.Size object representing the current number |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 91 | * of rows and columns in this screen. |
| 92 | */ |
| 93 | hterm.Screen.prototype.getSize = function() { |
| 94 | return new hterm.Size(this.columnCount_, this.rowsArray.length); |
| 95 | }; |
| 96 | |
| 97 | /** |
| 98 | * Return the current number of rows in this screen. |
| 99 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 100 | * @return {number} The number of rows in this screen. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 101 | */ |
| 102 | hterm.Screen.prototype.getHeight = function() { |
| 103 | return this.rowsArray.length; |
| 104 | }; |
| 105 | |
| 106 | /** |
| 107 | * Return the current number of columns in this screen. |
| 108 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 109 | * @return {number} The number of columns in this screen. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 110 | */ |
| 111 | hterm.Screen.prototype.getWidth = function() { |
| 112 | return this.columnCount_; |
| 113 | }; |
| 114 | |
| 115 | /** |
| 116 | * Set the maximum number of columns per row. |
| 117 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 118 | * @param {number} count The maximum number of columns per row. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 119 | */ |
| 120 | hterm.Screen.prototype.setColumnCount = function(count) { |
rginda | 2312fff | 2012-01-05 16:20:52 -0800 | [diff] [blame] | 121 | this.columnCount_ = count; |
| 122 | |
Mike Frysinger | bdb3480 | 2020-04-07 03:47:32 -0400 | [diff] [blame] | 123 | if (this.cursorPosition.column >= count) { |
rginda | cbbd748 | 2012-06-13 15:06:16 -0700 | [diff] [blame] | 124 | this.setCursorPosition(this.cursorPosition.row, count - 1); |
Mike Frysinger | bdb3480 | 2020-04-07 03:47:32 -0400 | [diff] [blame] | 125 | } |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | /** |
| 129 | * Remove the first row from the screen and return it. |
| 130 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 131 | * @return {!Element} The first row in this screen. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 132 | */ |
| 133 | hterm.Screen.prototype.shiftRow = function() { |
| 134 | return this.shiftRows(1)[0]; |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 135 | }; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 136 | |
| 137 | /** |
| 138 | * Remove rows from the top of the screen and return them as an array. |
| 139 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 140 | * @param {number} count The number of rows to remove. |
| 141 | * @return {!Array<!Element>} The selected rows. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 142 | */ |
| 143 | hterm.Screen.prototype.shiftRows = function(count) { |
| 144 | return this.rowsArray.splice(0, count); |
| 145 | }; |
| 146 | |
| 147 | /** |
| 148 | * Insert a row at the top of the screen. |
| 149 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 150 | * @param {!Element} row The row to insert. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 151 | */ |
| 152 | hterm.Screen.prototype.unshiftRow = function(row) { |
| 153 | this.rowsArray.splice(0, 0, row); |
| 154 | }; |
| 155 | |
| 156 | /** |
| 157 | * Insert rows at the top of the screen. |
| 158 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 159 | * @param {!Array<!Element>} rows The rows to insert. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 160 | */ |
| 161 | hterm.Screen.prototype.unshiftRows = function(rows) { |
| 162 | this.rowsArray.unshift.apply(this.rowsArray, rows); |
| 163 | }; |
| 164 | |
| 165 | /** |
| 166 | * Remove the last row from the screen and return it. |
| 167 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 168 | * @return {!Element} The last row in this screen. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 169 | */ |
| 170 | hterm.Screen.prototype.popRow = function() { |
| 171 | return this.popRows(1)[0]; |
| 172 | }; |
| 173 | |
| 174 | /** |
| 175 | * Remove rows from the bottom of the screen and return them as an array. |
| 176 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 177 | * @param {number} count The number of rows to remove. |
| 178 | * @return {!Array<!Element>} The selected rows. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 179 | */ |
| 180 | hterm.Screen.prototype.popRows = function(count) { |
| 181 | return this.rowsArray.splice(this.rowsArray.length - count, count); |
| 182 | }; |
| 183 | |
| 184 | /** |
| 185 | * Insert a row at the bottom of the screen. |
| 186 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 187 | * @param {!Element} row The row to insert. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 188 | */ |
| 189 | hterm.Screen.prototype.pushRow = function(row) { |
| 190 | this.rowsArray.push(row); |
| 191 | }; |
| 192 | |
| 193 | /** |
| 194 | * Insert rows at the bottom of the screen. |
| 195 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 196 | * @param {!Array<!Element>} rows The rows to insert. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 197 | */ |
| 198 | hterm.Screen.prototype.pushRows = function(rows) { |
| 199 | rows.push.apply(this.rowsArray, rows); |
| 200 | }; |
| 201 | |
| 202 | /** |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 203 | * Insert a row at the specified row of the screen. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 204 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 205 | * @param {number} index The index to insert the row. |
| 206 | * @param {!Element} row The row to insert. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 207 | */ |
| 208 | hterm.Screen.prototype.insertRow = function(index, row) { |
| 209 | this.rowsArray.splice(index, 0, row); |
| 210 | }; |
| 211 | |
| 212 | /** |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 213 | * Insert rows at the specified row of the screen. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 214 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 215 | * @param {number} index The index to insert the rows. |
| 216 | * @param {!Array<!Element>} rows The rows to insert. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 217 | */ |
| 218 | hterm.Screen.prototype.insertRows = function(index, rows) { |
Mike Frysinger | dc72779 | 2020-04-10 01:41:13 -0400 | [diff] [blame^] | 219 | for (let i = 0; i < rows.length; i++) { |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 220 | this.rowsArray.splice(index + i, 0, rows[i]); |
| 221 | } |
| 222 | }; |
| 223 | |
| 224 | /** |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 225 | * Remove a row from the screen and return it. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 226 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 227 | * @param {number} index The index of the row to remove. |
| 228 | * @return {!Element} The selected row. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 229 | */ |
| 230 | hterm.Screen.prototype.removeRow = function(index) { |
| 231 | return this.rowsArray.splice(index, 1)[0]; |
| 232 | }; |
| 233 | |
| 234 | /** |
| 235 | * Remove rows from the bottom of the screen and return them as an array. |
| 236 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 237 | * @param {number} index The index to start removing rows. |
| 238 | * @param {number} count The number of rows to remove. |
| 239 | * @return {!Array<!Element>} The selected rows. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 240 | */ |
| 241 | hterm.Screen.prototype.removeRows = function(index, count) { |
| 242 | return this.rowsArray.splice(index, count); |
| 243 | }; |
| 244 | |
| 245 | /** |
| 246 | * Invalidate the current cursor position. |
| 247 | * |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 248 | * This sets this.cursorPosition to (0, 0) and clears out some internal |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 249 | * data. |
| 250 | * |
| 251 | * Attempting to insert or overwrite text while the cursor position is invalid |
| 252 | * will raise an obscure exception. |
| 253 | */ |
| 254 | hterm.Screen.prototype.invalidateCursorPosition = function() { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 255 | this.cursorPosition.move(0, 0); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 256 | this.cursorRowNode_ = null; |
| 257 | this.cursorNode_ = null; |
Joel Hockey | add2f7e | 2019-09-20 16:37:35 -0700 | [diff] [blame] | 258 | this.cursorOffset_ = 0; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 259 | }; |
| 260 | |
| 261 | /** |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 262 | * Clear the contents of the cursor row. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 263 | */ |
| 264 | hterm.Screen.prototype.clearCursorRow = function() { |
| 265 | this.cursorRowNode_.innerHTML = ''; |
rginda | a09e733 | 2012-08-17 12:49:51 -0700 | [diff] [blame] | 266 | this.cursorRowNode_.removeAttribute('line-overflow'); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 267 | this.cursorOffset_ = 0; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 268 | this.cursorPosition.column = 0; |
rginda | 2312fff | 2012-01-05 16:20:52 -0800 | [diff] [blame] | 269 | this.cursorPosition.overflow = false; |
Robert Ginda | 7fd5708 | 2012-09-25 14:41:47 -0700 | [diff] [blame] | 270 | |
Mike Frysinger | dc72779 | 2020-04-10 01:41:13 -0400 | [diff] [blame^] | 271 | let text; |
Robert Ginda | 7fd5708 | 2012-09-25 14:41:47 -0700 | [diff] [blame] | 272 | if (this.textAttributes.isDefault()) { |
| 273 | text = ''; |
| 274 | } else { |
Mike Frysinger | 73e5646 | 2019-07-17 00:23:46 -0500 | [diff] [blame] | 275 | text = ' '.repeat(this.columnCount_); |
Robert Ginda | 7fd5708 | 2012-09-25 14:41:47 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Zhu Qunying | 30d4071 | 2017-03-14 16:27:00 -0700 | [diff] [blame] | 278 | // We shouldn't honor inverse colors when clearing an area, to match |
| 279 | // xterm's back color erase behavior. |
Mike Frysinger | dc72779 | 2020-04-10 01:41:13 -0400 | [diff] [blame^] | 280 | const inverse = this.textAttributes.inverse; |
Edoardo Spadolini | 2fd4364 | 2014-08-23 22:59:57 +0200 | [diff] [blame] | 281 | this.textAttributes.inverse = false; |
| 282 | this.textAttributes.syncColors(); |
| 283 | |
Mike Frysinger | dc72779 | 2020-04-10 01:41:13 -0400 | [diff] [blame^] | 284 | const node = this.textAttributes.createContainer(text); |
Robert Ginda | 7fd5708 | 2012-09-25 14:41:47 -0700 | [diff] [blame] | 285 | this.cursorRowNode_.appendChild(node); |
| 286 | this.cursorNode_ = node; |
Edoardo Spadolini | 2fd4364 | 2014-08-23 22:59:57 +0200 | [diff] [blame] | 287 | |
| 288 | this.textAttributes.inverse = inverse; |
| 289 | this.textAttributes.syncColors(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 290 | }; |
| 291 | |
| 292 | /** |
rginda | a09e733 | 2012-08-17 12:49:51 -0700 | [diff] [blame] | 293 | * Mark the current row as having overflowed to the next line. |
| 294 | * |
| 295 | * The line overflow state is used when converting a range of rows into text. |
| 296 | * It makes it possible to recombine two or more overflow terminal rows into |
| 297 | * a single line. |
| 298 | * |
| 299 | * This is distinct from the cursor being in the overflow state. Cursor |
| 300 | * overflow indicates that printing at the cursor position will commit a |
| 301 | * line overflow, unless it is preceded by a repositioning of the cursor |
| 302 | * to a non-overflow state. |
| 303 | */ |
| 304 | hterm.Screen.prototype.commitLineOverflow = function() { |
| 305 | this.cursorRowNode_.setAttribute('line-overflow', true); |
| 306 | }; |
| 307 | |
| 308 | /** |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 309 | * Relocate the cursor to a give row and column. |
| 310 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 311 | * @param {number} row The zero based row. |
| 312 | * @param {number} column The zero based column. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 313 | */ |
| 314 | hterm.Screen.prototype.setCursorPosition = function(row, column) { |
rginda | 11057d5 | 2012-04-25 12:29:56 -0700 | [diff] [blame] | 315 | if (!this.rowsArray.length) { |
| 316 | console.warn('Attempt to set cursor position on empty screen.'); |
| 317 | return; |
| 318 | } |
| 319 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 320 | if (row >= this.rowsArray.length) { |
rginda | cbbd748 | 2012-06-13 15:06:16 -0700 | [diff] [blame] | 321 | console.error('Row out of bounds: ' + row); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 322 | row = this.rowsArray.length - 1; |
| 323 | } else if (row < 0) { |
rginda | cbbd748 | 2012-06-13 15:06:16 -0700 | [diff] [blame] | 324 | console.error('Row out of bounds: ' + row); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 325 | row = 0; |
| 326 | } |
| 327 | |
| 328 | if (column >= this.columnCount_) { |
rginda | cbbd748 | 2012-06-13 15:06:16 -0700 | [diff] [blame] | 329 | console.error('Column out of bounds: ' + column); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 330 | column = this.columnCount_ - 1; |
| 331 | } else if (column < 0) { |
rginda | cbbd748 | 2012-06-13 15:06:16 -0700 | [diff] [blame] | 332 | console.error('Column out of bounds: ' + column); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 333 | column = 0; |
| 334 | } |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 335 | |
rginda | 2312fff | 2012-01-05 16:20:52 -0800 | [diff] [blame] | 336 | this.cursorPosition.overflow = false; |
| 337 | |
Mike Frysinger | dc72779 | 2020-04-10 01:41:13 -0400 | [diff] [blame^] | 338 | const rowNode = this.rowsArray[row]; |
| 339 | let node = rowNode.firstChild; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 340 | |
| 341 | if (!node) { |
| 342 | node = rowNode.ownerDocument.createTextNode(''); |
| 343 | rowNode.appendChild(node); |
| 344 | } |
| 345 | |
Mike Frysinger | dc72779 | 2020-04-10 01:41:13 -0400 | [diff] [blame^] | 346 | let currentColumn = 0; |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 347 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 348 | if (rowNode == this.cursorRowNode_) { |
| 349 | if (column >= this.cursorPosition.column - this.cursorOffset_) { |
| 350 | node = this.cursorNode_; |
| 351 | currentColumn = this.cursorPosition.column - this.cursorOffset_; |
| 352 | } |
| 353 | } else { |
| 354 | this.cursorRowNode_ = rowNode; |
| 355 | } |
| 356 | |
| 357 | this.cursorPosition.move(row, column); |
| 358 | |
| 359 | while (node) { |
Mike Frysinger | dc72779 | 2020-04-10 01:41:13 -0400 | [diff] [blame^] | 360 | const offset = column - currentColumn; |
| 361 | const width = hterm.TextAttributes.nodeWidth(node); |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 362 | if (!node.nextSibling || width > offset) { |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 363 | this.cursorNode_ = node; |
| 364 | this.cursorOffset_ = offset; |
| 365 | return; |
| 366 | } |
| 367 | |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 368 | currentColumn += width; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 369 | node = node.nextSibling; |
| 370 | } |
| 371 | }; |
| 372 | |
| 373 | /** |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 374 | * Set the provided selection object to be a caret selection at the current |
| 375 | * cursor position. |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 376 | * |
| 377 | * @param {!Selection} selection |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 378 | */ |
| 379 | hterm.Screen.prototype.syncSelectionCaret = function(selection) { |
Rob Spies | 06533ba | 2014-04-24 11:20:37 -0700 | [diff] [blame] | 380 | try { |
| 381 | selection.collapse(this.cursorNode_, this.cursorOffset_); |
| 382 | } catch (firefoxIgnoredException) { |
| 383 | // FF can throw an exception if the range is off, rather than just not |
| 384 | // performing the collapse. |
| 385 | } |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 386 | }; |
| 387 | |
| 388 | /** |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 389 | * Split a single node into two nodes at the given offset. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 390 | * |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 391 | * For example: |
| 392 | * Given the DOM fragment '<div><span>Hello World</span></div>', call splitNode_ |
Zhu Qunying | 30d4071 | 2017-03-14 16:27:00 -0700 | [diff] [blame] | 393 | * passing the span and an offset of 6. This would modify the fragment to |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 394 | * become: '<div><span>Hello </span><span>World</span></div>'. If the span |
| 395 | * had any attributes they would have been copied to the new span as well. |
| 396 | * |
| 397 | * The to-be-split node must have a container, so that the new node can be |
| 398 | * placed next to it. |
| 399 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 400 | * @param {!Node} node The node to split. |
| 401 | * @param {number} offset The offset into the node where the split should |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 402 | * occur. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 403 | */ |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 404 | hterm.Screen.prototype.splitNode_ = function(node, offset) { |
Mike Frysinger | dc72779 | 2020-04-10 01:41:13 -0400 | [diff] [blame^] | 405 | const afterNode = node.cloneNode(false); |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 406 | |
Mike Frysinger | dc72779 | 2020-04-10 01:41:13 -0400 | [diff] [blame^] | 407 | const textContent = node.textContent; |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 408 | node.textContent = hterm.TextAttributes.nodeSubstr(node, 0, offset); |
| 409 | afterNode.textContent = lib.wc.substr(textContent, offset); |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 410 | |
Mike Frysinger | bdb3480 | 2020-04-07 03:47:32 -0400 | [diff] [blame] | 411 | if (afterNode.textContent) { |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 412 | node.parentNode.insertBefore(afterNode, node.nextSibling); |
Mike Frysinger | bdb3480 | 2020-04-07 03:47:32 -0400 | [diff] [blame] | 413 | } |
| 414 | if (!node.textContent) { |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 415 | node.parentNode.removeChild(node); |
Mike Frysinger | bdb3480 | 2020-04-07 03:47:32 -0400 | [diff] [blame] | 416 | } |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 417 | }; |
| 418 | |
| 419 | /** |
rginda | a9abdd8 | 2012-08-06 18:05:09 -0700 | [diff] [blame] | 420 | * Ensure that text is clipped and the cursor is clamped to the column count. |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 421 | */ |
rginda | a9abdd8 | 2012-08-06 18:05:09 -0700 | [diff] [blame] | 422 | hterm.Screen.prototype.maybeClipCurrentRow = function() { |
Mike Frysinger | dc72779 | 2020-04-10 01:41:13 -0400 | [diff] [blame^] | 423 | let width = hterm.TextAttributes.nodeWidth(lib.notNull(this.cursorRowNode_)); |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 424 | |
| 425 | if (width <= this.columnCount_) { |
rginda | a9abdd8 | 2012-08-06 18:05:09 -0700 | [diff] [blame] | 426 | // Current row does not need clipping, but may need clamping. |
| 427 | if (this.cursorPosition.column >= this.columnCount_) { |
| 428 | this.setCursorPosition(this.cursorPosition.row, this.columnCount_ - 1); |
| 429 | this.cursorPosition.overflow = true; |
| 430 | } |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 431 | |
rginda | a9abdd8 | 2012-08-06 18:05:09 -0700 | [diff] [blame] | 432 | return; |
| 433 | } |
| 434 | |
| 435 | // Save off the current column so we can maybe restore it later. |
Mike Frysinger | dc72779 | 2020-04-10 01:41:13 -0400 | [diff] [blame^] | 436 | const currentColumn = this.cursorPosition.column; |
rginda | a9abdd8 | 2012-08-06 18:05:09 -0700 | [diff] [blame] | 437 | |
| 438 | // Move the cursor to the final column. |
| 439 | this.setCursorPosition(this.cursorPosition.row, this.columnCount_ - 1); |
| 440 | |
| 441 | // Remove any text that partially overflows. |
Joel Hockey | add2f7e | 2019-09-20 16:37:35 -0700 | [diff] [blame] | 442 | width = hterm.TextAttributes.nodeWidth(lib.notNull(this.cursorNode_)); |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 443 | |
| 444 | if (this.cursorOffset_ < width - 1) { |
| 445 | this.cursorNode_.textContent = hterm.TextAttributes.nodeSubstr( |
| 446 | this.cursorNode_, 0, this.cursorOffset_ + 1); |
rginda | a9abdd8 | 2012-08-06 18:05:09 -0700 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | // Remove all nodes after the cursor. |
Mike Frysinger | dc72779 | 2020-04-10 01:41:13 -0400 | [diff] [blame^] | 450 | const rowNode = this.cursorRowNode_; |
| 451 | let node = this.cursorNode_.nextSibling; |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 452 | |
| 453 | while (node) { |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 454 | rowNode.removeChild(node); |
| 455 | node = this.cursorNode_.nextSibling; |
| 456 | } |
| 457 | |
Robert Ginda | 7fd5708 | 2012-09-25 14:41:47 -0700 | [diff] [blame] | 458 | if (currentColumn < this.columnCount_) { |
rginda | a9abdd8 | 2012-08-06 18:05:09 -0700 | [diff] [blame] | 459 | // If the cursor was within the screen before we started then restore its |
| 460 | // position. |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 461 | this.setCursorPosition(this.cursorPosition.row, currentColumn); |
rginda | a9abdd8 | 2012-08-06 18:05:09 -0700 | [diff] [blame] | 462 | } else { |
| 463 | // Otherwise leave it at the the last column in the overflow state. |
| 464 | this.cursorPosition.overflow = true; |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 465 | } |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 466 | }; |
| 467 | |
| 468 | /** |
| 469 | * Insert a string at the current character position using the current |
| 470 | * text attributes. |
| 471 | * |
rginda | a09e733 | 2012-08-17 12:49:51 -0700 | [diff] [blame] | 472 | * You must call maybeClipCurrentRow() after in order to clip overflowed |
| 473 | * text and clamp the cursor. |
| 474 | * |
| 475 | * It is also up to the caller to properly maintain the line overflow state |
| 476 | * using hterm.Screen..commitLineOverflow(). |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 477 | * |
| 478 | * @param {string} str The string to insert. |
| 479 | * @param {number=} wcwidth The cached lib.wc.strWidth value for |str|. Will be |
| 480 | * calculated on demand if need be. Passing in a cached value helps speed |
| 481 | * up processing as this is a hot codepath. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 482 | */ |
Mike Frysinger | 1adc26e | 2020-04-08 00:17:30 -0400 | [diff] [blame] | 483 | hterm.Screen.prototype.insertString = function(str, wcwidth = undefined) { |
Mike Frysinger | dc72779 | 2020-04-10 01:41:13 -0400 | [diff] [blame^] | 484 | let cursorNode = this.cursorNode_; |
| 485 | let cursorNodeText = cursorNode.textContent; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 486 | |
Robert Ginda | a21dfb3 | 2013-10-31 14:17:45 -0700 | [diff] [blame] | 487 | this.cursorRowNode_.removeAttribute('line-overflow'); |
| 488 | |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 489 | // We may alter the width of the string by prepending some missing |
| 490 | // whitespaces, so we need to record the string width ahead of time. |
Mike Frysinger | bdb3480 | 2020-04-07 03:47:32 -0400 | [diff] [blame] | 491 | if (wcwidth === undefined) { |
Mike Frysinger | 6380bed | 2017-08-24 18:46:39 -0400 | [diff] [blame] | 492 | wcwidth = lib.wc.strWidth(str); |
Mike Frysinger | bdb3480 | 2020-04-07 03:47:32 -0400 | [diff] [blame] | 493 | } |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 494 | |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 495 | // No matter what, before this function exits the cursor column will have |
| 496 | // moved this much. |
Mike Frysinger | 6380bed | 2017-08-24 18:46:39 -0400 | [diff] [blame] | 497 | this.cursorPosition.column += wcwidth; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 498 | |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 499 | // Local cache of the cursor offset. |
Mike Frysinger | dc72779 | 2020-04-10 01:41:13 -0400 | [diff] [blame^] | 500 | let offset = this.cursorOffset_; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 501 | |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 502 | // Reverse offset is the offset measured from the end of the string. |
| 503 | // Zero implies that the cursor is at the end of the cursor node. |
Mike Frysinger | dc72779 | 2020-04-10 01:41:13 -0400 | [diff] [blame^] | 504 | let reverseOffset = hterm.TextAttributes.nodeWidth(cursorNode) - offset; |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 505 | |
| 506 | if (reverseOffset < 0) { |
| 507 | // A negative reverse offset means the cursor is positioned past the end |
| 508 | // of the characters on this line. We'll need to insert the missing |
| 509 | // whitespace. |
Mike Frysinger | 73e5646 | 2019-07-17 00:23:46 -0500 | [diff] [blame] | 510 | const ws = ' '.repeat(-reverseOffset); |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 511 | |
Brad Town | 7de8330 | 2015-03-12 02:10:32 -0700 | [diff] [blame] | 512 | // This whitespace should be completely unstyled. Underline, background |
| 513 | // color, and strikethrough would be visible on whitespace, so we can't use |
| 514 | // one of those spans to hold the text. |
Edoardo Spadolini | 198828a | 2014-08-08 00:22:51 +0200 | [diff] [blame] | 515 | if (!(this.textAttributes.underline || |
Brad Town | 7de8330 | 2015-03-12 02:10:32 -0700 | [diff] [blame] | 516 | this.textAttributes.strikethrough || |
Edoardo Spadolini | 198828a | 2014-08-08 00:22:51 +0200 | [diff] [blame] | 517 | this.textAttributes.background || |
| 518 | this.textAttributes.wcNode || |
Mike Frysinger | 1e98c0f | 2017-08-15 01:21:31 -0400 | [diff] [blame] | 519 | !this.textAttributes.asciiNode || |
Edoardo Spadolini | 198828a | 2014-08-08 00:22:51 +0200 | [diff] [blame] | 520 | this.textAttributes.tileData != null)) { |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 521 | // Best case scenario, we can just pretend the spaces were part of the |
| 522 | // original string. |
| 523 | str = ws + str; |
Mike Frysinger | 6a4f241 | 2017-08-31 01:11:25 -0400 | [diff] [blame] | 524 | } else if (cursorNode.nodeType == Node.TEXT_NODE || |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 525 | !(cursorNode.wcNode || |
Mike Frysinger | 1e98c0f | 2017-08-15 01:21:31 -0400 | [diff] [blame] | 526 | !cursorNode.asciiNode || |
Edoardo Spadolini | 198828a | 2014-08-08 00:22:51 +0200 | [diff] [blame] | 527 | cursorNode.tileNode || |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 528 | cursorNode.style.textDecoration || |
Mike Frysinger | 09c54f4 | 2017-12-15 01:12:30 -0500 | [diff] [blame] | 529 | cursorNode.style.textDecorationStyle || |
| 530 | cursorNode.style.textDecorationLine || |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 531 | cursorNode.style.backgroundColor)) { |
| 532 | // Second best case, the current node is able to hold the whitespace. |
| 533 | cursorNode.textContent = (cursorNodeText += ws); |
| 534 | } else { |
| 535 | // Worst case, we have to create a new node to hold the whitespace. |
Mike Frysinger | dc72779 | 2020-04-10 01:41:13 -0400 | [diff] [blame^] | 536 | const wsNode = cursorNode.ownerDocument.createTextNode(ws); |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 537 | this.cursorRowNode_.insertBefore(wsNode, cursorNode.nextSibling); |
| 538 | this.cursorNode_ = cursorNode = wsNode; |
| 539 | this.cursorOffset_ = offset = -reverseOffset; |
| 540 | cursorNodeText = ws; |
| 541 | } |
| 542 | |
| 543 | // We now know for sure that we're at the last character of the cursor node. |
| 544 | reverseOffset = 0; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 545 | } |
| 546 | |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 547 | if (this.textAttributes.matchesContainer(cursorNode)) { |
| 548 | // The new text can be placed directly in the cursor node. |
| 549 | if (reverseOffset == 0) { |
| 550 | cursorNode.textContent = cursorNodeText + str; |
| 551 | } else if (offset == 0) { |
| 552 | cursorNode.textContent = str + cursorNodeText; |
| 553 | } else { |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 554 | cursorNode.textContent = |
| 555 | hterm.TextAttributes.nodeSubstr(cursorNode, 0, offset) + |
| 556 | str + hterm.TextAttributes.nodeSubstr(cursorNode, offset); |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 557 | } |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 558 | |
Mike Frysinger | 6380bed | 2017-08-24 18:46:39 -0400 | [diff] [blame] | 559 | this.cursorOffset_ += wcwidth; |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 560 | return; |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 561 | } |
| 562 | |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 563 | // The cursor node is the wrong style for the new text. If we're at the |
| 564 | // beginning or end of the cursor node, then the adjacent node is also a |
| 565 | // potential candidate. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 566 | |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 567 | if (offset == 0) { |
| 568 | // At the beginning of the cursor node, the check the previous sibling. |
Mike Frysinger | dc72779 | 2020-04-10 01:41:13 -0400 | [diff] [blame^] | 569 | const previousSibling = cursorNode.previousSibling; |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 570 | if (previousSibling && |
| 571 | this.textAttributes.matchesContainer(previousSibling)) { |
| 572 | previousSibling.textContent += str; |
| 573 | this.cursorNode_ = previousSibling; |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 574 | this.cursorOffset_ = lib.wc.strWidth(previousSibling.textContent); |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 575 | return; |
| 576 | } |
| 577 | |
Mike Frysinger | dc72779 | 2020-04-10 01:41:13 -0400 | [diff] [blame^] | 578 | const newNode = this.textAttributes.createContainer(str); |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 579 | this.cursorRowNode_.insertBefore(newNode, cursorNode); |
| 580 | this.cursorNode_ = newNode; |
Mike Frysinger | 6380bed | 2017-08-24 18:46:39 -0400 | [diff] [blame] | 581 | this.cursorOffset_ = wcwidth; |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 582 | return; |
| 583 | } |
| 584 | |
| 585 | if (reverseOffset == 0) { |
| 586 | // At the end of the cursor node, the check the next sibling. |
Mike Frysinger | dc72779 | 2020-04-10 01:41:13 -0400 | [diff] [blame^] | 587 | const nextSibling = cursorNode.nextSibling; |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 588 | if (nextSibling && |
| 589 | this.textAttributes.matchesContainer(nextSibling)) { |
| 590 | nextSibling.textContent = str + nextSibling.textContent; |
| 591 | this.cursorNode_ = nextSibling; |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 592 | this.cursorOffset_ = lib.wc.strWidth(str); |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 593 | return; |
| 594 | } |
| 595 | |
Mike Frysinger | dc72779 | 2020-04-10 01:41:13 -0400 | [diff] [blame^] | 596 | const newNode = this.textAttributes.createContainer(str); |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 597 | this.cursorRowNode_.insertBefore(newNode, nextSibling); |
| 598 | this.cursorNode_ = newNode; |
| 599 | // We specifically need to include any missing whitespace here, since it's |
| 600 | // going in a new node. |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 601 | this.cursorOffset_ = hterm.TextAttributes.nodeWidth(newNode); |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 602 | return; |
| 603 | } |
| 604 | |
| 605 | // Worst case, we're somewhere in the middle of the cursor node. We'll |
| 606 | // have to split it into two nodes and insert our new container in between. |
| 607 | this.splitNode_(cursorNode, offset); |
Mike Frysinger | dc72779 | 2020-04-10 01:41:13 -0400 | [diff] [blame^] | 608 | const newNode = this.textAttributes.createContainer(str); |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 609 | this.cursorRowNode_.insertBefore(newNode, cursorNode.nextSibling); |
| 610 | this.cursorNode_ = newNode; |
Mike Frysinger | 6380bed | 2017-08-24 18:46:39 -0400 | [diff] [blame] | 611 | this.cursorOffset_ = wcwidth; |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 612 | }; |
| 613 | |
| 614 | /** |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 615 | * Overwrite the text at the current cursor position. |
| 616 | * |
rginda | a09e733 | 2012-08-17 12:49:51 -0700 | [diff] [blame] | 617 | * You must call maybeClipCurrentRow() after in order to clip overflowed |
| 618 | * text and clamp the cursor. |
| 619 | * |
| 620 | * It is also up to the caller to properly maintain the line overflow state |
| 621 | * using hterm.Screen..commitLineOverflow(). |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 622 | * |
| 623 | * @param {string} str The source string for overwriting existing content. |
| 624 | * @param {number=} wcwidth The cached lib.wc.strWidth value for |str|. Will be |
| 625 | * calculated on demand if need be. Passing in a cached value helps speed |
| 626 | * up processing as this is a hot codepath. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 627 | */ |
Mike Frysinger | 1adc26e | 2020-04-08 00:17:30 -0400 | [diff] [blame] | 628 | hterm.Screen.prototype.overwriteString = function(str, wcwidth = undefined) { |
Mike Frysinger | dc72779 | 2020-04-10 01:41:13 -0400 | [diff] [blame^] | 629 | const maxLength = this.columnCount_ - this.cursorPosition.column; |
Mike Frysinger | bdb3480 | 2020-04-07 03:47:32 -0400 | [diff] [blame] | 630 | if (!maxLength) { |
Mike Frysinger | 159b739 | 2019-03-26 11:08:32 -0700 | [diff] [blame] | 631 | return; |
Mike Frysinger | bdb3480 | 2020-04-07 03:47:32 -0400 | [diff] [blame] | 632 | } |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 633 | |
Mike Frysinger | bdb3480 | 2020-04-07 03:47:32 -0400 | [diff] [blame] | 634 | if (wcwidth === undefined) { |
Mike Frysinger | 6380bed | 2017-08-24 18:46:39 -0400 | [diff] [blame] | 635 | wcwidth = lib.wc.strWidth(str); |
Mike Frysinger | bdb3480 | 2020-04-07 03:47:32 -0400 | [diff] [blame] | 636 | } |
Mike Frysinger | 6380bed | 2017-08-24 18:46:39 -0400 | [diff] [blame] | 637 | |
Joel Hockey | add2f7e | 2019-09-20 16:37:35 -0700 | [diff] [blame] | 638 | if (this.textAttributes.matchesContainer(lib.notNull(this.cursorNode_)) && |
| 639 | this.cursorNode_.textContent.substr(this.cursorOffset_) == |
| 640 | str) { |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 641 | // This overwrite would be a no-op, just move the cursor and return. |
Mike Frysinger | 6380bed | 2017-08-24 18:46:39 -0400 | [diff] [blame] | 642 | this.cursorOffset_ += wcwidth; |
| 643 | this.cursorPosition.column += wcwidth; |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 644 | return; |
| 645 | } |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 646 | |
Mike Frysinger | 6380bed | 2017-08-24 18:46:39 -0400 | [diff] [blame] | 647 | this.deleteChars(Math.min(wcwidth, maxLength)); |
| 648 | this.insertString(str, wcwidth); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 649 | }; |
| 650 | |
| 651 | /** |
| 652 | * Forward-delete one or more characters at the current cursor position. |
| 653 | * |
| 654 | * Text to the right of the deleted characters is shifted left. Only affects |
| 655 | * characters on the same row as the cursor. |
| 656 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 657 | * @param {number} count The column width of characters to delete. This is |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 658 | * clamped to the column width minus the cursor column. |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 659 | * @return {number} The column width of the characters actually deleted. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 660 | */ |
| 661 | hterm.Screen.prototype.deleteChars = function(count) { |
Mike Frysinger | dc72779 | 2020-04-10 01:41:13 -0400 | [diff] [blame^] | 662 | let node = this.cursorNode_; |
| 663 | let offset = this.cursorOffset_; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 664 | |
Mike Frysinger | dc72779 | 2020-04-10 01:41:13 -0400 | [diff] [blame^] | 665 | const currentCursorColumn = this.cursorPosition.column; |
Robert Ginda | 7fd5708 | 2012-09-25 14:41:47 -0700 | [diff] [blame] | 666 | count = Math.min(count, this.columnCount_ - currentCursorColumn); |
Mike Frysinger | bdb3480 | 2020-04-07 03:47:32 -0400 | [diff] [blame] | 667 | if (!count) { |
Robert Ginda | 7fd5708 | 2012-09-25 14:41:47 -0700 | [diff] [blame] | 668 | return 0; |
Mike Frysinger | bdb3480 | 2020-04-07 03:47:32 -0400 | [diff] [blame] | 669 | } |
Robert Ginda | 7fd5708 | 2012-09-25 14:41:47 -0700 | [diff] [blame] | 670 | |
Mike Frysinger | dc72779 | 2020-04-10 01:41:13 -0400 | [diff] [blame^] | 671 | const rv = count; |
| 672 | let startLength, endLength; |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 673 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 674 | while (node && count) { |
Mike Frysinger | 859bcbd | 2017-08-28 23:48:43 -0400 | [diff] [blame] | 675 | // Sanity check so we don't loop forever, but we don't also go quietly. |
| 676 | if (count < 0) { |
| 677 | console.error(`Deleting ${rv} chars went negative: ${count}`); |
| 678 | break; |
| 679 | } |
| 680 | |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 681 | startLength = hterm.TextAttributes.nodeWidth(node); |
| 682 | node.textContent = hterm.TextAttributes.nodeSubstr(node, 0, offset) + |
| 683 | hterm.TextAttributes.nodeSubstr(node, offset + count); |
| 684 | endLength = hterm.TextAttributes.nodeWidth(node); |
Mike Frysinger | 859bcbd | 2017-08-28 23:48:43 -0400 | [diff] [blame] | 685 | |
| 686 | // Deal with splitting wide characters. There are two ways: we could delete |
| 687 | // the first column or the second column. In both cases, we delete the wide |
| 688 | // character and replace one of the columns with a space (since the other |
| 689 | // was deleted). If there are more chars to delete, the next loop will pick |
| 690 | // up the slack. |
| 691 | if (node.wcNode && offset < startLength && |
Joel Hockey | d36efd6 | 2019-09-30 14:16:20 -0700 | [diff] [blame] | 692 | ((endLength && startLength == endLength) || |
| 693 | (!endLength && offset == 1))) { |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 694 | // No characters were deleted when there should be. We're probably trying |
| 695 | // to delete one column width from a wide character node. We remove the |
| 696 | // wide character node here and replace it with a single space. |
Mike Frysinger | dc72779 | 2020-04-10 01:41:13 -0400 | [diff] [blame^] | 697 | const spaceNode = this.textAttributes.createContainer(' '); |
Mike Frysinger | 859bcbd | 2017-08-28 23:48:43 -0400 | [diff] [blame] | 698 | node.parentNode.insertBefore(spaceNode, offset ? node : node.nextSibling); |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 699 | node.textContent = ''; |
| 700 | endLength = 0; |
| 701 | count -= 1; |
Mike Frysinger | bdb3480 | 2020-04-07 03:47:32 -0400 | [diff] [blame] | 702 | } else { |
Mike Frysinger | 859bcbd | 2017-08-28 23:48:43 -0400 | [diff] [blame] | 703 | count -= startLength - endLength; |
Mike Frysinger | bdb3480 | 2020-04-07 03:47:32 -0400 | [diff] [blame] | 704 | } |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 705 | |
Mike Frysinger | dc72779 | 2020-04-10 01:41:13 -0400 | [diff] [blame^] | 706 | const nextNode = node.nextSibling; |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 707 | if (endLength == 0 && node != this.cursorNode_) { |
| 708 | node.parentNode.removeChild(node); |
| 709 | } |
| 710 | node = nextNode; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 711 | offset = 0; |
| 712 | } |
Robert Ginda | 7fd5708 | 2012-09-25 14:41:47 -0700 | [diff] [blame] | 713 | |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 714 | // Remove this.cursorNode_ if it is an empty non-text node. |
Mike Frysinger | 6a4f241 | 2017-08-31 01:11:25 -0400 | [diff] [blame] | 715 | if (this.cursorNode_.nodeType != Node.TEXT_NODE && |
| 716 | !this.cursorNode_.textContent) { |
Mike Frysinger | dc72779 | 2020-04-10 01:41:13 -0400 | [diff] [blame^] | 717 | const cursorNode = this.cursorNode_; |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 718 | if (cursorNode.previousSibling) { |
| 719 | this.cursorNode_ = cursorNode.previousSibling; |
| 720 | this.cursorOffset_ = hterm.TextAttributes.nodeWidth( |
| 721 | cursorNode.previousSibling); |
| 722 | } else if (cursorNode.nextSibling) { |
| 723 | this.cursorNode_ = cursorNode.nextSibling; |
| 724 | this.cursorOffset_ = 0; |
| 725 | } else { |
Mike Frysinger | dc72779 | 2020-04-10 01:41:13 -0400 | [diff] [blame^] | 726 | const emptyNode = this.cursorRowNode_.ownerDocument.createTextNode(''); |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 727 | this.cursorRowNode_.appendChild(emptyNode); |
| 728 | this.cursorNode_ = emptyNode; |
| 729 | this.cursorOffset_ = 0; |
| 730 | } |
| 731 | this.cursorRowNode_.removeChild(cursorNode); |
| 732 | } |
| 733 | |
Robert Ginda | 7fd5708 | 2012-09-25 14:41:47 -0700 | [diff] [blame] | 734 | return rv; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 735 | }; |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 736 | |
| 737 | /** |
| 738 | * Finds first X-ROW of a line containing specified X-ROW. |
| 739 | * Used to support line overflow. |
| 740 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 741 | * @param {!Node} row X-ROW to begin search for first row of line. |
| 742 | * @return {!Node} The X-ROW that is at the beginning of the line. |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 743 | **/ |
| 744 | hterm.Screen.prototype.getLineStartRow_ = function(row) { |
| 745 | while (row.previousSibling && |
| 746 | row.previousSibling.hasAttribute('line-overflow')) { |
| 747 | row = row.previousSibling; |
| 748 | } |
| 749 | return row; |
| 750 | }; |
| 751 | |
| 752 | /** |
| 753 | * Gets text of a line beginning with row. |
| 754 | * Supports line overflow. |
| 755 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 756 | * @param {!Node} row First X-ROW of line. |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 757 | * @return {string} Text content of line. |
| 758 | **/ |
| 759 | hterm.Screen.prototype.getLineText_ = function(row) { |
Joel Hockey | add2f7e | 2019-09-20 16:37:35 -0700 | [diff] [blame] | 760 | let rowText = ''; |
| 761 | let rowOrNull = row; |
| 762 | while (rowOrNull) { |
| 763 | rowText += rowOrNull.textContent; |
| 764 | if (rowOrNull.hasAttribute('line-overflow')) { |
| 765 | rowOrNull = rowOrNull.nextSibling; |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 766 | } else { |
| 767 | break; |
| 768 | } |
| 769 | } |
| 770 | return rowText; |
| 771 | }; |
| 772 | |
| 773 | /** |
| 774 | * Returns X-ROW that is ancestor of the node. |
| 775 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 776 | * @param {!Node} node Node to get X-ROW ancestor for. |
Joel Hockey | add2f7e | 2019-09-20 16:37:35 -0700 | [diff] [blame] | 777 | * @return {?Node} X-ROW ancestor of node, or null if not found. |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 778 | **/ |
| 779 | hterm.Screen.prototype.getXRowAncestor_ = function(node) { |
Joel Hockey | add2f7e | 2019-09-20 16:37:35 -0700 | [diff] [blame] | 780 | let nodeOrNull = node; |
| 781 | while (nodeOrNull) { |
Mike Frysinger | bdb3480 | 2020-04-07 03:47:32 -0400 | [diff] [blame] | 782 | if (nodeOrNull.nodeName === 'X-ROW') { |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 783 | break; |
Mike Frysinger | bdb3480 | 2020-04-07 03:47:32 -0400 | [diff] [blame] | 784 | } |
Joel Hockey | add2f7e | 2019-09-20 16:37:35 -0700 | [diff] [blame] | 785 | nodeOrNull = nodeOrNull.parentNode; |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 786 | } |
Joel Hockey | add2f7e | 2019-09-20 16:37:35 -0700 | [diff] [blame] | 787 | return nodeOrNull; |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 788 | }; |
| 789 | |
| 790 | /** |
| 791 | * Returns position within line of character at offset within node. |
| 792 | * Supports line overflow. |
| 793 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 794 | * @param {!Node} row X-ROW at beginning of line. |
| 795 | * @param {!Node} node Node to get position of. |
| 796 | * @param {number} offset Offset into node. |
| 797 | * @return {number} Position within line of character at offset within node. |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 798 | **/ |
| 799 | hterm.Screen.prototype.getPositionWithOverflow_ = function(row, node, offset) { |
Mike Frysinger | bdb3480 | 2020-04-07 03:47:32 -0400 | [diff] [blame] | 800 | if (!node) { |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 801 | return -1; |
Mike Frysinger | bdb3480 | 2020-04-07 03:47:32 -0400 | [diff] [blame] | 802 | } |
Mike Frysinger | dc72779 | 2020-04-10 01:41:13 -0400 | [diff] [blame^] | 803 | const ancestorRow = this.getXRowAncestor_(node); |
Mike Frysinger | bdb3480 | 2020-04-07 03:47:32 -0400 | [diff] [blame] | 804 | if (!ancestorRow) { |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 805 | return -1; |
Mike Frysinger | bdb3480 | 2020-04-07 03:47:32 -0400 | [diff] [blame] | 806 | } |
Mike Frysinger | dc72779 | 2020-04-10 01:41:13 -0400 | [diff] [blame^] | 807 | let position = 0; |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 808 | while (ancestorRow != row) { |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 809 | position += hterm.TextAttributes.nodeWidth(row); |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 810 | if (row.hasAttribute('line-overflow') && row.nextSibling) { |
| 811 | row = row.nextSibling; |
| 812 | } else { |
| 813 | return -1; |
| 814 | } |
| 815 | } |
| 816 | return position + this.getPositionWithinRow_(row, node, offset); |
| 817 | }; |
| 818 | |
| 819 | /** |
| 820 | * Returns position within row of character at offset within node. |
| 821 | * Does not support line overflow. |
| 822 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 823 | * @param {!Node} row X-ROW to get position within. |
| 824 | * @param {!Node} node Node to get position for. |
| 825 | * @param {number} offset Offset within node to get position for. |
| 826 | * @return {number} Position within row of character at offset within node. |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 827 | **/ |
| 828 | hterm.Screen.prototype.getPositionWithinRow_ = function(row, node, offset) { |
| 829 | if (node.parentNode != row) { |
Mike Frysinger | 498192d | 2017-06-26 18:23:31 -0400 | [diff] [blame] | 830 | // If we traversed to the top node, then there's nothing to find here. |
Mike Frysinger | bdb3480 | 2020-04-07 03:47:32 -0400 | [diff] [blame] | 831 | if (node.parentNode == null) { |
Mike Frysinger | 498192d | 2017-06-26 18:23:31 -0400 | [diff] [blame] | 832 | return -1; |
Mike Frysinger | bdb3480 | 2020-04-07 03:47:32 -0400 | [diff] [blame] | 833 | } |
Mike Frysinger | 498192d | 2017-06-26 18:23:31 -0400 | [diff] [blame] | 834 | |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 835 | return this.getPositionWithinRow_(node.parentNode, node, offset) + |
| 836 | this.getPositionWithinRow_(row, node.parentNode, 0); |
| 837 | } |
Mike Frysinger | dc72779 | 2020-04-10 01:41:13 -0400 | [diff] [blame^] | 838 | let position = 0; |
| 839 | for (let i = 0; i < row.childNodes.length; i++) { |
| 840 | const currentNode = row.childNodes[i]; |
Mike Frysinger | bdb3480 | 2020-04-07 03:47:32 -0400 | [diff] [blame] | 841 | if (currentNode == node) { |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 842 | return position + offset; |
Mike Frysinger | bdb3480 | 2020-04-07 03:47:32 -0400 | [diff] [blame] | 843 | } |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 844 | position += hterm.TextAttributes.nodeWidth(currentNode); |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 845 | } |
| 846 | return -1; |
| 847 | }; |
| 848 | |
| 849 | /** |
| 850 | * Returns the node and offset corresponding to position within line. |
| 851 | * Supports line overflow. |
| 852 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 853 | * @param {!Node} row X-ROW at beginning of line. |
| 854 | * @param {number} position Position within line to retrieve node and offset. |
Joel Hockey | add2f7e | 2019-09-20 16:37:35 -0700 | [diff] [blame] | 855 | * @return {?Array} Two element array containing node and offset respectively. |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 856 | **/ |
| 857 | hterm.Screen.prototype.getNodeAndOffsetWithOverflow_ = function(row, position) { |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 858 | while (row && position > hterm.TextAttributes.nodeWidth(row)) { |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 859 | if (row.hasAttribute('line-overflow') && row.nextSibling) { |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 860 | position -= hterm.TextAttributes.nodeWidth(row); |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 861 | row = row.nextSibling; |
| 862 | } else { |
Joel Hockey | add2f7e | 2019-09-20 16:37:35 -0700 | [diff] [blame] | 863 | return [null, -1]; |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 864 | } |
| 865 | } |
| 866 | return this.getNodeAndOffsetWithinRow_(row, position); |
| 867 | }; |
| 868 | |
| 869 | /** |
| 870 | * Returns the node and offset corresponding to position within row. |
| 871 | * Does not support line overflow. |
| 872 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 873 | * @param {!Node} row X-ROW to get position within. |
| 874 | * @param {number} position Position within row to retrieve node and offset. |
Joel Hockey | add2f7e | 2019-09-20 16:37:35 -0700 | [diff] [blame] | 875 | * @return {?Array} Two element array containing node and offset respectively. |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 876 | **/ |
| 877 | hterm.Screen.prototype.getNodeAndOffsetWithinRow_ = function(row, position) { |
Mike Frysinger | dc72779 | 2020-04-10 01:41:13 -0400 | [diff] [blame^] | 878 | for (let i = 0; i < row.childNodes.length; i++) { |
| 879 | const node = row.childNodes[i]; |
| 880 | const nodeTextWidth = hterm.TextAttributes.nodeWidth(node); |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 881 | if (position <= nodeTextWidth) { |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 882 | if (node.nodeName === 'SPAN') { |
| 883 | /** Drill down to node contained by SPAN. **/ |
| 884 | return this.getNodeAndOffsetWithinRow_(node, position); |
| 885 | } else { |
| 886 | return [node, position]; |
| 887 | } |
| 888 | } |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 889 | position -= nodeTextWidth; |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 890 | } |
| 891 | return null; |
| 892 | }; |
| 893 | |
| 894 | /** |
| 895 | * Returns the node and offset corresponding to position within line. |
| 896 | * Supports line overflow. |
| 897 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 898 | * @param {!Node} row X-ROW at beginning of line. |
| 899 | * @param {number} start Start position of range within line. |
| 900 | * @param {number} end End position of range within line. |
| 901 | * @param {!Range} range Range to modify. |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 902 | **/ |
| 903 | hterm.Screen.prototype.setRange_ = function(row, start, end, range) { |
Mike Frysinger | dc72779 | 2020-04-10 01:41:13 -0400 | [diff] [blame^] | 904 | const startNodeAndOffset = this.getNodeAndOffsetWithOverflow_(row, start); |
Mike Frysinger | bdb3480 | 2020-04-07 03:47:32 -0400 | [diff] [blame] | 905 | if (startNodeAndOffset == null) { |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 906 | return; |
Mike Frysinger | bdb3480 | 2020-04-07 03:47:32 -0400 | [diff] [blame] | 907 | } |
Mike Frysinger | dc72779 | 2020-04-10 01:41:13 -0400 | [diff] [blame^] | 908 | const endNodeAndOffset = this.getNodeAndOffsetWithOverflow_(row, end); |
Mike Frysinger | bdb3480 | 2020-04-07 03:47:32 -0400 | [diff] [blame] | 909 | if (endNodeAndOffset == null) { |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 910 | return; |
Mike Frysinger | bdb3480 | 2020-04-07 03:47:32 -0400 | [diff] [blame] | 911 | } |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 912 | range.setStart(startNodeAndOffset[0], startNodeAndOffset[1]); |
| 913 | range.setEnd(endNodeAndOffset[0], endNodeAndOffset[1]); |
| 914 | }; |
| 915 | |
| 916 | /** |
John Lin | cae9b73 | 2018-03-08 13:56:35 +0800 | [diff] [blame] | 917 | * Expands selection to surrounding string with word break matches. |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 918 | * |
Joel Hockey | add2f7e | 2019-09-20 16:37:35 -0700 | [diff] [blame] | 919 | * @param {?Selection} selection Selection to expand. |
John Lin | cae9b73 | 2018-03-08 13:56:35 +0800 | [diff] [blame] | 920 | * @param {string} leftMatch left word break match. |
| 921 | * @param {string} rightMatch right word break match. |
| 922 | * @param {string} insideMatch inside word break match. |
| 923 | */ |
| 924 | hterm.Screen.prototype.expandSelectionWithWordBreakMatches_ = |
| 925 | function(selection, leftMatch, rightMatch, insideMatch) { |
Mike Frysinger | bdb3480 | 2020-04-07 03:47:32 -0400 | [diff] [blame] | 926 | if (!selection) { |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 927 | return; |
Mike Frysinger | bdb3480 | 2020-04-07 03:47:32 -0400 | [diff] [blame] | 928 | } |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 929 | |
Mike Frysinger | dc72779 | 2020-04-10 01:41:13 -0400 | [diff] [blame^] | 930 | const range = selection.getRangeAt(0); |
Mike Frysinger | bdb3480 | 2020-04-07 03:47:32 -0400 | [diff] [blame] | 931 | if (!range || range.toString().match(/\s/)) { |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 932 | return; |
Mike Frysinger | bdb3480 | 2020-04-07 03:47:32 -0400 | [diff] [blame] | 933 | } |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 934 | |
Joel Hockey | add2f7e | 2019-09-20 16:37:35 -0700 | [diff] [blame] | 935 | const rowElement = this.getXRowAncestor_(lib.notNull(range.startContainer)); |
Mike Frysinger | bdb3480 | 2020-04-07 03:47:32 -0400 | [diff] [blame] | 936 | if (!rowElement) { |
Raymes Khoury | 334625a | 2018-06-25 10:29:40 +1000 | [diff] [blame] | 937 | return; |
Mike Frysinger | bdb3480 | 2020-04-07 03:47:32 -0400 | [diff] [blame] | 938 | } |
Raymes Khoury | 334625a | 2018-06-25 10:29:40 +1000 | [diff] [blame] | 939 | const row = this.getLineStartRow_(rowElement); |
Mike Frysinger | bdb3480 | 2020-04-07 03:47:32 -0400 | [diff] [blame] | 940 | if (!row) { |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 941 | return; |
Mike Frysinger | bdb3480 | 2020-04-07 03:47:32 -0400 | [diff] [blame] | 942 | } |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 943 | |
Mike Frysinger | dc72779 | 2020-04-10 01:41:13 -0400 | [diff] [blame^] | 944 | const startPosition = this.getPositionWithOverflow_( |
Joel Hockey | add2f7e | 2019-09-20 16:37:35 -0700 | [diff] [blame] | 945 | row, lib.notNull(range.startContainer), range.startOffset); |
Mike Frysinger | bdb3480 | 2020-04-07 03:47:32 -0400 | [diff] [blame] | 946 | if (startPosition == -1) { |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 947 | return; |
Mike Frysinger | bdb3480 | 2020-04-07 03:47:32 -0400 | [diff] [blame] | 948 | } |
Mike Frysinger | dc72779 | 2020-04-10 01:41:13 -0400 | [diff] [blame^] | 949 | const endPosition = this.getPositionWithOverflow_( |
Joel Hockey | add2f7e | 2019-09-20 16:37:35 -0700 | [diff] [blame] | 950 | row, lib.notNull(range.endContainer), range.endOffset); |
Mike Frysinger | bdb3480 | 2020-04-07 03:47:32 -0400 | [diff] [blame] | 951 | if (endPosition == -1) { |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 952 | return; |
Mike Frysinger | bdb3480 | 2020-04-07 03:47:32 -0400 | [diff] [blame] | 953 | } |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 954 | |
Mike Frysinger | 390878a | 2020-04-07 20:04:51 -0400 | [diff] [blame] | 955 | // Move start to the left. |
Mike Frysinger | dc72779 | 2020-04-10 01:41:13 -0400 | [diff] [blame^] | 956 | const rowText = this.getLineText_(row); |
| 957 | const lineUpToRange = lib.wc.substring(rowText, 0, endPosition); |
| 958 | const leftRegularExpression = new RegExp(leftMatch + insideMatch + '$'); |
| 959 | const expandedStart = lineUpToRange.search(leftRegularExpression); |
Mike Frysinger | bdb3480 | 2020-04-07 03:47:32 -0400 | [diff] [blame] | 960 | if (expandedStart == -1 || expandedStart > startPosition) { |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 961 | return; |
Mike Frysinger | bdb3480 | 2020-04-07 03:47:32 -0400 | [diff] [blame] | 962 | } |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 963 | |
Mike Frysinger | 390878a | 2020-04-07 20:04:51 -0400 | [diff] [blame] | 964 | // Move end to the right. |
Mike Frysinger | dc72779 | 2020-04-10 01:41:13 -0400 | [diff] [blame^] | 965 | const lineFromRange = lib.wc.substring(rowText, startPosition, |
| 966 | lib.wc.strWidth(rowText)); |
| 967 | const rightRegularExpression = new RegExp('^' + insideMatch + rightMatch); |
| 968 | const found = lineFromRange.match(rightRegularExpression); |
Mike Frysinger | bdb3480 | 2020-04-07 03:47:32 -0400 | [diff] [blame] | 969 | if (!found) { |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 970 | return; |
Mike Frysinger | bdb3480 | 2020-04-07 03:47:32 -0400 | [diff] [blame] | 971 | } |
Mike Frysinger | dc72779 | 2020-04-10 01:41:13 -0400 | [diff] [blame^] | 972 | const expandedEnd = startPosition + lib.wc.strWidth(found[0]); |
Mike Frysinger | bdb3480 | 2020-04-07 03:47:32 -0400 | [diff] [blame] | 973 | if (expandedEnd == -1 || expandedEnd < endPosition) { |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 974 | return; |
Mike Frysinger | bdb3480 | 2020-04-07 03:47:32 -0400 | [diff] [blame] | 975 | } |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 976 | |
| 977 | this.setRange_(row, expandedStart, expandedEnd, range); |
| 978 | selection.addRange(range); |
| 979 | }; |
Mike Frysinger | a2cacaa | 2017-11-29 13:51:09 -0800 | [diff] [blame] | 980 | |
| 981 | /** |
John Lin | cae9b73 | 2018-03-08 13:56:35 +0800 | [diff] [blame] | 982 | * Expands selection to surrounding string using the user's settings. |
| 983 | * |
Joel Hockey | add2f7e | 2019-09-20 16:37:35 -0700 | [diff] [blame] | 984 | * @param {?Selection} selection Selection to expand. |
John Lin | cae9b73 | 2018-03-08 13:56:35 +0800 | [diff] [blame] | 985 | */ |
| 986 | hterm.Screen.prototype.expandSelection = function(selection) { |
| 987 | this.expandSelectionWithWordBreakMatches_( |
| 988 | selection, |
Joel Hockey | add2f7e | 2019-09-20 16:37:35 -0700 | [diff] [blame] | 989 | lib.notNull(this.wordBreakMatchLeft), |
| 990 | lib.notNull(this.wordBreakMatchRight), |
| 991 | lib.notNull(this.wordBreakMatchMiddle)); |
Mike Frysinger | 8416e0a | 2017-05-17 09:09:46 -0400 | [diff] [blame] | 992 | }; |
John Lin | cae9b73 | 2018-03-08 13:56:35 +0800 | [diff] [blame] | 993 | |
| 994 | /** |
| 995 | * Expands selection to surrounding URL using a set of fixed match settings. |
| 996 | * |
Joel Hockey | add2f7e | 2019-09-20 16:37:35 -0700 | [diff] [blame] | 997 | * @param {?Selection} selection Selection to expand. |
John Lin | cae9b73 | 2018-03-08 13:56:35 +0800 | [diff] [blame] | 998 | */ |
| 999 | hterm.Screen.prototype.expandSelectionForUrl = function(selection) { |
| 1000 | this.expandSelectionWithWordBreakMatches_( |
| 1001 | selection, |
Mike Frysinger | 1a1a180 | 2020-01-29 21:38:55 -0500 | [diff] [blame] | 1002 | '[^\\s[\\](){}<>"\'^!@#$%&*,;:`\u{2018}\u{201c}\u{2039}\u{ab}]', |
| 1003 | '[^\\s[\\](){}<>"\'^!@#$%&*,;:~.`\u{2019}\u{201d}\u{203a}\u{bb}]', |
Mike Frysinger | 9e11e49 | 2020-01-06 14:29:57 +0545 | [diff] [blame] | 1004 | '[^\\s[\\](){}<>"\'^]*'); |
John Lin | cae9b73 | 2018-03-08 13:56:35 +0800 | [diff] [blame] | 1005 | }; |
| 1006 | |
| 1007 | /** |
Mike Frysinger | a2cacaa | 2017-11-29 13:51:09 -0800 | [diff] [blame] | 1008 | * Save the current cursor state to the corresponding screens. |
| 1009 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 1010 | * @param {!hterm.VT} vt The VT object to read graphic codeset details from. |
Mike Frysinger | a2cacaa | 2017-11-29 13:51:09 -0800 | [diff] [blame] | 1011 | */ |
| 1012 | hterm.Screen.prototype.saveCursorAndState = function(vt) { |
| 1013 | this.cursorState_.save(vt); |
| 1014 | }; |
| 1015 | |
| 1016 | /** |
| 1017 | * Restore the saved cursor state in the corresponding screens. |
| 1018 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 1019 | * @param {!hterm.VT} vt The VT object to write graphic codeset details to. |
Mike Frysinger | a2cacaa | 2017-11-29 13:51:09 -0800 | [diff] [blame] | 1020 | */ |
| 1021 | hterm.Screen.prototype.restoreCursorAndState = function(vt) { |
| 1022 | this.cursorState_.restore(vt); |
| 1023 | }; |
| 1024 | |
| 1025 | /** |
| 1026 | * Track all the things related to the current "cursor". |
| 1027 | * |
| 1028 | * The set of things saved & restored here is defined by DEC: |
| 1029 | * https://vt100.net/docs/vt510-rm/DECSC.html |
| 1030 | * - Cursor position |
| 1031 | * - Character attributes set by the SGR command |
| 1032 | * - Character sets (G0, G1, G2, or G3) currently in GL and GR |
| 1033 | * - Wrap flag (autowrap or no autowrap) |
| 1034 | * - State of origin mode (DECOM) |
| 1035 | * - Selective erase attribute |
| 1036 | * - Any single shift 2 (SS2) or single shift 3 (SS3) functions sent |
| 1037 | * |
| 1038 | * These are done on a per-screen basis. |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 1039 | * |
| 1040 | * @param {!hterm.Screen} screen The screen this cursor is tied to. |
Joel Hockey | add2f7e | 2019-09-20 16:37:35 -0700 | [diff] [blame] | 1041 | * @constructor |
Mike Frysinger | a2cacaa | 2017-11-29 13:51:09 -0800 | [diff] [blame] | 1042 | */ |
| 1043 | hterm.Screen.CursorState = function(screen) { |
| 1044 | this.screen_ = screen; |
| 1045 | this.cursor = null; |
| 1046 | this.textAttributes = null; |
| 1047 | this.GL = this.GR = this.G0 = this.G1 = this.G2 = this.G3 = null; |
| 1048 | }; |
| 1049 | |
| 1050 | /** |
| 1051 | * Save all the cursor state. |
| 1052 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 1053 | * @param {!hterm.VT} vt The VT object to read graphic codeset details from. |
Mike Frysinger | a2cacaa | 2017-11-29 13:51:09 -0800 | [diff] [blame] | 1054 | */ |
| 1055 | hterm.Screen.CursorState.prototype.save = function(vt) { |
| 1056 | this.cursor = vt.terminal.saveCursor(); |
| 1057 | |
| 1058 | this.textAttributes = this.screen_.textAttributes.clone(); |
| 1059 | |
| 1060 | this.GL = vt.GL; |
| 1061 | this.GR = vt.GR; |
| 1062 | |
| 1063 | this.G0 = vt.G0; |
| 1064 | this.G1 = vt.G1; |
| 1065 | this.G2 = vt.G2; |
| 1066 | this.G3 = vt.G3; |
| 1067 | }; |
| 1068 | |
| 1069 | /** |
| 1070 | * Restore the previously saved cursor state. |
| 1071 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 1072 | * @param {!hterm.VT} vt The VT object to write graphic codeset details to. |
Mike Frysinger | a2cacaa | 2017-11-29 13:51:09 -0800 | [diff] [blame] | 1073 | */ |
| 1074 | hterm.Screen.CursorState.prototype.restore = function(vt) { |
| 1075 | vt.terminal.restoreCursor(this.cursor); |
| 1076 | |
| 1077 | // Cursor restore includes char attributes (bold/etc...), but does not change |
| 1078 | // the color palette (which are a terminal setting). |
| 1079 | const tattrs = this.textAttributes.clone(); |
Joel Hockey | 42dba8f | 2020-03-26 16:21:11 -0700 | [diff] [blame] | 1080 | tattrs.colorPaletteOverrides = |
| 1081 | this.screen_.textAttributes.colorPaletteOverrides; |
Mike Frysinger | a2cacaa | 2017-11-29 13:51:09 -0800 | [diff] [blame] | 1082 | tattrs.syncColors(); |
| 1083 | |
| 1084 | this.screen_.textAttributes = tattrs; |
| 1085 | |
| 1086 | vt.GL = this.GL; |
| 1087 | vt.GR = this.GR; |
| 1088 | |
| 1089 | vt.G0 = this.G0; |
| 1090 | vt.G1 = this.G1; |
| 1091 | vt.G2 = this.G2; |
| 1092 | vt.G3 = this.G3; |
| 1093 | }; |