diff --git a/src/shared/prose-url-boundary.ts b/src/shared/prose-url-boundary.ts index f7071462576..ef0cab5d840 100644 --- a/src/shared/prose-url-boundary.ts +++ b/src/shared/prose-url-boundary.ts @@ -5,11 +5,17 @@ const PROSE_URL_TERMINATOR_SOURCE = String.raw`(?![\0-\x7f])[\p{Ps}\p{Pe}\p{Pi}\ const NOT_PROSE_URL_TERMINATOR = `(?!${PROSE_URL_TERMINATOR_SOURCE})` -/** xterm's default web-links url pattern, also ended by prose punctuation; needs the `u` flag. */ -export const PROSE_HTTP_URL_SOURCE = - String.raw`https?:\/\/(?:${NOT_PROSE_URL_TERMINATOR}[^\s"'!*(){}|\\^<>\x60])*` + +// Why no lookbehind: the mobile terminal WebView bundles this, and WebKit before Safari 16.4 cannot parse it. +/** A bracketed IPv6 host after the scheme, e.g. `user@[::1]`; its `]` is not trailing punctuation. */ +export const IPV6_HTTP_HOST_SOURCE = String.raw`(?:(?:${NOT_PROSE_URL_TERMINATOR}[^\s"'!*(){}|\\^<>\x60\/?#\[\]])*@)?\[[\dA-Fa-f:.]+\]` + +const PROSE_HTTP_URL_BODY_SOURCE = + String.raw`(?:${NOT_PROSE_URL_TERMINATOR}[^\s"'!*(){}|\\^<>\x60])*` + String.raw`${NOT_PROSE_URL_TERMINATOR}[^\s"':,.!?{}|\\^~\[\]\x60()<>]` +/** xterm's default web-links url pattern, also ended by prose punctuation; needs the `u` flag. */ +export const PROSE_HTTP_URL_SOURCE = String.raw`https?:\/\/(?:${IPV6_HTTP_HOST_SOURCE}(?:${PROSE_HTTP_URL_BODY_SOURCE})?|${PROSE_HTTP_URL_BODY_SOURCE})` + const PROSE_URL_TERMINATOR = new RegExp(PROSE_URL_TERMINATOR_SOURCE, 'u') const PROSE_URL_END = new RegExp(String.raw`\s|${PROSE_URL_TERMINATOR_SOURCE}`, 'u') const PROSE_URL_TERMINATOR_RUN = new RegExp(`((?:${PROSE_URL_TERMINATOR_SOURCE})+)`, 'u') diff --git a/src/shared/terminal-http-url-extraction.test.ts b/src/shared/terminal-http-url-extraction.test.ts index 5fbf08d38f2..b00f4d579b9 100644 --- a/src/shared/terminal-http-url-extraction.test.ts +++ b/src/shared/terminal-http-url-extraction.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from 'vitest' -import { extractTerminalHttpLinks } from './terminal-http-url-extraction' +import { extractTerminalHttpLinks, TERMINAL_HTTP_URL_REGEX } from './terminal-http-url-extraction' const urlsIn = (line: string): string[] => extractTerminalHttpLinks(line).map((link) => link.url) @@ -32,4 +32,19 @@ describe('extractTerminalHttpLinks', () => { expect(urlsIn('https://example.com/b。')).toEqual(['https://example.com/b']) expect(urlsIn('詳細はhttps://example.com/docs……')).toEqual(['https://example.com/docs']) }) + + it.each([ + ['http://[::1]', 'http://[::1]'], + ['see http://[::1]. now', 'http://[::1]'], + ['Server at http://[::1]: ready', 'http://[::1]'], + ['(http://user@[::1])', 'http://user@[::1]'], + ['http://[::1]:5173/app]', 'http://[::1]:5173/app'], + ['[see http://example.com/a[b]]', 'http://example.com/a[b'] + ])('keeps only an IPv6 host closing bracket in %j', (line, expected) => { + // Why: xterm's WebLinksAddon matches with the regex; clicks and hard wraps use the extractor. + expect(line.match(TERMINAL_HTTP_URL_REGEX)?.[0]).toBe(expected) + expect( + extractTerminalHttpLinks(line).map((link) => line.slice(link.startIndex, link.endIndex)) + ).toEqual([expected]) + }) }) diff --git a/src/shared/terminal-http-url-extraction.ts b/src/shared/terminal-http-url-extraction.ts index d00470bc3ee..8ce625581db 100644 --- a/src/shared/terminal-http-url-extraction.ts +++ b/src/shared/terminal-http-url-extraction.ts @@ -1,5 +1,11 @@ import { TERMINAL_HTTP_URL_MAX_LENGTH } from './terminal-http-link-limits' -import { isProseUrlTerminator, PROSE_HTTP_URL_SOURCE } from './prose-url-boundary' +import { + IPV6_HTTP_HOST_SOURCE, + isProseUrlTerminator, + PROSE_HTTP_URL_SOURCE +} from './prose-url-boundary' + +const IPV6_HTTP_AUTHORITY = new RegExp(String.raw`^https?:\/\/${IPV6_HTTP_HOST_SOURCE}`, 'iu') type ParsedTerminalHttpLink = { url: string @@ -82,8 +88,14 @@ function trimHttpUrlTrailingPunctuation( startIndex: number, rawEndIndex: number ): number { + const minEndIndex = + startIndex + + (IPV6_HTTP_AUTHORITY.exec(lineText.slice(startIndex, rawEndIndex))?.[0].length ?? 0) let endIndex = rawEndIndex - while (endIndex > startIndex && isHttpUrlTrailingPunctuation(lineText.charCodeAt(endIndex - 1))) { + while ( + endIndex > minEndIndex && + isHttpUrlTrailingPunctuation(lineText.charCodeAt(endIndex - 1)) + ) { endIndex -= 1 } return endIndex