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 multiline input not working anymore #459

Merged
merged 2 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 9 additions & 14 deletions src/lib/merge-classlist.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { ConfigUtils } from './config-utils'
import { IMPORTANT_MODIFIER, sortModifiers } from './parse-class-name'

const SPLIT_CLASSES_REGEX = /\s+/

export const mergeClassList = (classList: string, configUtils: ConfigUtils) => {
const { parseClassName, getClassGroupId, getConflictingClassGroupIds } = configUtils

Expand All @@ -12,23 +14,12 @@ export const mergeClassList = (classList: string, configUtils: ConfigUtils) => {
* @example 'md:!pr'
*/
const classGroupsInConflict: string[] = []
const classNames = classList.trim().split(SPLIT_CLASSES_REGEX)

let result = ''
let index = classList.length - 1

while (index >= 0) {
while (classList[index] === ' ') {
index -= 1
}

if (index < 0) {
break
}

const nextIndex = classList.lastIndexOf(' ', index)
const originalClassName = classList.slice(nextIndex + 1, index + 1)

index = nextIndex
for (let index = classNames.length - 1; index >= 0; index -= 1) {
const originalClassName = classNames[index]!

const { modifiers, hasImportantModifier, baseClassName, maybePostfixModifierPosition } =
parseClassName(originalClassName)
Expand All @@ -42,13 +33,15 @@ export const mergeClassList = (classList: string, configUtils: ConfigUtils) => {

if (!classGroupId) {
if (!hasPostfixModifier) {
// Not a Tailwind class
result = originalClassName + (result.length > 0 ? ' ' + result : result)
continue
}

classGroupId = getClassGroupId(baseClassName)

if (!classGroupId) {
// Not a Tailwind class
result = originalClassName + (result.length > 0 ? ' ' + result : result)
continue
}
Expand All @@ -65,6 +58,7 @@ export const mergeClassList = (classList: string, configUtils: ConfigUtils) => {
const classId = modifierId + classGroupId

if (classGroupsInConflict.includes(classId)) {
// Tailwind class omitted due to conflict
continue
}

Expand All @@ -76,6 +70,7 @@ export const mergeClassList = (classList: string, configUtils: ConfigUtils) => {
classGroupsInConflict.push(modifierId + group)
}

// Tailwind class not in conflict
result = originalClassName + (result.length > 0 ? ' ' + result : result)
}

Expand Down
6 changes: 6 additions & 0 deletions tests/wonky-inputs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,10 @@ test('handles wonky inputs', () => {
expect(twMerge(' block ')).toBe('block')
expect(twMerge(' block px-2 py-4 ')).toBe('block px-2 py-4')
expect(twMerge(' block px-2', ' ', ' py-4 ')).toBe('block px-2 py-4')
expect(twMerge('block\npx-2')).toBe('block px-2')
expect(twMerge('\nblock\npx-2\n')).toBe('block px-2')
expect(twMerge(' block\n \n px-2 \n py-4 ')).toBe('block px-2 py-4')
expect(twMerge('\r block\n\r \n px-2 \n py-4 ')).toBe(
'block px-2 py-4',
)
})
Loading