Skip to content

fix(compiler-core): preserve start/end whitespace nodes in whitespace: 'preserve' mode #13513

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

alex-snezhko
Copy link
Contributor

@alex-snezhko alex-snezhko commented Jun 23, 2025

close #7789

This would solve the scenario highlighted in this issue of a single child whitespace-only text node being wiped, and would also more generically handle scenarios like: <span> <!--comment-->Value<!--comment--> </span> where the whitespace is currently wiped in preserve mode perhaps unexpectedly.

Furthermore, I think this behavior change would also closer align with the documented behavior: https://vuejs.org/api/application#app-config-compileroptions-whitespace

  1. Whitespace characters between elements that contain newlines are removed.
    [...]

Setting this option to 'preserve' will disable (2) and (3).

This is definitely a more "radical" solution compared to other ones proposed. Some notes/acknowledgements:

  1. This is a breaking change
  2. This makes the preserve behavior different from Vue 2 preserve behavior (e.g. Vue 2.7.14 strips <span> </span> to <span></span> in preserve mode)
  3. This will make bundle sizes larger for users using whitespace: 'preserve' due to extra whitespace nodes

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features

    • Improved handling of whitespace preservation in templates, ensuring more accurate whitespace behavior for both root and nested elements when using the "preserve" strategy.
  • Tests

    • Expanded and refined test coverage for whitespace management, including nuanced scenarios for elements with comments, newlines, and implicit default slot content in components.
    • Updated tests to reflect preservation of whitespace around elements and transition groups in end-to-end scenarios.

Copy link

coderabbitai bot commented Jun 23, 2025

Walkthrough

Whitespace handling logic in the template parser was updated to distinguish between root and nested elements, refining how whitespace-only text nodes are preserved or removed when using the "preserve" strategy. Corresponding and expanded test cases were added to verify nuanced whitespace preservation in various scenarios, including slot content and comments. Additionally, a new test was added to verify slot behavior with implicit default slots before and after named slots, and an end-to-end test was updated to reflect whitespace preservation in transition group outputs.

Changes

Files Change Summary
packages/compiler-core/src/parser.ts Refined condenseWhitespace to accept isRoot and updated calls to distinguish root/nested nodes.
packages/compiler-core/tests/parse.spec.ts Expanded and adjusted whitespace preservation tests for root and nested elements, comments, etc.
packages/compiler-core/tests/transforms/vSlot.spec.ts Added test for implicit default slot content before and after named slot template with whitespace.
packages/vue/tests/e2e/TransitionGroup.spec.ts Updated expected HTML outputs to include preserved leading and trailing whitespace around elements.

Sequence Diagram(s)

sequenceDiagram
    participant Parser
    participant condenseWhitespace

    Parser->>condenseWhitespace: condenseWhitespace(nodes, isRoot)
    alt isRoot == true
        condenseWhitespace->>condenseWhitespace: Remove leading/trailing whitespace nodes
    else isRoot == false
        condenseWhitespace->>condenseWhitespace: Preserve whitespace nodes at edges
    end
    condenseWhitespace-->>Parser: Return processed nodes
Loading

Assessment against linked issues

Objective Addressed Explanation
Whitespace should be preserved as expected when whitespace: 'preserve' is set (#7789)
Whitespace handling should distinguish between root and nested elements (#7789)
Add/expand tests to verify whitespace preservation in various scenarios (#7789)

Suggested labels

ready to merge, scope: compiler

Poem

In burrows deep, where whitespace hides,
The parser now with care decides—
To keep or trim, at root or not,
Each space and line, precisely caught.
With tests anew, the code’s refined,
A tidy warren, well-aligned!
🐇✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9db8765 and eaf39f0.

📒 Files selected for processing (1)
  • packages/vue/__tests__/e2e/TransitionGroup.spec.ts (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (5)
  • GitHub Check: Redirect rules
  • GitHub Check: Header rules
  • GitHub Check: Pages changed
  • GitHub Check: test / unit-test-windows
  • GitHub Check: test / unit-test
🔇 Additional comments (1)
packages/vue/__tests__/e2e/TransitionGroup.spec.ts (1)

626-654: LGTM! Test expectations correctly updated for whitespace preservation.

The test expectations have been appropriately updated to reflect the new whitespace preservation behavior when whitespace: 'preserve' is configured. The preserved whitespace patterns match the template's indentation structure and are consistently applied across all transition phases (initial state, transition start, next frame, and final state).

This change aligns perfectly with the PR objective of preserving whitespace text nodes in 'preserve' mode, ensuring the test validates that transition hooks are correctly avoided for these preserved whitespace nodes.

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

pkg-pr-new bot commented Jun 23, 2025

Open in StackBlitz

@vue/compiler-core

npm i https://pkg.pr.new/@vue/compiler-core@13513

@vue/compiler-dom

npm i https://pkg.pr.new/@vue/compiler-dom@13513

@vue/compiler-sfc

npm i https://pkg.pr.new/@vue/compiler-sfc@13513

@vue/compiler-ssr

npm i https://pkg.pr.new/@vue/compiler-ssr@13513

@vue/reactivity

npm i https://pkg.pr.new/@vue/reactivity@13513

@vue/runtime-core

npm i https://pkg.pr.new/@vue/runtime-core@13513

@vue/runtime-dom

npm i https://pkg.pr.new/@vue/runtime-dom@13513

@vue/server-renderer

npm i https://pkg.pr.new/@vue/server-renderer@13513

@vue/shared

npm i https://pkg.pr.new/@vue/shared@13513

vue

npm i https://pkg.pr.new/vue@13513

@vue/compat

npm i https://pkg.pr.new/@vue/compat@13513

commit: eaf39f0

@alex-snezhko
Copy link
Contributor Author

Another note is that my implementation excludes children of root nodes because in general I think little value is provided by considering root children. It would result in virtually all components having leading and trailing whitespace text nodes if using the recommended Vue template formatting, and would also break the assignment of $el on template refs because of these whitespace text nodes.

Copy link

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 101 kB 38.3 kB 34.5 kB
vue.global.prod.js 159 kB (+18 B) 58.5 kB (+14 B) 52.1 kB (+46 B)

Usages

Name Size Gzip Brotli
createApp (CAPI only) 46.5 kB 18.2 kB 16.7 kB
createApp 54.5 kB 21.2 kB 19.4 kB
createSSRApp 58.7 kB 22.9 kB 20.9 kB
defineCustomElement 59.4 kB 22.8 kB 20.8 kB
overall 68.5 kB 26.4 kB 24 kB

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

Successfully merging this pull request may close these issues.

Whitespace preserve does not work
2 participants