From 641e28797879c7dab9d43eb178daa05fab86706c Mon Sep 17 00:00:00 2001 From: MellowCo <799478052@qq.com> Date: Fri, 28 Oct 2022 13:44:35 +0800 Subject: [PATCH] feat: update content-x and change handler type (https://github.com/unocss/unocss/pull/1741) --- src/rules/static.ts | 3 +-- src/utils/handlers/handlers.ts | 12 +++++++----- test/__snapshots__/preset-mini.test.ts.snap | 13 +++++++------ .../preset-weapp-rules.test.ts.snap | 13 +++++++------ test/__snapshots__/preset-weapp.test.ts.snap | 13 +++++++------ test/assets/preset-mini-targets.ts | 11 ++++++----- test/handler.test.ts | 17 ++++++++--------- 7 files changed, 43 insertions(+), 39 deletions(-) diff --git a/src/rules/static.ts b/src/rules/static.ts index 8fe47c6..255136d 100644 --- a/src/rules/static.ts +++ b/src/rules/static.ts @@ -77,8 +77,7 @@ export const contentVisibility: Rule[] = [ ] 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: '""' }], ] diff --git a/src/utils/handlers/handlers.ts b/src/utils/handlers/handlers.ts index fd5c222..99431ba 100644 --- a/src/utils/handlers/handlers.ts +++ b/src/utils/handlers/handlers.ts @@ -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 @@ -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 diff --git a/test/__snapshots__/preset-mini.test.ts.snap b/test/__snapshots__/preset-mini.test.ts.snap index 48571ee..e472065 100644 --- a/test/__snapshots__/preset-mini.test.ts.snap +++ b/test/__snapshots__/preset-mini.test.ts.snap @@ -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);} @@ -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;} diff --git a/test/__snapshots__/preset-weapp-rules.test.ts.snap b/test/__snapshots__/preset-weapp-rules.test.ts.snap index 830211d..59052e6 100644 --- a/test/__snapshots__/preset-weapp-rules.test.ts.snap +++ b/test/__snapshots__/preset-weapp-rules.test.ts.snap @@ -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);} @@ -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;} diff --git a/test/__snapshots__/preset-weapp.test.ts.snap b/test/__snapshots__/preset-weapp.test.ts.snap index e0995d2..5ad7bbe 100644 --- a/test/__snapshots__/preset-weapp.test.ts.snap +++ b/test/__snapshots__/preset-weapp.test.ts.snap @@ -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);} @@ -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;} diff --git a/test/assets/preset-mini-targets.ts b/test/assets/preset-mini-targets.ts index 6769f7a..0b68eb0 100644 --- a/test/assets/preset-mini-targets.ts +++ b/test/assets/preset-mini-targets.ts @@ -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 @@ -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', diff --git a/test/handler.test.ts b/test/handler.test.ts index 4e04239..5eb832e 100644 --- a/test/handler.test.ts +++ b/test/handler.test.ts @@ -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-\\\\\\"\\""') }) })