Skip to content

Commit

Permalink
feat: support attributify usage with < sign
Browse files Browse the repository at this point in the history
  • Loading branch information
sibbng committed Feb 26, 2023
1 parent be6cf6b commit 8942a52
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
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
22 changes: 13 additions & 9 deletions packages/shared-common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,24 +88,28 @@ export function getMatchedPositions(code: string, matched: string[], hasVariantG
Array.from(matched)
.forEach((v) => {
const match = isAttributifySelector(v)

if (!match) {
// highlight classes that includes `><`
if (v.match(/[><]/)) {
for (const match of code.matchAll(new RegExp(escapeRegExp(v), 'g'))) {
const start = match.index!
const end = start + match[0].length
result.push([start, end, match[0]])
}
}
highlightLessBiggerThanSign(v)
plain.add(v)
}
else if (!match[2]) {
highlightLessBiggerThanSign(match[1])
plain.add(match[1])
}
else { attributify.push(match) }
})

// highlight classes that includes `><`
function highlightLessBiggerThanSign(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) => {
Expand Down
28 changes: 28 additions & 0 deletions test/pos.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,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>'))
.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

0 comments on commit 8942a52

Please sign in to comment.