tailwindcss/packages/tailwindcss/src/utils/segment.test.ts
Robin Malfait cb17447ff1
Ensure strings are consumed as-is when using internal segment() (#13608)
* ensure we handle strings as-in

When encountering strings when using `segment` we didn't really treat
them as actual strings. This means that if you used any parens,
brackets, or curlies then we wanted them to be properly balanced.

This should not be the case, whenever we encounter a string, we want to
consume it as-is and don't want to worry about bracket balancing. We
will now consume it until the end of the string (and make sure that
escaped closing quotes are not seen as real closing quotes).

* update changelog

* drop unnecessary test

Already had this test

* ensure we utilities and variants defined

* add example test that parses with unbalanced brackets inside quotes

* improve changelog entry

* hoist comment
2024-04-30 12:22:11 -04:00

53 lines
1.8 KiB
TypeScript

import { expect, it } from 'vitest'
import { segment } from './segment'
it('should result in a single segment when the separator is not present', () => {
expect(segment('foo', ':')).toEqual(['foo'])
})
it('should split by the separator', () => {
expect(segment('foo:bar:baz', ':')).toEqual(['foo', 'bar', 'baz'])
})
it('should not split inside of parens', () => {
expect(segment('a:(b:c):d', ':')).toEqual(['a', '(b:c)', 'd'])
})
it('should not split inside of brackets', () => {
expect(segment('a:[b:c]:d', ':')).toEqual(['a', '[b:c]', 'd'])
})
it('should not split inside of curlies', () => {
expect(segment('a:{b:c}:d', ':')).toEqual(['a', '{b:c}', 'd'])
})
it('should not split inside of double quotes', () => {
expect(segment('a:"b:c":d', ':')).toEqual(['a', '"b:c"', 'd'])
})
it('should not split inside of single quotes', () => {
expect(segment("a:'b:c':d", ':')).toEqual(['a', "'b:c'", 'd'])
})
it('should not crash when double quotes are unbalanced', () => {
expect(segment('a:"b:c:d', ':')).toEqual(['a', '"b:c:d'])
})
it('should not crash when single quotes are unbalanced', () => {
expect(segment("a:'b:c:d", ':')).toEqual(['a', "'b:c:d"])
})
it('should skip escaped double quotes', () => {
expect(segment(String.raw`a:"b:c\":d":e`, ':')).toEqual(['a', String.raw`"b:c\":d"`, 'e'])
})
it('should skip escaped single quotes', () => {
expect(segment(String.raw`a:'b:c\':d':e`, ':')).toEqual(['a', String.raw`'b:c\':d'`, 'e'])
})
it('should split by the escape sequence which is escape as well', () => {
expect(segment('a\\b\\c\\d', '\\')).toEqual(['a', 'b', 'c', 'd'])
expect(segment('a\\(b\\c)\\d', '\\')).toEqual(['a', '(b\\c)', 'd'])
expect(segment('a\\[b\\c]\\d', '\\')).toEqual(['a', '[b\\c]', 'd'])
expect(segment('a\\{b\\c}\\d', '\\')).toEqual(['a', '{b\\c}', 'd'])
})