Skip to content

Commit

Permalink
Vue: Better parsing of v- directives
Browse files Browse the repository at this point in the history
- The parsing of `v-` directives expected that this always ended in
`">`, however it is possible that there are other attributes after an
`v-` directive also it made the assumption that there couldn't be any
spaces in the value of the directive. Therefore it could result in
incorrect lexing where almost the whole file could be marked as an
LiteralString.
- Explicitely mark `=` as an operator token.
- Tests added.
- Ref: https://codeberg.org/forgejo/forgejo/issues/2945
  • Loading branch information
Gusted committed Apr 1, 2024
1 parent 32c053f commit 90531d5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lexers/embedded/vue.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,10 @@
<token type="LiteralString"/>
</bygroups>
</rule>
<rule pattern="(:[\S]+)(=&#34;[\S]+&#34;)">
<rule pattern="(:[\S]+)(=)(&#34;[\S]+&#34;)">
<bygroups>
<token type="NameTag"/>
<token type="Operator"/>
<token type="LiteralString"/>
</bygroups>
</rule>
Expand All @@ -104,9 +105,10 @@
<token type="Punctuation"/>
</bygroups>
</rule>
<rule pattern="(v-[\w]+)(=&#34;[\S]+&#34;)(&gt;)">
<rule pattern="(v-[\w]+)(=)(&#34;[\S| ]+&#34;)(&gt;|\s)">
<bygroups>
<token type="NameTag"/>
<token type="Operator"/>
<token type="LiteralString"/>
<token type="Punctuation"/>
</bygroups>
Expand Down
6 changes: 6 additions & 0 deletions lexers/testdata/vue.actual
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<template>
<button class="button">This is MyButton</button>
<ul v-if="examples.length" bordered>
<li
v-for="example in examples"
:key="`${example.id}`"
/>
</ul>
</template>

<script>
Expand Down
28 changes: 28 additions & 0 deletions lexers/testdata/vue.expected
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,34 @@
{"type":"Punctuation","value":"\u003c/"},
{"type":"NameTag","value":"button"},
{"type":"Punctuation","value":"\u003e"},
{"type":"Text","value":"\n "},
{"type":"Punctuation","value":"\u003c"},
{"type":"NameTag","value":"ul"},
{"type":"Text","value":" "},
{"type":"NameTag","value":"v-if"},
{"type":"Operator","value":"="},
{"type":"LiteralString","value":"\"examples.length\""},
{"type":"Punctuation","value":" "},
{"type":"NameAttribute","value":"bordered"},
{"type":"Punctuation","value":"\u003e"},
{"type":"Text","value":"\n "},
{"type":"Punctuation","value":"\u003c"},
{"type":"NameTag","value":"li"},
{"type":"Text","value":"\n "},
{"type":"NameTag","value":"v-for"},
{"type":"Operator","value":"="},
{"type":"LiteralString","value":"\"example in examples\""},
{"type":"Punctuation","value":"\n"},
{"type":"Text","value":" "},
{"type":"NameTag","value":":key"},
{"type":"Operator","value":"="},
{"type":"LiteralString","value":"\"`${example.id}`\""},
{"type":"Text","value":"\n "},
{"type":"Punctuation","value":"/\u003e"},
{"type":"Text","value":"\n "},
{"type":"Punctuation","value":"\u003c/"},
{"type":"NameTag","value":"ul"},
{"type":"Punctuation","value":"\u003e"},
{"type":"Text","value":"\n"},
{"type":"Punctuation","value":"\u003c/"},
{"type":"NameTag","value":"template"},
Expand Down

0 comments on commit 90531d5

Please sign in to comment.