Skip to content

Terminal scrollback search highlights the wrong columns when a line contains a character that grows when lowercased #804

Closed

Description

What happens

frontend/workspace/src/components/terminal-search.ts lowercases each scrollback line to find matches, then reports the offsets it found:

const value = line.toLocaleLowerCase()
return Array.from({ length: value.length }, (_, column) => column)
  .filter((column) => value.indexOf(needle, column) === column)
  .map((column) => ({ column, row, length: query.length }))

toLocaleLowerCase() can change a string's UTF-16 length. Lowercasing İ (U+0130) yields i + U+0307, one code unit becoming two. So an index into value is not an index into line.

length compounds it: it uses the un-folded query.length, while the match actually spans needle.length folded code units.

The consumer passes these straight to xterm, which selects against the original buffer line:

// terminal.tsx:308
t.select(match.column, match.row, match.length)

Why it is wrong

Measured:

line         "İST build"   length 9
lowercased   "i̇st build"  length 10

search "build" -> { column: 5, length: 5 }   the true column in the original is 4
search "st"    -> { column: 2, length: 2 }   the true column is 1
search on "abc build" -> { column: 4 }        correct (ASCII)

Any terminal output containing İ before the match shifts every subsequent match one cell right, so t.select highlights the wrong run of cells. The row and the match count stay correct, which is why it reads as "the search works but the highlight is slightly wrong".

toLocaleLowerCase() is called with no explicit locale, so it follows the browser locale; Turkish and Azeri locales are where this is most likely to be hit in practice.

Reproduction

terminalMatches(["İST build"], "build")
// currently: [{ column: 5, row: 0, length: 5 }]
// expected:  [{ column: 4, row: 0, length: 5 }]

The three existing tests are all ASCII, which is why this was not caught.

Suggested fix

Track the original offset of each folded code unit while folding, and translate the match range back to the original line:

let value = ""
const origin: number[] = []
let offset = 0
for (const char of line) {
  const folded = char.toLocaleLowerCase()
  value += folded
  for (let unit = 0; unit < folded.length; unit++) origin.push(offset)
  offset += char.length
}

then map each match to { column: origin[column], length: origin[column + needle.length - 1] + 1 - origin[column] }. indexOf guarantees column + needle.length <= value.length, so the final origin lookup is always in range.

length also becomes correct on its own: it is derived from the original line rather than from the query, which matters when a single folded character spans more than one code unit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions