MCPcopy
hub / github.com/tailwindlabs/tailwindcss / isValidStaticUtilityName

Function isValidStaticUtilityName

packages/tailwindcss/src/utilities.ts:6703–6790  ·  view source on GitHub ↗
(name: string)

Source from the content-addressed store, hash-verified

6701const DASH = 45
6702
6703export function isValidStaticUtilityName(name: string): boolean {
6704 let match = UTILITY_ROOT.exec(name)
6705 if (match === null) return false // Invalid root
6706
6707 let root = match[0]
6708 let value = name.slice(root.length)
6709
6710 // Root should not end in `-` if there is no value
6711 //
6712 // `tab-size-`
6713 // --------- Root
6714 if (value.length === 0 && root.endsWith('-')) {
6715 return false
6716 }
6717
6718 // No remaining value is valid
6719 //
6720 // `tab-size`
6721 // -------- Root
6722 if (value.length === 0) {
6723 return true
6724 }
6725
6726 // Any valid (static) utility should be valid including:
6727 // - Bare values with `.`: `p-1.5`
6728 // - Bare values with `%`: `w-50%`
6729 // - With an embedded modifier: `text-xs/8`
6730
6731 let seenSlash = false
6732 for (let i = 0; i < value.length; i++) {
6733 let charCode = value.charCodeAt(i)
6734 switch (charCode) {
6735 case PERCENT: {
6736 // A percentage is only valid at the end of the value
6737 if (i !== value.length - 1) return false
6738
6739 // A percent is only valid when preceded by a digit. E.g.: `w-%` is invalid
6740 let previousChar = value[i - 1] || root[root.length - 1] || ''
6741 let previousCharCode = previousChar.charCodeAt(0)
6742 if (previousCharCode < ZERO || previousCharCode > NINE) return false
6743 break
6744 }
6745
6746 case SLASH: {
6747 // A slash must be followed by at least 1 character. E.g.: `foo/` is invalid
6748 if (i === value.length - 1) return false
6749
6750 // A slash can only appear once. E.g.: `foo/bar/baz` is invalid
6751 if (seenSlash) return false
6752 seenSlash = true
6753 break
6754 }
6755
6756 case DOT: {
6757 // Dots are only allowed between digits. E.g.: `p-1.a` is invalid
6758 let previousChar = value[i - 1] || root[root.length - 1] || ''
6759 let previousCharCode = previousChar.charCodeAt(0)
6760 if (previousCharCode < ZERO || previousCharCode > NINE) return false

Callers 2

utilities.test.tsFile · 0.90
createCssUtilityFunction · 0.85

Calls 1

execMethod · 0.80

Tested by

no test coverage detected