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

Fix: Module types should support urls without quotes #2467

Merged
merged 1 commit into from Aug 16, 2023

Conversation

jackw
Copy link
Contributor

@jackw jackw commented Aug 8, 2023

This PR addresses the issue of undefined being concatenated into url strings when urls aren't wrapped in quotes.

I believe the fixture css I've added is valid for urls. I've not used chomp before. It changed some dist files when I ran it locally but I'm guessing you wouldn't want me to commit these changes in the PR. 🤔

fixes: #2460

@@ -40,7 +40,7 @@ import { resolveUrl } from '../common.js';
return res.text()
.then(function (source) {
source = source.replace(/url\(\s*(?:(["'])((?:\\.|[^\n\\"'])+)\1|((?:\\.|[^\s,"'()\\])+))\s*\)/g, function (match, quotes, relUrl1, relUrl2) {
return 'url(' + quotes + resolveUrl(relUrl1 || relUrl2, url) + quotes + ')';
return ['url(', quotes, resolveUrl(relUrl1 || relUrl2, url), quotes, ')'].join('');
Copy link
Member

Choose a reason for hiding this comment

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

Can you please explain the bug that this is resolving? This refactoring looks entirely equivalent to me?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, I should have probably added the following to the PR description...

It's valid CSS to not include quotes in urls like background-image: url(my/image.png);. However the code above that tries to resolve urls doesn't seem to cater for this. So if quotes is undefined the string concatenation adds undefined to the result and the image fails to load. e.g.

'url(' + undefined + 'my/image.png' + undefined + ')' // --> "url(undefinedmy/image.pngundefined)"

However using array.join we can take advantage of the spec to discard unwanted undefined from the result.

If element0 is undefined or null, let R be the empty String; otherwise, Let R be ToString(element0).

e.g.

['url(', undefined, 'my/image.png', undefined, ')'].join('') // --> "url(my/image.png)" 

I'm more than happy to refactor if you feel the approach I've taken isn't explicit enough.

@guybedford
Copy link
Member

Thanks for clarifying, makes sense now!

@guybedford guybedford merged commit 61bc72f into systemjs:main Aug 16, 2023
1 of 2 checks passed
@JennerChen
Copy link

JennerChen commented Dec 23, 2023

@guybedford latest version 6.14.2 still not working. maybe need release new one.
system.min.js still use old one
image
https://g.alicdn.com/code/lib/systemjs/6.14.2/system.min.js

system.js is correct
image
https://g.alicdn.com/code/lib/systemjs/6.14.2/system.js

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.

systemjs load css suport url(image/test.svg) without quotes in url
3 participants