Skip to content

Commit aaa69f0

Browse files
fix(shared): preserve hyperlink labels terminated by the C1 string terminator (#45470)
Co-authored-by: SAPHID <saphid@users.noreply.github.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>
1 parent fe10f4b commit aaa69f0

2 files changed

Lines changed: 34 additions & 8 deletions

File tree

‎packages/shared/src/terminalOutput.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,25 @@ describe("appendTerminalOutput", () => {
204204
}
205205
});
206206

207+
it.each([
208+
["\u001b]8;;https://example.test\u009cvisible\u001b]8;;\u009c", "visible"],
209+
["\u009b31mRED\u009b0m", "\u001b[0;31mRED\u001b[0m"],
210+
[
211+
"\u001bPq\u009cOK\u009dhidden\u0007\u0090\u009c\u009e\u009c\u009f\u009c\u0098\u009cdone",
212+
"OKdone",
213+
],
214+
[`\u001b]0;${"a".repeat(100)}8;left\u009cright\u0007visible`, "rightvisible"],
215+
])("treats C1 introducers and ST like their ESC forms at every split: %j", (input, expected) => {
216+
expect(normalizeTerminalOutput(input).text).toBe(expected);
217+
for (let cut = 0; cut <= input.length; cut += 1) {
218+
const state = feed([input.slice(0, cut)]);
219+
const done = appendTerminalOutput(state, input.slice(cut));
220+
expect(done.text).toBe(expected);
221+
expect(done.pending).toBe("");
222+
expect(feed([terminalOutputResumeText(state), input.slice(cut)]).text).toBe(expected);
223+
}
224+
});
225+
207226
it("drops an escape sequence that never terminates and resumes after it", () => {
208227
expect(feed(["ok\u001b]", "x".repeat(200), "\nafter"]).text).toBe("ok\nafter");
209228
expect(feed(["ok\u001b]0;title\u001b", "\u001b[31mred"]).text).toBe(

‎packages/shared/src/terminalOutput.ts‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -367,10 +367,11 @@ function applySgr(attributes: Attributes, params: ReadonlyArray<number>): void {
367367
}
368368

369369
const STRING_SEQUENCE_INTRODUCERS = new Set(["]", "P", "X", "_", "^"]);
370+
// C1 DCS, SOS, CSI, OSC, PM and APC, each equal to ESC followed by its code minus 0x40.
371+
const C1_INTRODUCERS = new Set([0x90, 0x98, 0x9b, 0x9d, 0x9e, 0x9f]);
370372

371373
/** Length of a complete escape sequence whose introducer follows `index`, or -1 if unfinished. */
372-
function escapeLength(input: string, index: number): number {
373-
const next = input[index + 1];
374+
function escapeLength(input: string, index: number, next = input[index + 1]): number {
374375
if (next === undefined) return -1;
375376
// A second ESC restarts the sequence.
376377
if (next === ESC) return 1;
@@ -398,7 +399,7 @@ function escapeLength(input: string, index: number): number {
398399
// ST (and BEL for OSC) ends string sequences; also stop at newlines so stray binary introducers cannot hide later output.
399400
for (let cursor = index + 2; cursor < input.length; cursor += 1) {
400401
if (next === "]" && input[cursor] === "\u0007") return cursor - index + 1;
401-
if (input[cursor] === "\u0018" || input[cursor] === "\u001a") return cursor - index + 1;
402+
if ("\u0018\u001a\u009c".includes(input[cursor]!)) return cursor - index + 1;
402403
if (input[cursor] === "\n") return cursor - index;
403404
// Like xterm.js, an ESC that does not start ST ends the string and begins the next sequence.
404405
if (input[cursor] === ESC) {
@@ -677,12 +678,18 @@ class Terminal {
677678
}
678679
flush(index);
679680
printable = index + 1;
680-
if (code === 0x1b) {
681-
let start = index + 1;
682-
while (this.control(input.charCodeAt(start))) start += 1;
683-
const length = escapeLength(input, start - 1);
681+
const c1 = C1_INTRODUCERS.has(code) ? String.fromCharCode(code - 0x40) : undefined;
682+
if (code === 0x1b || c1 !== undefined) {
683+
// A C1 introducer stands in for ESC plus its 7-bit introducer, so parse it as that pair.
684+
let start = c1 === undefined ? index + 1 : index;
685+
while (c1 === undefined && this.control(input.charCodeAt(start))) start += 1;
686+
const length = escapeLength(input, start - 1, c1);
684687
const end = length === -1 ? -1 : start - 1 + length;
685-
let sequence = ESC + input.slice(start, end === -1 ? input.length : end);
688+
const rest = input.slice(
689+
c1 === undefined ? start : start + 1,
690+
end === -1 ? input.length : end,
691+
);
692+
let sequence = ESC + (c1 ?? "") + rest;
686693
if (sequence[1] === "[") {
687694
// Apply controls now and leave them out of the CSI, including any pending fragment.
688695
sequence = sequence.replace(/[\u0007\b\n\r]/gu, (control) => {

0 commit comments

Comments
 (0)