Skip to content

Inline <style> url() with escaped \) is incorrectly transformed into invalid CSS #20816

@tjramage

Description

@tjramage

Describe the bug

When an inline <style> contains an unquoted url(...) with an escaped ) (which is valid CSS syntax), Vite’s dev server transforms it into an invalid quoted form.

Input (index.html):

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>Vite CSS URL Bug</title>
	<style>
		/* 
			This is valid CSS: the `\)` escapes the middle paren,
			but Vite will incorrectly wrap this with broken quotes
		*/
		p {
			background-image: url(public/awkward-name\)2.png);
		}
	</style>
</head>
<body>
	<p>Hello</p>
</body>
</html>

Config file (vite.config.js):

import { defineConfig } from 'vite'

export default defineConfig({
	appType: 'mpa',
	server: { port: 5173 }
})

Reproduction

https://stackblitz.com/edit/vitejs-vite-wdgyzxhy?file=index.html

Steps to reproduce

  1. Create a new folder with both input.html and vite.config.js inside (as per above)
  2. Open your terminal, cd into the new folder, and run the following commands:
npm init -y
npm install vite --save-dev
npx vite
  1. Observe the raw HTML response returned by Vite:
curl -s http://localhost:5173/ | grep -A8 '<style>'
<style>
	/*
		This is valid CSS: the `\)` escapes the middle paren,
		but Vite will incorrectly wrap this with broken quotes
	*/
	p {
		background-image: url("public/awkward-name\")2.png);
	}
</style>

This produces invalid CSS because the URL string is quoted incorrectly around the middle (escaped) paren, and it also appends the trailing 2.png); afterwards.

What should happen

Either of these are valid and spec-compliant:

/* preserve original */
background-image: url(public/awkward-name\)2.png);

/* or proper quoted form */
background-image: url("public/awkward-name)2.png");

System Info

System:
    OS: macOS 26.0
    CPU: (10) arm64 Apple M2 Pro
    Memory: 102.13 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.15.0 - ~/.nvm/versions/node/v22.15.0/bin/node
    npm: 10.9.2 - ~/.nvm/versions/node/v22.15.0/bin/npm
    bun: 1.2.20 - ~/.bun/bin/bun
  Browsers:
    Chrome: 140.0.7339.186
    Chrome Canary: 142.0.7427.0
    Safari: 26.0
  npmPackages:
    vite: ^7.1.7 => 7.1.7

Used Package Manager

npm

Logs

No response

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions