Skip to content

Commit d33880c

Browse files
committed
Vue: Handle more edge cases
- 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. - Handle `-` in HTML element names. - Explicitely mark `=` as an operator token. - Tests added. - Ref: https://codeberg.org/forgejo/forgejo/issues/2945
1 parent 32c053f commit d33880c

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

Diff for: lexers/embedded/vue.xml

+6-4
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,10 @@
8383
<token type="LiteralString"/>
8484
</bygroups>
8585
</rule>
86-
<rule pattern="(:[\S]+)(=&#34;[\S]+&#34;)">
86+
<rule pattern="(:[\S]+)(=)(&#34;[\S]+&#34;)">
8787
<bygroups>
8888
<token type="NameTag"/>
89+
<token type="Operator"/>
8990
<token type="LiteralString"/>
9091
</bygroups>
9192
</rule>
@@ -104,9 +105,10 @@
104105
<token type="Punctuation"/>
105106
</bygroups>
106107
</rule>
107-
<rule pattern="(v-[\w]+)(=&#34;[\S]+&#34;)(&gt;)">
108+
<rule pattern="(v-[\w]+)(=)(&#34;[\S ]+&#34;)(&gt;|\s)">
108109
<bygroups>
109110
<token type="NameTag"/>
111+
<token type="Operator"/>
110112
<token type="LiteralString"/>
111113
<token type="Punctuation"/>
112114
</bygroups>
@@ -258,14 +260,14 @@
258260
</rule>
259261
</state>
260262
<state name="vue">
261-
<rule pattern="(&lt;)([\w]+)">
263+
<rule pattern="(&lt;)([\w-]+)">
262264
<bygroups>
263265
<token type="Punctuation"/>
264266
<token type="NameTag"/>
265267
</bygroups>
266268
<push state="tag"/>
267269
</rule>
268-
<rule pattern="(&lt;)(/)([\w]+)(&gt;)">
270+
<rule pattern="(&lt;)(/)([\w-]+)(&gt;)">
269271
<bygroups>
270272
<token type="Punctuation"/>
271273
<token type="Punctuation"/>

Diff for: lexers/testdata/vue.actual

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
<template>
22
<button class="button">This is MyButton</button>
3+
<ul-extendded v-if="examples.length" bordered>
4+
<li
5+
v-for="example in examples"
6+
:key="`${example.id}`"
7+
/>
8+
</ul-extendded>
39
</template>
410

511
<script>

Diff for: lexers/testdata/vue.expected

+28
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,34 @@
1818
{"type":"Punctuation","value":"\u003c/"},
1919
{"type":"NameTag","value":"button"},
2020
{"type":"Punctuation","value":"\u003e"},
21+
{"type":"Text","value":"\n "},
22+
{"type":"Punctuation","value":"\u003c"},
23+
{"type":"NameTag","value":"ul-extendded"},
24+
{"type":"Text","value":" "},
25+
{"type":"NameTag","value":"v-if"},
26+
{"type":"Operator","value":"="},
27+
{"type":"LiteralString","value":"\"examples.length\""},
28+
{"type":"Punctuation","value":" "},
29+
{"type":"NameAttribute","value":"bordered"},
30+
{"type":"Punctuation","value":"\u003e"},
31+
{"type":"Text","value":"\n "},
32+
{"type":"Punctuation","value":"\u003c"},
33+
{"type":"NameTag","value":"li"},
34+
{"type":"Text","value":"\n "},
35+
{"type":"NameTag","value":"v-for"},
36+
{"type":"Operator","value":"="},
37+
{"type":"LiteralString","value":"\"example in examples\""},
38+
{"type":"Punctuation","value":"\n"},
39+
{"type":"Text","value":" "},
40+
{"type":"NameTag","value":":key"},
41+
{"type":"Operator","value":"="},
42+
{"type":"LiteralString","value":"\"`${example.id}`\""},
43+
{"type":"Text","value":"\n "},
44+
{"type":"Punctuation","value":"/\u003e"},
45+
{"type":"Text","value":"\n "},
46+
{"type":"Punctuation","value":"\u003c/"},
47+
{"type":"NameTag","value":"ul-extendded"},
48+
{"type":"Punctuation","value":"\u003e"},
2149
{"type":"Text","value":"\n"},
2250
{"type":"Punctuation","value":"\u003c/"},
2351
{"type":"NameTag","value":"template"},

0 commit comments

Comments
 (0)