Skip to content
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

CSS modules with path which contains spaces does not work as expected #1626

Open
JounQin opened this issue Dec 26, 2024 · 4 comments
Open

CSS modules with path which contains spaces does not work as expected #1626

JounQin opened this issue Dec 26, 2024 · 4 comments

Comments

@JounQin
Copy link
Contributor

JounQin commented Dec 26, 2024

Bug report

Actual Behavior

generated src_render_js.js:

image

source render.js:

import * as styles from './space folder/render.module.css';

export function render() {
  const el = document.createElement('div');
  el.className = styles.text; // styles.text contains a space which breaks styling
  document.getElementsByTagName('body')[0].appendChild(el);
  el.innerHTML = 'hello, world';
}

Expected Behavior

https://stackoverflow.com/questions/50812118/is-it-possible-to-use-the-space-character-in-css-class-names

The generated class is src-space folder-render-module__text-WVNeAh which contains a space, but it's not possible to escape spaces, so the spaces in path should be changed to __ for example.

How Do We Reproduce?

https://github.com/JounQin/rspack-css-modules-spaces

Run pnpm dev:webpack

Please paste the results of npx webpack-cli info here, and mention other relevant information

@JounQin
Copy link
Contributor Author

JounQin commented Dec 26, 2024

I tried to add space pattern into

const filenameReservedRegex = /[<>:"/\\|?*]/g;
and then it works as expected, I'd raise a PR if it is accepted.

@JounQin
Copy link
Contributor Author

JounQin commented Dec 26, 2024

Not sure if #997 related.

@JounQin
Copy link
Contributor Author

JounQin commented Jan 13, 2025

@alexander-akait Any time to take a look?

@alexander-akait
Copy link
Member

Yeah, technically we have CSS.escape to escape special character before inserting them in JS and it works good for non space characters, but there are some limitation with the class attribute

CSS.escape('1@a'); // -> \\31 \\@a, and you can using them in HTML class attribute
CSS.escape('a b'); // -> a\\ b, you can't

But https://html.spec.whatwg.org/multipage/dom.html#global-attributes:classes-2

When specified on HTML elements, the class attribute must have a value that is a set of space-separated tokens representing the various classes that the element belongs to.

Let's fix it

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

No branches or pull requests

2 participants