From 06e89549a88f89fd95978346345f554398d05a11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Breitbart?= Date: Fri, 23 Nov 2018 23:30:37 +0100 Subject: [PATCH 01/41] typed array support for parser --- src/EscapeSequenceParser.test.ts | 294 +++++++++++++++++-------------- src/EscapeSequenceParser.ts | 24 +-- src/InputHandler.test.ts | 4 +- src/InputHandler.ts | 92 ++++++---- src/Types.ts | 8 +- 5 files changed, 236 insertions(+), 186 deletions(-) diff --git a/src/EscapeSequenceParser.test.ts b/src/EscapeSequenceParser.test.ts index e92f4125..3e310b08 100644 --- a/src/EscapeSequenceParser.test.ts +++ b/src/EscapeSequenceParser.test.ts @@ -50,8 +50,12 @@ const testTerminal: any = { compare: function (value: any): void { chai.expect(this.calls.slice()).eql(value); // weird bug w'o slicing here }, - print: function (data: string, start: number, end: number): void { - this.calls.push(['print', data.substring(start, end)]); + print: function (data: Uint16Array, start: number, end: number): void { + let s = ''; + for (let i = start; i < end; ++i) { + s += String.fromCharCode(data[i]); + } + this.calls.push(['print', s]); }, actionOSC: function (s: string): void { this.calls.push(['osc', s]); @@ -68,8 +72,12 @@ const testTerminal: any = { actionDCSHook: function (collect: string, params: number[], flag: string): void { this.calls.push(['dcs hook', collect, params, flag]); }, - actionDCSPrint: function (data: string, start: number, end: number): void { - this.calls.push(['dcs put', data.substring(start, end)]); + actionDCSPrint: function (data: Uint16Array, start: number, end: number): void { + let s = ''; + for (let i = start; i < end; ++i) { + s += String.fromCharCode(data[i]); + } + this.calls.push(['dcs put', s]); }, actionDCSUnhook: function (): void { this.calls.push(['dcs unhook']); @@ -81,7 +89,7 @@ class DcsTest implements IDcsHandler { hook(collect: string, params: number[], flag: number): void { testTerminal.actionDCSHook(collect, params, String.fromCharCode(flag)); } - put(data: string, start: number, end: number): void { + put(data: Uint16Array, start: number, end: number): void { testTerminal.actionDCSPrint(data, start, end); } unhook(): void { @@ -155,6 +163,14 @@ interface IRun { parser: TestEscapeSequenceParser; } +// translate string based parse calls into typed array based +function parse(parser: TestEscapeSequenceParser, data: string): void { + const container = new Uint16Array(data.length); + for (let i = 0; i < data.length; ++i) { + container[i] = data.charCodeAt(i); + } + parser.parse(container, data.length); +} describe('EscapeSequenceParser', function (): void { let parser: TestEscapeSequenceParser | null = null; @@ -205,12 +221,12 @@ describe('EscapeSequenceParser', function (): void { it('state GROUND execute action', function (): void { parser.reset(); testTerminal.clear(); - const exes = r(0x00, 0x18); - exes.concat(['\x19']); - exes.concat(r(0x1c, 0x20)); + let exes = r(0x00, 0x18); + exes = exes.concat(['\x19']); + exes = exes.concat(r(0x1c, 0x20)); for (let i = 0; i < exes.length; ++i) { parser.currentState = ParserState.GROUND; - parser.parse(exes[i]); + parse(parser, exes[i]); chai.expect(parser.currentState).equal(ParserState.GROUND); testTerminal.compare([['exe', exes[i]]]); parser.reset(); @@ -223,7 +239,7 @@ describe('EscapeSequenceParser', function (): void { const printables = r(0x20, 0x7f); // NOTE: DEL excluded for (let i = 0; i < printables.length; ++i) { parser.currentState = ParserState.GROUND; - parser.parse(printables[i]); + parse(parser, printables[i]); chai.expect(parser.currentState).equal(ParserState.GROUND); testTerminal.compare([['print', printables[i]]]); parser.reset(); @@ -245,13 +261,13 @@ describe('EscapeSequenceParser', function (): void { for (state in states) { for (let i = 0; i < exes.length; ++i) { parser.currentState = state; - parser.parse(exes[i]); + parse(parser, exes[i]); chai.expect(parser.currentState).equal(ParserState.GROUND); testTerminal.compare((state in exceptions ? exceptions[state][exes[i]] : 0) || [['exe', exes[i]]]); parser.reset(); testTerminal.clear(); } - parser.parse('\x9c'); + parse(parser, '\x9c'); chai.expect(parser.currentState).equal(ParserState.GROUND); testTerminal.compare([]); parser.reset(); @@ -265,7 +281,7 @@ describe('EscapeSequenceParser', function (): void { parser.osc = '#'; parser.params = [23]; parser.collect = '#'; - parser.parse('\x1b'); + parse(parser, '\x1b'); chai.expect(parser.currentState).equal(ParserState.ESCAPE); chai.expect(parser.osc).equal(''); chai.expect(parser.params).eql([0]); @@ -276,12 +292,12 @@ describe('EscapeSequenceParser', function (): void { it('state ESCAPE execute rules', function (): void { parser.reset(); testTerminal.clear(); - const exes = r(0x00, 0x18); - exes.concat(['\x19']); - exes.concat(r(0x1c, 0x20)); + let exes = r(0x00, 0x18); + exes = exes.concat(['\x19']); + exes = exes.concat(r(0x1c, 0x20)); for (let i = 0; i < exes.length; ++i) { parser.currentState = ParserState.ESCAPE; - parser.parse(exes[i]); + parse(parser, exes[i]); chai.expect(parser.currentState).equal(ParserState.ESCAPE); testTerminal.compare([['exe', exes[i]]]); parser.reset(); @@ -292,7 +308,7 @@ describe('EscapeSequenceParser', function (): void { parser.reset(); testTerminal.clear(); parser.currentState = ParserState.ESCAPE; - parser.parse('\x7f'); + parse(parser, '\x7f'); chai.expect(parser.currentState).equal(ParserState.ESCAPE); testTerminal.compare([]); parser.reset(); @@ -301,13 +317,13 @@ describe('EscapeSequenceParser', function (): void { it('trans ESCAPE --> GROUND with ecs_dispatch action', function (): void { parser.reset(); testTerminal.clear(); - const dispatches = r(0x30, 0x50); - dispatches.concat(r(0x51, 0x58)); - dispatches.concat(['\x59', '\x5a', '\x5c']); - dispatches.concat(r(0x60, 0x7f)); + let dispatches = r(0x30, 0x50); + dispatches = dispatches.concat(r(0x51, 0x58)); + dispatches = dispatches.concat(['\x59', '\x5a']); // excluded \x5c + dispatches = dispatches.concat(r(0x60, 0x7f)); for (let i = 0; i < dispatches.length; ++i) { parser.currentState = ParserState.ESCAPE; - parser.parse(dispatches[i]); + parse(parser, dispatches[i]); chai.expect(parser.currentState).equal(ParserState.GROUND); testTerminal.compare([['esc', '', dispatches[i]]]); parser.reset(); @@ -319,7 +335,7 @@ describe('EscapeSequenceParser', function (): void { const collect = r(0x20, 0x30); for (let i = 0; i < collect.length; ++i) { parser.currentState = ParserState.ESCAPE; - parser.parse(collect[i]); + parse(parser, collect[i]); chai.expect(parser.currentState).equal(ParserState.ESCAPE_INTERMEDIATE); chai.expect(parser.collect).equal(collect[i]); parser.reset(); @@ -328,12 +344,12 @@ describe('EscapeSequenceParser', function (): void { it('state ESCAPE_INTERMEDIATE execute rules', function (): void { parser.reset(); testTerminal.clear(); - const exes = r(0x00, 0x18); - exes.concat(['\x19']); - exes.concat(r(0x1c, 0x20)); + let exes = r(0x00, 0x18); + exes = exes.concat(['\x19']); + exes = exes.concat(r(0x1c, 0x20)); for (let i = 0; i < exes.length; ++i) { parser.currentState = ParserState.ESCAPE_INTERMEDIATE; - parser.parse(exes[i]); + parse(parser, exes[i]); chai.expect(parser.currentState).equal(ParserState.ESCAPE_INTERMEDIATE); testTerminal.compare([['exe', exes[i]]]); parser.reset(); @@ -344,7 +360,7 @@ describe('EscapeSequenceParser', function (): void { parser.reset(); testTerminal.clear(); parser.currentState = ParserState.ESCAPE_INTERMEDIATE; - parser.parse('\x7f'); + parse(parser, '\x7f'); chai.expect(parser.currentState).equal(ParserState.ESCAPE_INTERMEDIATE); testTerminal.compare([]); parser.reset(); @@ -355,7 +371,7 @@ describe('EscapeSequenceParser', function (): void { const collect = r(0x20, 0x30); for (let i = 0; i < collect.length; ++i) { parser.currentState = ParserState.ESCAPE_INTERMEDIATE; - parser.parse(collect[i]); + parse(parser, collect[i]); chai.expect(parser.currentState).equal(ParserState.ESCAPE_INTERMEDIATE); chai.expect(parser.collect).equal(collect[i]); parser.reset(); @@ -367,7 +383,7 @@ describe('EscapeSequenceParser', function (): void { const collect = r(0x30, 0x7f); for (let i = 0; i < collect.length; ++i) { parser.currentState = ParserState.ESCAPE_INTERMEDIATE; - parser.parse(collect[i]); + parse(parser, collect[i]); chai.expect(parser.currentState).equal(ParserState.GROUND); // '\x5c' --> ESC + \ (7bit ST) parser does not expose this as it already got handled testTerminal.compare((collect[i] === '\x5c') ? [] : [['esc', '', collect[i]]]); @@ -382,7 +398,7 @@ describe('EscapeSequenceParser', function (): void { parser.osc = '#'; parser.params = [123]; parser.collect = '#'; - parser.parse('['); + parse(parser, '['); chai.expect(parser.currentState).equal(ParserState.CSI_ENTRY); chai.expect(parser.osc).equal(''); chai.expect(parser.params).eql([0]); @@ -394,7 +410,7 @@ describe('EscapeSequenceParser', function (): void { parser.osc = '#'; parser.params = [123]; parser.collect = '#'; - parser.parse('\x9b'); + parse(parser, '\x9b'); chai.expect(parser.currentState).equal(ParserState.CSI_ENTRY); chai.expect(parser.osc).equal(''); chai.expect(parser.params).eql([0]); @@ -405,12 +421,12 @@ describe('EscapeSequenceParser', function (): void { it('state CSI_ENTRY execute rules', function (): void { parser.reset(); testTerminal.clear(); - const exes = r(0x00, 0x18); - exes.concat(['\x19']); - exes.concat(r(0x1c, 0x20)); + let exes = r(0x00, 0x18); + exes = exes.concat(['\x19']); + exes = exes.concat(r(0x1c, 0x20)); for (let i = 0; i < exes.length; ++i) { parser.currentState = ParserState.CSI_ENTRY; - parser.parse(exes[i]); + parse(parser, exes[i]); chai.expect(parser.currentState).equal(ParserState.CSI_ENTRY); testTerminal.compare([['exe', exes[i]]]); parser.reset(); @@ -421,7 +437,7 @@ describe('EscapeSequenceParser', function (): void { parser.reset(); testTerminal.clear(); parser.currentState = ParserState.CSI_ENTRY; - parser.parse('\x7f'); + parse(parser, '\x7f'); chai.expect(parser.currentState).equal(ParserState.CSI_ENTRY); testTerminal.compare([]); parser.reset(); @@ -432,7 +448,7 @@ describe('EscapeSequenceParser', function (): void { const dispatches = r(0x40, 0x7f); for (let i = 0; i < dispatches.length; ++i) { parser.currentState = ParserState.CSI_ENTRY; - parser.parse(dispatches[i]); + parse(parser, dispatches[i]); chai.expect(parser.currentState).equal(ParserState.GROUND); testTerminal.compare([['csi', '', [0], dispatches[i]]]); parser.reset(); @@ -445,19 +461,19 @@ describe('EscapeSequenceParser', function (): void { const collect = ['\x3c', '\x3d', '\x3e', '\x3f']; for (let i = 0; i < params.length; ++i) { parser.currentState = ParserState.CSI_ENTRY; - parser.parse(params[i]); + parse(parser, params[i]); chai.expect(parser.currentState).equal(ParserState.CSI_PARAM); chai.expect(parser.params).eql([params[i].charCodeAt(0) - 48]); parser.reset(); } parser.currentState = ParserState.CSI_ENTRY; - parser.parse('\x3b'); + parse(parser, '\x3b'); chai.expect(parser.currentState).equal(ParserState.CSI_PARAM); chai.expect(parser.params).eql([0, 0]); parser.reset(); for (let i = 0; i < collect.length; ++i) { parser.currentState = ParserState.CSI_ENTRY; - parser.parse(collect[i]); + parse(parser, collect[i]); chai.expect(parser.currentState).equal(ParserState.CSI_PARAM); chai.expect(parser.collect).equal(collect[i]); parser.reset(); @@ -466,12 +482,12 @@ describe('EscapeSequenceParser', function (): void { it('state CSI_PARAM execute rules', function (): void { parser.reset(); testTerminal.clear(); - const exes = r(0x00, 0x18); - exes.concat(['\x19']); - exes.concat(r(0x1c, 0x20)); + let exes = r(0x00, 0x18); + exes = exes.concat(['\x19']); + exes = exes.concat(r(0x1c, 0x20)); for (let i = 0; i < exes.length; ++i) { parser.currentState = ParserState.CSI_PARAM; - parser.parse(exes[i]); + parse(parser, exes[i]); chai.expect(parser.currentState).equal(ParserState.CSI_PARAM); testTerminal.compare([['exe', exes[i]]]); parser.reset(); @@ -483,13 +499,13 @@ describe('EscapeSequenceParser', function (): void { const params = ['\x30', '\x31', '\x32', '\x33', '\x34', '\x35', '\x36', '\x37', '\x38', '\x39']; for (let i = 0; i < params.length; ++i) { parser.currentState = ParserState.CSI_PARAM; - parser.parse(params[i]); + parse(parser, params[i]); chai.expect(parser.currentState).equal(ParserState.CSI_PARAM); chai.expect(parser.params).eql([params[i].charCodeAt(0) - 48]); parser.reset(); } parser.currentState = ParserState.CSI_PARAM; - parser.parse('\x3b'); + parse(parser, '\x3b'); chai.expect(parser.currentState).equal(ParserState.CSI_PARAM); chai.expect(parser.params).eql([0, 0]); parser.reset(); @@ -498,7 +514,7 @@ describe('EscapeSequenceParser', function (): void { parser.reset(); testTerminal.clear(); parser.currentState = ParserState.CSI_PARAM; - parser.parse('\x7f'); + parse(parser, '\x7f'); chai.expect(parser.currentState).equal(ParserState.CSI_PARAM); testTerminal.compare([]); parser.reset(); @@ -510,7 +526,7 @@ describe('EscapeSequenceParser', function (): void { for (let i = 0; i < dispatches.length; ++i) { parser.currentState = ParserState.CSI_PARAM; parser.params = [0, 1]; - parser.parse(dispatches[i]); + parse(parser, dispatches[i]); chai.expect(parser.currentState).equal(ParserState.GROUND); testTerminal.compare([['csi', '', [0, 1], dispatches[i]]]); parser.reset(); @@ -522,7 +538,7 @@ describe('EscapeSequenceParser', function (): void { const collect = r(0x20, 0x30); for (let i = 0; i < collect.length; ++i) { parser.currentState = ParserState.CSI_ENTRY; - parser.parse(collect[i]); + parse(parser, collect[i]); chai.expect(parser.currentState).equal(ParserState.CSI_INTERMEDIATE); chai.expect(parser.collect).equal(collect[i]); parser.reset(); @@ -533,7 +549,7 @@ describe('EscapeSequenceParser', function (): void { const collect = r(0x20, 0x30); for (let i = 0; i < collect.length; ++i) { parser.currentState = ParserState.CSI_PARAM; - parser.parse(collect[i]); + parse(parser, collect[i]); chai.expect(parser.currentState).equal(ParserState.CSI_INTERMEDIATE); chai.expect(parser.collect).equal(collect[i]); parser.reset(); @@ -542,12 +558,12 @@ describe('EscapeSequenceParser', function (): void { it('state CSI_INTERMEDIATE execute rules', function (): void { parser.reset(); testTerminal.clear(); - const exes = r(0x00, 0x18); - exes.concat(['\x19']); - exes.concat(r(0x1c, 0x20)); + let exes = r(0x00, 0x18); + exes = exes.concat(['\x19']); + exes = exes.concat(r(0x1c, 0x20)); for (let i = 0; i < exes.length; ++i) { parser.currentState = ParserState.CSI_INTERMEDIATE; - parser.parse(exes[i]); + parse(parser, exes[i]); chai.expect(parser.currentState).equal(ParserState.CSI_INTERMEDIATE); testTerminal.compare([['exe', exes[i]]]); parser.reset(); @@ -559,7 +575,7 @@ describe('EscapeSequenceParser', function (): void { const collect = r(0x20, 0x30); for (let i = 0; i < collect.length; ++i) { parser.currentState = ParserState.CSI_INTERMEDIATE; - parser.parse(collect[i]); + parse(parser, collect[i]); chai.expect(parser.currentState).equal(ParserState.CSI_INTERMEDIATE); chai.expect(parser.collect).equal(collect[i]); parser.reset(); @@ -569,7 +585,7 @@ describe('EscapeSequenceParser', function (): void { parser.reset(); testTerminal.clear(); parser.currentState = ParserState.CSI_INTERMEDIATE; - parser.parse('\x7f'); + parse(parser, '\x7f'); chai.expect(parser.currentState).equal(ParserState.CSI_INTERMEDIATE); testTerminal.compare([]); parser.reset(); @@ -581,7 +597,7 @@ describe('EscapeSequenceParser', function (): void { for (let i = 0; i < dispatches.length; ++i) { parser.currentState = ParserState.CSI_INTERMEDIATE; parser.params = [0, 1]; - parser.parse(dispatches[i]); + parse(parser, dispatches[i]); chai.expect(parser.currentState).equal(ParserState.GROUND); testTerminal.compare([['csi', '', [0, 1], dispatches[i]]]); parser.reset(); @@ -591,7 +607,7 @@ describe('EscapeSequenceParser', function (): void { it('trans CSI_ENTRY --> CSI_IGNORE', function (): void { parser.reset(); parser.currentState = ParserState.CSI_ENTRY; - parser.parse('\x3a'); + parse(parser, '\x3a'); chai.expect(parser.currentState).equal(ParserState.CSI_IGNORE); parser.reset(); }); @@ -600,7 +616,7 @@ describe('EscapeSequenceParser', function (): void { const chars = ['\x3a', '\x3c', '\x3d', '\x3e', '\x3f']; for (let i = 0; i < chars.length; ++i) { parser.currentState = ParserState.CSI_PARAM; - parser.parse('\x3b' + chars[i]); + parse(parser, '\x3b' + chars[i]); chai.expect(parser.currentState).equal(ParserState.CSI_IGNORE); chai.expect(parser.params).eql([0, 0]); parser.reset(); @@ -611,7 +627,7 @@ describe('EscapeSequenceParser', function (): void { const chars = r(0x30, 0x40); for (let i = 0; i < chars.length; ++i) { parser.currentState = ParserState.CSI_INTERMEDIATE; - parser.parse(chars[i]); + parse(parser, chars[i]); chai.expect(parser.currentState).equal(ParserState.CSI_IGNORE); chai.expect(parser.params).eql([0]); parser.reset(); @@ -620,12 +636,12 @@ describe('EscapeSequenceParser', function (): void { it('state CSI_IGNORE execute rules', function (): void { parser.reset(); testTerminal.clear(); - const exes = r(0x00, 0x18); - exes.concat(['\x19']); - exes.concat(r(0x1c, 0x20)); + let exes = r(0x00, 0x18); + exes = exes.concat(['\x19']); + exes = exes.concat(r(0x1c, 0x20)); for (let i = 0; i < exes.length; ++i) { parser.currentState = ParserState.CSI_IGNORE; - parser.parse(exes[i]); + parse(parser, exes[i]); chai.expect(parser.currentState).equal(ParserState.CSI_IGNORE); testTerminal.compare([['exe', exes[i]]]); parser.reset(); @@ -635,11 +651,11 @@ describe('EscapeSequenceParser', function (): void { it('state CSI_IGNORE ignore', function (): void { parser.reset(); testTerminal.clear(); - const ignored = r(0x20, 0x40); - ignored.concat(['\x7f']); + let ignored = r(0x20, 0x40); + ignored = ignored.concat(['\x7f']); for (let i = 0; i < ignored.length; ++i) { parser.currentState = ParserState.CSI_IGNORE; - parser.parse(ignored[i]); + parse(parser, ignored[i]); chai.expect(parser.currentState).equal(ParserState.CSI_IGNORE); testTerminal.compare([]); parser.reset(); @@ -652,7 +668,7 @@ describe('EscapeSequenceParser', function (): void { for (let i = 0; i < dispatches.length; ++i) { parser.currentState = ParserState.CSI_IGNORE; parser.params = [0, 1]; - parser.parse(dispatches[i]); + parse(parser, dispatches[i]); chai.expect(parser.currentState).equal(ParserState.GROUND); testTerminal.compare([]); parser.reset(); @@ -664,7 +680,7 @@ describe('EscapeSequenceParser', function (): void { // C0 let initializers = ['\x58', '\x5e', '\x5f']; for (let i = 0; i < initializers.length; ++i) { - parser.parse('\x1b' + initializers[i]); + parse(parser, '\x1b' + initializers[i]); chai.expect(parser.currentState).equal(ParserState.SOS_PM_APC_STRING); parser.reset(); } @@ -673,7 +689,7 @@ describe('EscapeSequenceParser', function (): void { parser.currentState = state; initializers = ['\x98', '\x9e', '\x9f']; for (let i = 0; i < initializers.length; ++i) { - parser.parse(initializers[i]); + parse(parser, initializers[i]); chai.expect(parser.currentState).equal(ParserState.SOS_PM_APC_STRING); parser.reset(); } @@ -681,13 +697,13 @@ describe('EscapeSequenceParser', function (): void { }); it('state SOS_PM_APC_STRING ignore rules', function (): void { parser.reset(); - const ignored = r(0x00, 0x18); - ignored.concat(['\x19']); - ignored.concat(r(0x1c, 0x20)); - ignored.concat(r(0x20, 0x80)); + let ignored = r(0x00, 0x18); + ignored = ignored.concat(['\x19']); + ignored = ignored.concat(r(0x1c, 0x20)); + ignored = ignored.concat(r(0x20, 0x80)); for (let i = 0; i < ignored.length; ++i) { parser.currentState = ParserState.SOS_PM_APC_STRING; - parser.parse(ignored[i]); + parse(parser, ignored[i]); chai.expect(parser.currentState).equal(ParserState.SOS_PM_APC_STRING); parser.reset(); } @@ -695,13 +711,13 @@ describe('EscapeSequenceParser', function (): void { it('trans ANYWHERE/ESCAPE --> OSC_STRING', function (): void { parser.reset(); // C0 - parser.parse('\x1b]'); + parse(parser, '\x1b]'); chai.expect(parser.currentState).equal(ParserState.OSC_STRING); parser.reset(); // C1 for (state in states) { parser.currentState = state; - parser.parse('\x9d'); + parse(parser, '\x9d'); chai.expect(parser.currentState).equal(ParserState.OSC_STRING); parser.reset(); } @@ -714,7 +730,7 @@ describe('EscapeSequenceParser', function (): void { '\x12', '\x13', '\x14', '\x15', '\x16', '\x17', '\x19', '\x1c', '\x1d', '\x1e', '\x1f']; for (let i = 0; i < ignored.length; ++i) { parser.currentState = ParserState.OSC_STRING; - parser.parse(ignored[i]); + parse(parser, ignored[i]); chai.expect(parser.currentState).equal(ParserState.OSC_STRING); chai.expect(parser.osc).equal(''); parser.reset(); @@ -725,7 +741,7 @@ describe('EscapeSequenceParser', function (): void { const puts = r(0x20, 0x80); for (let i = 0; i < puts.length; ++i) { parser.currentState = ParserState.OSC_STRING; - parser.parse(puts[i]); + parse(parser, puts[i]); chai.expect(parser.currentState).equal(ParserState.OSC_STRING); chai.expect(parser.osc).equal(puts[i]); parser.reset(); @@ -734,13 +750,13 @@ describe('EscapeSequenceParser', function (): void { it('state DCS_ENTRY', function (): void { parser.reset(); // C0 - parser.parse('\x1bP'); + parse(parser, '\x1bP'); chai.expect(parser.currentState).equal(ParserState.DCS_ENTRY); parser.reset(); // C1 for (state in states) { parser.currentState = state; - parser.parse('\x90'); + parse(parser, '\x90'); chai.expect(parser.currentState).equal(ParserState.DCS_ENTRY); parser.reset(); } @@ -753,7 +769,7 @@ describe('EscapeSequenceParser', function (): void { '\x12', '\x13', '\x14', '\x15', '\x16', '\x17', '\x19', '\x1c', '\x1d', '\x1e', '\x1f', '\x7f']; for (let i = 0; i < ignored.length; ++i) { parser.currentState = ParserState.DCS_ENTRY; - parser.parse(ignored[i]); + parse(parser, ignored[i]); chai.expect(parser.currentState).equal(ParserState.DCS_ENTRY); parser.reset(); } @@ -764,19 +780,19 @@ describe('EscapeSequenceParser', function (): void { const collect = ['\x3c', '\x3d', '\x3e', '\x3f']; for (let i = 0; i < params.length; ++i) { parser.currentState = ParserState.DCS_ENTRY; - parser.parse(params[i]); + parse(parser, params[i]); chai.expect(parser.currentState).equal(ParserState.DCS_PARAM); chai.expect(parser.params).eql([params[i].charCodeAt(0) - 48]); parser.reset(); } parser.currentState = ParserState.DCS_ENTRY; - parser.parse('\x3b'); + parse(parser, '\x3b'); chai.expect(parser.currentState).equal(ParserState.DCS_PARAM); chai.expect(parser.params).eql([0, 0]); parser.reset(); for (let i = 0; i < collect.length; ++i) { parser.currentState = ParserState.DCS_ENTRY; - parser.parse(collect[i]); + parse(parser, collect[i]); chai.expect(parser.currentState).equal(ParserState.DCS_PARAM); chai.expect(parser.collect).equal(collect[i]); parser.reset(); @@ -790,7 +806,7 @@ describe('EscapeSequenceParser', function (): void { '\x12', '\x13', '\x14', '\x15', '\x16', '\x17', '\x19', '\x1c', '\x1d', '\x1e', '\x1f', '\x7f']; for (let i = 0; i < ignored.length; ++i) { parser.currentState = ParserState.DCS_PARAM; - parser.parse(ignored[i]); + parse(parser, ignored[i]); chai.expect(parser.currentState).equal(ParserState.DCS_PARAM); parser.reset(); } @@ -800,13 +816,13 @@ describe('EscapeSequenceParser', function (): void { const params = ['\x30', '\x31', '\x32', '\x33', '\x34', '\x35', '\x36', '\x37', '\x38', '\x39']; for (let i = 0; i < params.length; ++i) { parser.currentState = ParserState.DCS_PARAM; - parser.parse(params[i]); + parse(parser, params[i]); chai.expect(parser.currentState).equal(ParserState.DCS_PARAM); chai.expect(parser.params).eql([params[i].charCodeAt(0) - 48]); parser.reset(); } parser.currentState = ParserState.DCS_PARAM; - parser.parse('\x3b'); + parse(parser, '\x3b'); chai.expect(parser.currentState).equal(ParserState.DCS_PARAM); chai.expect(parser.params).eql([0, 0]); parser.reset(); @@ -814,7 +830,7 @@ describe('EscapeSequenceParser', function (): void { it('trans DCS_ENTRY --> DCS_IGNORE', function (): void { parser.reset(); parser.currentState = ParserState.DCS_ENTRY; - parser.parse('\x3a'); + parse(parser, '\x3a'); chai.expect(parser.currentState).equal(ParserState.DCS_IGNORE); parser.reset(); }); @@ -823,7 +839,7 @@ describe('EscapeSequenceParser', function (): void { const chars = ['\x3a', '\x3c', '\x3d', '\x3e', '\x3f']; for (let i = 0; i < chars.length; ++i) { parser.currentState = ParserState.DCS_PARAM; - parser.parse('\x3b' + chars[i]); + parse(parser, '\x3b' + chars[i]); chai.expect(parser.currentState).equal(ParserState.DCS_IGNORE); chai.expect(parser.params).eql([0, 0]); parser.reset(); @@ -834,21 +850,21 @@ describe('EscapeSequenceParser', function (): void { const chars = r(0x30, 0x40); for (let i = 0; i < chars.length; ++i) { parser.currentState = ParserState.DCS_INTERMEDIATE; - parser.parse(chars[i]); + parse(parser, chars[i]); chai.expect(parser.currentState).equal(ParserState.DCS_IGNORE); parser.reset(); } }); it('state DCS_IGNORE ignore rules', function (): void { parser.reset(); - const ignored = [ + let ignored = [ '\x00', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07', '\x08', '\x09', '\x0a', '\x0b', '\x0c', '\x0d', '\x0e', '\x0f', '\x10', '\x11', '\x12', '\x13', '\x14', '\x15', '\x16', '\x17', '\x19', '\x1c', '\x1d', '\x1e', '\x1f', '\x7f']; - ignored.concat(r(0x20, 0x80)); + ignored = ignored.concat(r(0x20, 0x80)); for (let i = 0; i < ignored.length; ++i) { parser.currentState = ParserState.DCS_IGNORE; - parser.parse(ignored[i]); + parse(parser, ignored[i]); chai.expect(parser.currentState).equal(ParserState.DCS_IGNORE); parser.reset(); } @@ -858,7 +874,7 @@ describe('EscapeSequenceParser', function (): void { const collect = r(0x20, 0x30); for (let i = 0; i < collect.length; ++i) { parser.currentState = ParserState.DCS_ENTRY; - parser.parse(collect[i]); + parse(parser, collect[i]); chai.expect(parser.currentState).equal(ParserState.DCS_INTERMEDIATE); chai.expect(parser.collect).equal(collect[i]); parser.reset(); @@ -869,7 +885,7 @@ describe('EscapeSequenceParser', function (): void { const collect = r(0x20, 0x30); for (let i = 0; i < collect.length; ++i) { parser.currentState = ParserState.DCS_PARAM; - parser.parse(collect[i]); + parse(parser, collect[i]); chai.expect(parser.currentState).equal(ParserState.DCS_INTERMEDIATE); chai.expect(parser.collect).equal(collect[i]); parser.reset(); @@ -883,7 +899,7 @@ describe('EscapeSequenceParser', function (): void { '\x12', '\x13', '\x14', '\x15', '\x16', '\x17', '\x19', '\x1c', '\x1d', '\x1e', '\x1f', '\x7f']; for (let i = 0; i < ignored.length; ++i) { parser.currentState = ParserState.DCS_INTERMEDIATE; - parser.parse(ignored[i]); + parse(parser, ignored[i]); chai.expect(parser.currentState).equal(ParserState.DCS_INTERMEDIATE); parser.reset(); } @@ -893,7 +909,7 @@ describe('EscapeSequenceParser', function (): void { const collect = r(0x20, 0x30); for (let i = 0; i < collect.length; ++i) { parser.currentState = ParserState.DCS_INTERMEDIATE; - parser.parse(collect[i]); + parse(parser, collect[i]); chai.expect(parser.currentState).equal(ParserState.DCS_INTERMEDIATE); chai.expect(parser.collect).equal(collect[i]); parser.reset(); @@ -904,7 +920,7 @@ describe('EscapeSequenceParser', function (): void { const chars = r(0x30, 0x40); for (let i = 0; i < chars.length; ++i) { parser.currentState = ParserState.DCS_INTERMEDIATE; - parser.parse('\x20' + chars[i]); + parse(parser, '\x20' + chars[i]); chai.expect(parser.currentState).equal(ParserState.DCS_IGNORE); chai.expect(parser.collect).equal('\x20'); parser.reset(); @@ -916,7 +932,7 @@ describe('EscapeSequenceParser', function (): void { const collect = r(0x40, 0x7f); for (let i = 0; i < collect.length; ++i) { parser.currentState = ParserState.DCS_ENTRY; - parser.parse(collect[i]); + parse(parser, collect[i]); chai.expect(parser.currentState).equal(ParserState.DCS_PASSTHROUGH); testTerminal.compare([['dcs hook', '', [0], collect[i]]]); parser.reset(); @@ -929,7 +945,7 @@ describe('EscapeSequenceParser', function (): void { const collect = r(0x40, 0x7f); for (let i = 0; i < collect.length; ++i) { parser.currentState = ParserState.DCS_PARAM; - parser.parse(collect[i]); + parse(parser, collect[i]); chai.expect(parser.currentState).equal(ParserState.DCS_PASSTHROUGH); testTerminal.compare([['dcs hook', '', [0], collect[i]]]); parser.reset(); @@ -942,7 +958,7 @@ describe('EscapeSequenceParser', function (): void { const collect = r(0x40, 0x7f); for (let i = 0; i < collect.length; ++i) { parser.currentState = ParserState.DCS_INTERMEDIATE; - parser.parse(collect[i]); + parse(parser, collect[i]); chai.expect(parser.currentState).equal(ParserState.DCS_PASSTHROUGH); testTerminal.compare([['dcs hook', '', [0], collect[i]]]); parser.reset(); @@ -952,14 +968,14 @@ describe('EscapeSequenceParser', function (): void { it('state DCS_PASSTHROUGH put action', function (): void { parser.reset(); testTerminal.clear(); - const puts = r(0x00, 0x18); - puts.concat(['\x19']); - puts.concat(r(0x1c, 0x20)); - puts.concat(r(0x20, 0x7f)); + let puts = r(0x00, 0x18); + puts = puts.concat(['\x19']); + puts = puts.concat(r(0x1c, 0x20)); + puts = puts.concat(r(0x20, 0x7f)); for (let i = 0; i < puts.length; ++i) { parser.currentState = ParserState.DCS_PASSTHROUGH; parser.mockActiveDcsHandler(); - parser.parse(puts[i]); + parse(parser, puts[i]); chai.expect(parser.currentState).equal(ParserState.DCS_PASSTHROUGH); testTerminal.compare([['dcs put', puts[i]]]); parser.reset(); @@ -970,7 +986,7 @@ describe('EscapeSequenceParser', function (): void { parser.reset(); testTerminal.clear(); parser.currentState = ParserState.DCS_PASSTHROUGH; - parser.parse('\x7f'); + parse(parser, '\x7f'); chai.expect(parser.currentState).equal(ParserState.DCS_PASSTHROUGH); testTerminal.compare([]); parser.reset(); @@ -989,7 +1005,7 @@ describe('EscapeSequenceParser', function (): void { parser.reset(); testTerminal.clear(); } - parser.parse(s); + parse(parser, s); testTerminal.compare(value); }; }); @@ -1067,7 +1083,7 @@ describe('EscapeSequenceParser', function (): void { parser.reset(); testTerminal.clear(); parser.currentState = ParserState.CSI_IGNORE; - parser.parse('€öäü'); + parse(parser, '€öäü'); chai.expect(parser.currentState).equal(ParserState.CSI_IGNORE); testTerminal.compare([]); parser.reset(); @@ -1077,7 +1093,7 @@ describe('EscapeSequenceParser', function (): void { parser.reset(); testTerminal.clear(); parser.currentState = ParserState.DCS_IGNORE; - parser.parse('€öäü'); + parse(parser, '€öäü'); chai.expect(parser.currentState).equal(ParserState.DCS_IGNORE); testTerminal.compare([]); parser.reset(); @@ -1087,7 +1103,7 @@ describe('EscapeSequenceParser', function (): void { parser.reset(); testTerminal.clear(); parser.currentState = ParserState.DCS_PASSTHROUGH; - parser.parse('\x901;2;3+$a€öäü'); + parse(parser, '\x901;2;3+$a€öäü'); chai.expect(parser.currentState).equal(ParserState.DCS_PASSTHROUGH); testTerminal.compare([['dcs hook', '+$', [1, 2, 3], 'a'], ['dcs put', '€öäü']]); parser.reset(); @@ -1097,7 +1113,7 @@ describe('EscapeSequenceParser', function (): void { parser.reset(); testTerminal.clear(); parser.currentState = ParserState.GROUND; - parser.parse('\x9c'); + parse(parser, '\x9c'); chai.expect(parser.currentState).equal(ParserState.GROUND); testTerminal.compare([]); parser.reset(); @@ -1127,15 +1143,17 @@ describe('EscapeSequenceParser', function (): void { clearAccu(); }); it('print handler', function (): void { - parser2.setPrintHandler(function (data: string, start: number, end: number): void { - print += data.substring(start, end); + parser2.setPrintHandler(function (data: Uint16Array, start: number, end: number): void { + for (let i = start; i < end; ++i) { + print += String.fromCharCode(data[i]); + } }); - parser2.parse(INPUT); + parse(parser2, INPUT); chai.expect(print).equal('hello world!$>'); parser2.clearPrintHandler(); parser2.clearPrintHandler(); // should not throw clearAccu(); - parser2.parse(INPUT); + parse(parser2, INPUT); chai.expect(print).equal(''); }); it('ESC handler', function (): void { @@ -1145,28 +1163,28 @@ describe('EscapeSequenceParser', function (): void { parser2.setEscHandler('E', function (): void { esc.push('E'); }); - parser2.parse(INPUT); + parse(parser2, INPUT); chai.expect(esc).eql(['%G', 'E']); parser2.clearEscHandler('%G'); parser2.clearEscHandler('%G'); // should not throw clearAccu(); - parser2.parse(INPUT); + parse(parser2, INPUT); chai.expect(esc).eql(['E']); parser2.clearEscHandler('E'); clearAccu(); - parser2.parse(INPUT); + parse(parser2, INPUT); chai.expect(esc).eql([]); }); it('CSI handler', function (): void { parser2.setCsiHandler('m', function (params: number[], collect: string): void { csi.push(['m', params, collect]); }); - parser2.parse(INPUT); + parse(parser2, INPUT); chai.expect(csi).eql([['m', [1, 31], ''], ['m', [0], '']]); parser2.clearCsiHandler('m'); parser2.clearCsiHandler('m'); // should not throw clearAccu(); - parser2.parse(INPUT); + parse(parser2, INPUT); chai.expect(csi).eql([]); }); it('EXECUTE handler', function (): void { @@ -1176,24 +1194,24 @@ describe('EscapeSequenceParser', function (): void { parser2.setExecuteHandler('\r', function (): void { exe.push('\r'); }); - parser2.parse(INPUT); + parse(parser2, INPUT); chai.expect(exe).eql(['\r', '\n']); parser2.clearExecuteHandler('\r'); parser2.clearExecuteHandler('\r'); // should not throw clearAccu(); - parser2.parse(INPUT); + parse(parser2, INPUT); chai.expect(exe).eql(['\n']); }); it('OSC handler', function (): void { parser2.setOscHandler(1, function (data: string): void { osc.push([1, data]); }); - parser2.parse(INPUT); + parse(parser2, INPUT); chai.expect(osc).eql([[1, 'foo=bar']]); parser2.clearOscHandler(1); parser2.clearOscHandler(1); // should not throw clearAccu(); - parser2.parse(INPUT); + parse(parser2, INPUT); chai.expect(osc).eql([]); }); it('DCS handler', function (): void { @@ -1201,15 +1219,19 @@ describe('EscapeSequenceParser', function (): void { hook: function (collect: string, params: number[], flag: number): void { dcs.push(['hook', collect, params, flag]); }, - put: function (data: string, start: number, end: number): void { - dcs.push(['put', data.substring(start, end)]); + put: function (data: Uint16Array, start: number, end: number): void { + let s = ''; + for (let i = start; i < end; ++i) { + s += String.fromCharCode(data[i]); + } + dcs.push(['put', s]); }, unhook: function (): void { dcs.push(['unhook']); } }); - parser2.parse('\x1bP1;2;3+pabc'); - parser2.parse(';de\x9c'); + parse(parser2, '\x1bP1;2;3+pabc'); + parse(parser2, ';de\x9c'); chai.expect(dcs).eql([ ['hook', '+', [1, 2, 3], 'p'.charCodeAt(0)], ['put', 'abc'], ['put', ';de'], @@ -1218,8 +1240,8 @@ describe('EscapeSequenceParser', function (): void { parser2.clearDcsHandler('+p'); parser2.clearDcsHandler('+p'); // should not throw clearAccu(); - parser2.parse('\x1bP1;2;3+pabc'); - parser2.parse(';de\x9c'); + parse(parser2, '\x1bP1;2;3+pabc'); + parse(parser2, ';de\x9c'); chai.expect(dcs).eql([]); }); it('ERROR handler', function (): void { @@ -1228,7 +1250,7 @@ describe('EscapeSequenceParser', function (): void { errorState = state; return state; }); - parser2.parse('\x1b[1;2;€;3m'); // faulty escape sequence + parse(parser2, '\x1b[1;2;€;3m'); // faulty escape sequence chai.expect(errorState).eql({ position: 6, code: '€'.charCodeAt(0), @@ -1243,7 +1265,7 @@ describe('EscapeSequenceParser', function (): void { parser2.clearErrorHandler(); parser2.clearErrorHandler(); // should not throw errorState = null; - parser2.parse('\x1b[1;2;a;3m'); + parse(parser2, '\x1b[1;2;a;3m'); chai.expect(errorState).eql(null); }); }); diff --git a/src/EscapeSequenceParser.ts b/src/EscapeSequenceParser.ts index b38c50f5..9d0da3ef 100644 --- a/src/EscapeSequenceParser.ts +++ b/src/EscapeSequenceParser.ts @@ -125,6 +125,7 @@ export const VT500_TRANSITION_TABLE = (function (): TransitionTable { table.addMany(PRINTABLES, ParserState.SOS_PM_APC_STRING, ParserAction.IGNORE, ParserState.SOS_PM_APC_STRING); table.addMany(EXECUTABLES, ParserState.SOS_PM_APC_STRING, ParserAction.IGNORE, ParserState.SOS_PM_APC_STRING); table.add(0x9c, ParserState.SOS_PM_APC_STRING, ParserAction.IGNORE, ParserState.GROUND); + table.add(0x7f, ParserState.SOS_PM_APC_STRING, ParserAction.IGNORE, ParserState.SOS_PM_APC_STRING); // csi entries table.add(0x5b, ParserState.ESCAPE, ParserAction.CLEAR, ParserState.CSI_ENTRY); table.addMany(r(0x40, 0x7f), ParserState.CSI_ENTRY, ParserAction.CSI_DISPATCH, ParserState.GROUND); @@ -192,7 +193,7 @@ export const VT500_TRANSITION_TABLE = (function (): TransitionTable { */ class DcsDummy implements IDcsHandler { hook(collect: string, params: number[], flag: number): void { } - put(data: string, start: number, end: number): void { } + put(data: Uint16Array, start: number, end: number): void { } unhook(): void { } } @@ -218,7 +219,7 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP protected _collect: string; // handler lookup containers - protected _printHandler: (data: string, start: number, end: number) => void; + protected _printHandler: (data: Uint16Array, start: number, end: number) => void; protected _executeHandlers: any; protected _csiHandlers: any; protected _escHandlers: any; @@ -228,7 +229,7 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP protected _errorHandler: (state: IParsingState) => IParsingState; // fallback handlers - protected _printHandlerFb: (data: string, start: number, end: number) => void; + protected _printHandlerFb: (data: Uint16Array, start: number, end: number) => void; protected _executeHandlerFb: (code: number) => void; protected _csiHandlerFb: (collect: string, params: number[], flag: number) => void; protected _escHandlerFb: (collect: string, flag: number) => void; @@ -284,7 +285,7 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP this._errorHandler = null; } - setPrintHandler(callback: (data: string, start: number, end: number) => void): void { + setPrintHandler(callback: (data: Uint16Array, start: number, end: number) => void): void { this._printHandler = callback; } clearPrintHandler(): void { @@ -356,7 +357,7 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP this._activeDcsHandler = null; } - parse(data: string): void { + parse(data: Uint16Array, length: number): void { let code = 0; let transition = 0; let error = false; @@ -371,15 +372,14 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP let callback: Function | null = null; // process input string - const l = data.length; - for (let i = 0; i < l; ++i) { - code = data.charCodeAt(i); + for (let i = 0; i < length; ++i) { + code = data[i]; // shortcut for most chars (print action) if (currentState === ParserState.GROUND && code > 0x1f && code < 0x80) { print = (~print) ? print : i; do i++; - while (i < l && data.charCodeAt(i) > 0x1f && data.charCodeAt(i) < 0x80); + while (i < length && data[i] > 0x1f && data[i] < 0x80); i--; continue; } @@ -517,7 +517,7 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP osc = ''; break; case ParserAction.OSC_PUT: - osc += data.charAt(i); + osc += String.fromCharCode(code); break; case ParserAction.OSC_END: if (osc && code !== 0x18 && code !== 0x1a) { @@ -549,9 +549,9 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP // push leftover pushable buffers to terminal if (currentState === ParserState.GROUND && ~print) { - this._printHandler(data, print, data.length); + this._printHandler(data, print, length); } else if (currentState === ParserState.DCS_PASSTHROUGH && ~dcs && dcsHandler) { - dcsHandler.put(data, dcs, data.length); + dcsHandler.put(data, dcs, length); } // save non pushable buffers diff --git a/src/InputHandler.test.ts b/src/InputHandler.test.ts index b2fea06a..c92d317d 100644 --- a/src/InputHandler.test.ts +++ b/src/InputHandler.test.ts @@ -503,7 +503,9 @@ describe('InputHandler', () => { it('should not cause an infinite loop (regression test)', () => { const term = new Terminal(); const inputHandler = new InputHandler(term); - inputHandler.print(String.fromCharCode(0x200B), 0, 1); + const container = new Uint16Array(10); + container[0] = 0x200B; + inputHandler.print(container, 0, 1); }); }); diff --git a/src/InputHandler.ts b/src/InputHandler.ts index 7604b01f..9f548606 100644 --- a/src/InputHandler.ts +++ b/src/InputHandler.ts @@ -29,18 +29,26 @@ const GLEVEL: {[key: string]: number} = {'(': 0, ')': 1, '*': 2, '+': 3, '-': 1, * Request Terminfo String * not supported */ -class RequestTerminfo implements IDcsHandler { - private _data: string; + class RequestTerminfo implements IDcsHandler { + private _data: Uint16Array = new Uint16Array(0); constructor(private _terminal: any) { } hook(collect: string, params: number[], flag: number): void { - this._data = ''; } - put(data: string, start: number, end: number): void { - this._data += data.substring(start, end); + put(data: Uint16Array, start: number, end: number): void { + const tmp = new Uint16Array(this._data.length + end - start); + tmp.set(this._data); + tmp.set(data.subarray(start, end), this._data.length); + this._data = data; } unhook(): void { + let data = ''; + for (let i = 0; i < this._data.length; ++i) { + data += String.fromCharCode(this._data[i]); + } + this._data = new Uint16Array(0); // dont hold memory longer than needed // invalid: DCS 0 + r Pt ST - this._terminal.handler(`${C0.ESC}P0+r${this._data}${C0.ESC}\\`); + this._terminal.handler(`${C0.ESC}P0+r${data}${C0.ESC}\\`); + this._data = new Uint16Array(0); } } @@ -51,21 +59,27 @@ class RequestTerminfo implements IDcsHandler { * Response: DECRPSS (https://vt100.net/docs/vt510-rm/DECRPSS.html) */ class DECRQSS implements IDcsHandler { - private _data: string; + private _data: Uint16Array = new Uint16Array(0); constructor(private _terminal: any) { } hook(collect: string, params: number[], flag: number): void { - // reset data - this._data = ''; } - put(data: string, start: number, end: number): void { - this._data += data.substring(start, end); + put(data: Uint16Array, start: number, end: number): void { + const tmp = new Uint16Array(this._data.length + end - start); + tmp.set(this._data); + tmp.set(data.subarray(start, end), this._data.length); + this._data = data; } unhook(): void { - switch (this._data) { + let data = ''; + for (let i = 0; i < this._data.length; ++i) { + data += String.fromCharCode(this._data[i]); + } + this._data = new Uint16Array(0); // dont hold memory longer than needed + switch (data) { // valid: DCS 1 $ r Pt ST (xterm) case '"q': // DECSCA return this._terminal.handler(`${C0.ESC}P1$r0"q${C0.ESC}\\`); @@ -85,8 +99,8 @@ class DECRQSS implements IDcsHandler { return this._terminal.handler(`${C0.ESC}P1$r${style} q${C0.ESC}\\`); default: // invalid: DCS 0 $ r Pt ST (xterm) - this._terminal.error('Unknown DCS $q %s', this._data); - this._terminal.handler(`${C0.ESC}P0$r${this._data}${C0.ESC}\\`); + this._terminal.error('Unknown DCS $q %s', data); + this._terminal.handler(`${C0.ESC}P0$r${data}${C0.ESC}\\`); } } } @@ -97,11 +111,11 @@ class DECRQSS implements IDcsHandler { * not supported */ - /** - * DCS + p Pt ST (xterm) - * Set Terminfo Data - * not supported - */ +/** + * DCS + p Pt ST (xterm) + * Set Terminfo Data + * not supported + */ @@ -114,6 +128,7 @@ class DECRQSS implements IDcsHandler { */ export class InputHandler extends Disposable implements IInputHandler { private _surrogateFirst: string; + private _parseBuffer: Uint16Array = new Uint16Array(4096); constructor( protected _terminal: IInputHandlingTerminal, @@ -316,7 +331,13 @@ export class InputHandler extends Disposable implements IInputHandler { this._surrogateFirst = ''; } - this._parser.parse(data); + if (this._parseBuffer.length < data.length) { + this._parseBuffer = new Uint16Array(data.length); + } + for (let i = 0; i < data.length; ++i) { + this._parseBuffer[i] = data.charCodeAt(i); + } + this._parser.parse(this._parseBuffer, data.length); buffer = this._terminal.buffer; if (buffer.x !== cursorStartX || buffer.y !== cursorStartY) { @@ -324,9 +345,9 @@ export class InputHandler extends Disposable implements IInputHandler { } } - public print(data: string, start: number, end: number): void { - let char: string; + public print(data: Uint16Array, start: number, end: number): void { let code: number; + let char: string; let chWidth: number; const buffer: IBuffer = this._terminal.buffer; const charset: ICharset = this._terminal.charset; @@ -338,30 +359,30 @@ export class InputHandler extends Disposable implements IInputHandler { let bufferRow = buffer.lines.get(buffer.y + buffer.ybase); this._terminal.updateRange(buffer.y); - for (let stringPosition = start; stringPosition < end; ++stringPosition) { - char = data.charAt(stringPosition); - code = data.charCodeAt(stringPosition); + for (let pos = start; pos < end; ++pos) { + code = data[pos]; + char = String.fromCharCode(code); // surrogate pair handling if (0xD800 <= code && code <= 0xDBFF) { - if (++stringPosition >= end) { + if (++pos >= end) { // end of input: // handle pairs as true UTF-16 and wait for the second part // since we expect the input comming from a stream there is // a small chance that the surrogate pair got split // therefore we dont process the first char here, instead // it gets added as first char to the next processed chunk - this._surrogateFirst = char; + this._surrogateFirst = String.fromCharCode(code); continue; } - const second = data.charCodeAt(stringPosition); + const second = data[pos]; // if the second part is in surrogate pair range create the high codepoint // otherwise fall back to UCS-2 behavior (handle codepoints independently) if (0xDC00 <= second && second <= 0xDFFF) { code = (code - 0xD800) * 0x400 + second - 0xDC00 + 0x10000; - char += data.charAt(stringPosition); + char += String.fromCharCode(second); } else { - stringPosition--; + pos--; } } @@ -370,9 +391,14 @@ export class InputHandler extends Disposable implements IInputHandler { chWidth = wcwidth(code); // get charset replacement character - if (charset) { - char = charset[char] || char; - code = char.charCodeAt(0); + // charset are only defined for ASCII, therefore we only + // search for an replacement char if code < 127 + if (code < 127 && charset) { + const ch = charset[char]; + if (ch) { + code = ch.charCodeAt(0); + char = ch; + } } if (screenReaderMode) { diff --git a/src/Types.ts b/src/Types.ts index 0f60e6f7..b1c125dc 100644 --- a/src/Types.ts +++ b/src/Types.ts @@ -111,7 +111,7 @@ export interface ICompositionHelper { */ export interface IInputHandler { parse(data: string): void; - print(data: string, start: number, end: number): void; + print(data: Uint16Array, start: number, end: number): void; /** C0 BEL */ bell(): void; /** C0 LF */ lineFeed(): void; @@ -463,7 +463,7 @@ export interface IParsingState { */ export interface IDcsHandler { hook(collect: string, params: number[], flag: number): void; - put(data: string, start: number, end: number): void; + put(data: Uint16Array, start: number, end: number): void; unhook(): void; } @@ -480,9 +480,9 @@ export interface IEscapeSequenceParser extends IDisposable { * Parse string `data`. * @param data The data to parse. */ - parse(data: string): void; + parse(data: Uint16Array, length: number): void; - setPrintHandler(callback: (data: string, start: number, end: number) => void): void; + setPrintHandler(callback: (data: Uint16Array, start: number, end: number) => void): void; clearPrintHandler(): void; setExecuteHandler(flag: string, callback: () => void): void; From 591dfd506d0e18120bdaa2b895adb507ce11ee63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Breitbart?= Date: Thu, 29 Nov 2018 16:33:56 +0100 Subject: [PATCH 02/41] fix DCS handler --- src/InputHandler.ts | 27 +++++++----------------- src/common/TypedArrayUtils.test.ts | 32 +++++++++++++++++++++------- src/common/TypedArrayUtils.ts | 34 +++++++++++++++++++++++++----- 3 files changed, 61 insertions(+), 32 deletions(-) diff --git a/src/InputHandler.ts b/src/InputHandler.ts index 9f548606..3190dc60 100644 --- a/src/InputHandler.ts +++ b/src/InputHandler.ts @@ -13,6 +13,7 @@ import { wcwidth } from './CharWidth'; import { EscapeSequenceParser } from './EscapeSequenceParser'; import { ICharset } from './core/Types'; import { Disposable } from './common/Lifecycle'; +import { concat, utf16ToString } from './common/TypedArrayUtils'; /** * Map collect to glevel. Used in `selectCharset`. @@ -33,22 +34,16 @@ const GLEVEL: {[key: string]: number} = {'(': 0, ')': 1, '*': 2, '+': 3, '-': 1, private _data: Uint16Array = new Uint16Array(0); constructor(private _terminal: any) { } hook(collect: string, params: number[], flag: number): void { + this._data = new Uint16Array(0); } put(data: Uint16Array, start: number, end: number): void { - const tmp = new Uint16Array(this._data.length + end - start); - tmp.set(this._data); - tmp.set(data.subarray(start, end), this._data.length); - this._data = data; + this._data = concat(this._data, data.subarray(start, end)); } unhook(): void { - let data = ''; - for (let i = 0; i < this._data.length; ++i) { - data += String.fromCharCode(this._data[i]); - } - this._data = new Uint16Array(0); // dont hold memory longer than needed + const data = utf16ToString(this._data); + this._data = new Uint16Array(0); // invalid: DCS 0 + r Pt ST this._terminal.handler(`${C0.ESC}P0+r${data}${C0.ESC}\\`); - this._data = new Uint16Array(0); } } @@ -67,18 +62,12 @@ class DECRQSS implements IDcsHandler { } put(data: Uint16Array, start: number, end: number): void { - const tmp = new Uint16Array(this._data.length + end - start); - tmp.set(this._data); - tmp.set(data.subarray(start, end), this._data.length); - this._data = data; + this._data = concat(this._data, data.subarray(start, end)); } unhook(): void { - let data = ''; - for (let i = 0; i < this._data.length; ++i) { - data += String.fromCharCode(this._data[i]); - } - this._data = new Uint16Array(0); // dont hold memory longer than needed + const data = utf16ToString(this._data); + this._data = new Uint16Array(0); switch (data) { // valid: DCS 1 $ r Pt ST (xterm) case '"q': // DECSCA diff --git a/src/common/TypedArrayUtils.test.ts b/src/common/TypedArrayUtils.test.ts index 69a62abc..b25b9b9a 100644 --- a/src/common/TypedArrayUtils.test.ts +++ b/src/common/TypedArrayUtils.test.ts @@ -3,21 +3,20 @@ * @license MIT */ import { assert } from 'chai'; -import { fillFallback } from './TypedArrayUtils'; +import { fillFallback, concat, utf16ToString } from './TypedArrayUtils'; type TypedArray = Uint8Array | Uint16Array | Uint32Array | Uint8ClampedArray | Int8Array | Int16Array | Int32Array | Float32Array | Float64Array; -describe('polyfill conformance tests', function(): void { - - function deepEquals(a: TypedArray, b: TypedArray): void { - assert.equal(a.length, b.length); - for (let i = 0; i < a.length; ++i) { - assert.equal(a[i], b[i]); - } +function deepEquals(a: TypedArray, b: TypedArray): void { + assert.equal(a.length, b.length); + for (let i = 0; i < a.length; ++i) { + assert.equal(a[i], b[i]); } +} +describe('polyfill conformance tests', function(): void { describe('TypedArray.fill', function(): void { it('should work with all typed array types', function(): void { const u81 = new Uint8Array(5); @@ -87,3 +86,20 @@ describe('polyfill conformance tests', function(): void { }); }); }); + +describe('typed array convenience functions', () => { + it('concat', () => { + const a = new Uint8Array([1, 2, 3, 4, 5]); + const b = new Uint8Array([6, 7, 8, 9, 0]); + const merged = concat(a, b); + deepEquals(merged, new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 0])); + }); + it('utf16ToString', () => { + const s = 'abcdefg'; + const data = new Uint16Array(s.length); + for (let i = 0; i < s.length; ++i) { + data[i] = s.charCodeAt(i); + } + assert.equal(utf16ToString(data), s); + }); +}); diff --git a/src/common/TypedArrayUtils.ts b/src/common/TypedArrayUtils.ts index 6e1a3630..8b540548 100644 --- a/src/common/TypedArrayUtils.ts +++ b/src/common/TypedArrayUtils.ts @@ -3,15 +3,14 @@ * @license MIT */ -/** - * polyfill for TypedArray.fill - * This is needed to support .fill in all safari versions and IE 11. - */ - type TypedArray = Uint8Array | Uint16Array | Uint32Array | Uint8ClampedArray | Int8Array | Int16Array | Int32Array | Float32Array | Float64Array; +/** + * polyfill for TypedArray.fill + * This is needed to support .fill in all safari versions and IE 11. + */ export function fill(array: T, value: number, start?: number, end?: number): T { // all modern engines that support .fill if (array.fill) { @@ -39,3 +38,28 @@ export function fillFallback(array: T, value: number, star } return array; } + +/** + * Concat two typed arrays `a` and `b`. + * Returns a new typed array. + */ +export function concat(a: T, b: T): T { + const result = new (a.constructor as any)(a.length + b.length); + result.set(a); + result.set(b, a.length); + return result; +} + +/** + * Convert UTF16 char codes into JS string. + * Note the typed array is not limited to Uint16Array, make sure to align + * the values to 0-65535 integer for other typed array types, otherwise + * the conversion will fail. + */ +export function utf16ToString(data: T): string { + let s = ''; + for (let i = 0; i < data.length; ++i) { + s += String.fromCharCode(data[i]); + } + return s; +} From a6ef64dcb6c7a28e360f36104d40e54a5aa2f36e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Breitbart?= Date: Thu, 29 Nov 2018 16:39:17 +0100 Subject: [PATCH 03/41] always clear DCS buffer on HOOK --- src/InputHandler.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/InputHandler.ts b/src/InputHandler.ts index 3190dc60..7438cf1e 100644 --- a/src/InputHandler.ts +++ b/src/InputHandler.ts @@ -59,6 +59,7 @@ class DECRQSS implements IDcsHandler { constructor(private _terminal: any) { } hook(collect: string, params: number[], flag: number): void { + this._data = new Uint16Array(0); } put(data: Uint16Array, start: number, end: number): void { From 2483a98f63eac5ced2bd42d4e93aaf01b669da1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Breitbart?= Date: Thu, 29 Nov 2018 21:13:18 +0100 Subject: [PATCH 04/41] use faster string conversion --- src/common/TypedArrayUtils.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/common/TypedArrayUtils.ts b/src/common/TypedArrayUtils.ts index 8b540548..5d3bcb8a 100644 --- a/src/common/TypedArrayUtils.ts +++ b/src/common/TypedArrayUtils.ts @@ -57,9 +57,5 @@ export function concat(a: T, b: T): T { * the conversion will fail. */ export function utf16ToString(data: T): string { - let s = ''; - for (let i = 0; i < data.length; ++i) { - s += String.fromCharCode(data[i]); - } - return s; + return String.fromCharCode.apply(null, data); } From 338558d86c6a5a018347664bf7bdead3b89c3e63 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Mon, 31 Dec 2018 20:00:16 -0800 Subject: [PATCH 05/41] Make Terminal API properties readonly Fixes #1867 --- src/Buffer.test.ts | 4 ++-- src/BufferSet.test.ts | 4 ++-- src/Linkifier.test.ts | 14 +++++++------- src/SelectionManager.test.ts | 4 ++-- src/SelectionModel.test.ts | 4 ++-- src/Types.ts | 2 +- typings/xterm.d.ts | 18 +++++++++++------- 7 files changed, 27 insertions(+), 23 deletions(-) diff --git a/src/Buffer.test.ts b/src/Buffer.test.ts index 0546dfe8..9d1d50c8 100644 --- a/src/Buffer.test.ts +++ b/src/Buffer.test.ts @@ -19,8 +19,8 @@ describe('Buffer', () => { beforeEach(() => { terminal = new MockTerminal(); - terminal.cols = INIT_COLS; - terminal.rows = INIT_ROWS; + (terminal as any).cols = INIT_COLS; + (terminal as any).rows = INIT_ROWS; terminal.options.scrollback = 1000; buffer = new Buffer(terminal, true); }); diff --git a/src/BufferSet.test.ts b/src/BufferSet.test.ts index 26f9cd42..576a8ca4 100644 --- a/src/BufferSet.test.ts +++ b/src/BufferSet.test.ts @@ -15,8 +15,8 @@ describe('BufferSet', () => { beforeEach(() => { terminal = new MockTerminal(); - terminal.cols = 80; - terminal.rows = 24; + (terminal as any).cols = 80; + (terminal as any).rows = 24; terminal.options.scrollback = 1000; bufferSet = new BufferSet(terminal); }); diff --git a/src/Linkifier.test.ts b/src/Linkifier.test.ts index 0ba1294a..07dbf1b3 100644 --- a/src/Linkifier.test.ts +++ b/src/Linkifier.test.ts @@ -41,8 +41,8 @@ describe('Linkifier', () => { beforeEach(() => { terminal = new MockTerminal(); - terminal.cols = 100; - terminal.rows = 10; + (terminal as any).cols = 100; + (terminal as any).rows = 10; terminal.buffer = new MockBuffer(); (terminal.buffer).setLines(new CircularList(20)); terminal.buffer.ydisp = 0; @@ -65,7 +65,7 @@ describe('Linkifier', () => { function assertLinkifiesRow(rowText: string, linkMatcherRegex: RegExp, links: {x: number, length: number}[], done: MochaDone): void { addRow(rowText); linkifier.registerLinkMatcher(linkMatcherRegex, () => {}); - terminal.rows = terminal.buffer.lines.length - 1; + (terminal as any).rows = terminal.buffer.lines.length - 1; linkifier.linkifyRows(); // Allow linkify to happen setTimeout(() => { @@ -142,19 +142,19 @@ describe('Linkifier', () => { }); describe('multi-line links', () => { it('should match links that start on line 1/2 of a wrapped line and end on the last character of line 1/2', done => { - terminal.cols = 4; + (terminal as any).cols = 4; assertLinkifiesMultiLineLink('12345', /1234/, [{x1: 0, x2: 4, y1: 0, y2: 0}], done); }); it('should match links that start on line 1/2 of a wrapped line and wrap to line 2/2', done => { - terminal.cols = 4; + (terminal as any).cols = 4; assertLinkifiesMultiLineLink('12345', /12345/, [{x1: 0, x2: 1, y1: 0, y2: 1}], done); }); it('should match links that start and end on line 2/2 of a wrapped line', done => { - terminal.cols = 4; + (terminal as any).cols = 4; assertLinkifiesMultiLineLink('12345678', /5678/, [{x1: 0, x2: 4, y1: 1, y2: 1}], done); }); it('should match links that start on line 2/3 of a wrapped line and wrap to line 3/3', done => { - terminal.cols = 4; + (terminal as any).cols = 4; assertLinkifiesMultiLineLink('123456789', /56789/, [{x1: 0, x2: 1, y1: 1, y2: 2}], done); }); }); diff --git a/src/SelectionManager.test.ts b/src/SelectionManager.test.ts index 2f74ccda..21bc6754 100644 --- a/src/SelectionManager.test.ts +++ b/src/SelectionManager.test.ts @@ -45,8 +45,8 @@ describe('SelectionManager', () => { beforeEach(() => { terminal = new TestMockTerminal(); - terminal.cols = 80; - terminal.rows = 2; + (terminal as any).cols = 80; + (terminal as any).rows = 2; terminal.options.scrollback = 100; terminal.buffers = new BufferSet(terminal); terminal.buffer = terminal.buffers.active; diff --git a/src/SelectionModel.test.ts b/src/SelectionModel.test.ts index 8d4b30bb..d49f41d0 100644 --- a/src/SelectionModel.test.ts +++ b/src/SelectionModel.test.ts @@ -23,8 +23,8 @@ describe('SelectionManager', () => { beforeEach(() => { terminal = new MockTerminal(); - terminal.cols = 80; - terminal.rows = 2; + (terminal as any).cols = 80; + (terminal as any).rows = 2; terminal.options.scrollback = 10; terminal.buffers = new BufferSet(terminal); terminal.buffer = terminal.buffers.active; diff --git a/src/Types.ts b/src/Types.ts index 8ebb28d3..e6f36ac4 100644 --- a/src/Types.ts +++ b/src/Types.ts @@ -237,7 +237,7 @@ export interface IBufferAccessor { } export interface IElementAccessor { - element: HTMLElement; + readonly element: HTMLElement; } export interface ILinkifierAccessor { diff --git a/typings/xterm.d.ts b/typings/xterm.d.ts index 7528bb55..b0924296 100644 --- a/typings/xterm.d.ts +++ b/typings/xterm.d.ts @@ -325,28 +325,32 @@ declare module 'xterm' { /** * The element containing the terminal. */ - element: HTMLElement; + readonly element: HTMLElement; /** * The textarea that accepts input for the terminal. */ - textarea: HTMLTextAreaElement; + readonly textarea: HTMLTextAreaElement; /** - * The number of rows in the terminal's viewport. + * The number of rows in the terminal's viewport. Use + * `ITerminalOptions.rows` to set this in the constructor and + * `Terminal.resize` for when the terminal exists. */ - rows: number; + readonly rows: number; /** - * The number of columns in the terminal's viewport. + * The number of columns in the terminal's viewport. Use + * `ITerminalOptions.cols` to set this in the constructor and + * `Terminal.resize` for when the terminal exists. */ - cols: number; + readonly cols: number; /** * (EXPERIMENTAL) Get all markers registered against the buffer. If the alt * buffer is active this will always return []. */ - markers: IMarker[]; + readonly markers: IMarker[]; /** * Natural language strings that can be localized. From 5fd764a89d5bfeed0661608f38d3a3f7a301963f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Breitbart?= Date: Wed, 2 Jan 2019 18:10:51 +0100 Subject: [PATCH 06/41] dont pullin everything from .vscode folder --- .gitignore | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.gitignore b/.gitignore index b50ab2d9..2ee021f8 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,7 @@ coverage/ # Keep bundled code out of Git dist/ demo/dist/ + +# dont pullin other files from .vscode than launch.json +.vscode/ +!.vscode/launch.json From dfc853aef4506150689f98cf38b2992d99d8a9cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Breitbart?= Date: Wed, 2 Jan 2019 22:50:15 +0100 Subject: [PATCH 07/41] add TextDecoder for string to UTF32 --- src/EscapeSequenceParser.test.ts | 30 +++++------ src/core/input/TextDecoder.test.ts | 67 +++++++++++++++++++++++ src/core/input/TextDecoder.ts | 87 ++++++++++++++++++++++++++++++ 3 files changed, 169 insertions(+), 15 deletions(-) create mode 100644 src/core/input/TextDecoder.test.ts create mode 100644 src/core/input/TextDecoder.ts diff --git a/src/EscapeSequenceParser.test.ts b/src/EscapeSequenceParser.test.ts index 011ff2ae..64e61ba3 100644 --- a/src/EscapeSequenceParser.test.ts +++ b/src/EscapeSequenceParser.test.ts @@ -1192,7 +1192,7 @@ describe('EscapeSequenceParser', function (): void { const csiCustom: [string, number[], string][] = []; parser2.setCsiHandler('m', (params, collect) => csi.push(['m', params, collect])); parser2.addCsiHandler('m', (params, collect) => { csiCustom.push(['m', params, collect]); return true; }); - parser2.parse(INPUT); + parse(parser2, INPUT); chai.expect(csi).eql([], 'Should not fallback to original handler'); chai.expect(csiCustom).eql([['m', [1, 31], ''], ['m', [0], '']]); }); @@ -1200,7 +1200,7 @@ describe('EscapeSequenceParser', function (): void { const csiCustom: [string, number[], string][] = []; parser2.setCsiHandler('m', (params, collect) => csi.push(['m', params, collect])); parser2.addCsiHandler('m', (params, collect) => { csiCustom.push(['m', params, collect]); return false; }); - parser2.parse(INPUT); + parse(parser2, INPUT); chai.expect(csi).eql([['m', [1, 31], ''], ['m', [0], '']], 'Should fallback to original handler'); chai.expect(csiCustom).eql([['m', [1, 31], ''], ['m', [0], '']]); }); @@ -1210,7 +1210,7 @@ describe('EscapeSequenceParser', function (): void { parser2.setCsiHandler('m', (params, collect) => csi.push(['m', params, collect])); parser2.addCsiHandler('m', (params, collect) => { csiCustom.push(['m', params, collect]); return true; }); parser2.addCsiHandler('m', (params, collect) => { csiCustom2.push(['m', params, collect]); return false; }); - parser2.parse(INPUT); + parse(parser2, INPUT); chai.expect(csi).eql([], 'Should not fallback to original handler'); chai.expect(csiCustom).eql([['m', [1, 31], ''], ['m', [0], '']]); chai.expect(csiCustom2).eql([['m', [1, 31], ''], ['m', [0], '']]); @@ -1221,7 +1221,7 @@ describe('EscapeSequenceParser', function (): void { parser2.setCsiHandler('m', (params, collect) => csi.push(['m', params, collect])); parser2.addCsiHandler('m', (params, collect) => { csiCustom.push(['m', params, collect]); return true; }); parser2.addCsiHandler('m', (params, collect) => { csiCustom2.push(['m', params, collect]); return true; }); - parser2.parse(INPUT); + parse(parser2, INPUT); chai.expect(csi).eql([], 'Should not fallback to original handler'); chai.expect(csiCustom).eql([], 'Should not fallback once'); chai.expect(csiCustom2).eql([['m', [1, 31], ''], ['m', [0], '']]); @@ -1231,15 +1231,15 @@ describe('EscapeSequenceParser', function (): void { parser2.setCsiHandler('m', () => order.push(1)); parser2.addCsiHandler('m', () => { order.push(2); return false; }); parser2.addCsiHandler('m', () => { order.push(3); return false; }); - parser2.parse('\x1b[0m'); - chai.expect(order).eql([3, 2, 1]); + parse(parser2, INPUT); + chai.expect(order).eql([3, 2, 1, 3, 2, 1]); }); it('Dispose should work', () => { const csiCustom: [string, number[], string][] = []; parser2.setCsiHandler('m', (params, collect) => csi.push(['m', params, collect])); const customHandler = parser2.addCsiHandler('m', (params, collect) => { csiCustom.push(['m', params, collect]); return true; }); customHandler.dispose(); - parser2.parse(INPUT); + parse(parser2, INPUT); chai.expect(csi).eql([['m', [1, 31], ''], ['m', [0], '']]); chai.expect(csiCustom).eql([], 'Should not use custom handler as it was disposed'); }); @@ -1249,7 +1249,7 @@ describe('EscapeSequenceParser', function (): void { const customHandler = parser2.addCsiHandler('m', (params, collect) => { csiCustom.push(['m', params, collect]); return true; }); customHandler.dispose(); customHandler.dispose(); - parser2.parse(INPUT); + parse(parser2, INPUT); chai.expect(csi).eql([['m', [1, 31], ''], ['m', [0], '']]); chai.expect(csiCustom).eql([], 'Should not use custom handler as it was disposed'); }); @@ -1286,7 +1286,7 @@ describe('EscapeSequenceParser', function (): void { const oscCustom: [number, string][] = []; parser2.setOscHandler(1, data => osc.push([1, data])); parser2.addOscHandler(1, data => { oscCustom.push([1, data]); return true; }); - parser2.parse(INPUT); + parse(parser2, INPUT); chai.expect(osc).eql([], 'Should not fallback to original handler'); chai.expect(oscCustom).eql([[1, 'foo=bar']]); }); @@ -1294,7 +1294,7 @@ describe('EscapeSequenceParser', function (): void { const oscCustom: [number, string][] = []; parser2.setOscHandler(1, data => osc.push([1, data])); parser2.addOscHandler(1, data => { oscCustom.push([1, data]); return false; }); - parser2.parse(INPUT); + parse(parser2, INPUT); chai.expect(osc).eql([[1, 'foo=bar']], 'Should fallback to original handler'); chai.expect(oscCustom).eql([[1, 'foo=bar']]); }); @@ -1304,7 +1304,7 @@ describe('EscapeSequenceParser', function (): void { parser2.setOscHandler(1, data => osc.push([1, data])); parser2.addOscHandler(1, data => { oscCustom.push([1, data]); return true; }); parser2.addOscHandler(1, data => { oscCustom2.push([1, data]); return false; }); - parser2.parse(INPUT); + parse(parser2, INPUT); chai.expect(osc).eql([], 'Should not fallback to original handler'); chai.expect(oscCustom).eql([[1, 'foo=bar']]); chai.expect(oscCustom2).eql([[1, 'foo=bar']]); @@ -1315,7 +1315,7 @@ describe('EscapeSequenceParser', function (): void { parser2.setOscHandler(1, data => osc.push([1, data])); parser2.addOscHandler(1, data => { oscCustom.push([1, data]); return true; }); parser2.addOscHandler(1, data => { oscCustom2.push([1, data]); return true; }); - parser2.parse(INPUT); + parse(parser2, INPUT); chai.expect(osc).eql([], 'Should not fallback to original handler'); chai.expect(oscCustom).eql([], 'Should not fallback once'); chai.expect(oscCustom2).eql([[1, 'foo=bar']]); @@ -1325,7 +1325,7 @@ describe('EscapeSequenceParser', function (): void { parser2.setOscHandler(1, () => order.push(1)); parser2.addOscHandler(1, () => { order.push(2); return false; }); parser2.addOscHandler(1, () => { order.push(3); return false; }); - parser2.parse('\x1b]1;foo=bar\x1b\\'); + parse(parser2, '\x1b]1;foo=bar\x1b\\'); chai.expect(order).eql([3, 2, 1]); }); it('Dispose should work', () => { @@ -1333,7 +1333,7 @@ describe('EscapeSequenceParser', function (): void { parser2.setOscHandler(1, data => osc.push([1, data])); const customHandler = parser2.addOscHandler(1, data => { oscCustom.push([1, data]); return true; }); customHandler.dispose(); - parser2.parse(INPUT); + parse(parser2, INPUT); chai.expect(osc).eql([[1, 'foo=bar']]); chai.expect(oscCustom).eql([], 'Should not use custom handler as it was disposed'); }); @@ -1343,7 +1343,7 @@ describe('EscapeSequenceParser', function (): void { const customHandler = parser2.addOscHandler(1, data => { oscCustom.push([1, data]); return true; }); customHandler.dispose(); customHandler.dispose(); - parser2.parse(INPUT); + parse(parser2, INPUT); chai.expect(osc).eql([[1, 'foo=bar']]); chai.expect(oscCustom).eql([], 'Should not use custom handler as it was disposed'); }); diff --git a/src/core/input/TextDecoder.test.ts b/src/core/input/TextDecoder.test.ts new file mode 100644 index 00000000..f69fbded --- /dev/null +++ b/src/core/input/TextDecoder.test.ts @@ -0,0 +1,67 @@ +/** + * Copyright (c) 2019 The xterm.js authors. All rights reserved. + * @license MIT + */ + +import { assert } from 'chai'; +import { StringToUtf32, stringFromCodePoint } from './TextDecoder'; + + +// convert UTF32 codepoints to string +function toString(data: Uint32Array, length: number): string { + if ((String as any).fromCodePoint) { + return (String as any).fromCodePoint.apply(null, data.subarray(0, length)); + } + let result = ''; + for (let i = 0; i < length; ++i) { + result += stringFromCodePoint(data[i]); + } + return result; +} + +describe('StringToUtf32 Decoder', () => { + describe('full codepoint test', () => { + it('0..65535', () => { + const decoder = new StringToUtf32(); + const target = new Uint32Array(5); + for (let i = 0; i < 65536; ++i) { + // skip surrogate pairs + if (i >= 0xD800 && i <= 0xDFFF) { + continue; + } + const length = decoder.decode(String.fromCharCode(i), target); + assert.equal(length, 1); + assert.equal(target[0], i); + assert.equal(toString(target, length), String.fromCharCode(i)); + decoder.clear(); + } + }); + it('65536..0x10FFFF (surrogates)', function(): void { + this.timeout(20000); + const decoder = new StringToUtf32(); + const target = new Uint32Array(5); + for (let i = 65536; i < 0x10FFFF; ++i) { + const codePoint = i - 0x10000; + const s = String.fromCharCode((codePoint >> 10) + 0xD800) + String.fromCharCode((codePoint % 0x400) + 0xDC00); + const length = decoder.decode(s, target); + assert.equal(length, 1); + assert.equal(target[0], i); + assert.equal(toString(target, length), s); + decoder.clear(); + } + }); + }); + describe('stream handling', () => { + it('surrogates mixed advance by 1', () => { + const decoder = new StringToUtf32(); + const target = new Uint32Array(5); + const input = 'Ä€𝄞Ö𝄞€Ü𝄞€'; + let decoded = ''; + for (let i = 0; i < input.length; ++i) { + const written = decoder.decode(input[i], target); + decoded += toString(target, written); + } + assert(decoded, 'Ä€𝄞Ö𝄞€Ü𝄞€'); + }); + }); +}); diff --git a/src/core/input/TextDecoder.ts b/src/core/input/TextDecoder.ts new file mode 100644 index 00000000..77e6971c --- /dev/null +++ b/src/core/input/TextDecoder.ts @@ -0,0 +1,87 @@ +/** + * Copyright (c) 2019 The xterm.js authors. All rights reserved. + * @license MIT + */ + +/** + * StringToUtf32 - decodes UTF16 sequences into UTF32 codepoints. + * To keep the decoder in line with JS strings it handles single surrogates as UCS2. + */ +export class StringToUtf32 { + private _interim: number = 0; + + /** + * Clears interim and resets decoder to clean state. + */ + public clear(): void { + this._interim = 0; + } + + /** + * Decode JS string to UTF32 codepoints. + * The methods assumes stream input and will store partly transmitted + * surrogate pairs and decode them with the next data chunk. + * Note: The method does no bound checks for target, therefore make sure + * the provided input data does not exceed the size of `target`. + * Returns the number of written codepoints in `target`. + */ + decode(input: string, target: Uint32Array): number { + const length = input.length; + + if (!length) { + return 0; + } + + let size = 0; + let startPos = 0; + + // handle leftover surrogate high + if (this._interim) { + const second = input.charCodeAt(startPos++); + if (0xDC00 <= second && second <= 0xDFFF) { + target[size++] = (this._interim - 0xD800) * 0x400 + second - 0xDC00 + 0x10000; + } else { + // illegal codepoint (USC2 handling) + target[size++] = this._interim; + target[size++] = second; + } + this._interim = 0; + } + + for (let i = startPos; i < length; ++i) { + const code = input.charCodeAt(i); + // surrogate pair first + if (0xD800 <= code && code <= 0xDBFF) { + if (++i >= length) { + this._interim = code; + return size; + } + const second = input.charCodeAt(i); + if (0xDC00 <= second && second <= 0xDFFF) { + target[size++] = (code - 0xD800) * 0x400 + second - 0xDC00 + 0x10000; + } else { + // illegal codepoint (USC2 handling) + target[size++] = code; + target[size++] = second; + } + continue; + } + target[size++] = code; + } + return size; + } +} + +/** + * Polyfill - Convert UTF32 codepoint into JS string. + */ +export function stringFromCodePoint(codePoint: number): string { + if ((String as any).fromCodePoint) { + return (String as any).fromCodePoint(codePoint); + } + if (codePoint > 0xFFFF) { + codePoint -= 0x10000; + return String.fromCharCode((codePoint >> 10) + 0xD800) + String.fromCharCode((codePoint % 0x400) + 0xDC00); + } + return String.fromCharCode(codePoint); +} From 2918a6763f5023b3032069c3785d2bc6abf0df78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Breitbart?= Date: Wed, 2 Jan 2019 22:55:50 +0100 Subject: [PATCH 08/41] fix wrongly changed test case --- src/EscapeSequenceParser.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/EscapeSequenceParser.test.ts b/src/EscapeSequenceParser.test.ts index 64e61ba3..3c9db585 100644 --- a/src/EscapeSequenceParser.test.ts +++ b/src/EscapeSequenceParser.test.ts @@ -1231,8 +1231,8 @@ describe('EscapeSequenceParser', function (): void { parser2.setCsiHandler('m', () => order.push(1)); parser2.addCsiHandler('m', () => { order.push(2); return false; }); parser2.addCsiHandler('m', () => { order.push(3); return false; }); - parse(parser2, INPUT); - chai.expect(order).eql([3, 2, 1, 3, 2, 1]); + parse(parser2, '\x1b[0m'); + chai.expect(order).eql([3, 2, 1]); }); it('Dispose should work', () => { const csiCustom: [string, number[], string][] = []; From 8fc66a39e32c6465ad5e49f492f0f5530b72983c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Breitbart?= Date: Thu, 3 Jan 2019 00:48:00 +0100 Subject: [PATCH 09/41] switch parse buffer to UTF32 --- src/EscapeSequenceParser.test.ts | 27 ++++++------ src/EscapeSequenceParser.ts | 14 +++---- src/InputHandler.test.ts | 2 +- src/InputHandler.ts | 66 ++++++++---------------------- src/Types.ts | 8 ++-- src/common/TypedArrayUtils.test.ts | 4 +- src/common/TypedArrayUtils.ts | 15 +++++-- 7 files changed, 57 insertions(+), 79 deletions(-) diff --git a/src/EscapeSequenceParser.test.ts b/src/EscapeSequenceParser.test.ts index 3c9db585..135cc393 100644 --- a/src/EscapeSequenceParser.test.ts +++ b/src/EscapeSequenceParser.test.ts @@ -6,6 +6,7 @@ import { ParserState, IDcsHandler, IParsingState } from './Types'; import { EscapeSequenceParser, TransitionTable, VT500_TRANSITION_TABLE } from './EscapeSequenceParser'; import * as chai from 'chai'; +import { StringToUtf32, stringFromCodePoint } from './core/input/TextDecoder'; function r(a: number, b: number): string[] { let c = b - a; @@ -50,10 +51,10 @@ const testTerminal: any = { compare: function (value: any): void { chai.expect(this.calls.slice()).eql(value); // weird bug w'o slicing here }, - print: function (data: Uint16Array, start: number, end: number): void { + print: function (data: Uint32Array, start: number, end: number): void { let s = ''; for (let i = start; i < end; ++i) { - s += String.fromCharCode(data[i]); + s += stringFromCodePoint(data[i]); } this.calls.push(['print', s]); }, @@ -72,10 +73,10 @@ const testTerminal: any = { actionDCSHook: function (collect: string, params: number[], flag: string): void { this.calls.push(['dcs hook', collect, params, flag]); }, - actionDCSPrint: function (data: Uint16Array, start: number, end: number): void { + actionDCSPrint: function (data: Uint32Array, start: number, end: number): void { let s = ''; for (let i = start; i < end; ++i) { - s += String.fromCharCode(data[i]); + s += stringFromCodePoint(data[i]); } this.calls.push(['dcs put', s]); }, @@ -89,7 +90,7 @@ class DcsTest implements IDcsHandler { hook(collect: string, params: number[], flag: number): void { testTerminal.actionDCSHook(collect, params, String.fromCharCode(flag)); } - put(data: Uint16Array, start: number, end: number): void { + put(data: Uint32Array, start: number, end: number): void { testTerminal.actionDCSPrint(data, start, end); } unhook(): void { @@ -165,11 +166,9 @@ interface IRun { // translate string based parse calls into typed array based function parse(parser: TestEscapeSequenceParser, data: string): void { - const container = new Uint16Array(data.length); - for (let i = 0; i < data.length; ++i) { - container[i] = data.charCodeAt(i); - } - parser.parse(container, data.length); + const container = new Uint32Array(data.length); + const decoder = new StringToUtf32(); + parser.parse(container, decoder.decode(data, container)); } describe('EscapeSequenceParser', function (): void { @@ -1143,9 +1142,9 @@ describe('EscapeSequenceParser', function (): void { clearAccu(); }); it('print handler', function (): void { - parser2.setPrintHandler(function (data: Uint16Array, start: number, end: number): void { + parser2.setPrintHandler(function (data: Uint32Array, start: number, end: number): void { for (let i = start; i < end; ++i) { - print += String.fromCharCode(data[i]); + print += stringFromCodePoint(data[i]); } }); parse(parser2, INPUT); @@ -1353,10 +1352,10 @@ describe('EscapeSequenceParser', function (): void { hook: function (collect: string, params: number[], flag: number): void { dcs.push(['hook', collect, params, flag]); }, - put: function (data: Uint16Array, start: number, end: number): void { + put: function (data: Uint32Array, start: number, end: number): void { let s = ''; for (let i = start; i < end; ++i) { - s += String.fromCharCode(data[i]); + s += stringFromCodePoint(data[i]); } dcs.push(['put', s]); }, diff --git a/src/EscapeSequenceParser.ts b/src/EscapeSequenceParser.ts index cee9957b..7bd425d4 100644 --- a/src/EscapeSequenceParser.ts +++ b/src/EscapeSequenceParser.ts @@ -6,7 +6,7 @@ import { ParserState, ParserAction, IParsingState, IDcsHandler, IEscapeSequenceParser } from './Types'; import { IDisposable } from 'xterm'; import { Disposable } from './common/Lifecycle'; -import { utf16ToString } from './common/TypedArrayUtils'; +import { utf32ToString } from './common/TypedArrayUtils'; interface IHandlerCollection { [key: string]: T[]; @@ -204,7 +204,7 @@ export const VT500_TRANSITION_TABLE = (function (): TransitionTable { */ class DcsDummy implements IDcsHandler { hook(collect: string, params: number[], flag: number): void { } - put(data: Uint16Array, start: number, end: number): void { } + put(data: Uint32Array, start: number, end: number): void { } unhook(): void { } } @@ -230,7 +230,7 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP protected _collect: string; // handler lookup containers - protected _printHandler: (data: Uint16Array, start: number, end: number) => void; + protected _printHandler: (data: Uint32Array, start: number, end: number) => void; protected _executeHandlers: any; protected _csiHandlers: IHandlerCollection; protected _escHandlers: any; @@ -240,7 +240,7 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP protected _errorHandler: (state: IParsingState) => IParsingState; // fallback handlers - protected _printHandlerFb: (data: Uint16Array, start: number, end: number) => void; + protected _printHandlerFb: (data: Uint32Array, start: number, end: number) => void; protected _executeHandlerFb: (code: number) => void; protected _csiHandlerFb: (collect: string, params: number[], flag: number) => void; protected _escHandlerFb: (collect: string, flag: number) => void; @@ -296,7 +296,7 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP this._errorHandler = null; } - setPrintHandler(callback: (data: Uint16Array, start: number, end: number) => void): void { + setPrintHandler(callback: (data: Uint32Array, start: number, end: number) => void): void { this._printHandler = callback; } clearPrintHandler(): void { @@ -399,7 +399,7 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP this._activeDcsHandler = null; } - parse(data: Uint16Array, length: number): void { + parse(data: Uint32Array, length: number): void { let code = 0; let transition = 0; let error = false; @@ -567,7 +567,7 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP if (j >= length || (code = data[j]) < 0x20 || (code > 0x7f && code <= 0x9f)) { - osc += utf16ToString(data.subarray(i, j)); + osc += utf32ToString(data.subarray(i, j)); i = j - 1; break; } diff --git a/src/InputHandler.test.ts b/src/InputHandler.test.ts index b92d009b..a7963a2c 100644 --- a/src/InputHandler.test.ts +++ b/src/InputHandler.test.ts @@ -337,7 +337,7 @@ describe('InputHandler', () => { it('should not cause an infinite loop (regression test)', () => { const term = new Terminal(); const inputHandler = new InputHandler(term); - const container = new Uint16Array(10); + const container = new Uint32Array(10); container[0] = 0x200B; inputHandler.print(container, 0, 1); }); diff --git a/src/InputHandler.ts b/src/InputHandler.ts index 3a2dab11..e270a5f3 100644 --- a/src/InputHandler.ts +++ b/src/InputHandler.ts @@ -14,7 +14,8 @@ import { EscapeSequenceParser } from './EscapeSequenceParser'; import { ICharset } from './core/Types'; import { IDisposable } from 'xterm'; import { Disposable } from './common/Lifecycle'; -import { concat, utf16ToString } from './common/TypedArrayUtils'; +import { concat, utf32ToString } from './common/TypedArrayUtils'; +import { StringToUtf32, stringFromCodePoint } from './core/input/TextDecoder'; /** * Map collect to glevel. Used in `selectCharset`. @@ -32,17 +33,17 @@ const GLEVEL: {[key: string]: number} = {'(': 0, ')': 1, '*': 2, '+': 3, '-': 1, * not supported */ class RequestTerminfo implements IDcsHandler { - private _data: Uint16Array = new Uint16Array(0); + private _data: Uint32Array = new Uint32Array(0); constructor(private _terminal: any) { } hook(collect: string, params: number[], flag: number): void { - this._data = new Uint16Array(0); + this._data = new Uint32Array(0); } - put(data: Uint16Array, start: number, end: number): void { + put(data: Uint32Array, start: number, end: number): void { this._data = concat(this._data, data.subarray(start, end)); } unhook(): void { - const data = utf16ToString(this._data); - this._data = new Uint16Array(0); + const data = utf32ToString(this._data); + this._data = new Uint32Array(0); // invalid: DCS 0 + r Pt ST this._terminal.handler(`${C0.ESC}P0+r${data}${C0.ESC}\\`); } @@ -55,21 +56,21 @@ const GLEVEL: {[key: string]: number} = {'(': 0, ')': 1, '*': 2, '+': 3, '-': 1, * Response: DECRPSS (https://vt100.net/docs/vt510-rm/DECRPSS.html) */ class DECRQSS implements IDcsHandler { - private _data: Uint16Array = new Uint16Array(0); + private _data: Uint32Array = new Uint32Array(0); constructor(private _terminal: any) { } hook(collect: string, params: number[], flag: number): void { - this._data = new Uint16Array(0); + this._data = new Uint32Array(0); } - put(data: Uint16Array, start: number, end: number): void { + put(data: Uint32Array, start: number, end: number): void { this._data = concat(this._data, data.subarray(start, end)); } unhook(): void { - const data = utf16ToString(this._data); - this._data = new Uint16Array(0); + const data = utf32ToString(this._data); + this._data = new Uint32Array(0); switch (data) { // valid: DCS 1 $ r Pt ST (xterm) case '"q': // DECSCA @@ -118,8 +119,8 @@ class DECRQSS implements IDcsHandler { * each function's header comment. */ export class InputHandler extends Disposable implements IInputHandler { - private _surrogateFirst: string; - private _parseBuffer: Uint16Array = new Uint16Array(4096); + private _parseBuffer: Uint32Array = new Uint32Array(4096); + private _stringDecoder: StringToUtf32 = new StringToUtf32(); constructor( protected _terminal: IInputHandlingTerminal, @@ -129,8 +130,6 @@ export class InputHandler extends Disposable implements IInputHandler { this.register(this._parser); - this._surrogateFirst = ''; - /** * custom fallback handlers */ @@ -316,19 +315,13 @@ export class InputHandler extends Disposable implements IInputHandler { this._terminal.log('data: ' + data); } - // apply leftover surrogate high from last write - if (this._surrogateFirst) { - data = this._surrogateFirst + data; - this._surrogateFirst = ''; - } - if (this._parseBuffer.length < data.length) { - this._parseBuffer = new Uint16Array(data.length); + this._parseBuffer = new Uint32Array(data.length); } for (let i = 0; i < data.length; ++i) { this._parseBuffer[i] = data.charCodeAt(i); } - this._parser.parse(this._parseBuffer, data.length); + this._parser.parse(this._parseBuffer, this._stringDecoder.decode(data, this._parseBuffer)); buffer = this._terminal.buffer; if (buffer.x !== cursorStartX || buffer.y !== cursorStartY) { @@ -336,7 +329,7 @@ export class InputHandler extends Disposable implements IInputHandler { } } - public print(data: Uint16Array, start: number, end: number): void { + public print(data: Uint32Array, start: number, end: number): void { let code: number; let char: string; let chWidth: number; @@ -352,30 +345,7 @@ export class InputHandler extends Disposable implements IInputHandler { this._terminal.updateRange(buffer.y); for (let pos = start; pos < end; ++pos) { code = data[pos]; - char = String.fromCharCode(code); - - // surrogate pair handling - if (0xD800 <= code && code <= 0xDBFF) { - if (++pos >= end) { - // end of input: - // handle pairs as true UTF-16 and wait for the second part - // since we expect the input comming from a stream there is - // a small chance that the surrogate pair got split - // therefore we dont process the first char here, instead - // it gets added as first char to the next processed chunk - this._surrogateFirst = String.fromCharCode(code); - continue; - } - const second = data[pos]; - // if the second part is in surrogate pair range create the high codepoint - // otherwise fall back to UCS-2 behavior (handle codepoints independently) - if (0xDC00 <= second && second <= 0xDFFF) { - code = (code - 0xD800) * 0x400 + second - 0xDC00 + 0x10000; - char += String.fromCharCode(second); - } else { - pos--; - } - } + char = stringFromCodePoint(code); // calculate print space // expensive call, therefore we save width in line buffer diff --git a/src/Types.ts b/src/Types.ts index ca50afd5..60b86de1 100644 --- a/src/Types.ts +++ b/src/Types.ts @@ -111,7 +111,7 @@ export interface ICompositionHelper { */ export interface IInputHandler { parse(data: string): void; - print(data: Uint16Array, start: number, end: number): void; + print(data: Uint32Array, start: number, end: number): void; /** C0 BEL */ bell(): void; /** C0 LF */ lineFeed(): void; @@ -463,7 +463,7 @@ export interface IParsingState { */ export interface IDcsHandler { hook(collect: string, params: number[], flag: number): void; - put(data: Uint16Array, start: number, end: number): void; + put(data: Uint32Array, start: number, end: number): void; unhook(): void; } @@ -480,9 +480,9 @@ export interface IEscapeSequenceParser extends IDisposable { * Parse string `data`. * @param data The data to parse. */ - parse(data: Uint16Array, length: number): void; + parse(data: Uint32Array, length: number): void; - setPrintHandler(callback: (data: Uint16Array, start: number, end: number) => void): void; + setPrintHandler(callback: (data: Uint32Array, start: number, end: number) => void): void; clearPrintHandler(): void; setExecuteHandler(flag: string, callback: () => void): void; diff --git a/src/common/TypedArrayUtils.test.ts b/src/common/TypedArrayUtils.test.ts index b25b9b9a..79546ca9 100644 --- a/src/common/TypedArrayUtils.test.ts +++ b/src/common/TypedArrayUtils.test.ts @@ -3,7 +3,7 @@ * @license MIT */ import { assert } from 'chai'; -import { fillFallback, concat, utf16ToString } from './TypedArrayUtils'; +import { fillFallback, concat, utf32ToString } from './TypedArrayUtils'; type TypedArray = Uint8Array | Uint16Array | Uint32Array | Uint8ClampedArray | Int8Array | Int16Array | Int32Array @@ -100,6 +100,6 @@ describe('typed array convenience functions', () => { for (let i = 0; i < s.length; ++i) { data[i] = s.charCodeAt(i); } - assert.equal(utf16ToString(data), s); + assert.equal(utf32ToString(data), s); }); }); diff --git a/src/common/TypedArrayUtils.ts b/src/common/TypedArrayUtils.ts index 5d3bcb8a..5ec1fd48 100644 --- a/src/common/TypedArrayUtils.ts +++ b/src/common/TypedArrayUtils.ts @@ -1,3 +1,5 @@ +import { stringFromCodePoint } from '../core/input/TextDecoder'; + /** * Copyright (c) 2018 The xterm.js authors. All rights reserved. * @license MIT @@ -51,11 +53,18 @@ export function concat(a: T, b: T): T { } /** - * Convert UTF16 char codes into JS string. + * Convert UTF32 char codes into JS string. * Note the typed array is not limited to Uint16Array, make sure to align * the values to 0-65535 integer for other typed array types, otherwise * the conversion will fail. */ -export function utf16ToString(data: T): string { - return String.fromCharCode.apply(null, data); +export function utf32ToString(data: T): string { + if ((String as any).fromCodePoint) { + return (String as any).fromCodePoint.apply(null, data); + } + let result = ''; + for (let i = 0; i < data.length; ++i) { + result += stringFromCodePoint(data[i]); + } + return result; } From 27b91f6b00c3a8d8d25bb0850518b8ccb75d8485 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Breitbart?= Date: Thu, 3 Jan 2019 02:55:11 +0100 Subject: [PATCH 10/41] speedup utf32ToString conversion --- src/EscapeSequenceParser.ts | 2 +- src/common/TypedArrayUtils.ts | 20 +++++++++----------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/EscapeSequenceParser.ts b/src/EscapeSequenceParser.ts index 7bd425d4..ec7a9da7 100644 --- a/src/EscapeSequenceParser.ts +++ b/src/EscapeSequenceParser.ts @@ -567,7 +567,7 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP if (j >= length || (code = data[j]) < 0x20 || (code > 0x7f && code <= 0x9f)) { - osc += utf32ToString(data.subarray(i, j)); + osc += utf32ToString(data, i, j); i = j - 1; break; } diff --git a/src/common/TypedArrayUtils.ts b/src/common/TypedArrayUtils.ts index 5ec1fd48..5f5be782 100644 --- a/src/common/TypedArrayUtils.ts +++ b/src/common/TypedArrayUtils.ts @@ -1,5 +1,3 @@ -import { stringFromCodePoint } from '../core/input/TextDecoder'; - /** * Copyright (c) 2018 The xterm.js authors. All rights reserved. * @license MIT @@ -54,17 +52,17 @@ export function concat(a: T, b: T): T { /** * Convert UTF32 char codes into JS string. - * Note the typed array is not limited to Uint16Array, make sure to align - * the values to 0-65535 integer for other typed array types, otherwise - * the conversion will fail. */ -export function utf32ToString(data: T): string { - if ((String as any).fromCodePoint) { - return (String as any).fromCodePoint.apply(null, data); - } +export function utf32ToString(data: T, start: number = 0, end: number = data.length): string { let result = ''; - for (let i = 0; i < data.length; ++i) { - result += stringFromCodePoint(data[i]); + let cp; + for (let i = start; i < end; ++i) { + if ((cp = data[i]) > 0xFFFF) { + cp -= 0x10000; + result += String.fromCharCode((cp >> 10) + 0xD800) + String.fromCharCode((cp % 0x400) + 0xDC00); + } else { + result += String.fromCharCode(cp); + } } return result; } From 249f8800af98bf9a715b77bedd086bd350f975c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Breitbart?= Date: Mon, 7 Jan 2019 11:30:26 +0100 Subject: [PATCH 11/41] account empty cells in stringIndexToBufferIndex --- src/Buffer.test.ts | 6 +++--- src/Buffer.ts | 8 +++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Buffer.test.ts b/src/Buffer.test.ts index 0546dfe8..663c7000 100644 --- a/src/Buffer.test.ts +++ b/src/Buffer.test.ts @@ -511,7 +511,7 @@ describe('Buffer', () => { const s = terminal.buffer.iterator(true).next().content; assert.equal(input, s); for (let i = 10; i < input.length; ++i) { - const bufferIndex = terminal.buffer.stringIndexToBufferIndex(0, i); + const bufferIndex = terminal.buffer.stringIndexToBufferIndex(0, i, true); const j = (i - 0) << 1; assert.deepEqual([(j / terminal.cols) | 0, j % terminal.cols], bufferIndex); } @@ -523,7 +523,7 @@ describe('Buffer', () => { const s = terminal.buffer.iterator(true).next().content; assert.equal(input, s); for (let i = 0; i < input.length; ++i) { - const bufferIndex = terminal.buffer.stringIndexToBufferIndex(0, i); + const bufferIndex = terminal.buffer.stringIndexToBufferIndex(0, i, true); assert.equal(input[i], terminal.buffer.lines.get(bufferIndex[0]).get(bufferIndex[1])[CHAR_DATA_CHAR_INDEX]); } }); @@ -535,7 +535,7 @@ describe('Buffer', () => { const s = terminal.buffer.iterator(true).next().content; assert.equal(input, s); for (let i = 0; i < input.length; ++i) { - const bufferIndex = terminal.buffer.stringIndexToBufferIndex(0, i); + const bufferIndex = terminal.buffer.stringIndexToBufferIndex(0, i, true); assert.equal( (!(i % 3)) ? input[i] diff --git a/src/Buffer.ts b/src/Buffer.ts index 625a2497..7b3bcee5 100644 --- a/src/Buffer.ts +++ b/src/Buffer.ts @@ -220,14 +220,16 @@ export class Buffer implements IBuffer { * @param stringIndex index within the string * @param startCol column offset the string was retrieved from */ - public stringIndexToBufferIndex(lineIndex: number, stringIndex: number): BufferIndex { + public stringIndexToBufferIndex(lineIndex: number, stringIndex: number, trimRight: boolean = false): BufferIndex { while (stringIndex) { const line = this.lines.get(lineIndex); if (!line) { return [-1, -1]; } - for (let i = 0; i < line.length; ++i) { - stringIndex -= line.get(i)[CHAR_DATA_CHAR_INDEX].length; + const length = (trimRight) ? line.getTrimmedLength() : line.length; + for (let i = 0; i < length; ++i) { + if (line.get(i)[CHAR_DATA_WIDTH_INDEX]) + stringIndex -= line.get(i)[CHAR_DATA_CHAR_INDEX].length || 1; if (stringIndex < 0) { return [lineIndex, i]; } From 883ad01bd508e6df1f69127449b31710c8d9b6c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Breitbart?= Date: Mon, 7 Jan 2019 11:39:16 +0100 Subject: [PATCH 12/41] make linter happy --- src/Buffer.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Buffer.ts b/src/Buffer.ts index 7b3bcee5..0ad86bf1 100644 --- a/src/Buffer.ts +++ b/src/Buffer.ts @@ -228,8 +228,9 @@ export class Buffer implements IBuffer { } const length = (trimRight) ? line.getTrimmedLength() : line.length; for (let i = 0; i < length; ++i) { - if (line.get(i)[CHAR_DATA_WIDTH_INDEX]) - stringIndex -= line.get(i)[CHAR_DATA_CHAR_INDEX].length || 1; + if (line.get(i)[CHAR_DATA_WIDTH_INDEX]) { + stringIndex -= line.get(i)[CHAR_DATA_CHAR_INDEX].length || 1; // WHITESPACE_CELL_CHAR.length + } if (stringIndex < 0) { return [lineIndex, i]; } From 329d40ab14676dd02e5478dbd2c207c7a2bcdfe8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Breitbart?= Date: Sat, 12 Jan 2019 13:52:26 +0100 Subject: [PATCH 13/41] cleanup DCS handler --- src/InputHandler.ts | 31 +++++++------------------------ 1 file changed, 7 insertions(+), 24 deletions(-) diff --git a/src/InputHandler.ts b/src/InputHandler.ts index e270a5f3..dbb01608 100644 --- a/src/InputHandler.ts +++ b/src/InputHandler.ts @@ -27,28 +27,6 @@ const GLEVEL: {[key: string]: number} = {'(': 0, ')': 1, '*': 2, '+': 3, '-': 1, * DCS subparser implementations */ - /** - * DCS + q Pt ST (xterm) - * Request Terminfo String - * not supported - */ - class RequestTerminfo implements IDcsHandler { - private _data: Uint32Array = new Uint32Array(0); - constructor(private _terminal: any) { } - hook(collect: string, params: number[], flag: number): void { - this._data = new Uint32Array(0); - } - put(data: Uint32Array, start: number, end: number): void { - this._data = concat(this._data, data.subarray(start, end)); - } - unhook(): void { - const data = utf32ToString(this._data); - this._data = new Uint32Array(0); - // invalid: DCS 0 + r Pt ST - this._terminal.handler(`${C0.ESC}P0+r${data}${C0.ESC}\\`); - } -} - /** * DCS $ q Pt ST * DECRQSS (https://vt100.net/docs/vt510-rm/DECRQSS.html) @@ -92,7 +70,7 @@ class DECRQSS implements IDcsHandler { default: // invalid: DCS 0 $ r Pt ST (xterm) this._terminal.error('Unknown DCS $q %s', data); - this._terminal.handler(`${C0.ESC}P0$r${data}${C0.ESC}\\`); + this._terminal.handler(`${C0.ESC}P0$r${C0.ESC}\\`); } } } @@ -103,6 +81,12 @@ class DECRQSS implements IDcsHandler { * not supported */ +/** + * DCS + q Pt ST (xterm) + * Request Terminfo String + * not implemented + */ + /** * DCS + p Pt ST (xterm) * Set Terminfo Data @@ -292,7 +276,6 @@ export class InputHandler extends Disposable implements IInputHandler { * DCS handler */ this._parser.setDcsHandler('$q', new DECRQSS(this._terminal)); - this._parser.setDcsHandler('+q', new RequestTerminfo(this._terminal)); } public dispose(): void { From 07d3407892a35e6afeb6a0e4c3355868ab686546 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Thu, 17 Jan 2019 09:48:18 -0800 Subject: [PATCH 14/41] Remove unused var and unneeded defensive check ...items as an arg will always be an array --- demo/start.js | 1 - src/common/CircularList.ts | 32 +++++++++++++++----------------- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/demo/start.js b/demo/start.js index 78f1ff1d..278c572f 100644 --- a/demo/start.js +++ b/demo/start.js @@ -5,7 +5,6 @@ * This file is the entry point for browserify. */ -const cp = require('child_process'); const path = require('path'); const webpack = require('webpack'); const startServer = require('./server.js'); diff --git a/src/common/CircularList.ts b/src/common/CircularList.ts index 9faf534a..d23e693f 100644 --- a/src/common/CircularList.ts +++ b/src/common/CircularList.ts @@ -144,24 +144,22 @@ export class CircularList extends EventEmitter implements ICircularList { this._length -= deleteCount; } - if (items && items.length) { - // Add items - for (let i = this._length - 1; i >= start; i--) { - this._array[this._getCyclicIndex(i + items.length)] = this._array[this._getCyclicIndex(i)]; - } - for (let i = 0; i < items.length; i++) { - this._array[this._getCyclicIndex(start + i)] = items[i]; - } + // Add items + for (let i = this._length - 1; i >= start; i--) { + this._array[this._getCyclicIndex(i + items.length)] = this._array[this._getCyclicIndex(i)]; + } + for (let i = 0; i < items.length; i++) { + this._array[this._getCyclicIndex(start + i)] = items[i]; + } - // Adjust length as needed - if (this._length + items.length > this._maxLength) { - const countToTrim = (this._length + items.length) - this._maxLength; - this._startIndex += countToTrim; - this._length = this._maxLength; - this.emit('trim', countToTrim); - } else { - this._length += items.length; - } + // Adjust length as needed + if (this._length + items.length > this._maxLength) { + const countToTrim = (this._length + items.length) - this._maxLength; + this._startIndex += countToTrim; + this._length = this._maxLength; + this.emit('trim', countToTrim); + } else { + this._length += items.length; } } From 4855b60ffeef2cfd1e3bb36eb958e68e5713ea8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Breitbart?= Date: Fri, 25 Jan 2019 17:35:12 +0100 Subject: [PATCH 15/41] move utf32ToString to TextDecoder.ts --- src/EscapeSequenceParser.ts | 2 +- src/InputHandler.ts | 4 +- src/common/TypedArrayUtils.test.ts | 10 +-- src/common/TypedArrayUtils.ts | 22 +----- src/common/Types.ts | 4 ++ src/core/input/TextDecoder.test.ts | 112 ++++++++++++++--------------- src/core/input/TextDecoder.ts | 25 +++++-- 7 files changed, 86 insertions(+), 93 deletions(-) diff --git a/src/EscapeSequenceParser.ts b/src/EscapeSequenceParser.ts index ec7a9da7..7b65624d 100644 --- a/src/EscapeSequenceParser.ts +++ b/src/EscapeSequenceParser.ts @@ -6,7 +6,7 @@ import { ParserState, ParserAction, IParsingState, IDcsHandler, IEscapeSequenceParser } from './Types'; import { IDisposable } from 'xterm'; import { Disposable } from './common/Lifecycle'; -import { utf32ToString } from './common/TypedArrayUtils'; +import { utf32ToString } from './core/input/TextDecoder'; interface IHandlerCollection { [key: string]: T[]; diff --git a/src/InputHandler.ts b/src/InputHandler.ts index a702e2a6..2f53cfcb 100644 --- a/src/InputHandler.ts +++ b/src/InputHandler.ts @@ -14,8 +14,8 @@ import { EscapeSequenceParser } from './EscapeSequenceParser'; import { ICharset } from './core/Types'; import { IDisposable } from 'xterm'; import { Disposable } from './common/Lifecycle'; -import { concat, utf32ToString } from './common/TypedArrayUtils'; -import { StringToUtf32, stringFromCodePoint } from './core/input/TextDecoder'; +import { concat } from './common/TypedArrayUtils'; +import { StringToUtf32, stringFromCodePoint, utf32ToString } from './core/input/TextDecoder'; /** * Map collect to glevel. Used in `selectCharset`. diff --git a/src/common/TypedArrayUtils.test.ts b/src/common/TypedArrayUtils.test.ts index 79546ca9..99b0fd82 100644 --- a/src/common/TypedArrayUtils.test.ts +++ b/src/common/TypedArrayUtils.test.ts @@ -3,7 +3,7 @@ * @license MIT */ import { assert } from 'chai'; -import { fillFallback, concat, utf32ToString } from './TypedArrayUtils'; +import { fillFallback, concat } from './TypedArrayUtils'; type TypedArray = Uint8Array | Uint16Array | Uint32Array | Uint8ClampedArray | Int8Array | Int16Array | Int32Array @@ -94,12 +94,4 @@ describe('typed array convenience functions', () => { const merged = concat(a, b); deepEquals(merged, new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 0])); }); - it('utf16ToString', () => { - const s = 'abcdefg'; - const data = new Uint16Array(s.length); - for (let i = 0; i < s.length; ++i) { - data[i] = s.charCodeAt(i); - } - assert.equal(utf32ToString(data), s); - }); }); diff --git a/src/common/TypedArrayUtils.ts b/src/common/TypedArrayUtils.ts index 5f5be782..380dff35 100644 --- a/src/common/TypedArrayUtils.ts +++ b/src/common/TypedArrayUtils.ts @@ -2,10 +2,7 @@ * Copyright (c) 2018 The xterm.js authors. All rights reserved. * @license MIT */ - -type TypedArray = Uint8Array | Uint16Array | Uint32Array | Uint8ClampedArray - | Int8Array | Int16Array | Int32Array - | Float32Array | Float64Array; +import { TypedArray } from './Types'; /** * polyfill for TypedArray.fill @@ -49,20 +46,3 @@ export function concat(a: T, b: T): T { result.set(b, a.length); return result; } - -/** - * Convert UTF32 char codes into JS string. - */ -export function utf32ToString(data: T, start: number = 0, end: number = data.length): string { - let result = ''; - let cp; - for (let i = start; i < end; ++i) { - if ((cp = data[i]) > 0xFFFF) { - cp -= 0x10000; - result += String.fromCharCode((cp >> 10) + 0xD800) + String.fromCharCode((cp % 0x400) + 0xDC00); - } else { - result += String.fromCharCode(cp); - } - } - return result; -} diff --git a/src/common/Types.ts b/src/common/Types.ts index 8a416bf1..29b5febb 100644 --- a/src/common/Types.ts +++ b/src/common/Types.ts @@ -35,3 +35,7 @@ export interface ICircularList extends IEventEmitter { trimStart(count: number): void; shiftElements(start: number, count: number, offset: number): void; } + +export type TypedArray = Uint8Array | Uint16Array | Uint32Array | Uint8ClampedArray + | Int8Array | Int16Array | Int32Array + | Float32Array | Float64Array; diff --git a/src/core/input/TextDecoder.test.ts b/src/core/input/TextDecoder.test.ts index f69fbded..12f3099a 100644 --- a/src/core/input/TextDecoder.test.ts +++ b/src/core/input/TextDecoder.test.ts @@ -4,64 +4,64 @@ */ import { assert } from 'chai'; -import { StringToUtf32, stringFromCodePoint } from './TextDecoder'; +import { StringToUtf32, stringFromCodePoint, utf32ToString } from './TextDecoder'; - -// convert UTF32 codepoints to string -function toString(data: Uint32Array, length: number): string { - if ((String as any).fromCodePoint) { - return (String as any).fromCodePoint.apply(null, data.subarray(0, length)); - } - let result = ''; - for (let i = 0; i < length; ++i) { - result += stringFromCodePoint(data[i]); - } - return result; -} - -describe('StringToUtf32 Decoder', () => { - describe('full codepoint test', () => { - it('0..65535', () => { - const decoder = new StringToUtf32(); - const target = new Uint32Array(5); - for (let i = 0; i < 65536; ++i) { - // skip surrogate pairs - if (i >= 0xD800 && i <= 0xDFFF) { - continue; - } - const length = decoder.decode(String.fromCharCode(i), target); - assert.equal(length, 1); - assert.equal(target[0], i); - assert.equal(toString(target, length), String.fromCharCode(i)); - decoder.clear(); - } - }); - it('65536..0x10FFFF (surrogates)', function(): void { - this.timeout(20000); - const decoder = new StringToUtf32(); - const target = new Uint32Array(5); - for (let i = 65536; i < 0x10FFFF; ++i) { - const codePoint = i - 0x10000; - const s = String.fromCharCode((codePoint >> 10) + 0xD800) + String.fromCharCode((codePoint % 0x400) + 0xDC00); - const length = decoder.decode(s, target); - assert.equal(length, 1); - assert.equal(target[0], i); - assert.equal(toString(target, length), s); - decoder.clear(); - } - }); +describe('text encodings', () => { + it('stringFromCodePoint/utf32ToString', () => { + const s = 'abcdefg'; + const data = new Uint32Array(s.length); + for (let i = 0; i < s.length; ++i) { + data[i] = s.charCodeAt(i); + assert.equal(stringFromCodePoint(data[i]), s[i]); + } + assert.equal(utf32ToString(data), s); }); - describe('stream handling', () => { - it('surrogates mixed advance by 1', () => { - const decoder = new StringToUtf32(); - const target = new Uint32Array(5); - const input = 'Ä€𝄞Ö𝄞€Ü𝄞€'; - let decoded = ''; - for (let i = 0; i < input.length; ++i) { - const written = decoder.decode(input[i], target); - decoded += toString(target, written); - } - assert(decoded, 'Ä€𝄞Ö𝄞€Ü𝄞€'); + + describe('StringToUtf32 Decoder', () => { + describe('full codepoint test', () => { + it('0..65535', () => { + const decoder = new StringToUtf32(); + const target = new Uint32Array(5); + for (let i = 0; i < 65536; ++i) { + // skip surrogate pairs + if (i >= 0xD800 && i <= 0xDFFF) { + continue; + } + const length = decoder.decode(String.fromCharCode(i), target); + assert.equal(length, 1); + assert.equal(target[0], i); + assert.equal(utf32ToString(target, 0, length), String.fromCharCode(i)); + decoder.clear(); + } + }); + it('65536..0x10FFFF (surrogates)', function(): void { + this.timeout(20000); + const decoder = new StringToUtf32(); + const target = new Uint32Array(5); + for (let i = 65536; i < 0x10FFFF; ++i) { + const codePoint = i - 0x10000; + const s = String.fromCharCode((codePoint >> 10) + 0xD800) + String.fromCharCode((codePoint % 0x400) + 0xDC00); + const length = decoder.decode(s, target); + assert.equal(length, 1); + assert.equal(target[0], i); + assert.equal(utf32ToString(target, 0, length), s); + decoder.clear(); + } + }); + }); + + describe('stream handling', () => { + it('surrogates mixed advance by 1', () => { + const decoder = new StringToUtf32(); + const target = new Uint32Array(5); + const input = 'Ä€𝄞Ö𝄞€Ü𝄞€'; + let decoded = ''; + for (let i = 0; i < input.length; ++i) { + const written = decoder.decode(input[i], target); + decoded += utf32ToString(target, written); + } + assert(decoded, 'Ä€𝄞Ö𝄞€Ü𝄞€'); + }); }); }); }); diff --git a/src/core/input/TextDecoder.ts b/src/core/input/TextDecoder.ts index 77e6971c..f83959e6 100644 --- a/src/core/input/TextDecoder.ts +++ b/src/core/input/TextDecoder.ts @@ -2,6 +2,7 @@ * Copyright (c) 2019 The xterm.js authors. All rights reserved. * @license MIT */ +import { TypedArray } from '../../common/Types'; /** * StringToUtf32 - decodes UTF16 sequences into UTF32 codepoints. @@ -73,15 +74,31 @@ export class StringToUtf32 { } /** - * Polyfill - Convert UTF32 codepoint into JS string. + * Convert UTF32 codepoint into JS string. */ export function stringFromCodePoint(codePoint: number): string { - if ((String as any).fromCodePoint) { - return (String as any).fromCodePoint(codePoint); - } if (codePoint > 0xFFFF) { codePoint -= 0x10000; return String.fromCharCode((codePoint >> 10) + 0xD800) + String.fromCharCode((codePoint % 0x400) + 0xDC00); } return String.fromCharCode(codePoint); } + +/** + * Convert UTF32 char codes into JS string. + * Basically the same as `stringFromCodePoint` but for multiple codepoints + * in a loop (which is a lot faster). + */ +export function utf32ToString(data: T, start: number = 0, end: number = data.length): string { + let result = ''; + let cp; + for (let i = start; i < end; ++i) { + if ((cp = data[i]) > 0xFFFF) { + cp -= 0x10000; + result += String.fromCharCode((cp >> 10) + 0xD800) + String.fromCharCode((cp % 0x400) + 0xDC00); + } else { + result += String.fromCharCode(cp); + } + } + return result; +} From 87e97897488c4bbffbed41163484a931b61d7926 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Breitbart?= Date: Fri, 25 Jan 2019 18:16:01 +0100 Subject: [PATCH 16/41] more explicit docs for DCS parsers --- src/Types.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Types.ts b/src/Types.ts index 7caa1335..5bd97b10 100644 --- a/src/Types.ts +++ b/src/Types.ts @@ -452,14 +452,22 @@ export interface IParsingState { * DCS handler signature for EscapeSequenceParser. * EscapeSequenceParser handles DCS commands via separate * subparsers that get hook/unhooked and can handle -* arbitrary amount of print data. +* arbitrary amount of data. +* * On entering a DSC sequence `hook` is called by * `EscapeSequenceParser`. Use it to initialize or reset * states needed to handle the current DCS sequence. +* Note: A DCS parser is only instantiated once, therefore +* you cannot rely on the ctor to reinitialize state. +* * EscapeSequenceParser will call `put` several times if the -* parsed string got splitted, therefore you might have to collect -* `data` until `unhook` is called. `unhook` marks the end -* of the current DCS sequence. +* parsed data got split, therefore you might have to collect +* `data` until `unhook` is called. +* Note: `data` is borrowed, if you cannot process the data +* in chunks you have to copy it, doing otherwise will lead to +* data losses or corruption. +* +* `unhook` marks the end of the current DCS sequence. */ export interface IDcsHandler { hook(collect: string, params: number[], flag: number): void; From 2a1f25e7dce1254dde2b24e74bfbd2c97fc8e29c Mon Sep 17 00:00:00 2001 From: Thomas Zilz Date: Sun, 27 Jan 2019 13:15:37 +0100 Subject: [PATCH 17/41] Make textarea positioning work with css transformations on parent elements --- src/Terminal.ts | 6 +++--- src/ui/Clipboard.ts | 44 +++++++++++++++++++++++++------------------- 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/src/Terminal.ts b/src/Terminal.ts index 4ddd0431..c1fc8ec8 100644 --- a/src/Terminal.ts +++ b/src/Terminal.ts @@ -566,12 +566,12 @@ export class Terminal extends EventEmitter implements ITerminal, IDisposable, II // Firefox doesn't appear to fire the contextmenu event on right click this.register(addDisposableDomListener(this.element, 'mousedown', (event: MouseEvent) => { if (event.button === 2) { - rightClickHandler(event, this.textarea, this.selectionManager, this.options.rightClickSelectsWord); + rightClickHandler(event, this, this.selectionManager, this.options.rightClickSelectsWord); } })); } else { this.register(addDisposableDomListener(this.element, 'contextmenu', (event: MouseEvent) => { - rightClickHandler(event, this.textarea, this.selectionManager, this.options.rightClickSelectsWord); + rightClickHandler(event, this, this.selectionManager, this.options.rightClickSelectsWord); })); } @@ -583,7 +583,7 @@ export class Terminal extends EventEmitter implements ITerminal, IDisposable, II // that the regular click event doesn't fire for the middle mouse button. this.register(addDisposableDomListener(this.element, 'auxclick', (event: MouseEvent) => { if (event.button === 1) { - moveTextAreaUnderMouseCursor(event, this.textarea); + moveTextAreaUnderMouseCursor(event, this); } })); } diff --git a/src/ui/Clipboard.ts b/src/ui/Clipboard.ts index b1acba9d..2570a8b1 100644 --- a/src/ui/Clipboard.ts +++ b/src/ui/Clipboard.ts @@ -85,26 +85,32 @@ export function pasteHandler(ev: ClipboardEvent, term: ITerminal): void { * @param ev The original right click event to be handled. * @param textarea The terminal's textarea. */ -export function moveTextAreaUnderMouseCursor(ev: MouseEvent, textarea: HTMLTextAreaElement): void { - // Bring textarea at the cursor position - textarea.style.position = 'fixed'; - textarea.style.width = '20px'; - textarea.style.height = '20px'; - textarea.style.left = (ev.clientX - 10) + 'px'; - textarea.style.top = (ev.clientY - 10) + 'px'; - textarea.style.zIndex = '1000'; +export function moveTextAreaUnderMouseCursor(ev: MouseEvent, term: ITerminal): void { - textarea.focus(); + // Calculate textarea position relative to the screen element + const pos = term.screenElement.getBoundingClientRect(); + const left = ev.clientX - pos.left - 10; + const top = ev.clientY - pos.top - 10; + + // Bring textarea at the cursor position + term.textarea.style.position = 'absolute'; + term.textarea.style.width = '20px'; + term.textarea.style.height = '20px'; + term.textarea.style.left = `${left}px`; + term.textarea.style.top = `${top}px`; + term.textarea.style.zIndex = '1000'; + + term.textarea.focus(); // Reset the terminal textarea's styling // Timeout needs to be long enough for click event to be handled. setTimeout(() => { - textarea.style.position = null; - textarea.style.width = null; - textarea.style.height = null; - textarea.style.left = null; - textarea.style.top = null; - textarea.style.zIndex = null; + term.textarea.style.position = null; + term.textarea.style.width = null; + term.textarea.style.height = null; + term.textarea.style.left = null; + term.textarea.style.top = null; + term.textarea.style.zIndex = null; }, 200); } @@ -115,14 +121,14 @@ export function moveTextAreaUnderMouseCursor(ev: MouseEvent, textarea: HTMLTextA * @param selectionManager The terminal's selection manager. * @param shouldSelectWord If true and there is no selection the current word will be selected */ -export function rightClickHandler(ev: MouseEvent, textarea: HTMLTextAreaElement, selectionManager: ISelectionManager, shouldSelectWord: boolean): void { - moveTextAreaUnderMouseCursor(ev, textarea); +export function rightClickHandler(ev: MouseEvent, term: ITerminal, selectionManager: ISelectionManager, shouldSelectWord: boolean): void { + moveTextAreaUnderMouseCursor(ev, term); if (shouldSelectWord && !selectionManager.isClickInSelection(ev)) { selectionManager.selectWordAtCursor(ev); } // Get textarea ready to copy from the context menu - textarea.value = selectionManager.selectionText; - textarea.select(); + term.textarea.value = selectionManager.selectionText; + term.textarea.select(); } From 90a41a113a16c5e445b4bf56556a1ec475983c8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Breitbart?= Date: Sun, 27 Jan 2019 13:22:34 +0100 Subject: [PATCH 18/41] review changes --- src/core/input/TextDecoder.ts | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/core/input/TextDecoder.ts b/src/core/input/TextDecoder.ts index f83959e6..1ff00940 100644 --- a/src/core/input/TextDecoder.ts +++ b/src/core/input/TextDecoder.ts @@ -2,7 +2,6 @@ * Copyright (c) 2019 The xterm.js authors. All rights reserved. * @license MIT */ -import { TypedArray } from '../../common/Types'; /** * StringToUtf32 - decodes UTF16 sequences into UTF32 codepoints. @@ -78,6 +77,7 @@ export class StringToUtf32 { */ export function stringFromCodePoint(codePoint: number): string { if (codePoint > 0xFFFF) { + // UTF32 to UTF16 conversion (see comments in utf32ToString) codePoint -= 0x10000; return String.fromCharCode((codePoint >> 10) + 0xD800) + String.fromCharCode((codePoint % 0x400) + 0xDC00); } @@ -89,15 +89,20 @@ export function stringFromCodePoint(codePoint: number): string { * Basically the same as `stringFromCodePoint` but for multiple codepoints * in a loop (which is a lot faster). */ -export function utf32ToString(data: T, start: number = 0, end: number = data.length): string { +export function utf32ToString(data: Uint32Array, start: number = 0, end: number = data.length): string { let result = ''; - let cp; for (let i = start; i < end; ++i) { - if ((cp = data[i]) > 0xFFFF) { - cp -= 0x10000; - result += String.fromCharCode((cp >> 10) + 0xD800) + String.fromCharCode((cp % 0x400) + 0xDC00); + let codepoint = data[i]; + if (codepoint > 0xFFFF) { + // JS string are encoded as UTF16, thus a non BMP codepoint gets converted into a surrogate pair + // conversion rules: + // - subtract 0x10000 from code point, leaving a 20 bit number + // - add high 10 bits to 0xD800 --> first surrogate + // - add low 10 bits to 0xDC00 --> second surrogate + codepoint -= 0x10000; + result += String.fromCharCode((codepoint >> 10) + 0xD800) + String.fromCharCode((codepoint % 0x400) + 0xDC00); } else { - result += String.fromCharCode(cp); + result += String.fromCharCode(codepoint); } } return result; From e67bef69f49d967f030c35e8abbd50f186a0eb72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Breitbart?= Date: Sun, 27 Jan 2019 17:28:15 +0100 Subject: [PATCH 19/41] cleanup types --- src/common/TypedArrayUtils.ts | 6 +++++- src/common/Types.ts | 4 ---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/common/TypedArrayUtils.ts b/src/common/TypedArrayUtils.ts index 380dff35..54699835 100644 --- a/src/common/TypedArrayUtils.ts +++ b/src/common/TypedArrayUtils.ts @@ -2,7 +2,11 @@ * Copyright (c) 2018 The xterm.js authors. All rights reserved. * @license MIT */ -import { TypedArray } from './Types'; + +export type TypedArray = Uint8Array | Uint16Array | Uint32Array | Uint8ClampedArray + | Int8Array | Int16Array | Int32Array + | Float32Array | Float64Array; + /** * polyfill for TypedArray.fill diff --git a/src/common/Types.ts b/src/common/Types.ts index 29b5febb..8a416bf1 100644 --- a/src/common/Types.ts +++ b/src/common/Types.ts @@ -35,7 +35,3 @@ export interface ICircularList extends IEventEmitter { trimStart(count: number): void; shiftElements(start: number, count: number, offset: number): void; } - -export type TypedArray = Uint8Array | Uint16Array | Uint32Array | Uint8ClampedArray - | Int8Array | Int16Array | Int32Array - | Float32Array | Float64Array; From 358d70d7218df418c8e10b9a6aa43ccf9786dccb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Breitbart?= Date: Sun, 27 Jan 2019 17:34:58 +0100 Subject: [PATCH 20/41] cleanup gitignore --- .gitignore | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.gitignore b/.gitignore index 2ee021f8..b50ab2d9 100644 --- a/.gitignore +++ b/.gitignore @@ -23,7 +23,3 @@ coverage/ # Keep bundled code out of Git dist/ demo/dist/ - -# dont pullin other files from .vscode than launch.json -.vscode/ -!.vscode/launch.json From fc1692b3eb10cacb52284dd11e7e343c40ace716 Mon Sep 17 00:00:00 2001 From: Nikita Chuklinov Date: Wed, 30 Jan 2019 20:34:16 +0300 Subject: [PATCH 21/41] clear state if selection doesn't exists --- src/renderer/SelectionRenderLayer.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/renderer/SelectionRenderLayer.ts b/src/renderer/SelectionRenderLayer.ts index 81782ee8..fc16a2fb 100644 --- a/src/renderer/SelectionRenderLayer.ts +++ b/src/renderer/SelectionRenderLayer.ts @@ -55,6 +55,7 @@ export class SelectionRenderLayer extends BaseRenderLayer { // Selection does not exist if (!start || !end) { + this._clearState(); return; } From 33e46682b1a8cbb7bc0946749adee2b1b1113ebd Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Wed, 30 Jan 2019 15:18:28 -0800 Subject: [PATCH 22/41] Fix various problems with reflow - No longer reflow lines where the full unwrapped line contains the cursor - Add guards to prevent y and ybase becoming invalid values Part of Microsoft/vscode#67364 Fixes #1910 --- src/Buffer.ts | 44 +++++++++++++++++++++++++++++--------------- src/BufferReflow.ts | 14 ++++++++++---- 2 files changed, 39 insertions(+), 19 deletions(-) diff --git a/src/Buffer.ts b/src/Buffer.ts index 52d9572d..bc2d5f20 100644 --- a/src/Buffer.ts +++ b/src/Buffer.ts @@ -3,13 +3,13 @@ * @license MIT */ -import { CircularList, IInsertEvent, IDeleteEvent } from './common/CircularList'; -import { CharData, ITerminal, IBuffer, IBufferLine, BufferIndex, IBufferStringIterator, IBufferStringIteratorResult } from './Types'; -import { EventEmitter } from './common/EventEmitter'; import { IMarker } from 'xterm'; import { BufferLine } from './BufferLine'; +import { reflowLargerApplyNewLayout, reflowLargerCreateNewLayout, reflowLargerGetLinesToRemove, reflowSmallerGetNewLineLengths } from './BufferReflow'; +import { CircularList, IDeleteEvent, IInsertEvent } from './common/CircularList'; +import { EventEmitter } from './common/EventEmitter'; import { DEFAULT_COLOR } from './renderer/atlas/Types'; -import { reflowSmallerGetNewLineLengths, reflowLargerGetLinesToRemove, reflowLargerCreateNewLayout, reflowLargerApplyNewLayout } from './BufferReflow'; +import { BufferIndex, CharData, IBuffer, IBufferLine, IBufferStringIterator, IBufferStringIteratorResult, ITerminal } from './Types'; export const DEFAULT_ATTR = (0 << 18) | (DEFAULT_COLOR << 9) | (256 << 0); export const CHAR_DATA_ATTR_INDEX = 0; @@ -212,7 +212,7 @@ export class Buffer implements IBuffer { this.scrollBottom = newRows - 1; if (this._hasScrollback) { - this._reflow(newCols); + this._reflow(newCols, newRows); // Trim the end of the line off if cols shrunk if (this._cols > newCols) { @@ -226,7 +226,7 @@ export class Buffer implements IBuffer { this._rows = newRows; } - private _reflow(newCols: number): void { + private _reflow(newCols: number, newRows: number): void { if (this._cols === newCols) { return; } @@ -235,12 +235,12 @@ export class Buffer implements IBuffer { if (newCols > this._cols) { this._reflowLarger(newCols); } else { - this._reflowSmaller(newCols); + this._reflowSmaller(newCols, newRows); } } private _reflowLarger(newCols: number): void { - const toRemove: number[] = reflowLargerGetLinesToRemove(this.lines, newCols); + const toRemove: number[] = reflowLargerGetLinesToRemove(this.lines, newCols, this.ybase + this.y); if (toRemove.length > 0) { const newLayoutResult = reflowLargerCreateNewLayout(this.lines, toRemove); reflowLargerApplyNewLayout(this.lines, newLayoutResult.layout); @@ -253,9 +253,13 @@ export class Buffer implements IBuffer { let viewportAdjustments = countRemoved; while (viewportAdjustments-- > 0) { if (this.ybase === 0) { - this.y--; - // Add an extra row at the bottom of the viewport - this.lines.push(new BufferLine(newCols, FILL_CHAR_DATA)); + if (this.y > 0) { + this.y--; + } + if (this.lines.length < this._rows) { + // Add an extra row at the bottom of the viewport + this.lines.push(new BufferLine(newCols, FILL_CHAR_DATA)); + } } else { if (this.ydisp === this.ybase) { this.ydisp--; @@ -265,7 +269,7 @@ export class Buffer implements IBuffer { } } - private _reflowSmaller(newCols: number): void { + private _reflowSmaller(newCols: number, newRows: number): void { // Gather all BufferLines that need to be inserted into the Buffer here so that they can be // batched up and only committed once const toInsert = []; @@ -285,6 +289,13 @@ export class Buffer implements IBuffer { wrappedLines.unshift(nextLine); } + // If these lines contain the cursor don't touch them, the program will handle fixing up + // wrapped lines with the cursor + const absoluteY = this.ybase + this.y; + if (absoluteY >= y && absoluteY < y + wrappedLines.length) { + continue; + } + const lastLineLength = wrappedLines[wrappedLines.length - 1].getTrimmedLength(); const destLineLengths = reflowSmallerGetNewLineLengths(wrappedLines, this._cols, newCols); const linesToAdd = destLineLengths.length - wrappedLines.length; @@ -357,10 +368,13 @@ export class Buffer implements IBuffer { this.ydisp++; } } else { - if (this.ybase === this.ydisp) { - this.ydisp++; + // Ensure ybase does not exceed its maximum value + if (this.ybase < Math.min(this.lines.maxLength, this.lines.length + countToInsert) - newRows) { + if (this.ybase === this.ydisp) { + this.ydisp++; + } + this.ybase++; } - this.ybase++; } } } diff --git a/src/BufferReflow.ts b/src/BufferReflow.ts index 59934e46..24ab69e6 100644 --- a/src/BufferReflow.ts +++ b/src/BufferReflow.ts @@ -3,10 +3,10 @@ * @license MIT */ +import { FILL_CHAR_DATA } from './Buffer'; import { BufferLine } from './BufferLine'; import { CircularList, IDeleteEvent } from './common/CircularList'; import { IBufferLine } from './Types'; -import { FILL_CHAR_DATA } from './Buffer'; export interface INewLayoutResult { layout: number[]; @@ -19,7 +19,7 @@ export interface INewLayoutResult { * @param lines The buffer lines. * @param newCols The columns after resize. */ -export function reflowLargerGetLinesToRemove(lines: CircularList, newCols: number): number[] { +export function reflowLargerGetLinesToRemove(lines: CircularList, newCols: number, bufferAbsoluteY: number): number[] { // Gather all BufferLines that need to be removed from the Buffer here so that they can be // batched up and only committed once const toRemove: number[] = []; @@ -39,6 +39,13 @@ export function reflowLargerGetLinesToRemove(lines: CircularList, n nextLine = lines.get(++i) as BufferLine; } + // If these lines contain the cursor don't touch them, the program will handle fixing up wrapped + // lines with the cursor + if (bufferAbsoluteY >= y && bufferAbsoluteY < i) { + y += wrappedLines.length - 1; + continue; + } + // Copy buffer data to new locations let destLineIndex = 0; let destCol = wrappedLines[destLineIndex].getTrimmedLength(); @@ -64,7 +71,7 @@ export function reflowLargerGetLinesToRemove(lines: CircularList, n } // Make sure the last cell isn't wide, if it is copy it to the current dest - if (destCol === 0) { + if (destCol === 0 && destLineIndex !== 0) { if (wrappedLines[destLineIndex - 1].getWidth(newCols - 1) === 2) { wrappedLines[destLineIndex].copyCellsFrom(wrappedLines[destLineIndex - 1], newCols - 1, destCol++, 1, false); // Null out the end of the last row @@ -166,7 +173,6 @@ export function reflowLargerApplyNewLayout(lines: CircularList, new */ export function reflowSmallerGetNewLineLengths(wrappedLines: BufferLine[], oldCols: number, newCols: number): number[] { const newLineLengths: number[] = []; - const cellsNeeded = wrappedLines.map(l => l.getTrimmedLength()).reduce((p, c) => p + c); // Use srcCol and srcLine to find the new wrapping point, use that to get the cellsAvailable and From db31a44da3da0100ae26d0fb6a20eeecf61881f6 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Wed, 30 Jan 2019 22:14:47 -0800 Subject: [PATCH 23/41] Fix tests --- src/Buffer.test.ts | 66 +++++++++++++++++++++++++++------------------- 1 file changed, 39 insertions(+), 27 deletions(-) diff --git a/src/Buffer.test.ts b/src/Buffer.test.ts index a56fd63a..2a244472 100644 --- a/src/Buffer.test.ts +++ b/src/Buffer.test.ts @@ -265,6 +265,7 @@ describe('Buffer', () => { const char = String.fromCharCode(code); firstLine.set(i, [null, char, 1, code]); } + buffer.y = 1; assert.equal(buffer.lines.get(0).length, 5); assert.equal(buffer.lines.get(0).translateToString(), 'abcde'); buffer.resize(1, 10); @@ -296,7 +297,7 @@ describe('Buffer', () => { buffer.fillViewportRows(); terminal.options.scrollback = 1; buffer.resize(10, 5); - const lastLine = buffer.lines.get(4); + const lastLine = buffer.lines.get(3); for (let i = 0; i < 10; i++) { const code = 'a'.charCodeAt(0) + i; const char = String.fromCharCode(code); @@ -308,27 +309,27 @@ describe('Buffer', () => { assert.equal(buffer.y, 4); assert.equal(buffer.ybase, 1); assert.equal(buffer.lines.length, 6); - assert.equal(buffer.lines.get(0).translateToString(), ' '); - assert.equal(buffer.lines.get(1).translateToString(), 'ab'); - assert.equal(buffer.lines.get(2).translateToString(), 'cd'); - assert.equal(buffer.lines.get(3).translateToString(), 'ef'); - assert.equal(buffer.lines.get(4).translateToString(), 'gh'); - assert.equal(buffer.lines.get(5).translateToString(), 'ij'); + assert.equal(buffer.lines.get(0).translateToString(), 'ab'); + assert.equal(buffer.lines.get(1).translateToString(), 'cd'); + assert.equal(buffer.lines.get(2).translateToString(), 'ef'); + assert.equal(buffer.lines.get(3).translateToString(), 'gh'); + assert.equal(buffer.lines.get(4).translateToString(), 'ij'); + assert.equal(buffer.lines.get(5).translateToString(), ' '); buffer.resize(1, 5); assert.equal(buffer.y, 4); assert.equal(buffer.ybase, 1); assert.equal(buffer.lines.length, 6); - assert.equal(buffer.lines.get(0).translateToString(), 'e'); - assert.equal(buffer.lines.get(1).translateToString(), 'f'); - assert.equal(buffer.lines.get(2).translateToString(), 'g'); - assert.equal(buffer.lines.get(3).translateToString(), 'h'); - assert.equal(buffer.lines.get(4).translateToString(), 'i'); - assert.equal(buffer.lines.get(5).translateToString(), 'j'); + assert.equal(buffer.lines.get(0).translateToString(), 'f'); + assert.equal(buffer.lines.get(1).translateToString(), 'g'); + assert.equal(buffer.lines.get(2).translateToString(), 'h'); + assert.equal(buffer.lines.get(3).translateToString(), 'i'); + assert.equal(buffer.lines.get(4).translateToString(), 'j'); + assert.equal(buffer.lines.get(5).translateToString(), ' '); buffer.resize(10, 5); - assert.equal(buffer.y, 0); + assert.equal(buffer.y, 1); assert.equal(buffer.ybase, 0); assert.equal(buffer.lines.length, 5); - assert.equal(buffer.lines.get(0).translateToString(), 'efghij '); + assert.equal(buffer.lines.get(0).translateToString(), 'fghij '); assert.equal(buffer.lines.get(1).translateToString(), ' '); assert.equal(buffer.lines.get(2).translateToString(), ' '); assert.equal(buffer.lines.get(3).translateToString(), ' '); @@ -339,6 +340,7 @@ describe('Buffer', () => { // 3+ lines removed on a reflow actually remove the right lines buffer.fillViewportRows(); buffer.resize(10, 10); + buffer.y = 2; const firstLine = buffer.lines.get(0); const secondLine = buffer.lines.get(1); for (let i = 0; i < 10; i++) { @@ -358,8 +360,8 @@ describe('Buffer', () => { assert.equal(buffer.lines.get(i).translateToString(), ' '); } buffer.resize(2, 10); - assert.equal(buffer.ybase, 0); - assert.equal(buffer.lines.length, 10); + assert.equal(buffer.ybase, 1); + assert.equal(buffer.lines.length, 11); assert.equal(buffer.lines.get(0).translateToString(), 'ab'); assert.equal(buffer.lines.get(1).translateToString(), 'cd'); assert.equal(buffer.lines.get(2).translateToString(), 'ef'); @@ -370,7 +372,10 @@ describe('Buffer', () => { assert.equal(buffer.lines.get(7).translateToString(), '45'); assert.equal(buffer.lines.get(8).translateToString(), '67'); assert.equal(buffer.lines.get(9).translateToString(), '89'); + assert.equal(buffer.lines.get(10).translateToString(), ' '); buffer.resize(10, 10); + assert.equal(buffer.ybase, 0); + assert.equal(buffer.lines.length, 10); assert.equal(buffer.lines.get(0).translateToString(), 'abcdefghij'); assert.equal(buffer.lines.get(1).translateToString(), '0123456789'); for (let i = 2; i < 10; i++) { @@ -379,22 +384,23 @@ describe('Buffer', () => { }); it('should transfer combined char data over to reflowed lines', () => { buffer.fillViewportRows(); - buffer.resize(4, 2); + buffer.resize(4, 3); + buffer.y = 2; const firstLine = buffer.lines.get(0); firstLine.set(0, [ null, 'a', 1, 'a'.charCodeAt(0) ]); firstLine.set(1, [ null, 'b', 1, 'b'.charCodeAt(0) ]); firstLine.set(2, [ null, 'c', 1, 'c'.charCodeAt(0) ]); firstLine.set(3, [ null, '😁', 1, '😁'.charCodeAt(0) ]); - assert.equal(buffer.lines.length, 2); + assert.equal(buffer.lines.length, 3); assert.equal(buffer.lines.get(0).translateToString(), 'abc😁'); assert.equal(buffer.lines.get(1).translateToString(), ' '); - buffer.resize(2, 2); + buffer.resize(2, 3); assert.equal(buffer.lines.get(0).translateToString(), 'ab'); assert.equal(buffer.lines.get(1).translateToString(), 'c😁'); }); it('should adjust markers when reflowing', () => { buffer.fillViewportRows(); - buffer.resize(10, 15); + buffer.resize(10, 16); for (let i = 0; i < 10; i++) { const code = 'a'.charCodeAt(0) + i; const char = String.fromCharCode(code); @@ -410,6 +416,7 @@ describe('Buffer', () => { const char = String.fromCharCode(code); buffer.lines.get(2).set(i, [null, char, 1, code]); } + buffer.y = 3; // Buffer: // abcdefghij // 0123456789 @@ -423,7 +430,7 @@ describe('Buffer', () => { assert.equal(firstMarker.line, 0); assert.equal(secondMarker.line, 1); assert.equal(thirdMarker.line, 2); - buffer.resize(2, 15); + buffer.resize(2, 16); assert.equal(buffer.lines.get(0).translateToString(), 'ab'); assert.equal(buffer.lines.get(1).translateToString(), 'cd'); assert.equal(buffer.lines.get(2).translateToString(), 'ef'); @@ -442,7 +449,7 @@ describe('Buffer', () => { assert.equal(firstMarker.line, 0, 'first marker should remain unchanged'); assert.equal(secondMarker.line, 5, 'second marker should be shifted since the first line wrapped'); assert.equal(thirdMarker.line, 10, 'third marker should be shifted since the first and second lines wrapped'); - buffer.resize(10, 15); + buffer.resize(10, 16); assert.equal(buffer.lines.get(0).translateToString(), 'abcdefghij'); assert.equal(buffer.lines.get(1).translateToString(), '0123456789'); assert.equal(buffer.lines.get(2).translateToString(), 'klmnopqrst'); @@ -456,7 +463,7 @@ describe('Buffer', () => { it('should dispose markers whose rows are trimmed during a reflow', () => { buffer.fillViewportRows(); terminal.options.scrollback = 1; - buffer.resize(10, 10); + buffer.resize(10, 11); for (let i = 0; i < 10; i++) { const code = 'a'.charCodeAt(0) + i; const char = String.fromCharCode(code); @@ -472,6 +479,7 @@ describe('Buffer', () => { const char = String.fromCharCode(code); buffer.lines.get(2).set(i, [null, char, 1, code]); } + buffer.y = 10; // Buffer: // abcdefghij // 0123456789 @@ -479,14 +487,14 @@ describe('Buffer', () => { const firstMarker = buffer.addMarker(0); const secondMarker = buffer.addMarker(1); const thirdMarker = buffer.addMarker(2); - buffer.y = 2; + buffer.y = 3; assert.equal(buffer.lines.get(0).translateToString(), 'abcdefghij'); assert.equal(buffer.lines.get(1).translateToString(), '0123456789'); assert.equal(buffer.lines.get(2).translateToString(), 'klmnopqrst'); assert.equal(firstMarker.line, 0); assert.equal(secondMarker.line, 1); assert.equal(thirdMarker.line, 2); - buffer.resize(2, 10); + buffer.resize(2, 11); assert.equal(buffer.lines.get(0).translateToString(), 'ij'); assert.equal(buffer.lines.get(1).translateToString(), '01'); assert.equal(buffer.lines.get(2).translateToString(), '23'); @@ -503,7 +511,7 @@ describe('Buffer', () => { assert.equal(firstMarker.isDisposed, true, 'first marker was trimmed'); assert.equal(secondMarker.isDisposed, false); assert.equal(thirdMarker.isDisposed, false); - buffer.resize(10, 10); + buffer.resize(10, 11); assert.equal(buffer.lines.get(0).translateToString(), 'ij '); assert.equal(buffer.lines.get(1).translateToString(), '0123456789'); assert.equal(buffer.lines.get(2).translateToString(), 'klmnopqrst'); @@ -513,6 +521,7 @@ describe('Buffer', () => { it('should wrap wide characters correctly when reflowing larger', () => { buffer.fillViewportRows(); buffer.resize(12, 10); + buffer.y = 2; for (let i = 0; i < 12; i += 4) { buffer.lines.get(0).set(i, [null, '汉', 2, '汉'.charCodeAt(0)]); buffer.lines.get(1).set(i, [null, '汉', 2, '汉'.charCodeAt(0)]); @@ -547,6 +556,7 @@ describe('Buffer', () => { it('should wrap wide characters correctly when reflowing smaller', () => { buffer.fillViewportRows(); buffer.resize(12, 10); + buffer.y = 2; for (let i = 0; i < 12; i += 4) { buffer.lines.get(0).set(i, [null, '汉', 2, '汉'.charCodeAt(0)]); buffer.lines.get(1).set(i, [null, '汉', 2, '汉'.charCodeAt(0)]); @@ -937,6 +947,7 @@ describe('Buffer', () => { describe('&& ydisp === ybase', () => { it('should trim lines and keep ydisp = ybase', () => { buffer.ydisp = 10; + buffer.y = 13; buffer.resize(2, 10); assert.equal(buffer.ydisp, 10); assert.equal(buffer.ybase, 10); @@ -962,6 +973,7 @@ describe('Buffer', () => { describe('&& ydisp !== ybase', () => { it('should trim lines and not change ydisp', () => { buffer.ydisp = 5; + buffer.y = 13; buffer.resize(2, 10); assert.equal(buffer.ydisp, 5); assert.equal(buffer.ybase, 10); From 7b7f85e3cf0976b157a64c727276ad3b9d807e4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Breitbart?= Date: Thu, 31 Jan 2019 22:56:55 +0100 Subject: [PATCH 24/41] add test for correct tab handling, docs --- src/Buffer.test.ts | 7 +++++++ src/Buffer.ts | 4 +++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Buffer.test.ts b/src/Buffer.test.ts index 1ef2271f..53de19b7 100644 --- a/src/Buffer.test.ts +++ b/src/Buffer.test.ts @@ -1312,6 +1312,13 @@ describe('Buffer', () => { terminal.buffer.lines.get(bufferIndex[0]).get(bufferIndex[1])[CHAR_DATA_CHAR_INDEX]); } }); + + it('should handle \t in lines correctly', () => { + const input = '\thttps://google.de'; + terminal.writeSync(input); + const s = terminal.buffer.iterator(true).next().content; + assert.equal(s, Array(terminal.getOption('tabStopWidth') + 1).join(' ') + 'https://google.de'); + }); }); describe('BufferStringIterator', function(): void { it('iterator does not overflow buffer limits', function(): void { diff --git a/src/Buffer.ts b/src/Buffer.ts index 8eddf73f..7f4b6071 100644 --- a/src/Buffer.ts +++ b/src/Buffer.ts @@ -458,7 +458,9 @@ export class Buffer implements IBuffer { const length = (trimRight) ? line.getTrimmedLength() : line.length; for (let i = 0; i < length; ++i) { if (line.get(i)[CHAR_DATA_WIDTH_INDEX]) { - stringIndex -= line.get(i)[CHAR_DATA_CHAR_INDEX].length || 1; // WHITESPACE_CELL_CHAR.length + // empty cells report a string length of 0, but get replaced + // with a whitespace in translateToString, thus replace with 1 + stringIndex -= line.get(i)[CHAR_DATA_CHAR_INDEX].length || 1; } if (stringIndex < 0) { return [lineIndex, i]; From 828f14f2abd58f42497903e745fbfc0af02c34bd Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Thu, 31 Jan 2019 14:00:39 -0800 Subject: [PATCH 25/41] Improve docs on Terminal.resize --- typings/xterm.d.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/typings/xterm.d.ts b/typings/xterm.d.ts index dc9ebbae..d813bc5f 100644 --- a/typings/xterm.d.ts +++ b/typings/xterm.d.ts @@ -443,7 +443,9 @@ declare module 'xterm' { addDisposableListener(type: string, handler: (...args: any[]) => void): IDisposable; /** - * Resizes the terminal. + * Resizes the terminal. It's best practice to debounce calls to resize, + * this will help ensure that the pty can respond to the resize event + * before another one occurs. * @param x The number of columns to resize to. * @param y The number of rows to resize to. */ From 54a1319f57c4a8f38f91209705d4d85f402940b0 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Fri, 1 Feb 2019 18:23:10 -0800 Subject: [PATCH 26/41] v3.11.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 5ed3a4b8..fa9cc070 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "xterm", "description": "Full xterm terminal, in your browser", - "version": "3.10.0", + "version": "3.11.0", "main": "lib/public/Terminal.js", "types": "typings/xterm.d.ts", "repository": "https://github.com/xtermjs/xterm.js", From 1829cb7609029405230e69803c0262f2768a40ff Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Fri, 1 Feb 2019 19:28:07 -0800 Subject: [PATCH 27/41] Add roadmap wiki link to README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 35ccc9b6..f22c6c00 100644 --- a/README.md +++ b/README.md @@ -163,7 +163,7 @@ Do you use xterm.js in your application as well? Please [open a Pull Request](ht Xterm.js follows a monthly release cycle roughly. -All current and past releases are available on this repo's [Releases page](https://github.com/sourcelair/xterm.js/releases), while a rough roadmap is available by looking through [Milestones](https://github.com/sourcelair/xterm.js/milestones). +All current and past releases are available on this repo's [Releases page](https://github.com/sourcelair/xterm.js/releases), you can view the [high-level roadmap on the wiki](https://github.com/xtermjs/xterm.js/wiki/Roadmap) and see what we're working on now by looking through [Milestones](https://github.com/sourcelair/xterm.js/milestones). ## Contributing From f3aac10bbd32efb4c5f648d7fe7b7cdd10a25a55 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Sun, 3 Feb 2019 10:46:20 -0800 Subject: [PATCH 28/41] Update license year --- LICENSE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LICENSE b/LICENSE index 28adbdad..4472336c 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2017-2018, The xterm.js authors (https://github.com/xtermjs/xterm.js) +Copyright (c) 2017-2019, The xterm.js authors (https://github.com/xtermjs/xterm.js) Copyright (c) 2014-2016, SourceLair Private Company (https://www.sourcelair.com) Copyright (c) 2012-2013, Christopher Jeffrey (https://github.com/chjj/) From 69d3a4667f6d3ade4a89e71be066de0e69d82d16 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Mon, 4 Feb 2019 13:28:14 +0200 Subject: [PATCH 29/41] fix: Renderer: IntersectionObserver can produce more then 1 entry --- src/renderer/Renderer.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/renderer/Renderer.ts b/src/renderer/Renderer.ts index 02328877..b8ef87aa 100644 --- a/src/renderer/Renderer.ts +++ b/src/renderer/Renderer.ts @@ -70,7 +70,7 @@ export class Renderer extends EventEmitter implements IRenderer { // Detect whether IntersectionObserver is detected and enable renderer pause // and resume based on terminal visibility if so if ('IntersectionObserver' in window) { - const observer = new IntersectionObserver(e => this.onIntersectionChange(e[0]), { threshold: 0 }); + const observer = new IntersectionObserver(e => this.onIntersectionChange(e[e.length - 1]), { threshold: 0 }); observer.observe(this._terminal.element); this.register({ dispose: () => observer.disconnect() }); } From b88230db8ba0d54fb194f5b903215a53b017a743 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Tue, 5 Feb 2019 08:20:16 -0800 Subject: [PATCH 30/41] Make sure the viewport is filled when reflowing a row change Fixes #1926 --- src/Buffer.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Buffer.ts b/src/Buffer.ts index 7f4b6071..e40fa8b4 100644 --- a/src/Buffer.ts +++ b/src/Buffer.ts @@ -233,22 +233,22 @@ export class Buffer implements IBuffer { // Iterate through rows, ignore the last one as it cannot be wrapped if (newCols > this._cols) { - this._reflowLarger(newCols); + this._reflowLarger(newCols, newRows); } else { this._reflowSmaller(newCols, newRows); } } - private _reflowLarger(newCols: number): void { + private _reflowLarger(newCols: number, newRows: number): void { const toRemove: number[] = reflowLargerGetLinesToRemove(this.lines, newCols, this.ybase + this.y); if (toRemove.length > 0) { const newLayoutResult = reflowLargerCreateNewLayout(this.lines, toRemove); reflowLargerApplyNewLayout(this.lines, newLayoutResult.layout); - this._reflowLargerAdjustViewport(newCols, newLayoutResult.countRemoved); + this._reflowLargerAdjustViewport(newCols, newRows, newLayoutResult.countRemoved); } } - private _reflowLargerAdjustViewport(newCols: number, countRemoved: number): void { + private _reflowLargerAdjustViewport(newCols: number, newRows: number, countRemoved: number): void { // Adjust viewport based on number of items removed let viewportAdjustments = countRemoved; while (viewportAdjustments-- > 0) { @@ -256,7 +256,7 @@ export class Buffer implements IBuffer { if (this.y > 0) { this.y--; } - if (this.lines.length < this._rows) { + if (this.lines.length < newRows) { // Add an extra row at the bottom of the viewport this.lines.push(new BufferLine(newCols, FILL_CHAR_DATA)); } From d17dfbc73fc61d467ad079e4e0b90ae778687a88 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Fri, 8 Feb 2019 12:13:00 -0800 Subject: [PATCH 31/41] Cover a case when resizing smaller making y go out of bounds --- src/Buffer.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Buffer.ts b/src/Buffer.ts index e40fa8b4..d017aa7e 100644 --- a/src/Buffer.ts +++ b/src/Buffer.ts @@ -360,7 +360,7 @@ export class Buffer implements IBuffer { let viewportAdjustments = linesToAdd - trimmedLines; while (viewportAdjustments-- > 0) { if (this.ybase === 0) { - if (this.y < this._rows - 1) { + if (this.y < newRows - 1) { this.y++; this.lines.pop(); } else { From 84d7bfeacce308c0dc762e038a20ea36b03b44cd Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Mon, 11 Feb 2019 05:14:55 -0800 Subject: [PATCH 32/41] Remove font-family from .css file Fixes #1935 --- src/xterm.css | 1 - 1 file changed, 1 deletion(-) diff --git a/src/xterm.css b/src/xterm.css index 24cd475f..2e47b1a1 100644 --- a/src/xterm.css +++ b/src/xterm.css @@ -36,7 +36,6 @@ */ .xterm { - font-family: courier-new, courier, monospace; font-feature-settings: "liga" 0; position: relative; user-select: none; From 78426d8a12c56f40cf1c2d74fb53c23f2982ebb5 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Mon, 11 Feb 2019 06:05:14 -0800 Subject: [PATCH 33/41] Make the composition view use the same font as the terminal --- src/CompositionHelper.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/CompositionHelper.ts b/src/CompositionHelper.ts index 31ad866b..5f838c7a 100644 --- a/src/CompositionHelper.ts +++ b/src/CompositionHelper.ts @@ -203,6 +203,7 @@ export class CompositionHelper { this._compositionView.style.top = cursorTop + 'px'; this._compositionView.style.height = cellHeight + 'px'; this._compositionView.style.lineHeight = cellHeight + 'px'; + this._compositionView.style.fontFamily = this._terminal.options.fontFamily; // Sync the textarea to the exact position of the composition view so the IME knows where the // text is. const compositionViewBounds = this._compositionView.getBoundingClientRect(); From c934200c86ccaca419cae6aae210880a52791c46 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Mon, 11 Feb 2019 06:07:31 -0800 Subject: [PATCH 34/41] Also set font size --- src/CompositionHelper.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/CompositionHelper.ts b/src/CompositionHelper.ts index 5f838c7a..840bef55 100644 --- a/src/CompositionHelper.ts +++ b/src/CompositionHelper.ts @@ -204,6 +204,7 @@ export class CompositionHelper { this._compositionView.style.height = cellHeight + 'px'; this._compositionView.style.lineHeight = cellHeight + 'px'; this._compositionView.style.fontFamily = this._terminal.options.fontFamily; + this._compositionView.style.fontSize = this._terminal.options.fontSize + 'px'; // Sync the textarea to the exact position of the composition view so the IME knows where the // text is. const compositionViewBounds = this._compositionView.getBoundingClientRect(); From 9f29eeed2338dbc8861c65a1a35631fc34d980e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=8F=E8=99=AB?= Date: Tue, 19 Feb 2019 17:19:19 +0800 Subject: [PATCH 35/41] Update README.md (Jumpserver)[https://github.com/jumpserver/] use xterm.js --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index f22c6c00..59e406e9 100644 --- a/README.md +++ b/README.md @@ -108,6 +108,7 @@ Xterm.js is used in several world-class applications to provide great terminal e - [**SourceLair**](https://www.sourcelair.com/): In-browser IDE that provides its users with fully-featured Linux terminals based on xterm.js - [**Microsoft Visual Studio Code**](http://code.visualstudio.com/): Modern, versatile and powerful open source code editor that provides an integrated terminal based on xterm.js +- [**Jumpserver**](https://github.com/jumpserver/luna): Jumpserver Luna project, Jumpserver is a bastion server project, Luna use xterm.js for web terminal emulation. - [**ttyd**](https://github.com/tsl0922/ttyd): A command-line tool for sharing terminal over the web, with fully-featured terminal emulation based on xterm.js - [**Katacoda**](https://www.katacoda.com/): Katacoda is an Interactive Learning Platform for software developers, covering the latest Cloud Native technologies. - [**Eclipse Che**](http://www.eclipse.org/che): Developer workspace server, cloud IDE, and Eclipse next-generation IDE. From e1c1c7a4f217f75eea4ed71fd0afcf5a1f14a93f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=8F=E8=99=AB?= Date: Wed, 20 Feb 2019 14:57:16 +0800 Subject: [PATCH 36/41] Update README.md move it to the bottom --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 59e406e9..17bc9fb4 100644 --- a/README.md +++ b/README.md @@ -108,7 +108,6 @@ Xterm.js is used in several world-class applications to provide great terminal e - [**SourceLair**](https://www.sourcelair.com/): In-browser IDE that provides its users with fully-featured Linux terminals based on xterm.js - [**Microsoft Visual Studio Code**](http://code.visualstudio.com/): Modern, versatile and powerful open source code editor that provides an integrated terminal based on xterm.js -- [**Jumpserver**](https://github.com/jumpserver/luna): Jumpserver Luna project, Jumpserver is a bastion server project, Luna use xterm.js for web terminal emulation. - [**ttyd**](https://github.com/tsl0922/ttyd): A command-line tool for sharing terminal over the web, with fully-featured terminal emulation based on xterm.js - [**Katacoda**](https://www.katacoda.com/): Katacoda is an Interactive Learning Platform for software developers, covering the latest Cloud Native technologies. - [**Eclipse Che**](http://www.eclipse.org/che): Developer workspace server, cloud IDE, and Eclipse next-generation IDE. @@ -155,6 +154,7 @@ computational environment for Jupyter, supporting interactive data science and s - [**Juno**](http://junolab.org/): A flexible Julia IDE, based on Atom. - [**webssh**](https://github.com/huashengdun/webssh): Web based ssh client. - [**info-beamer hosted**](https://info-beamer.com): Uses Xterm.js to manage digital signage devices from the web dashboard. +- [**Jumpserver**](https://github.com/jumpserver/luna): Jumpserver Luna project, Jumpserver is a bastion server project, Luna use xterm.js for web terminal emulation. [And much more...](https://github.com/xtermjs/xterm.js/network/dependents) From 0854f846533689b253e3a6b6924216b2b52592f3 Mon Sep 17 00:00:00 2001 From: Sebastian Pfitzner Date: Tue, 26 Feb 2019 11:28:51 +0100 Subject: [PATCH 37/41] actually fix mouse handler before term attached --- src/InputHandler.ts | 8 ++++++-- src/Terminal.ts | 5 +++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/InputHandler.ts b/src/InputHandler.ts index 2f53cfcb..7405ff9f 100644 --- a/src/InputHandler.ts +++ b/src/InputHandler.ts @@ -1284,7 +1284,9 @@ export class InputHandler extends Disposable implements IInputHandler { if (this._terminal.element) { this._terminal.element.classList.add('enable-mouse-events'); } - this._terminal.selectionManager.disable(); + if (this._terminal.selectionManager) { + this._terminal.selectionManager.disable(); + } this._terminal.log('Binding to mouse events.'); break; case 1004: // send focusin/focusout events @@ -1474,7 +1476,9 @@ export class InputHandler extends Disposable implements IInputHandler { if (this._terminal.element) { this._terminal.element.classList.remove('enable-mouse-events'); } - this._terminal.selectionManager.enable(); + if (this._terminal.selectionManager) { + this._terminal.selectionManager.enable(); + } break; case 1004: // send focusin/focusout events this._terminal.sendFocus = false; diff --git a/src/Terminal.ts b/src/Terminal.ts index c1fc8ec8..cb9bc675 100644 --- a/src/Terminal.ts +++ b/src/Terminal.ts @@ -738,6 +738,11 @@ export class Terminal extends EventEmitter implements ITerminal, IDisposable, II this.mouseHelper = new MouseHelper(this.renderer); // apply mouse event classes set by escape codes before terminal was attached this.element.classList.toggle('enable-mouse-events', this.mouseEvents); + if (this.mouseEvents) { + this.selectionManager.disable() + } else { + this.selectionManager.enable() + } if (this.options.screenReaderMode) { // Note that this must be done *after* the renderer is created in order to From 4e479b455a659a53c7a26d05549bd38ba301c9be Mon Sep 17 00:00:00 2001 From: Ioannis Cherouvim <743305+cherouvim@users.noreply.github.com> Date: Fri, 1 Mar 2019 14:40:21 +0200 Subject: [PATCH 38/41] docs: Consistent style on lists. Added some missing dots, capitalized a couple of lines. --- CONTRIBUTING.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index e7924027..e8102a2e 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -33,21 +33,21 @@ opening an issue, read these pointers. You can find issues to work on by looking at the [help wanted](https://github.com/xtermjs/xterm.js/issues?q=is%3Aissue+is%3Aopen+label%3A%22help+wanted%22) or [good first issue](https://github.com/xtermjs/xterm.js/issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22) issues. It's a good idea to comment on the issue saying that you're taking it, just in case someone else comes along and you duplicate work. Once you have your issue, here are the steps to contribute: - Fork [xterm.js](https://github.com/sourcelair/xterm.js/) - ([how to fork a repo](https://help.github.com/articles/fork-a-repo)) -- Get the [xterm.js demo](https://github.com/xtermjs/xterm.js/wiki/Contributing#running-the-demo) running -- Make your changes + ([how to fork a repo](https://help.github.com/articles/fork-a-repo)). +- Get the [xterm.js demo](https://github.com/xtermjs/xterm.js/wiki/Contributing#running-the-demo) running. +- Make your changes. - If your changes are easy to test or likely to regress, add tests. Tests go into `test`, directory. - Follow the general code style of the rest of the project (see below). - Submit a pull request ([how to create a pull request](https://help.github.com/articles/fork-a-repo)). Don't put more than one feature/fix in a single pull request. -By contributing code to xterm.js you +By contributing code to xterm.js you: - - agree to license the contributed code under xterm.js' [MIT + - Agree to license the contributed code under xterm.js' [MIT license](LICENSE). - - confirm that you have the right to contribute and license the code + - Confirm that you have the right to contribute and license the code in question. (Either you hold all rights on the code, or the rights holder has explicitly granted the right to use it like this, through a compatible open source license or through a direct From a8a0344d1ac5ea411ea244a73754a06ffda2d308 Mon Sep 17 00:00:00 2001 From: Ioannis Cherouvim <743305+cherouvim@users.noreply.github.com> Date: Fri, 1 Mar 2019 14:51:19 +0200 Subject: [PATCH 39/41] docs: style improvements - Added a trailing dot at the end of each list line. - Consistent usage of `xterm.js` in "Real-world uses". --- README.md | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 17bc9fb4..d70e8b7e 100644 --- a/README.md +++ b/README.md @@ -7,17 +7,17 @@ Xterm.js is a front-end component written in TypeScript that lets applications b ## Features -- **Terminal apps just work**: Xterm.js works with most terminal apps such as `bash`, `vim` and `tmux`, this includes support for curses-based apps and mouse event support -- **Perfomant**: Xterm.js is *really* fast, it even includes a GPU-accelerated renderer -- **Rich unicode support**: Supports CJK, emojis and IMEs -- **Self-contained**: Requires zero dependencies to work -- **Accessible**: Screen reader support can be turned on using the `screenReaderMode` option +- **Terminal apps just work**: Xterm.js works with most terminal apps such as `bash`, `vim` and `tmux`, this includes support for curses-based apps and mouse event support. +- **Perfomant**: Xterm.js is *really* fast, it even includes a GPU-accelerated renderer. +- **Rich unicode support**: Supports CJK, emojis and IMEs. +- **Self-contained**: Requires zero dependencies to work. +- **Accessible**: Screen reader support can be turned on using the `screenReaderMode` option. - **And much more**: Links, theming, addons, well documented API, etc. ## What xterm.js is not -- Xterm.js is not a terminal application that you can download and use on your computer -- Xterm.js is not `bash`. Xterm.js can be connected to processes like `bash` and let you interact with them (provide input, receive output) +- Xterm.js is not a terminal application that you can download and use on your computer. +- Xterm.js is not `bash`. Xterm.js can be connected to processes like `bash` and let you interact with them (provide input, receive output). ## Getting Started @@ -41,7 +41,7 @@ To start using xterm.js on your browser, add the `xterm.js` and `xterm.css` to t @@ -106,9 +106,9 @@ Note that some APIs are marked *experimental*, these are added to enable experim ## Real-world uses Xterm.js is used in several world-class applications to provide great terminal experiences. -- [**SourceLair**](https://www.sourcelair.com/): In-browser IDE that provides its users with fully-featured Linux terminals based on xterm.js -- [**Microsoft Visual Studio Code**](http://code.visualstudio.com/): Modern, versatile and powerful open source code editor that provides an integrated terminal based on xterm.js -- [**ttyd**](https://github.com/tsl0922/ttyd): A command-line tool for sharing terminal over the web, with fully-featured terminal emulation based on xterm.js +- [**SourceLair**](https://www.sourcelair.com/): In-browser IDE that provides its users with fully-featured Linux terminals based on `xterm.js`. +- [**Microsoft Visual Studio Code**](http://code.visualstudio.com/): Modern, versatile and powerful open source code editor that provides an integrated terminal based on `xterm.js`. +- [**ttyd**](https://github.com/tsl0922/ttyd): A command-line tool for sharing terminal over the web, with fully-featured terminal emulation based on `xterm.js`. - [**Katacoda**](https://www.katacoda.com/): Katacoda is an Interactive Learning Platform for software developers, covering the latest Cloud Native technologies. - [**Eclipse Che**](http://www.eclipse.org/che): Developer workspace server, cloud IDE, and Eclipse next-generation IDE. - [**Codenvy**](http://www.codenvy.com): Cloud workspaces for development teams. @@ -125,11 +125,10 @@ Xterm.js is used in several world-class applications to provide great terminal e - [**Selenoid UI**](https://github.com/aerokube/selenoid-ui): Simple UI for the scallable golang implementation of Selenium Hub named Selenoid. We use XTerm for streaming logs over websockets from docker containers. - [**Portainer**](https://portainer.io): Simple management UI for Docker. - [**SSHy**](https://github.com/stuicey/SSHy): HTML5 Based SSHv2 Web Client with E2E encryption utilising `xterm.js`, SJCL & websockets. -- [**JupyterLab**](https://github.com/jupyterlab/jupyterlab): An extensible -computational environment for Jupyter, supporting interactive data science and scientific computing across all programming languages. +- [**JupyterLab**](https://github.com/jupyterlab/jupyterlab): An extensible computational environment for Jupyter, supporting interactive data science and scientific computing across all programming languages. - [**Theia**](https://github.com/theia-ide/theia): Theia is a cloud & desktop IDE framework implemented in TypeScript. - [**Opshell**](https://github.com/ricktbaker/opshell) Ops Helper tool to make life easier working with AWS instances across multiple organizations. -- [**Proxmox VE**](https://www.proxmox.com/en/proxmox-ve): Proxmox VE is a complete open-source platform for enterprise virtualization. It uses xterm.js for container terminals and the host shell. +- [**Proxmox VE**](https://www.proxmox.com/en/proxmox-ve): Proxmox VE is a complete open-source platform for enterprise virtualization. It uses `xterm.js` for container terminals and the host shell. - [**Script Runner**](https://github.com/ioquatix/script-runner): Run scripts (or a shell) in Atom. - [**Whack Whack Terminal**](https://github.com/Microsoft/WhackWhackTerminal): Terminal emulator for Visual Studio 2017. - [**VTerm**](https://github.com/vterm/vterm): Extensible terminal emulator based on Electron and React. @@ -138,23 +137,23 @@ computational environment for Jupyter, supporting interactive data science and s - [**Azure Cloud Shell**](https://shell.azure.com): Azure Cloud Shell is a Microsoft-managed admin machine built on Azure, for Azure. - [**atom-xterm**](https://atom.io/packages/atom-xterm): Atom plugin for providing terminals inside your Atom workspace. - [**rtty**](https://github.com/zhaojh329/rtty): A reverse proxy WebTTY. It is composed of the client and the server. -- [**Pisth**](https://github.com/ColdGrub1384/Pisth): An SFTP and SSH client for iOS +- [**Pisth**](https://github.com/ColdGrub1384/Pisth): An SFTP and SSH client for iOS. - [**abstruse**](https://github.com/bleenco/abstruse): Abstruse CI is a continuous integration platform based on Node.JS and Docker. - [**Azure Data Studio**](https://github.com/Microsoft/azuredatastudio): A data management tool that enables working with SQL Server, Azure SQL DB and SQL DW from Windows, macOS and Linux. -- [**FreeMAN**](https://github.com/matthew-matvei/freeman): A free, cross-platform file manager for power users +- [**FreeMAN**](https://github.com/matthew-matvei/freeman): A free, cross-platform file manager for power users. - [**Fluent Terminal**](https://github.com/felixse/FluentTerminal): A terminal emulator based on UWP and web technologies. -- [**Hyper**](https://hyper.is): A terminal built on web technologies +- [**Hyper**](https://hyper.is): A terminal built on web technologies. - [**Diag**](https://diag.ai): A better way to troubleshoot problems faster. Capture, share and reapply troubleshooting knowledge so you can focus on solving problems that matter. -- [**GoTTY**](https://github.com/yudai/gotty): A simple command line tool that shares your terminal as a web application based on xterm.js. +- [**GoTTY**](https://github.com/yudai/gotty): A simple command line tool that shares your terminal as a web application based on `xterm.js`. - [**genact**](https://github.com/svenstaro/genact): A nonsense activity generator. - [**cPanel & WHM**](https://cpanel.com): The hosting platform of choice. -- [**Nutanix**](https://github.com/nutanix): Nutanix Enterprise Cloud uses xterm in the webssh functionality within Nutanix Calm, and is also looking to move our old noserial (termjs) functionality to xterm.js +- [**Nutanix**](https://github.com/nutanix): Nutanix Enterprise Cloud uses xterm in the webssh functionality within Nutanix Calm, and is also looking to move our old noserial (termjs) functionality to `xterm.js`. - [**SSH Web Client**](https://github.com/roke22/PHP-SSH2-Web-Client): SSH Web Client with PHP. - [**Shellvault**](https://www.shellvault.io): The cloud-based SSH terminal you can access from anywhere. - [**Juno**](http://junolab.org/): A flexible Julia IDE, based on Atom. - [**webssh**](https://github.com/huashengdun/webssh): Web based ssh client. -- [**info-beamer hosted**](https://info-beamer.com): Uses Xterm.js to manage digital signage devices from the web dashboard. -- [**Jumpserver**](https://github.com/jumpserver/luna): Jumpserver Luna project, Jumpserver is a bastion server project, Luna use xterm.js for web terminal emulation. +- [**info-beamer hosted**](https://info-beamer.com): Uses `xterm.js` to manage digital signage devices from the web dashboard. +- [**Jumpserver**](https://github.com/jumpserver/luna): Jumpserver Luna project, Jumpserver is a bastion server project, Luna use `xterm.js` for web terminal emulation. [And much more...](https://github.com/xtermjs/xterm.js/network/dependents) From 09754fddf8e3ba0dbbd3840b65a69822482fb64b Mon Sep 17 00:00:00 2001 From: Ioannis Cherouvim <743305+cherouvim@users.noreply.github.com> Date: Fri, 1 Mar 2019 21:15:20 +0200 Subject: [PATCH 40/41] xterm.js references to the library should not be backticked --- README.md | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index d70e8b7e..5378f443 100644 --- a/README.md +++ b/README.md @@ -106,17 +106,17 @@ Note that some APIs are marked *experimental*, these are added to enable experim ## Real-world uses Xterm.js is used in several world-class applications to provide great terminal experiences. -- [**SourceLair**](https://www.sourcelair.com/): In-browser IDE that provides its users with fully-featured Linux terminals based on `xterm.js`. -- [**Microsoft Visual Studio Code**](http://code.visualstudio.com/): Modern, versatile and powerful open source code editor that provides an integrated terminal based on `xterm.js`. -- [**ttyd**](https://github.com/tsl0922/ttyd): A command-line tool for sharing terminal over the web, with fully-featured terminal emulation based on `xterm.js`. +- [**SourceLair**](https://www.sourcelair.com/): In-browser IDE that provides its users with fully-featured Linux terminals based on xterm.js. +- [**Microsoft Visual Studio Code**](http://code.visualstudio.com/): Modern, versatile and powerful open source code editor that provides an integrated terminal based on xterm.js. +- [**ttyd**](https://github.com/tsl0922/ttyd): A command-line tool for sharing terminal over the web, with fully-featured terminal emulation based on xterm.js. - [**Katacoda**](https://www.katacoda.com/): Katacoda is an Interactive Learning Platform for software developers, covering the latest Cloud Native technologies. - [**Eclipse Che**](http://www.eclipse.org/che): Developer workspace server, cloud IDE, and Eclipse next-generation IDE. - [**Codenvy**](http://www.codenvy.com): Cloud workspaces for development teams. -- [**CoderPad**](https://coderpad.io): Online interviewing platform for programmers. Run code in many programming languages, with results displayed by `xterm.js`. -- [**WebSSH2**](https://github.com/billchurch/WebSSH2): A web based SSH2 client using `xterm.js`, socket.io, and ssh2. +- [**CoderPad**](https://coderpad.io): Online interviewing platform for programmers. Run code in many programming languages, with results displayed by xterm.js. +- [**WebSSH2**](https://github.com/billchurch/WebSSH2): A web based SSH2 client using xterm.js, socket.io, and ssh2. - [**Spyder Terminal**](https://github.com/spyder-ide/spyder-terminal): A full fledged system terminal embedded on Spyder IDE. - [**Cloud Commander**](https://cloudcmd.io "Cloud Commander"): Orthodox web file manager with console and editor. -- [**Codevolve**](https://www.codevolve.com "Codevolve"): Online platform for interactive coding and web development courses. Live container-backed terminal uses `xterm.js`. +- [**Codevolve**](https://www.codevolve.com "Codevolve"): Online platform for interactive coding and web development courses. Live container-backed terminal uses xterm.js. - [**RStudio**](https://www.rstudio.com/products/RStudio "RStudio"): RStudio is an integrated development environment (IDE) for R. - [**Terminal for Atom**](https://github.com/jsmecham/atom-terminal-tab): A simple terminal for the Atom text editor. - [**Eclipse Orion**](https://orionhub.org): A modern, open source software development environment that runs in the cloud. Code, deploy and run in the cloud. @@ -124,11 +124,11 @@ Xterm.js is used in several world-class applications to provide great terminal e - [**Hexlet**](https://en.hexlet.io): Practical programming courses (JavaScript, PHP, Unix, databases, functional programming). A steady path from the first line of code to the first job. - [**Selenoid UI**](https://github.com/aerokube/selenoid-ui): Simple UI for the scallable golang implementation of Selenium Hub named Selenoid. We use XTerm for streaming logs over websockets from docker containers. - [**Portainer**](https://portainer.io): Simple management UI for Docker. -- [**SSHy**](https://github.com/stuicey/SSHy): HTML5 Based SSHv2 Web Client with E2E encryption utilising `xterm.js`, SJCL & websockets. +- [**SSHy**](https://github.com/stuicey/SSHy): HTML5 Based SSHv2 Web Client with E2E encryption utilising xterm.js, SJCL & websockets. - [**JupyterLab**](https://github.com/jupyterlab/jupyterlab): An extensible computational environment for Jupyter, supporting interactive data science and scientific computing across all programming languages. - [**Theia**](https://github.com/theia-ide/theia): Theia is a cloud & desktop IDE framework implemented in TypeScript. - [**Opshell**](https://github.com/ricktbaker/opshell) Ops Helper tool to make life easier working with AWS instances across multiple organizations. -- [**Proxmox VE**](https://www.proxmox.com/en/proxmox-ve): Proxmox VE is a complete open-source platform for enterprise virtualization. It uses `xterm.js` for container terminals and the host shell. +- [**Proxmox VE**](https://www.proxmox.com/en/proxmox-ve): Proxmox VE is a complete open-source platform for enterprise virtualization. It uses xterm.js for container terminals and the host shell. - [**Script Runner**](https://github.com/ioquatix/script-runner): Run scripts (or a shell) in Atom. - [**Whack Whack Terminal**](https://github.com/Microsoft/WhackWhackTerminal): Terminal emulator for Visual Studio 2017. - [**VTerm**](https://github.com/vterm/vterm): Extensible terminal emulator based on Electron and React. @@ -144,16 +144,16 @@ Xterm.js is used in several world-class applications to provide great terminal e - [**Fluent Terminal**](https://github.com/felixse/FluentTerminal): A terminal emulator based on UWP and web technologies. - [**Hyper**](https://hyper.is): A terminal built on web technologies. - [**Diag**](https://diag.ai): A better way to troubleshoot problems faster. Capture, share and reapply troubleshooting knowledge so you can focus on solving problems that matter. -- [**GoTTY**](https://github.com/yudai/gotty): A simple command line tool that shares your terminal as a web application based on `xterm.js`. +- [**GoTTY**](https://github.com/yudai/gotty): A simple command line tool that shares your terminal as a web application based on xterm.js. - [**genact**](https://github.com/svenstaro/genact): A nonsense activity generator. - [**cPanel & WHM**](https://cpanel.com): The hosting platform of choice. -- [**Nutanix**](https://github.com/nutanix): Nutanix Enterprise Cloud uses xterm in the webssh functionality within Nutanix Calm, and is also looking to move our old noserial (termjs) functionality to `xterm.js`. +- [**Nutanix**](https://github.com/nutanix): Nutanix Enterprise Cloud uses xterm in the webssh functionality within Nutanix Calm, and is also looking to move our old noserial (termjs) functionality to xterm.js. - [**SSH Web Client**](https://github.com/roke22/PHP-SSH2-Web-Client): SSH Web Client with PHP. - [**Shellvault**](https://www.shellvault.io): The cloud-based SSH terminal you can access from anywhere. - [**Juno**](http://junolab.org/): A flexible Julia IDE, based on Atom. - [**webssh**](https://github.com/huashengdun/webssh): Web based ssh client. -- [**info-beamer hosted**](https://info-beamer.com): Uses `xterm.js` to manage digital signage devices from the web dashboard. -- [**Jumpserver**](https://github.com/jumpserver/luna): Jumpserver Luna project, Jumpserver is a bastion server project, Luna use `xterm.js` for web terminal emulation. +- [**info-beamer hosted**](https://info-beamer.com): Uses xterm.js to manage digital signage devices from the web dashboard. +- [**Jumpserver**](https://github.com/jumpserver/luna): Jumpserver Luna project, Jumpserver is a bastion server project, Luna use xterm.js for web terminal emulation. [And much more...](https://github.com/xtermjs/xterm.js/network/dependents) From c63f15a9b26c770dcbcceca6dfaf33acb3121d67 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Mon, 4 Mar 2019 10:00:04 -0800 Subject: [PATCH 41/41] Fix lint --- src/Terminal.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Terminal.ts b/src/Terminal.ts index cb9bc675..5de369fe 100644 --- a/src/Terminal.ts +++ b/src/Terminal.ts @@ -739,9 +739,9 @@ export class Terminal extends EventEmitter implements ITerminal, IDisposable, II // apply mouse event classes set by escape codes before terminal was attached this.element.classList.toggle('enable-mouse-events', this.mouseEvents); if (this.mouseEvents) { - this.selectionManager.disable() + this.selectionManager.disable(); } else { - this.selectionManager.enable() + this.selectionManager.enable(); } if (this.options.screenReaderMode) {