Skip to content

Apply CSS format settings to style tags in HTML files#305031

Open
Sathvik-Chowdary-Veerapaneni wants to merge 1 commit intomicrosoft:mainfrom
Sathvik-Chowdary-Veerapaneni:css-format-html-style-283316
Open

Apply CSS format settings to style tags in HTML files#305031
Sathvik-Chowdary-Veerapaneni wants to merge 1 commit intomicrosoft:mainfrom
Sathvik-Chowdary-Veerapaneni:css-format-html-style-283316

Conversation

@Sathvik-Chowdary-Veerapaneni

Summary

Fixes #283316

CSS formatting settings (css.format.newlineBetweenSelectors, css.format.braceStyle, css.format.spaceAroundSelectorSeparator, etc.) were not being applied when formatting CSS within HTML <style> tags. The CSS mode in the HTML language server was missing a format method, so the CSS language service formatter was never invoked for embedded CSS regions.

Changes

  • Added a format method to the CSS mode in extensions/html-language-features/server/src/modes/cssMode.ts that:
    • Gets the embedded CSS document for the current HTML document
    • Merges the user's css.format.* settings with the base formatting options
    • Delegates to cssLanguageService.format() with the merged settings
  • Added a test case verifying that css.format.newlineBetweenSelectors: false is respected when formatting CSS inside HTML style tags

Root Cause

The formatting.ts orchestrator iterates through all embedded language mode ranges and calls mode.format() if it exists (line 73). However, the CSS mode returned by getCSSMode() never defined a format method, so it was silently skipped. The HTML formatter would handle the CSS content with only its own settings, ignoring CSS-specific format configuration.

Test Plan

  • Verified no type errors introduced (zero errors from cssMode.ts)
  • Added test: HTML & Styles with CSS format settings validates newlineBetweenSelectors: false is applied
  • Existing test HTML & Styles still passes (default formatting behavior unchanged)

The CSS mode in the HTML language server was missing a format method,
so CSS formatting settings (like newlineBetweenSelectors, braceStyle,
etc.) were not applied when formatting CSS within HTML <style> tags.

This adds a format method to the CSS mode that delegates to the CSS
language service with the user's css.format.* settings merged in.

Fixes microsoft#283316
@vs-code-engineering vs-code-engineering bot added this to the 1.114.0 milestone Mar 26, 2026
@aeschli
Copy link
Contributor

aeschli commented Mar 26, 2026

The JSBeautify formatter for HTML already formats CSS embedded in HTML. That's how the formatting of embedded CSS currently works.
So if you want to `css.format.* settings to be applied, you need to find a way to configure for JSBeautify formatter for HTML to us these rules. Maybe that needs improvements to JSBeautify.

The suggested fix of this PR is also possible, but likely will cause regressions on formatting around the boundaries between CSS and HTML: Dealing with indentation and spaces before and after <style> and style="xx"
We would need more tests with such scenarios.

@aeschli aeschli modified the milestones: 1.114.0, On Deck Mar 26, 2026
Copy link
Contributor

@aeschli aeschli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my comment.
Preferably fixed by better configuring JSBeautity also for the embedded CSS, or, if not possible, many more tests that make sure there are no regression with formatting/indentation around <style> and styles="..."

@Sathvik-Chowdary-Veerapaneni
Copy link
Author

@microsoft-github-policy-service agree

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.

css formating config not apply to style tag in html during format html file.

2 participants