From de8a9d659648b94ab31955aa0e6c399edb5594c5 Mon Sep 17 00:00:00 2001 From: MellowCo <799478052@qq.com> Date: Sun, 10 Jul 2022 19:48:44 +0800 Subject: [PATCH] feat: add tagged variants' pseudo in order https://github.com/unocss/unocss/issues/1157 --- src/variants/pseudo.ts | 27 +++++++-------------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/src/variants/pseudo.ts b/src/variants/pseudo.ts index e126ce5..f9aa754 100644 --- a/src/variants/pseudo.ts +++ b/src/variants/pseudo.ts @@ -82,7 +82,7 @@ const sortValue = (pseudo: string) => { } const taggedPseudoClassMatcher = (tag: string, parent: string, combinator: string): VariantObject => { - const rawRe = new RegExp(`${escapeRegExp(parent)}:`) + const rawRe = new RegExp(`^(${escapeRegExp(parent)}:)(\\S+)${escapeRegExp(combinator)}\\1`) const pseudoRE = new RegExp(`^${tag}-((?:(${PseudoClassFunctionsStr})-)?(${PseudoClassesStr}))[:-]`) const pseudoColonRE = new RegExp(`^${tag}-((?:(${PseudoClassFunctionsStr})-)?(${PseudoClassesColonStr}))[:]`) return { @@ -97,14 +97,13 @@ const taggedPseudoClassMatcher = (tag: string, parent: string, combinator: strin matcher: input.slice(match[0].length), handle: (input, next) => next({ ...input, - prefix: rawRe.test(input.prefix) - ? input.prefix.replace(rawRe, `${parent}${pseudo}:`) - : `${parent}${pseudo}${combinator}${input.prefix}`, + prefix: `${parent}${pseudo}${combinator}${input.prefix}`.replace(rawRe, '$1$2:'), sort: sortValue(match[3]), }), } } }, + multiPass: true, } } @@ -164,22 +163,10 @@ export const variantTaggedPseudoClasses = (options: PresetMiniOptions = {}): Var const attributify = !!options?.attributifyPseudo return [ - { - ...taggedPseudoClassMatcher('group', attributify ? '[group=""]' : '.group', ' '), - multiPass: true, - }, - { - ...taggedPseudoClassMatcher('peer', attributify ? '[peer=""]' : '.peer', '~'), - multiPass: true, - }, - { - ...taggedPseudoClassMatcher('parent', attributify ? '[parent=""]' : '.parent', '>'), - multiPass: true, - }, - { - ...taggedPseudoClassMatcher('previous', attributify ? '[previous=""]' : '.previous', '+'), - multiPass: true, - }, + taggedPseudoClassMatcher('group', attributify ? '[group=""]' : '.group', ' '), + taggedPseudoClassMatcher('peer', attributify ? '[peer=""]' : '.peer', '~'), + taggedPseudoClassMatcher('parent', attributify ? '[parent=""]' : '.parent', '>'), + taggedPseudoClassMatcher('previous', attributify ? '[previous=""]' : '.previous', '+'), ] }