Skip to content

Commit

Permalink
feat: update content-x and change handler type (unocss/unocss#1741)
Browse files Browse the repository at this point in the history
  • Loading branch information
MellowCo committed Oct 28, 2022
1 parent 386b392 commit 641e287
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 39 deletions.
3 changes: 1 addition & 2 deletions src/rules/static.ts
Expand Up @@ -77,8 +77,7 @@ export const contentVisibility: Rule<Theme>[] = [
]

export const contents: Rule[] = [
[/^content-\[(.+)\]$/, ([, v]) => ({ content: `"${v}"` })],
[/^content-(\$.+)]$/, ([, v]) => ({ content: h.cssvar(v) })],
[/^content-(.+)$/, ([, v]) => ({ content: h.bracket.cssvar(v) })],
['content-empty', { content: '""' }],
['content-none', { content: '""' }],
]
Expand Down
12 changes: 7 additions & 5 deletions src/utils/handlers/handlers.ts
Expand Up @@ -118,7 +118,7 @@ export function fraction(str: string) {
if (!Number.isNaN(num))
return `${round(num * 100)}%`
}
const bracketTypeRe = /^\[(color|length|position|raw|string):/i
const bracketTypeRe = /^\[(color|length|position|quoted|string):/i
function bracketWithType(str: string, requiredType?: string) {
if (str && str.startsWith('[') && str.endsWith(']')) {
let base: string | undefined
Expand Down Expand Up @@ -153,13 +153,15 @@ function bracketWithType(str: string, requiredType?: string) {
return

switch (hintedType) {
case 'raw': return base

case 'string': return base
.replace(/(^|[^\\])_/g, '$1 ')
.replace(/\\_/g, '_')
.replace(/(['\\])/g, '\\$1')
.replace(/^(.+)$/, '\'$1\'')

case 'quoted': return base
.replace(/(^|[^\\])_/g, '$1 ')
.replace(/\\_/g, '_')
.replace(/(["\\])/g, '\\$1')
.replace(/^(.+)$/, '"$1"')
}

return base
Expand Down
13 changes: 7 additions & 6 deletions test/__snapshots__/preset-mini.test.ts.snap
Expand Up @@ -155,7 +155,6 @@ page,::before,::after{--un-rotate:0;--un-rotate-x:0;--un-rotate-y:0;--un-rotate-
.bg-repeat-revert{background-repeat:revert;}
.bg-origin-border{background-origin:border-box;}
.bg-origin-initial{background-origin:initial;}
.content-\\\\$unocss-var{align-content:var(--unocss-var);}
.fw-\\\\$variable{font-weight:var(--variable);}
.items-\\\\$size{align-items:var(--size);}
.ws-\\\\$variable{white-space:var(--variable);}
Expand Down Expand Up @@ -416,11 +415,13 @@ div:hover .group-\\\\[div\\\\:hover\\\\]-\\\\[combinator\\\\:test-4\\\\]{combina
.content-visibility-hidden{content-visibility:hidden;}
.content-visibility-auto{content-visibility:auto;}
.content-visibility-unset{content-visibility:unset;}
.after\\\\:content-\\\\[unocss\\\\]::after,
.content-\\\\[unocss\\\\]{content:\\"unocss\\";}
.content-\\\\[\\\\!\\\\]{content:\\"!\\";}
.content-\\\\[attr\\\\(dashed-attr\\\\)\\\\]{content:\\"attr(dashed-attr)\\";}
.content-\\\\[attr\\\\(underlined_attr\\\\)\\\\]{content:\\"attr(underlined_attr)\\";}
.after\\\\:content-\\\\[quoted\\\\:unocss\\\\]::after,
.content-\\\\[quoted\\\\:unocss\\\\]{content:\\"unocss\\";}
.content-\\\\[normal\\\\]{content:normal;}
.content-\\\\[quoted\\\\:\\\\!\\\\]{content:\\"!\\";}
.content-\\\\[string\\\\:attr\\\\(dashed-attr\\\\)\\\\]{content:attr(dashed-attr);}
.content-\\\\[string\\\\:attr\\\\(underlined\\\\\\\\_attr\\\\)\\\\]{content:attr(underlined_attr);}
.content-\\\\$unocss-var{content:var(--unocss-var);}
.content-empty,
.content-none{content:\\"\\";}
.font-\\\\[system-ui\\\\]{font-family:system-ui;}
Expand Down
13 changes: 7 additions & 6 deletions test/__snapshots__/preset-weapp-rules.test.ts.snap
Expand Up @@ -121,7 +121,6 @@ page,::before,::after{--un-rotate:0;--un-rotate-x:0;--un-rotate-y:0;--un-rotate-
.bg-repeat-revert{background-repeat:revert;}
.bg-origin-border{background-origin:border-box;}
.bg-origin-initial{background-origin:initial;}
.content--rr1-unocss-var{align-content:var(--unocss-var);}
.fw--rr1-variable{font-weight:var(--variable);}
.items--rr1-size{align-items:var(--size);}
.ws--rr1-variable{white-space:var(--variable);}
Expand Down Expand Up @@ -379,11 +378,13 @@ div:hover .group--fl1r-div-cr1-hover-fr1r---fl1r-combinator-cr1-test-4-fr1r-{com
.content-visibility-hidden{content-visibility:hidden;}
.content-visibility-auto{content-visibility:auto;}
.content-visibility-unset{content-visibility:unset;}
.after-cr1-content--fl1r-unocss-fr1r-::after,
.content--fl1r-unocss-fr1r-{content:\\"unocss\\";}
.content--fl1r--er1--fr1r-{content:\\"!\\";}
.content--fl1r-attr-bl1r-dashed-attr-br1r--fr1r-{content:\\"attr(dashed-attr)\\";}
.content--fl1r-attr-bl1r-underlined_attr-br1r--fr1r-{content:\\"attr(underlined_attr)\\";}
.after-cr1-content--fl1r-quoted-cr1-unocss-fr1r-::after,
.content--fl1r-quoted-cr1-unocss-fr1r-{content:\\"unocss\\";}
.content--fl1r-normal-fr1r-{content:normal;}
.content--fl1r-quoted-cr1--er1--fr1r-{content:\\"!\\";}
.content--fl1r-string-cr1-attr-bl1r-dashed-attr-br1r--fr1r-{content:attr(dashed-attr);}
.content--fl1r-string-cr1-attr-bl1r-underlined\\\\\\\\_attr-br1r--fr1r-{content:attr(underlined_attr);}
.content--rr1-unocss-var{content:var(--unocss-var);}
.content-empty,
.content-none{content:\\"\\";}
.font--fl1r-system-ui-fr1r-{font-family:system-ui;}
Expand Down
13 changes: 7 additions & 6 deletions test/__snapshots__/preset-weapp.test.ts.snap
Expand Up @@ -121,7 +121,6 @@ page,::before,::after{--un-rotate:0;--un-rotate-x:0;--un-rotate-y:0;--un-rotate-
.bg-repeat-revert{background-repeat:revert;}
.bg-origin-border{background-origin:border-box;}
.bg-origin-initial{background-origin:initial;}
.content--r-unocss-var{align-content:var(--unocss-var);}
.fw--r-variable{font-weight:var(--variable);}
.items--r-size{align-items:var(--size);}
.ws--r-variable{white-space:var(--variable);}
Expand Down Expand Up @@ -379,11 +378,13 @@ div:hover .group--fl-div-c-hover-fr---fl-combinator-c-test-4-fr-{combinator:test
.content-visibility-hidden{content-visibility:hidden;}
.content-visibility-auto{content-visibility:auto;}
.content-visibility-unset{content-visibility:unset;}
.after-c-content--fl-unocss-fr-::after,
.content--fl-unocss-fr-{content:\\"unocss\\";}
.content--fl--e--fr-{content:\\"!\\";}
.content--fl-attr-bl-dashed-attr-br--fr-{content:\\"attr(dashed-attr)\\";}
.content--fl-attr-bl-underlined_attr-br--fr-{content:\\"attr(underlined_attr)\\";}
.after-c-content--fl-quoted-c-unocss-fr-::after,
.content--fl-quoted-c-unocss-fr-{content:\\"unocss\\";}
.content--fl-normal-fr-{content:normal;}
.content--fl-quoted-c--e--fr-{content:\\"!\\";}
.content--fl-string-c-attr-bl-dashed-attr-br--fr-{content:attr(dashed-attr);}
.content--fl-string-c-attr-bl-underlined\\\\\\\\_attr-br--fr-{content:attr(underlined_attr);}
.content--r-unocss-var{content:var(--unocss-var);}
.content-empty,
.content-none{content:\\"\\";}
.font--fl-system-ui-fr-{font-family:system-ui;}
Expand Down
11 changes: 6 additions & 5 deletions test/assets/preset-mini-targets.ts
Expand Up @@ -740,10 +740,11 @@ export const presetMiniTargets: string[] = [
// content
'content-empty',
'content-none',
'content-[!]',
'content-[unocss]',
'content-[attr(dashed-attr)]',
'content-[attr(underlined_attr)]',
'content-[normal]',
'content-[quoted:!]',
'content-[quoted:unocss]',
'content-[string:attr(dashed-attr)]',
'content-[string:attr(underlined\\_attr)]',
'content-$unocss-var',

// content visibility
Expand Down Expand Up @@ -1056,7 +1057,7 @@ export const presetMiniTargets: string[] = [

// variants - pseudo elements
'before:translate-y-full',
'after:content-[unocss]',
'after:content-[quoted:unocss]',
'placeholder-opacity-60',
'placeholder-color-opacity-60',
'placeholder-color-red-1',
Expand Down
17 changes: 8 additions & 9 deletions test/handler.test.ts
Expand Up @@ -32,16 +32,15 @@ describe('value handler', () => {
expect(h.bracket('[var(--A\\_B)]')).eql('var(--A_B)')
})

it('bracket raw-type', () => {
expect(h.bracket('[raw:a b]')).eql('a b')
expect(h.bracket('[raw:a_b]')).eql('a_b')
expect(h.bracket('[raw:a\\_b]')).eql('a\\_b')
expect(h.bracket('[raw:attr("data-label") ":" attr("data-value")]')).eql('attr("data-label") ":" attr("data-value")')
it('bracket string-type', () => {
expect(h.bracket('[string:a_b]')).eql('a b')
expect(h.bracket('[string:a\\_b]')).eql('a\_b')
expect(h.bracket('[string:attr(data-label)_":_"]')).eql('attr(data-label) ": "')
})

it('bracket string-type', () => {
expect(h.bracket('[string:a_b]')).eql('\'a b\'')
expect(h.bracket('[string:a\\_b]')).eql('\'a_b\'')
expect(h.bracket('[string:with-\\,-\'-and-"]')).eql('\'with-\\\\,-\\\'-and-"\'')
it('bracket quoted-type', () => {
expect(h.bracket('[quoted:a_b]')).eql('"a b"')
expect(h.bracket('[quoted:a\\_b]')).eql('"a_b"')
expect(h.bracket('[quoted:\'with-\\,-\'-and-"]')).toMatchInlineSnapshot('"\\"\'with-\\\\\\\\,-\'-and-\\\\\\"\\""')
})
})

0 comments on commit 641e287

Please sign in to comment.