]> git.proxmox.com Git - mirror_xterm.js.git/blob - src/SelectionManager.test.ts
Merge pull request #816 from joaomoreno/alt-selection
[mirror_xterm.js.git] / src / SelectionManager.test.ts
1 /**
2 * @license MIT
3 */
4 import jsdom = require('jsdom');
5 import { assert } from 'chai';
6 import { ITerminal, ICircularList } from './Interfaces';
7 import { CharMeasure } from './utils/CharMeasure';
8 import { CircularList } from './utils/CircularList';
9 import { SelectionManager } from './SelectionManager';
10 import { SelectionModel } from './SelectionModel';
11 import { BufferSet } from './BufferSet';
12
13 class TestSelectionManager extends SelectionManager {
14 constructor(
15 terminal: ITerminal,
16 buffer: ICircularList<[number, string, number][]>,
17 rowContainer: HTMLElement,
18 charMeasure: CharMeasure
19 ) {
20 super(terminal, buffer, rowContainer, charMeasure);
21 }
22
23 public get model(): SelectionModel { return this._model; }
24
25 public selectLineAt(line: number): void { this._selectLineAt(line); }
26 public selectWordAt(coords: [number, number]): void { this._selectWordAt(coords); }
27
28 // Disable DOM interaction
29 public enable(): void {}
30 public disable(): void {}
31 public refresh(): void {}
32 }
33
34 describe('SelectionManager', () => {
35 let dom: jsdom.JSDOM;
36 let window: Window;
37 let document: Document;
38
39 let terminal: ITerminal;
40 let bufferLines: ICircularList<[number, string, number][]>;
41 let rowContainer: HTMLElement;
42 let selectionManager: TestSelectionManager;
43
44 beforeEach(() => {
45 dom = new jsdom.JSDOM('');
46 window = dom.window;
47 document = window.document;
48 rowContainer = document.createElement('div');
49 terminal = <any>{ cols: 80, rows: 2 };
50 terminal.scrollback = 100;
51 terminal.buffers = new BufferSet(terminal);
52 terminal.buffer = terminal.buffers.active;
53 bufferLines = terminal.buffer.lines;
54 selectionManager = new TestSelectionManager(terminal, bufferLines, rowContainer, null);
55 });
56
57 function stringToRow(text: string): [number, string, number][] {
58 let result: [number, string, number][] = [];
59 for (let i = 0; i < text.length; i++) {
60 result.push([0, text.charAt(i), 1]);
61 }
62 return result;
63 }
64
65 describe('_selectWordAt', () => {
66 it('should expand selection for normal width chars', () => {
67 bufferLines.push(stringToRow('foo bar'));
68 selectionManager.selectWordAt([0, 0]);
69 assert.equal(selectionManager.selectionText, 'foo');
70 selectionManager.selectWordAt([1, 0]);
71 assert.equal(selectionManager.selectionText, 'foo');
72 selectionManager.selectWordAt([2, 0]);
73 assert.equal(selectionManager.selectionText, 'foo');
74 selectionManager.selectWordAt([3, 0]);
75 assert.equal(selectionManager.selectionText, ' ');
76 selectionManager.selectWordAt([4, 0]);
77 assert.equal(selectionManager.selectionText, 'bar');
78 selectionManager.selectWordAt([5, 0]);
79 assert.equal(selectionManager.selectionText, 'bar');
80 selectionManager.selectWordAt([6, 0]);
81 assert.equal(selectionManager.selectionText, 'bar');
82 });
83 it('should expand selection for whitespace', () => {
84 bufferLines.push(stringToRow('a b'));
85 selectionManager.selectWordAt([0, 0]);
86 assert.equal(selectionManager.selectionText, 'a');
87 selectionManager.selectWordAt([1, 0]);
88 assert.equal(selectionManager.selectionText, ' ');
89 selectionManager.selectWordAt([2, 0]);
90 assert.equal(selectionManager.selectionText, ' ');
91 selectionManager.selectWordAt([3, 0]);
92 assert.equal(selectionManager.selectionText, ' ');
93 selectionManager.selectWordAt([4, 0]);
94 assert.equal(selectionManager.selectionText, 'b');
95 });
96 it('should expand selection for wide characters', () => {
97 // Wide characters use a special format
98 bufferLines.push([
99 [null, '中', 2],
100 [null, '', 0],
101 [null, '文', 2],
102 [null, '', 0],
103 [null, ' ', 1],
104 [null, 'a', 1],
105 [null, '中', 2],
106 [null, '', 0],
107 [null, '文', 2],
108 [null, '', 0],
109 [null, 'b', 1],
110 [null, ' ', 1],
111 [null, 'f', 1],
112 [null, 'o', 1],
113 [null, 'o', 1]
114 ]);
115 // Ensure wide characters take up 2 columns
116 selectionManager.selectWordAt([0, 0]);
117 assert.equal(selectionManager.selectionText, '中文');
118 selectionManager.selectWordAt([1, 0]);
119 assert.equal(selectionManager.selectionText, '中文');
120 selectionManager.selectWordAt([2, 0]);
121 assert.equal(selectionManager.selectionText, '中文');
122 selectionManager.selectWordAt([3, 0]);
123 assert.equal(selectionManager.selectionText, '中文');
124 selectionManager.selectWordAt([4, 0]);
125 assert.equal(selectionManager.selectionText, ' ');
126 // Ensure wide characters work when wrapped in normal width characters
127 selectionManager.selectWordAt([5, 0]);
128 assert.equal(selectionManager.selectionText, 'a中文b');
129 selectionManager.selectWordAt([6, 0]);
130 assert.equal(selectionManager.selectionText, 'a中文b');
131 selectionManager.selectWordAt([7, 0]);
132 assert.equal(selectionManager.selectionText, 'a中文b');
133 selectionManager.selectWordAt([8, 0]);
134 assert.equal(selectionManager.selectionText, 'a中文b');
135 selectionManager.selectWordAt([9, 0]);
136 assert.equal(selectionManager.selectionText, 'a中文b');
137 selectionManager.selectWordAt([10, 0]);
138 assert.equal(selectionManager.selectionText, 'a中文b');
139 selectionManager.selectWordAt([11, 0]);
140 assert.equal(selectionManager.selectionText, ' ');
141 // Ensure normal width characters work fine in a line containing wide characters
142 selectionManager.selectWordAt([12, 0]);
143 assert.equal(selectionManager.selectionText, 'foo');
144 selectionManager.selectWordAt([13, 0]);
145 assert.equal(selectionManager.selectionText, 'foo');
146 selectionManager.selectWordAt([14, 0]);
147 assert.equal(selectionManager.selectionText, 'foo');
148 });
149 it('should select up to non-path characters that are commonly adjacent to paths', () => {
150 bufferLines.push(stringToRow('(cd)[ef]{gh}\'ij"'));
151 selectionManager.selectWordAt([0, 0]);
152 assert.equal(selectionManager.selectionText, '(cd');
153 selectionManager.selectWordAt([1, 0]);
154 assert.equal(selectionManager.selectionText, 'cd');
155 selectionManager.selectWordAt([2, 0]);
156 assert.equal(selectionManager.selectionText, 'cd');
157 selectionManager.selectWordAt([3, 0]);
158 assert.equal(selectionManager.selectionText, 'cd)');
159 selectionManager.selectWordAt([4, 0]);
160 assert.equal(selectionManager.selectionText, '[ef');
161 selectionManager.selectWordAt([5, 0]);
162 assert.equal(selectionManager.selectionText, 'ef');
163 selectionManager.selectWordAt([6, 0]);
164 assert.equal(selectionManager.selectionText, 'ef');
165 selectionManager.selectWordAt([7, 0]);
166 assert.equal(selectionManager.selectionText, 'ef]');
167 selectionManager.selectWordAt([8, 0]);
168 assert.equal(selectionManager.selectionText, '{gh');
169 selectionManager.selectWordAt([9, 0]);
170 assert.equal(selectionManager.selectionText, 'gh');
171 selectionManager.selectWordAt([10, 0]);
172 assert.equal(selectionManager.selectionText, 'gh');
173 selectionManager.selectWordAt([11, 0]);
174 assert.equal(selectionManager.selectionText, 'gh}');
175 selectionManager.selectWordAt([12, 0]);
176 assert.equal(selectionManager.selectionText, '\'ij');
177 selectionManager.selectWordAt([13, 0]);
178 assert.equal(selectionManager.selectionText, 'ij');
179 selectionManager.selectWordAt([14, 0]);
180 assert.equal(selectionManager.selectionText, 'ij');
181 selectionManager.selectWordAt([15, 0]);
182 assert.equal(selectionManager.selectionText, 'ij"');
183 });
184 });
185
186 describe('_selectLineAt', () => {
187 it('should select the entire line', () => {
188 bufferLines.push(stringToRow('foo bar'));
189 selectionManager.selectLineAt(0);
190 assert.equal(selectionManager.selectionText, 'foo bar', 'The selected text is correct');
191 assert.deepEqual(selectionManager.model.finalSelectionStart, [0, 0]);
192 assert.deepEqual(selectionManager.model.finalSelectionEnd, [terminal.cols, 0], 'The actual selection spans the entire column');
193 });
194 });
195
196 describe('selectAll', () => {
197 it('should select the entire buffer, beyond the viewport', () => {
198 bufferLines.push(stringToRow('1'));
199 bufferLines.push(stringToRow('2'));
200 bufferLines.push(stringToRow('3'));
201 bufferLines.push(stringToRow('4'));
202 bufferLines.push(stringToRow('5'));
203 selectionManager.selectAll();
204 terminal.buffer.ybase = bufferLines.length - terminal.rows;
205 assert.equal(selectionManager.selectionText, '1\n2\n3\n4\n5');
206 });
207 });
208
209 describe('hasSelection', () => {
210 it('should return whether there is a selection', () => {
211 selectionManager.model.selectionStart = [0, 0];
212 selectionManager.model.selectionStartLength = 0;
213 assert.equal(selectionManager.hasSelection, false);
214 selectionManager.model.selectionEnd = [0, 0];
215 assert.equal(selectionManager.hasSelection, false);
216 selectionManager.model.selectionEnd = [1, 0];
217 assert.equal(selectionManager.hasSelection, true);
218 selectionManager.model.selectionEnd = [0, 1];
219 assert.equal(selectionManager.hasSelection, true);
220 selectionManager.model.selectionEnd = [1, 1];
221 assert.equal(selectionManager.hasSelection, true);
222 });
223 });
224 });