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 | |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 7 | lib.rtdep('lib.f', 'lib.wc', |
rginda | cbbd748 | 2012-06-13 15:06:16 -0700 | [diff] [blame] | 8 | 'hterm.RowCol', 'hterm.Size', 'hterm.TextAttributes'); |
| 9 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 10 | /** |
| 11 | * @fileoverview This class represents a single terminal screen full of text. |
| 12 | * |
| 13 | * It maintains the current cursor position and has basic methods for text |
| 14 | * insert and overwrite, and adding or removing rows from the screen. |
| 15 | * |
| 16 | * This class has no knowledge of the scrollback buffer. |
| 17 | * |
| 18 | * The number of rows on the screen is determined only by the number of rows |
| 19 | * that the caller inserts into the screen. If a caller wants to ensure a |
| 20 | * constant number of rows on the screen, it's their responsibility to remove a |
| 21 | * row for each row inserted. |
| 22 | * |
| 23 | * The screen width, in contrast, is enforced locally. |
| 24 | * |
| 25 | * |
| 26 | * In practice... |
| 27 | * - The hterm.Terminal class holds two hterm.Screen instances. One for the |
| 28 | * primary screen and one for the alternate screen. |
| 29 | * |
| 30 | * - The html.Screen class only cares that rows are HTMLElements. In the |
| 31 | * larger context of hterm, however, the rows happen to be displayed by an |
| 32 | * hterm.ScrollPort and have to follow a few rules as a result. Each |
| 33 | * row must be rooted by the custom HTML tag 'x-row', and each must have a |
| 34 | * rowIndex property that corresponds to the index of the row in the context |
| 35 | * of the scrollback buffer. These invariants are enforced by hterm.Terminal |
| 36 | * because that is the class using the hterm.Screen in the context of an |
| 37 | * hterm.ScrollPort. |
| 38 | */ |
| 39 | |
| 40 | /** |
| 41 | * Create a new screen instance. |
| 42 | * |
| 43 | * The screen initially has no rows and a maximum column count of 0. |
| 44 | * |
| 45 | * @param {integer} opt_columnCount The maximum number of columns for this |
| 46 | * screen. See insertString() and overwriteString() for information about |
| 47 | * what happens when too many characters are added too a row. Defaults to |
| 48 | * 0 if not provided. |
| 49 | */ |
| 50 | hterm.Screen = function(opt_columnCount) { |
| 51 | /** |
| 52 | * Public, read-only access to the rows in this screen. |
| 53 | */ |
| 54 | this.rowsArray = []; |
| 55 | |
| 56 | // The max column width for this screen. |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 57 | this.columnCount_ = opt_columnCount || 80; |
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 | |
| 65 | // The node containing the row that the cursor is positioned on. |
| 66 | this.cursorRowNode_ = null; |
| 67 | |
| 68 | // The node containing the span of text that the cursor is positioned on. |
| 69 | this.cursorNode_ = null; |
| 70 | |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 71 | // The offset in column width into cursorNode_ where the cursor is positioned. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 72 | this.cursorOffset_ = null; |
| 73 | }; |
| 74 | |
| 75 | /** |
| 76 | * Return the screen size as an hterm.Size object. |
| 77 | * |
| 78 | * @return {hterm.Size} hterm.Size object representing the current number |
| 79 | * of rows and columns in this screen. |
| 80 | */ |
| 81 | hterm.Screen.prototype.getSize = function() { |
| 82 | return new hterm.Size(this.columnCount_, this.rowsArray.length); |
| 83 | }; |
| 84 | |
| 85 | /** |
| 86 | * Return the current number of rows in this screen. |
| 87 | * |
| 88 | * @return {integer} The number of rows in this screen. |
| 89 | */ |
| 90 | hterm.Screen.prototype.getHeight = function() { |
| 91 | return this.rowsArray.length; |
| 92 | }; |
| 93 | |
| 94 | /** |
| 95 | * Return the current number of columns in this screen. |
| 96 | * |
| 97 | * @return {integer} The number of columns in this screen. |
| 98 | */ |
| 99 | hterm.Screen.prototype.getWidth = function() { |
| 100 | return this.columnCount_; |
| 101 | }; |
| 102 | |
| 103 | /** |
| 104 | * Set the maximum number of columns per row. |
| 105 | * |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 106 | * @param {integer} count The maximum number of columns per row. |
| 107 | */ |
| 108 | hterm.Screen.prototype.setColumnCount = function(count) { |
rginda | 2312fff | 2012-01-05 16:20:52 -0800 | [diff] [blame] | 109 | this.columnCount_ = count; |
| 110 | |
rginda | cbbd748 | 2012-06-13 15:06:16 -0700 | [diff] [blame] | 111 | if (this.cursorPosition.column >= count) |
| 112 | this.setCursorPosition(this.cursorPosition.row, count - 1); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | /** |
| 116 | * Remove the first row from the screen and return it. |
| 117 | * |
| 118 | * @return {HTMLElement} The first row in this screen. |
| 119 | */ |
| 120 | hterm.Screen.prototype.shiftRow = function() { |
| 121 | return this.shiftRows(1)[0]; |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 122 | }; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 123 | |
| 124 | /** |
| 125 | * Remove rows from the top of the screen and return them as an array. |
| 126 | * |
| 127 | * @param {integer} count The number of rows to remove. |
| 128 | * @return {Array.<HTMLElement>} The selected rows. |
| 129 | */ |
| 130 | hterm.Screen.prototype.shiftRows = function(count) { |
| 131 | return this.rowsArray.splice(0, count); |
| 132 | }; |
| 133 | |
| 134 | /** |
| 135 | * Insert a row at the top of the screen. |
| 136 | * |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 137 | * @param {HTMLElement} row The row to insert. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 138 | */ |
| 139 | hterm.Screen.prototype.unshiftRow = function(row) { |
| 140 | this.rowsArray.splice(0, 0, row); |
| 141 | }; |
| 142 | |
| 143 | /** |
| 144 | * Insert rows at the top of the screen. |
| 145 | * |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 146 | * @param {Array.<HTMLElement>} rows The rows to insert. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 147 | */ |
| 148 | hterm.Screen.prototype.unshiftRows = function(rows) { |
| 149 | this.rowsArray.unshift.apply(this.rowsArray, rows); |
| 150 | }; |
| 151 | |
| 152 | /** |
| 153 | * Remove the last row from the screen and return it. |
| 154 | * |
| 155 | * @return {HTMLElement} The last row in this screen. |
| 156 | */ |
| 157 | hterm.Screen.prototype.popRow = function() { |
| 158 | return this.popRows(1)[0]; |
| 159 | }; |
| 160 | |
| 161 | /** |
| 162 | * Remove rows from the bottom of the screen and return them as an array. |
| 163 | * |
| 164 | * @param {integer} count The number of rows to remove. |
| 165 | * @return {Array.<HTMLElement>} The selected rows. |
| 166 | */ |
| 167 | hterm.Screen.prototype.popRows = function(count) { |
| 168 | return this.rowsArray.splice(this.rowsArray.length - count, count); |
| 169 | }; |
| 170 | |
| 171 | /** |
| 172 | * Insert a row at the bottom of the screen. |
| 173 | * |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 174 | * @param {HTMLElement} row The row to insert. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 175 | */ |
| 176 | hterm.Screen.prototype.pushRow = function(row) { |
| 177 | this.rowsArray.push(row); |
| 178 | }; |
| 179 | |
| 180 | /** |
| 181 | * Insert rows at the bottom of the screen. |
| 182 | * |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 183 | * @param {Array.<HTMLElement>} rows The rows to insert. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 184 | */ |
| 185 | hterm.Screen.prototype.pushRows = function(rows) { |
| 186 | rows.push.apply(this.rowsArray, rows); |
| 187 | }; |
| 188 | |
| 189 | /** |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 190 | * Insert a row at the specified row of the screen. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 191 | * |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 192 | * @param {integer} index The index to insert the row. |
| 193 | * @param {HTMLElement} row The row to insert. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 194 | */ |
| 195 | hterm.Screen.prototype.insertRow = function(index, row) { |
| 196 | this.rowsArray.splice(index, 0, row); |
| 197 | }; |
| 198 | |
| 199 | /** |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 200 | * Insert rows at the specified row of the screen. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 201 | * |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 202 | * @param {integer} index The index to insert the rows. |
| 203 | * @param {Array.<HTMLElement>} rows The rows to insert. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 204 | */ |
| 205 | hterm.Screen.prototype.insertRows = function(index, rows) { |
| 206 | for (var i = 0; i < rows.length; i++) { |
| 207 | this.rowsArray.splice(index + i, 0, rows[i]); |
| 208 | } |
| 209 | }; |
| 210 | |
| 211 | /** |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 212 | * Remove a row from the screen and return it. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 213 | * |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 214 | * @param {integer} index The index of the row to remove. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 215 | * @return {HTMLElement} The selected row. |
| 216 | */ |
| 217 | hterm.Screen.prototype.removeRow = function(index) { |
| 218 | return this.rowsArray.splice(index, 1)[0]; |
| 219 | }; |
| 220 | |
| 221 | /** |
| 222 | * Remove rows from the bottom of the screen and return them as an array. |
| 223 | * |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 224 | * @param {integer} index The index to start removing rows. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 225 | * @param {integer} count The number of rows to remove. |
| 226 | * @return {Array.<HTMLElement>} The selected rows. |
| 227 | */ |
| 228 | hterm.Screen.prototype.removeRows = function(index, count) { |
| 229 | return this.rowsArray.splice(index, count); |
| 230 | }; |
| 231 | |
| 232 | /** |
| 233 | * Invalidate the current cursor position. |
| 234 | * |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 235 | * This sets this.cursorPosition to (0, 0) and clears out some internal |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 236 | * data. |
| 237 | * |
| 238 | * Attempting to insert or overwrite text while the cursor position is invalid |
| 239 | * will raise an obscure exception. |
| 240 | */ |
| 241 | hterm.Screen.prototype.invalidateCursorPosition = function() { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 242 | this.cursorPosition.move(0, 0); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 243 | this.cursorRowNode_ = null; |
| 244 | this.cursorNode_ = null; |
| 245 | this.cursorOffset_ = null; |
| 246 | }; |
| 247 | |
| 248 | /** |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 249 | * Clear the contents of the cursor row. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 250 | */ |
| 251 | hterm.Screen.prototype.clearCursorRow = function() { |
| 252 | this.cursorRowNode_.innerHTML = ''; |
rginda | a09e733 | 2012-08-17 12:49:51 -0700 | [diff] [blame] | 253 | this.cursorRowNode_.removeAttribute('line-overflow'); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 254 | this.cursorOffset_ = 0; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 255 | this.cursorPosition.column = 0; |
rginda | 2312fff | 2012-01-05 16:20:52 -0800 | [diff] [blame] | 256 | this.cursorPosition.overflow = false; |
Robert Ginda | 7fd5708 | 2012-09-25 14:41:47 -0700 | [diff] [blame] | 257 | |
| 258 | var text; |
| 259 | if (this.textAttributes.isDefault()) { |
| 260 | text = ''; |
| 261 | } else { |
| 262 | text = lib.f.getWhitespace(this.columnCount_); |
| 263 | } |
| 264 | |
Zhu Qunying | 30d4071 | 2017-03-14 16:27:00 -0700 | [diff] [blame^] | 265 | // We shouldn't honor inverse colors when clearing an area, to match |
| 266 | // xterm's back color erase behavior. |
Edoardo Spadolini | 2fd4364 | 2014-08-23 22:59:57 +0200 | [diff] [blame] | 267 | var inverse = this.textAttributes.inverse; |
| 268 | this.textAttributes.inverse = false; |
| 269 | this.textAttributes.syncColors(); |
| 270 | |
Robert Ginda | 7fd5708 | 2012-09-25 14:41:47 -0700 | [diff] [blame] | 271 | var node = this.textAttributes.createContainer(text); |
| 272 | this.cursorRowNode_.appendChild(node); |
| 273 | this.cursorNode_ = node; |
Edoardo Spadolini | 2fd4364 | 2014-08-23 22:59:57 +0200 | [diff] [blame] | 274 | |
| 275 | this.textAttributes.inverse = inverse; |
| 276 | this.textAttributes.syncColors(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 277 | }; |
| 278 | |
| 279 | /** |
rginda | a09e733 | 2012-08-17 12:49:51 -0700 | [diff] [blame] | 280 | * Mark the current row as having overflowed to the next line. |
| 281 | * |
| 282 | * The line overflow state is used when converting a range of rows into text. |
| 283 | * It makes it possible to recombine two or more overflow terminal rows into |
| 284 | * a single line. |
| 285 | * |
| 286 | * This is distinct from the cursor being in the overflow state. Cursor |
| 287 | * overflow indicates that printing at the cursor position will commit a |
| 288 | * line overflow, unless it is preceded by a repositioning of the cursor |
| 289 | * to a non-overflow state. |
| 290 | */ |
| 291 | hterm.Screen.prototype.commitLineOverflow = function() { |
| 292 | this.cursorRowNode_.setAttribute('line-overflow', true); |
| 293 | }; |
| 294 | |
| 295 | /** |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 296 | * Relocate the cursor to a give row and column. |
| 297 | * |
| 298 | * @param {integer} row The zero based row. |
| 299 | * @param {integer} column The zero based column. |
| 300 | */ |
| 301 | hterm.Screen.prototype.setCursorPosition = function(row, column) { |
rginda | 11057d5 | 2012-04-25 12:29:56 -0700 | [diff] [blame] | 302 | if (!this.rowsArray.length) { |
| 303 | console.warn('Attempt to set cursor position on empty screen.'); |
| 304 | return; |
| 305 | } |
| 306 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 307 | if (row >= this.rowsArray.length) { |
rginda | cbbd748 | 2012-06-13 15:06:16 -0700 | [diff] [blame] | 308 | console.error('Row out of bounds: ' + row); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 309 | row = this.rowsArray.length - 1; |
| 310 | } else if (row < 0) { |
rginda | cbbd748 | 2012-06-13 15:06:16 -0700 | [diff] [blame] | 311 | console.error('Row out of bounds: ' + row); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 312 | row = 0; |
| 313 | } |
| 314 | |
| 315 | if (column >= this.columnCount_) { |
rginda | cbbd748 | 2012-06-13 15:06:16 -0700 | [diff] [blame] | 316 | console.error('Column out of bounds: ' + column); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 317 | column = this.columnCount_ - 1; |
| 318 | } else if (column < 0) { |
rginda | cbbd748 | 2012-06-13 15:06:16 -0700 | [diff] [blame] | 319 | console.error('Column out of bounds: ' + column); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 320 | column = 0; |
| 321 | } |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 322 | |
rginda | 2312fff | 2012-01-05 16:20:52 -0800 | [diff] [blame] | 323 | this.cursorPosition.overflow = false; |
| 324 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 325 | var rowNode = this.rowsArray[row]; |
| 326 | var node = rowNode.firstChild; |
| 327 | |
| 328 | if (!node) { |
| 329 | node = rowNode.ownerDocument.createTextNode(''); |
| 330 | rowNode.appendChild(node); |
| 331 | } |
| 332 | |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 333 | var currentColumn = 0; |
| 334 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 335 | if (rowNode == this.cursorRowNode_) { |
| 336 | if (column >= this.cursorPosition.column - this.cursorOffset_) { |
| 337 | node = this.cursorNode_; |
| 338 | currentColumn = this.cursorPosition.column - this.cursorOffset_; |
| 339 | } |
| 340 | } else { |
| 341 | this.cursorRowNode_ = rowNode; |
| 342 | } |
| 343 | |
| 344 | this.cursorPosition.move(row, column); |
| 345 | |
| 346 | while (node) { |
| 347 | var offset = column - currentColumn; |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 348 | var width = hterm.TextAttributes.nodeWidth(node); |
| 349 | if (!node.nextSibling || width > offset) { |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 350 | this.cursorNode_ = node; |
| 351 | this.cursorOffset_ = offset; |
| 352 | return; |
| 353 | } |
| 354 | |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 355 | currentColumn += width; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 356 | node = node.nextSibling; |
| 357 | } |
| 358 | }; |
| 359 | |
| 360 | /** |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 361 | * Set the provided selection object to be a caret selection at the current |
| 362 | * cursor position. |
| 363 | */ |
| 364 | hterm.Screen.prototype.syncSelectionCaret = function(selection) { |
Rob Spies | 06533ba | 2014-04-24 11:20:37 -0700 | [diff] [blame] | 365 | try { |
| 366 | selection.collapse(this.cursorNode_, this.cursorOffset_); |
| 367 | } catch (firefoxIgnoredException) { |
| 368 | // FF can throw an exception if the range is off, rather than just not |
| 369 | // performing the collapse. |
| 370 | } |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 371 | }; |
| 372 | |
| 373 | /** |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 374 | * Split a single node into two nodes at the given offset. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 375 | * |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 376 | * For example: |
| 377 | * Given the DOM fragment '<div><span>Hello World</span></div>', call splitNode_ |
Zhu Qunying | 30d4071 | 2017-03-14 16:27:00 -0700 | [diff] [blame^] | 378 | * 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] | 379 | * become: '<div><span>Hello </span><span>World</span></div>'. If the span |
| 380 | * had any attributes they would have been copied to the new span as well. |
| 381 | * |
| 382 | * The to-be-split node must have a container, so that the new node can be |
| 383 | * placed next to it. |
| 384 | * |
| 385 | * @param {HTMLNode} node The node to split. |
| 386 | * @param {integer} offset The offset into the node where the split should |
| 387 | * occur. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 388 | */ |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 389 | hterm.Screen.prototype.splitNode_ = function(node, offset) { |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 390 | var afterNode = node.cloneNode(false); |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 391 | |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 392 | var textContent = node.textContent; |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 393 | node.textContent = hterm.TextAttributes.nodeSubstr(node, 0, offset); |
| 394 | afterNode.textContent = lib.wc.substr(textContent, offset); |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 395 | |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 396 | if (afterNode.textContent) |
| 397 | node.parentNode.insertBefore(afterNode, node.nextSibling); |
| 398 | if (!node.textContent) |
| 399 | node.parentNode.removeChild(node); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 400 | }; |
| 401 | |
| 402 | /** |
rginda | a9abdd8 | 2012-08-06 18:05:09 -0700 | [diff] [blame] | 403 | * 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] | 404 | */ |
rginda | a9abdd8 | 2012-08-06 18:05:09 -0700 | [diff] [blame] | 405 | hterm.Screen.prototype.maybeClipCurrentRow = function() { |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 406 | var width = hterm.TextAttributes.nodeWidth(this.cursorRowNode_); |
| 407 | |
| 408 | if (width <= this.columnCount_) { |
rginda | a9abdd8 | 2012-08-06 18:05:09 -0700 | [diff] [blame] | 409 | // Current row does not need clipping, but may need clamping. |
| 410 | if (this.cursorPosition.column >= this.columnCount_) { |
| 411 | this.setCursorPosition(this.cursorPosition.row, this.columnCount_ - 1); |
| 412 | this.cursorPosition.overflow = true; |
| 413 | } |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 414 | |
rginda | a9abdd8 | 2012-08-06 18:05:09 -0700 | [diff] [blame] | 415 | return; |
| 416 | } |
| 417 | |
| 418 | // Save off the current column so we can maybe restore it later. |
| 419 | var currentColumn = this.cursorPosition.column; |
| 420 | |
| 421 | // Move the cursor to the final column. |
| 422 | this.setCursorPosition(this.cursorPosition.row, this.columnCount_ - 1); |
| 423 | |
| 424 | // Remove any text that partially overflows. |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 425 | width = hterm.TextAttributes.nodeWidth(this.cursorNode_); |
| 426 | |
| 427 | if (this.cursorOffset_ < width - 1) { |
| 428 | this.cursorNode_.textContent = hterm.TextAttributes.nodeSubstr( |
| 429 | this.cursorNode_, 0, this.cursorOffset_ + 1); |
rginda | a9abdd8 | 2012-08-06 18:05:09 -0700 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | // Remove all nodes after the cursor. |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 433 | var rowNode = this.cursorRowNode_; |
| 434 | var node = this.cursorNode_.nextSibling; |
| 435 | |
| 436 | while (node) { |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 437 | rowNode.removeChild(node); |
| 438 | node = this.cursorNode_.nextSibling; |
| 439 | } |
| 440 | |
Robert Ginda | 7fd5708 | 2012-09-25 14:41:47 -0700 | [diff] [blame] | 441 | if (currentColumn < this.columnCount_) { |
rginda | a9abdd8 | 2012-08-06 18:05:09 -0700 | [diff] [blame] | 442 | // If the cursor was within the screen before we started then restore its |
| 443 | // position. |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 444 | this.setCursorPosition(this.cursorPosition.row, currentColumn); |
rginda | a9abdd8 | 2012-08-06 18:05:09 -0700 | [diff] [blame] | 445 | } else { |
| 446 | // Otherwise leave it at the the last column in the overflow state. |
| 447 | this.cursorPosition.overflow = true; |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 448 | } |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 449 | }; |
| 450 | |
| 451 | /** |
| 452 | * Insert a string at the current character position using the current |
| 453 | * text attributes. |
| 454 | * |
rginda | a09e733 | 2012-08-17 12:49:51 -0700 | [diff] [blame] | 455 | * You must call maybeClipCurrentRow() after in order to clip overflowed |
| 456 | * text and clamp the cursor. |
| 457 | * |
| 458 | * It is also up to the caller to properly maintain the line overflow state |
| 459 | * using hterm.Screen..commitLineOverflow(). |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 460 | */ |
| 461 | hterm.Screen.prototype.insertString = function(str) { |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 462 | var cursorNode = this.cursorNode_; |
| 463 | var cursorNodeText = cursorNode.textContent; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 464 | |
Robert Ginda | a21dfb3 | 2013-10-31 14:17:45 -0700 | [diff] [blame] | 465 | this.cursorRowNode_.removeAttribute('line-overflow'); |
| 466 | |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 467 | // We may alter the width of the string by prepending some missing |
| 468 | // whitespaces, so we need to record the string width ahead of time. |
| 469 | var strWidth = lib.wc.strWidth(str); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 470 | |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 471 | // No matter what, before this function exits the cursor column will have |
| 472 | // moved this much. |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 473 | this.cursorPosition.column += strWidth; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 474 | |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 475 | // Local cache of the cursor offset. |
| 476 | var offset = this.cursorOffset_; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 477 | |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 478 | // Reverse offset is the offset measured from the end of the string. |
| 479 | // Zero implies that the cursor is at the end of the cursor node. |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 480 | var reverseOffset = hterm.TextAttributes.nodeWidth(cursorNode) - offset; |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 481 | |
| 482 | if (reverseOffset < 0) { |
| 483 | // A negative reverse offset means the cursor is positioned past the end |
| 484 | // of the characters on this line. We'll need to insert the missing |
| 485 | // whitespace. |
rginda | cbbd748 | 2012-06-13 15:06:16 -0700 | [diff] [blame] | 486 | var ws = lib.f.getWhitespace(-reverseOffset); |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 487 | |
Brad Town | 7de8330 | 2015-03-12 02:10:32 -0700 | [diff] [blame] | 488 | // This whitespace should be completely unstyled. Underline, background |
| 489 | // color, and strikethrough would be visible on whitespace, so we can't use |
| 490 | // one of those spans to hold the text. |
Edoardo Spadolini | 198828a | 2014-08-08 00:22:51 +0200 | [diff] [blame] | 491 | if (!(this.textAttributes.underline || |
Brad Town | 7de8330 | 2015-03-12 02:10:32 -0700 | [diff] [blame] | 492 | this.textAttributes.strikethrough || |
Edoardo Spadolini | 198828a | 2014-08-08 00:22:51 +0200 | [diff] [blame] | 493 | this.textAttributes.background || |
| 494 | this.textAttributes.wcNode || |
| 495 | this.textAttributes.tileData != null)) { |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 496 | // Best case scenario, we can just pretend the spaces were part of the |
| 497 | // original string. |
| 498 | str = ws + str; |
| 499 | } else if (cursorNode.nodeType == 3 || |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 500 | !(cursorNode.wcNode || |
Edoardo Spadolini | 198828a | 2014-08-08 00:22:51 +0200 | [diff] [blame] | 501 | cursorNode.tileNode || |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 502 | cursorNode.style.textDecoration || |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 503 | cursorNode.style.backgroundColor)) { |
| 504 | // Second best case, the current node is able to hold the whitespace. |
| 505 | cursorNode.textContent = (cursorNodeText += ws); |
| 506 | } else { |
| 507 | // Worst case, we have to create a new node to hold the whitespace. |
| 508 | var wsNode = cursorNode.ownerDocument.createTextNode(ws); |
| 509 | this.cursorRowNode_.insertBefore(wsNode, cursorNode.nextSibling); |
| 510 | this.cursorNode_ = cursorNode = wsNode; |
| 511 | this.cursorOffset_ = offset = -reverseOffset; |
| 512 | cursorNodeText = ws; |
| 513 | } |
| 514 | |
| 515 | // We now know for sure that we're at the last character of the cursor node. |
| 516 | reverseOffset = 0; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 517 | } |
| 518 | |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 519 | if (this.textAttributes.matchesContainer(cursorNode)) { |
| 520 | // The new text can be placed directly in the cursor node. |
| 521 | if (reverseOffset == 0) { |
| 522 | cursorNode.textContent = cursorNodeText + str; |
| 523 | } else if (offset == 0) { |
| 524 | cursorNode.textContent = str + cursorNodeText; |
| 525 | } else { |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 526 | cursorNode.textContent = |
| 527 | hterm.TextAttributes.nodeSubstr(cursorNode, 0, offset) + |
| 528 | str + hterm.TextAttributes.nodeSubstr(cursorNode, offset); |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 529 | } |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 530 | |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 531 | this.cursorOffset_ += strWidth; |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 532 | return; |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 533 | } |
| 534 | |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 535 | // The cursor node is the wrong style for the new text. If we're at the |
| 536 | // beginning or end of the cursor node, then the adjacent node is also a |
| 537 | // potential candidate. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 538 | |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 539 | if (offset == 0) { |
| 540 | // At the beginning of the cursor node, the check the previous sibling. |
| 541 | var previousSibling = cursorNode.previousSibling; |
| 542 | if (previousSibling && |
| 543 | this.textAttributes.matchesContainer(previousSibling)) { |
| 544 | previousSibling.textContent += str; |
| 545 | this.cursorNode_ = previousSibling; |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 546 | this.cursorOffset_ = lib.wc.strWidth(previousSibling.textContent); |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 547 | return; |
| 548 | } |
| 549 | |
| 550 | var newNode = this.textAttributes.createContainer(str); |
| 551 | this.cursorRowNode_.insertBefore(newNode, cursorNode); |
| 552 | this.cursorNode_ = newNode; |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 553 | this.cursorOffset_ = strWidth; |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 554 | return; |
| 555 | } |
| 556 | |
| 557 | if (reverseOffset == 0) { |
| 558 | // At the end of the cursor node, the check the next sibling. |
| 559 | var nextSibling = cursorNode.nextSibling; |
| 560 | if (nextSibling && |
| 561 | this.textAttributes.matchesContainer(nextSibling)) { |
| 562 | nextSibling.textContent = str + nextSibling.textContent; |
| 563 | this.cursorNode_ = nextSibling; |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 564 | this.cursorOffset_ = lib.wc.strWidth(str); |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 565 | return; |
| 566 | } |
| 567 | |
| 568 | var newNode = this.textAttributes.createContainer(str); |
| 569 | this.cursorRowNode_.insertBefore(newNode, nextSibling); |
| 570 | this.cursorNode_ = newNode; |
| 571 | // We specifically need to include any missing whitespace here, since it's |
| 572 | // going in a new node. |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 573 | this.cursorOffset_ = hterm.TextAttributes.nodeWidth(newNode); |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 574 | return; |
| 575 | } |
| 576 | |
| 577 | // Worst case, we're somewhere in the middle of the cursor node. We'll |
| 578 | // have to split it into two nodes and insert our new container in between. |
| 579 | this.splitNode_(cursorNode, offset); |
| 580 | var newNode = this.textAttributes.createContainer(str); |
| 581 | this.cursorRowNode_.insertBefore(newNode, cursorNode.nextSibling); |
| 582 | this.cursorNode_ = newNode; |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 583 | this.cursorOffset_ = strWidth; |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 584 | }; |
| 585 | |
| 586 | /** |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 587 | * Overwrite the text at the current cursor position. |
| 588 | * |
rginda | a09e733 | 2012-08-17 12:49:51 -0700 | [diff] [blame] | 589 | * You must call maybeClipCurrentRow() after in order to clip overflowed |
| 590 | * text and clamp the cursor. |
| 591 | * |
| 592 | * It is also up to the caller to properly maintain the line overflow state |
| 593 | * using hterm.Screen..commitLineOverflow(). |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 594 | */ |
| 595 | hterm.Screen.prototype.overwriteString = function(str) { |
| 596 | var maxLength = this.columnCount_ - this.cursorPosition.column; |
| 597 | if (!maxLength) |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 598 | return [str]; |
| 599 | |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 600 | var width = lib.wc.strWidth(str); |
| 601 | if (this.textAttributes.matchesContainer(this.cursorNode_) && |
| 602 | this.cursorNode_.textContent.substr(this.cursorOffset_) == str) { |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 603 | // This overwrite would be a no-op, just move the cursor and return. |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 604 | this.cursorOffset_ += width; |
| 605 | this.cursorPosition.column += width; |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 606 | return; |
| 607 | } |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 608 | |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 609 | this.deleteChars(Math.min(width, maxLength)); |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 610 | this.insertString(str); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 611 | }; |
| 612 | |
| 613 | /** |
| 614 | * Forward-delete one or more characters at the current cursor position. |
| 615 | * |
| 616 | * Text to the right of the deleted characters is shifted left. Only affects |
| 617 | * characters on the same row as the cursor. |
| 618 | * |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 619 | * @param {integer} count The column width of characters to delete. This is |
| 620 | * clamped to the column width minus the cursor column. |
| 621 | * @return {integer} The column width of the characters actually deleted. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 622 | */ |
| 623 | hterm.Screen.prototype.deleteChars = function(count) { |
| 624 | var node = this.cursorNode_; |
| 625 | var offset = this.cursorOffset_; |
| 626 | |
Robert Ginda | 7fd5708 | 2012-09-25 14:41:47 -0700 | [diff] [blame] | 627 | var currentCursorColumn = this.cursorPosition.column; |
| 628 | count = Math.min(count, this.columnCount_ - currentCursorColumn); |
| 629 | if (!count) |
| 630 | return 0; |
| 631 | |
| 632 | var rv = count; |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 633 | var startLength, endLength; |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 634 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 635 | while (node && count) { |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 636 | startLength = hterm.TextAttributes.nodeWidth(node); |
| 637 | node.textContent = hterm.TextAttributes.nodeSubstr(node, 0, offset) + |
| 638 | hterm.TextAttributes.nodeSubstr(node, offset + count); |
| 639 | endLength = hterm.TextAttributes.nodeWidth(node); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 640 | count -= startLength - endLength; |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 641 | if (offset < startLength && endLength && startLength == endLength) { |
| 642 | // No characters were deleted when there should be. We're probably trying |
| 643 | // to delete one column width from a wide character node. We remove the |
| 644 | // wide character node here and replace it with a single space. |
| 645 | var spaceNode = this.textAttributes.createContainer(' '); |
| 646 | node.parentNode.insertBefore(spaceNode, node.nextSibling); |
| 647 | node.textContent = ''; |
| 648 | endLength = 0; |
| 649 | count -= 1; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 650 | } |
| 651 | |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 652 | var nextNode = node.nextSibling; |
| 653 | if (endLength == 0 && node != this.cursorNode_) { |
| 654 | node.parentNode.removeChild(node); |
| 655 | } |
| 656 | node = nextNode; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 657 | offset = 0; |
| 658 | } |
Robert Ginda | 7fd5708 | 2012-09-25 14:41:47 -0700 | [diff] [blame] | 659 | |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 660 | // Remove this.cursorNode_ if it is an empty non-text node. |
| 661 | if (this.cursorNode_.nodeType != 3 && !this.cursorNode_.textContent) { |
| 662 | var cursorNode = this.cursorNode_; |
| 663 | if (cursorNode.previousSibling) { |
| 664 | this.cursorNode_ = cursorNode.previousSibling; |
| 665 | this.cursorOffset_ = hterm.TextAttributes.nodeWidth( |
| 666 | cursorNode.previousSibling); |
| 667 | } else if (cursorNode.nextSibling) { |
| 668 | this.cursorNode_ = cursorNode.nextSibling; |
| 669 | this.cursorOffset_ = 0; |
| 670 | } else { |
| 671 | var emptyNode = this.cursorRowNode_.ownerDocument.createTextNode(''); |
| 672 | this.cursorRowNode_.appendChild(emptyNode); |
| 673 | this.cursorNode_ = emptyNode; |
| 674 | this.cursorOffset_ = 0; |
| 675 | } |
| 676 | this.cursorRowNode_.removeChild(cursorNode); |
| 677 | } |
| 678 | |
Robert Ginda | 7fd5708 | 2012-09-25 14:41:47 -0700 | [diff] [blame] | 679 | return rv; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 680 | }; |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 681 | |
| 682 | /** |
| 683 | * Finds first X-ROW of a line containing specified X-ROW. |
| 684 | * Used to support line overflow. |
| 685 | * |
| 686 | * @param {Node} row X-ROW to begin search for first row of line. |
| 687 | * @return {Node} The X-ROW that is at the beginning of the line. |
| 688 | **/ |
| 689 | hterm.Screen.prototype.getLineStartRow_ = function(row) { |
| 690 | while (row.previousSibling && |
| 691 | row.previousSibling.hasAttribute('line-overflow')) { |
| 692 | row = row.previousSibling; |
| 693 | } |
| 694 | return row; |
| 695 | }; |
| 696 | |
| 697 | /** |
| 698 | * Gets text of a line beginning with row. |
| 699 | * Supports line overflow. |
| 700 | * |
| 701 | * @param {Node} row First X-ROW of line. |
| 702 | * @return {string} Text content of line. |
| 703 | **/ |
| 704 | hterm.Screen.prototype.getLineText_ = function(row) { |
| 705 | var rowText = ""; |
| 706 | while (row) { |
| 707 | rowText += row.textContent; |
| 708 | if (row.hasAttribute('line-overflow')) { |
| 709 | row = row.nextSibling; |
| 710 | } else { |
| 711 | break; |
| 712 | } |
| 713 | } |
| 714 | return rowText; |
| 715 | }; |
| 716 | |
| 717 | /** |
| 718 | * Returns X-ROW that is ancestor of the node. |
| 719 | * |
| 720 | * @param {Node} node Node to get X-ROW ancestor for. |
| 721 | * @return {Node} X-ROW ancestor of node, or null if not found. |
| 722 | **/ |
| 723 | hterm.Screen.prototype.getXRowAncestor_ = function(node) { |
| 724 | while (node) { |
| 725 | if (node.nodeName === 'X-ROW') |
| 726 | break; |
| 727 | node = node.parentNode; |
| 728 | } |
| 729 | return node; |
| 730 | }; |
| 731 | |
| 732 | /** |
| 733 | * Returns position within line of character at offset within node. |
| 734 | * Supports line overflow. |
| 735 | * |
| 736 | * @param {Node} row X-ROW at beginning of line. |
| 737 | * @param {Node} node Node to get position of. |
| 738 | * @param {integer} offset Offset into node. |
| 739 | * |
| 740 | * @return {integer} Position within line of character at offset within node. |
| 741 | **/ |
| 742 | hterm.Screen.prototype.getPositionWithOverflow_ = function(row, node, offset) { |
| 743 | if (!node) |
| 744 | return -1; |
| 745 | var ancestorRow = this.getXRowAncestor_(node); |
| 746 | if (!ancestorRow) |
| 747 | return -1; |
| 748 | var position = 0; |
| 749 | while (ancestorRow != row) { |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 750 | position += hterm.TextAttributes.nodeWidth(row); |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 751 | if (row.hasAttribute('line-overflow') && row.nextSibling) { |
| 752 | row = row.nextSibling; |
| 753 | } else { |
| 754 | return -1; |
| 755 | } |
| 756 | } |
| 757 | return position + this.getPositionWithinRow_(row, node, offset); |
| 758 | }; |
| 759 | |
| 760 | /** |
| 761 | * Returns position within row of character at offset within node. |
| 762 | * Does not support line overflow. |
| 763 | * |
| 764 | * @param {Node} row X-ROW to get position within. |
| 765 | * @param {Node} node Node to get position for. |
| 766 | * @param {integer} offset Offset within node to get position for. |
| 767 | * @return {integer} Position within row of character at offset within node. |
| 768 | **/ |
| 769 | hterm.Screen.prototype.getPositionWithinRow_ = function(row, node, offset) { |
| 770 | if (node.parentNode != row) { |
| 771 | return this.getPositionWithinRow_(node.parentNode, node, offset) + |
| 772 | this.getPositionWithinRow_(row, node.parentNode, 0); |
| 773 | } |
| 774 | var position = 0; |
| 775 | for (var i = 0; i < row.childNodes.length; i++) { |
| 776 | var currentNode = row.childNodes[i]; |
| 777 | if (currentNode == node) |
| 778 | return position + offset; |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 779 | position += hterm.TextAttributes.nodeWidth(currentNode); |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 780 | } |
| 781 | return -1; |
| 782 | }; |
| 783 | |
| 784 | /** |
| 785 | * Returns the node and offset corresponding to position within line. |
| 786 | * Supports line overflow. |
| 787 | * |
| 788 | * @param {Node} row X-ROW at beginning of line. |
| 789 | * @param {integer} position Position within line to retrieve node and offset. |
| 790 | * @return {Array} Two element array containing node and offset respectively. |
| 791 | **/ |
| 792 | hterm.Screen.prototype.getNodeAndOffsetWithOverflow_ = function(row, position) { |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 793 | while (row && position > hterm.TextAttributes.nodeWidth(row)) { |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 794 | if (row.hasAttribute('line-overflow') && row.nextSibling) { |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 795 | position -= hterm.TextAttributes.nodeWidth(row); |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 796 | row = row.nextSibling; |
| 797 | } else { |
| 798 | return -1; |
| 799 | } |
| 800 | } |
| 801 | return this.getNodeAndOffsetWithinRow_(row, position); |
| 802 | }; |
| 803 | |
| 804 | /** |
| 805 | * Returns the node and offset corresponding to position within row. |
| 806 | * Does not support line overflow. |
| 807 | * |
| 808 | * @param {Node} row X-ROW to get position within. |
| 809 | * @param {integer} position Position within row to retrieve node and offset. |
| 810 | * @return {Array} Two element array containing node and offset respectively. |
| 811 | **/ |
| 812 | hterm.Screen.prototype.getNodeAndOffsetWithinRow_ = function(row, position) { |
| 813 | for (var i = 0; i < row.childNodes.length; i++) { |
| 814 | var node = row.childNodes[i]; |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 815 | var nodeTextWidth = hterm.TextAttributes.nodeWidth(node); |
| 816 | if (position <= nodeTextWidth) { |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 817 | if (node.nodeName === 'SPAN') { |
| 818 | /** Drill down to node contained by SPAN. **/ |
| 819 | return this.getNodeAndOffsetWithinRow_(node, position); |
| 820 | } else { |
| 821 | return [node, position]; |
| 822 | } |
| 823 | } |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 824 | position -= nodeTextWidth; |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 825 | } |
| 826 | return null; |
| 827 | }; |
| 828 | |
| 829 | /** |
| 830 | * Returns the node and offset corresponding to position within line. |
| 831 | * Supports line overflow. |
| 832 | * |
| 833 | * @param {Node} row X-ROW at beginning of line. |
| 834 | * @param {integer} start Start position of range within line. |
| 835 | * @param {integer} end End position of range within line. |
| 836 | * @param {Range} range Range to modify. |
| 837 | **/ |
| 838 | hterm.Screen.prototype.setRange_ = function(row, start, end, range) { |
| 839 | var startNodeAndOffset = this.getNodeAndOffsetWithOverflow_(row, start); |
| 840 | if (startNodeAndOffset == null) |
| 841 | return; |
| 842 | var endNodeAndOffset = this.getNodeAndOffsetWithOverflow_(row, end); |
| 843 | if (endNodeAndOffset == null) |
| 844 | return; |
| 845 | range.setStart(startNodeAndOffset[0], startNodeAndOffset[1]); |
| 846 | range.setEnd(endNodeAndOffset[0], endNodeAndOffset[1]); |
| 847 | }; |
| 848 | |
| 849 | /** |
| 850 | * Expands selection to surround URLs. |
| 851 | * |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 852 | * @param {Selection} selection Selection to expand. |
| 853 | **/ |
| 854 | hterm.Screen.prototype.expandSelection = function(selection) { |
| 855 | if (!selection) |
| 856 | return; |
| 857 | |
| 858 | var range = selection.getRangeAt(0); |
| 859 | if (!range || range.toString().match(/\s/)) |
| 860 | return; |
| 861 | |
| 862 | var row = this.getLineStartRow_(this.getXRowAncestor_(range.startContainer)); |
| 863 | if (!row) |
| 864 | return; |
| 865 | |
| 866 | var startPosition = this.getPositionWithOverflow_(row, |
| 867 | range.startContainer, |
| 868 | range.startOffset); |
| 869 | if (startPosition == -1) |
| 870 | return; |
| 871 | var endPosition = this.getPositionWithOverflow_(row, |
| 872 | range.endContainer, |
| 873 | range.endOffset); |
| 874 | if (endPosition == -1) |
| 875 | return; |
| 876 | |
Robert Ginda | 5eba456 | 2014-08-11 11:05:54 -0700 | [diff] [blame] | 877 | // Matches can start with '~' or '.', since paths frequently do. |
| 878 | var leftMatch = '[^\\s\\[\\](){}<>"\'\\^!@#$%&*,;:`]'; |
| 879 | var rightMatch = '[^\\s\\[\\](){}<>"\'\\^!@#$%&*,;:~.`]'; |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 880 | var insideMatch = '[^\\s\\[\\](){}<>"\'\\^]*'; |
| 881 | |
| 882 | //Move start to the left. |
| 883 | var rowText = this.getLineText_(row); |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 884 | var lineUpToRange = lib.wc.substring(rowText, 0, endPosition); |
Robert Ginda | 5eba456 | 2014-08-11 11:05:54 -0700 | [diff] [blame] | 885 | var leftRegularExpression = new RegExp(leftMatch + insideMatch + "$"); |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 886 | var expandedStart = lineUpToRange.search(leftRegularExpression); |
| 887 | if (expandedStart == -1 || expandedStart > startPosition) |
| 888 | return; |
| 889 | |
| 890 | //Move end to the right. |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 891 | var lineFromRange = lib.wc.substring(rowText, startPosition, |
| 892 | lib.wc.strWidth(rowText)); |
Robert Ginda | 5eba456 | 2014-08-11 11:05:54 -0700 | [diff] [blame] | 893 | var rightRegularExpression = new RegExp("^" + insideMatch + rightMatch); |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 894 | var found = lineFromRange.match(rightRegularExpression); |
| 895 | if (!found) |
| 896 | return; |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 897 | var expandedEnd = startPosition + lib.wc.strWidth(found[0]); |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 898 | if (expandedEnd == -1 || expandedEnd < endPosition) |
| 899 | return; |
| 900 | |
| 901 | this.setRange_(row, expandedStart, expandedEnd, range); |
| 902 | selection.addRange(range); |
| 903 | }; |