Skip to content

Commit 2d3a748

Browse files
authored
Ensure the percentage data type is validated correctly (#8015)
* ensure the `percentage` data type is validated correctly When checking for our data types, we have to make sure that each part is correct, this wasn't happening for the `percentage` data type, which meant that `top_right_50%` was a valid percentage value because it ended in `%` which is not correct. Because of this, the generated code was non-existent because we got a warning: The class `bg-[top_right_50%]` is ambiguous and matches multiple utilities. Use `bg-[length:top_right_50%]` for `background-size: top right 50%` Use `bg-[position:top_right_50%]` for `background-position: top right 50%` If this is content and not a class, replace it with `bg-[top_right_50%]` to silence this warning. But this is not correct because this can never be a valid background size due to the `top right` section. This fixes it by validating each part individually, and now we get generated css. * update changelog
1 parent 010f787 commit 2d3a748

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2323
- Allow for custom properties in `rgb`, `rgba`, `hsl` and `hsla` colors ([#7933](https://github.com/tailwindlabs/tailwindcss/pull/7933))
2424
- Remove autoprefixer as explicit peer-dependency to avoid invalid warnings in situations where it isn't actually needed ([#7949](https://github.com/tailwindlabs/tailwindcss/pull/7949))
2525
- Types: allow for arbitrary theme values (for 3rd party plugins) ([#7926](https://github.com/tailwindlabs/tailwindcss/pull/7926))
26+
- Ensure the `percentage` data type is validated correctly ([#8015](https://github.com/tailwindlabs/tailwindcss/pull/8015))
2627

2728
### Changed
2829

src/util/dataTypes.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ export function number(value) {
5757
}
5858

5959
export function percentage(value) {
60-
return /%$/g.test(value) || cssFunctions.some((fn) => new RegExp(`^${fn}\\(.+?%`).test(value))
60+
return value.split(UNDERSCORE).every((part) => {
61+
return /%$/g.test(part) || cssFunctions.some((fn) => new RegExp(`^${fn}\\(.+?%`).test(part))
62+
})
6163
}
6264

6365
let lengthUnits = [

tests/arbitrary-values.test.js

+22
Original file line numberDiff line numberDiff line change
@@ -384,3 +384,25 @@ it('should not output unparsable arbitrary CSS values', () => {
384384
return expect(result.css).toMatchFormattedCss(``)
385385
})
386386
})
387+
388+
// Issue: https://github.com/tailwindlabs/tailwindcss/issues/7997
389+
// `top_right_50%` was a valid percentage before introducing this change
390+
it('should correctly validate each part when checking for `percentage` data types', () => {
391+
let config = {
392+
content: [{ raw: html`<div class="bg-[top_right_50%]"></div>` }],
393+
corePlugins: { preflight: false },
394+
plugins: [],
395+
}
396+
397+
let input = css`
398+
@tailwind utilities;
399+
`
400+
401+
return run(input, config).then((result) => {
402+
expect(result.css).toMatchFormattedCss(css`
403+
.bg-\[top_right_50\%\] {
404+
background-position: top right 50%;
405+
}
406+
`)
407+
})
408+
})

0 commit comments

Comments
 (0)