Skip to content

fix: clamp HTML completion ranges for unclosed string literals#308736

Closed
maruthang wants to merge 1 commit intomicrosoft:mainfrom
maruthang:fix/issue-273226-html-unclosed-string-completions
Closed

fix: clamp HTML completion ranges for unclosed string literals#308736
maruthang wants to merge 1 commit intomicrosoft:mainfrom
maruthang:fix/issue-273226-html-unclosed-string-completions

Conversation

@maruthang
Copy link
Copy Markdown
Contributor

Fixes #273226

Summary

Bug: When an HTML attribute value has an unclosed quote (e.g., <input type="che), selecting a completion replaces content beyond the cursor, deleting subsequent HTML tags.

Root Cause: The HTML parser treats everything after an unclosed quote as part of the attribute value string, so the completion replacement range extends into subsequent tags like </th>.

Fix: Post-process completion items in htmlMode.ts to clamp their text edit ranges before any < character found after the cursor position, preventing the replacement from leaking into subsequent HTML content.

Changes

  • extensions/html-language-features/server/src/modes/htmlMode.ts: Added range clamping logic after doComplete2 that scans for < between the cursor and the edit range end, and clamps the range to stop before it.
  • extensions/html-language-features/server/src/test/completions.test.ts: Added 3 regression tests covering unclosed quote completions, unclosed quote with trailing space, and properly closed quotes (to ensure no regression).

Test Plan

  • Added regression test: completion for <th><input type="che|</th> produces checkbox without replacing </th>
  • Added regression test: completion for <th><input type="che| </th> works correctly with space before closing tag
  • Added regression test: completion for <input type="che|"> still works normally with properly closed quotes
  • All existing HTML completion tests continue to pass

…soft#273226)

When an attribute value has an opening quote but no closing quote, the
HTML parser treats subsequent HTML tags as part of the attribute value,
causing completion replacement ranges to extend too far. Post-process
completion items to clamp their ranges before any '<' character after
the cursor position, preventing deletion of subsequent HTML content.
@aeschli
Copy link
Copy Markdown
Contributor

aeschli commented Apr 9, 2026

The correct place to fix this is in the https://github.com/microsoft/vscode-html-languageservice

@aeschli aeschli closed this Apr 9, 2026
@maruthang
Copy link
Copy Markdown
Contributor Author

Thanks @aeschli — you're right. I've submitted the fix upstream at microsoft/vscode-html-languageservice#239 instead.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[html] completions: better handle unclosed string literals

2 participants