Skip to content

Commit ab9fd95

Browse files
Use less hacky fix for urls detected as custom properties (#7275)
* Use less hacky fix for urls detected as custom properties * Add more test cases * Update changelog
1 parent d87bdb2 commit ab9fd95

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
- Fix preflight border color fallback ([#7288](https://github.com/tailwindlabs/tailwindcss/pull/7288))
1313
- Correctly parse shadow lengths without a leading zero ([#7289](https://github.com/tailwindlabs/tailwindcss/pull/7289))
1414
- Don't crash when scanning extremely long class candidates ([#7331](https://github.com/tailwindlabs/tailwindcss/pull/7331))
15+
- Use less hacky fix for urls detected as custom properties ([#7275](https://github.com/tailwindlabs/tailwindcss/pull/7275))
1516

1617
## [3.0.18] - 2022-01-28
1718

src/lib/generateRules.js

+28-2
Original file line numberDiff line numberDiff line change
@@ -262,11 +262,37 @@ function parseRules(rule, cache, options = {}) {
262262
const IS_VALID_PROPERTY_NAME = /^[a-z_-]/
263263

264264
function isValidPropName(name) {
265-
// TODO: properly fix this!
266-
return IS_VALID_PROPERTY_NAME.test(name) && !name.startsWith('http')
265+
return IS_VALID_PROPERTY_NAME.test(name)
266+
}
267+
268+
/**
269+
* @param {string} declaration
270+
* @returns {boolean}
271+
*/
272+
function looksLikeUri(declaration) {
273+
// Quick bailout for obvious non-urls
274+
// This doesn't support schemes that don't use a leading // but that's unlikely to be a problem
275+
if (!declaration.includes('://')) {
276+
return false
277+
}
278+
279+
try {
280+
const url = new URL(declaration)
281+
return url.scheme !== '' && url.host !== ''
282+
} catch (err) {
283+
// Definitely not a valid url
284+
return false
285+
}
267286
}
268287

269288
function isParsableCssValue(property, value) {
289+
// We don't want to to treat [https://example.com] as a custom property
290+
// Even though, according to the CSS grammar, it's a totally valid CSS declaration
291+
// So we short-circuit here by checking if the custom property looks like a url
292+
if (looksLikeUri(`${property}:${value}`)) {
293+
return false
294+
}
295+
270296
try {
271297
postcss.parse(`a{${property}:${value}}`).toResult()
272298
return true

tests/arbitrary-properties.test.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -352,13 +352,27 @@ it('should not generate invalid CSS', () => {
352352
let config = {
353353
content: [
354354
{
355-
raw: html`<div class="[https://en.wikipedia.org/wiki]"></div>`,
355+
raw: html`
356+
<div class="[https://en.wikipedia.org/wiki]"></div>
357+
<div class="[http://example.org]"></div>
358+
<div class="[http://example]"></div>
359+
<div class="[ftp://example]"></div>
360+
<div class="[stillworks:/example]"></div>
361+
`,
362+
363+
// NOTE: In this case `stillworks:/example` being generated is not ideal
364+
// but it at least doesn't produce invalid CSS when run through prettier
365+
// So we can let it through since it is technically valid
356366
},
357367
],
358368
corePlugins: { preflight: false },
359369
}
360370

361371
return run('@tailwind utilities', config).then((result) => {
362-
return expect(result.css).toMatchFormattedCss(css``)
372+
return expect(result.css).toMatchFormattedCss(css`
373+
.\[stillworks\:\/example\] {
374+
stillworks: /example;
375+
}
376+
`)
363377
})
364378
})

0 commit comments

Comments
 (0)