Skip to content

Commit 53865e4

Browse files
committed
Use less hacky fix for urls detected as custom properties
1 parent ce98735 commit 53865e4

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

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

0 commit comments

Comments
 (0)