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(vscode): fix the regressions caused by #2256 #2265

Merged
merged 3 commits into from
Mar 1, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/core/src/utils/variantGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const regexCache: Record<string, RegExp> = {}
export function makeRegexClassGroup(separators = ['-', ':']) {
const key = separators.join('|')
if (!regexCache[key])
regexCache[key] = new RegExp(`((?:[!@\\w+:_/-]|\\[&?>?:?\\S*\\])+?)(${key})\\(((?:[~!\\w\\s:/\\\\,%#.$?-]|\\[.*?\\])+?)\\)(?!\\s*?=>)`, 'gm')
regexCache[key] = new RegExp(`((?:[!@\\w+:_/-]|\\[&?>?:?\\S*\\])+?)(${key})\\(((?:[~!<>\\w\\s:/\\\\,%#.$?-]|\\[.*?\\])+?)\\)(?!\\s*?=>)`, 'gm')
regexCache[key].lastIndex = 0
return regexCache[key]
}
Expand Down
2 changes: 1 addition & 1 deletion packages/preset-attributify/src/extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const strippedPrefixes = [

const splitterRE = /[\s'"`;]+/g
const elementRE = /<[^>\s]*\s((?:'.*?'|".*?"|`.*?`|\{.*?\}|[^>]*?)*)/g
const valuedAttributeRE = /([?]|(?!\d|-{2}|-\d)[a-zA-Z0-9\u00A0-\uFFFF-_:!%-.~]+)=?(?:["]([^"]*)["]|[']([^']*)[']|[{]([^}]*)[}])?/gms
const valuedAttributeRE = /([?]|(?!\d|-{2}|-\d)[a-zA-Z0-9\u00A0-\uFFFF-_:!%-.~<]+)=?(?:["]([^"]*)["]|[']([^']*)[']|[{]([^}]*)[}])?/gms

export const defaultIgnoreAttributes = ['placeholder', 'fill', 'opacity']

Expand Down
24 changes: 19 additions & 5 deletions packages/shared-common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,31 @@ export function getMatchedPositions(code: string, matched: string[], hasVariantG
Array.from(matched)
.forEach((v) => {
const match = isAttributifySelector(v)
if (!match)
if (!match) {
highlightLessGreaterThanSign(v)
plain.add(v)
else if (!match[2])
}
else if (!match[2]) {
highlightLessGreaterThanSign(match[1])
plain.add(match[1])
else
attributify.push(match)
}
else { attributify.push(match) }
})

// highlight classes that includes `><`
function highlightLessGreaterThanSign(str: string) {
if (str.match(/[><]/)) {
for (const match of code.matchAll(new RegExp(escapeRegExp(str), 'g'))) {
const start = match.index!
const end = start + match[0].length
result.push([start, end, match[0]])
}
}
}

// highlight for plain classes
let start = 0
code.split(/([\s"'`;*]|:\(|\)"|\)\s)/g).forEach((i) => {
code.split(/([\s"'`;<>*]|:\(|\)"|\)\s)/g).forEach((i) => {
const end = start + i.length
if (isPug) {
result.push(...getPlainClassMatchedPositionsForPug(i, plain, start))
Expand Down
54 changes: 50 additions & 4 deletions test/pos.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('matched-positions', async () => {
],
})

expect(await match(uno, '<div border="b gray4 2 [&_span]:white"></div>'))
expect(await match(uno, '<div border="b gray4 2 [&_span]:white" hover="[&>span]:text-white" border></div>'))
.toMatchInlineSnapshot(`
[
[
Expand All @@ -41,6 +41,16 @@ describe('matched-positions', async () => {
37,
"[border=\\"[&_span]:white\\"]",
],
[
46,
65,
"[hover=\\"[&>span]:text-white\\"]",
],
[
67,
73,
"border",
],
]
`)
})
Expand Down Expand Up @@ -176,12 +186,15 @@ describe('matched-positions', async () => {
presets: [
presetUno(),
],
shortcuts: {
'<custom-shortcut': 'text-lg',
},
transformers: [
transformerVariantGroup(),
],
})

expect(await match(uno, '<div class="hover:(h-4 w-4 bg-green-300) disabled:opacity-50"></div>'))
expect(await match(uno, '<div class="hover:(h-4 w-4 bg-green-300 <custom-shortcut) disabled:opacity-50"></div>'))
.toMatchInlineSnapshot(`
[
[
Expand All @@ -200,8 +213,13 @@ describe('matched-positions', async () => {
"hover:bg-green-300",
],
[
41,
60,
40,
56,
"hover:<custom-shortcut",
],
[
58,
77,
"disabled:opacity-50",
],
]
Expand Down Expand Up @@ -309,6 +327,34 @@ describe('matched-positions-pug', async () => {
`)
})

test('attributify `><`', async () => {
const uno = createGenerator({
presets: [
presetAttributify(),
presetUno(),
],
shortcuts: {
'<custom-shortcut': 'text-teal',
},
})

expect(await match(uno, '<div border></div><div <custom-shortcut></div>'))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is so weird, is it even a valid char in attribute?

Copy link
Member Author

@sibbng sibbng Feb 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know the specification part. The only thing I can say is Nu Html Checker didn't like it https://validator.w3.org/nu/#cl1c5 But on the browser's side, it's just working. So, if the browser parser is fault-tolerant enough to support it, so should we. You can think of it as a stability test. It's not there because we endorse it, it's there because we're capable of it. Benefitting from it is up to end users.

.toMatchInlineSnapshot(`
[
[
5,
11,
"border",
],
[
23,
39,
"<custom-shortcut",
],
]
`)
})

test('@unocss/transformer-directives', async () => {
// \n could not be include
// div.p2(class="btn-center{@apply p1 m1;\n}") -> pug parse error
Expand Down