Skip to content

Commit

Permalink
Fix Perl and Mojolicious conflict on variables
Browse files Browse the repository at this point in the history
Mojolicious uses `%=` as one of the ways to inform the templating engine
Perl code will be embedded after it, however, it was being matched against
Perl's variable scope, `<span class="variable">%=</span>`, even though it's
not a valid Perl variable. This commit updates one of the Perl's variable
variants in other to allow only `$=` as valid variable for it, which has
special meaning, while `@=` and `%=` are not valid, thus solving the
conflict with the tag used by Mojolicious.
  • Loading branch information
bmeneg committed Oct 26, 2023
1 parent 14e122c commit 9b1e583
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/languages/perl.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,8 @@ export default function(hljs) {
)
},
{
begin: /[$%@][^\s\w{]/,
// Only $= is a special Perl variable and one can't declare @= or %=.
begin: /[$%@][^\s\w{=]|\$=/,
relevance: 0
}
],
Expand Down
8 changes: 4 additions & 4 deletions test/markup/mojolicious/default.expect.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<span class="language-perl">% layout <span class="hljs-string">&#x27;bootstrap&#x27;</span>;</span><span class="language-xml">
</span><span class="language-perl">% title <span class="hljs-string">&quot;Import your subs&quot;</span>;</span><span class="language-xml">
</span><span class="language-perl"><span class="hljs-variable">%=</span> form_for <span class="hljs-string">&#x27;/settings/import&#x27;</span> =&gt; (<span class="hljs-string">method =&gt;</span> <span class="hljs-string">&#x27;post&#x27;</span>, <span class="hljs-string">enctype =&gt;</span> <span class="hljs-string">&#x27;multipart/form-data&#x27;</span>) =&gt; begin</span><span class="language-xml">
</span><span class="language-perl"> <span class="hljs-variable">%=</span> file_field <span class="hljs-string">&#x27;opmlfile&#x27;</span> =&gt; <span class="hljs-string">multiple =&gt;</span> <span class="hljs-string">&#x27;true&#x27;</span></span><span class="language-xml">
</span><span class="language-perl"> <span class="hljs-variable">%=</span> submit_button <span class="hljs-string">&#x27;Import&#x27;</span>, <span class="hljs-string">&#x27;class&#x27;</span> =&gt; <span class="hljs-string">&#x27;btn&#x27;</span></span><span class="language-xml">
</span><span class="language-perl">%= form_for <span class="hljs-string">&#x27;/settings/import&#x27;</span> =&gt; (<span class="hljs-string">method =&gt;</span> <span class="hljs-string">&#x27;post&#x27;</span>, <span class="hljs-string">enctype =&gt;</span> <span class="hljs-string">&#x27;multipart/form-data&#x27;</span>) =&gt; begin</span><span class="language-xml">
</span><span class="language-perl"> %= file_field <span class="hljs-string">&#x27;opmlfile&#x27;</span> =&gt; <span class="hljs-string">multiple =&gt;</span> <span class="hljs-string">&#x27;true&#x27;</span></span><span class="language-xml">
</span><span class="language-perl"> %= submit_button <span class="hljs-string">&#x27;Import&#x27;</span>, <span class="hljs-string">&#x27;class&#x27;</span> =&gt; <span class="hljs-string">&#x27;btn&#x27;</span></span><span class="language-xml">
</span><span class="language-perl">% end</span><span class="language-xml">
<span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
</span><span class="language-perl">% <span class="hljs-keyword">if</span> (<span class="hljs-variable">$subs</span>) {</span><span class="language-xml">
Expand All @@ -13,7 +13,7 @@
<span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">dir</span>=<span class="hljs-string">&quot;&lt;%=</span></span></span><span class="language-perl"> <span class="hljs-variable">$dir</span> </span><span class="language-xml"><span class="hljs-tag"><span class="hljs-string">%&gt;&quot;</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;&lt;%=</span></span></span><span class="language-perl"> <span class="hljs-variable">$item</span>-&gt;{htmlUrl} </span><span class="language-xml"><span class="hljs-tag"><span class="hljs-string">%&gt;&quot;</span>&gt;</span>&lt;%==</span><span class="language-perl"> <span class="hljs-variable">$item</span>-&gt;{title} </span><span class="language-xml">%&gt;<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">dt</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">dd</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">b</span>&gt;</span>Categories<span class="hljs-tag">&lt;/<span class="hljs-name">b</span>&gt;</span>
</span><span class="language-perl"><span class="hljs-variable">%=</span> <span class="hljs-keyword">join</span> <span class="hljs-string">q{, }</span>, <span class="hljs-keyword">sort</span> @{ <span class="hljs-variable">$item</span>-&gt;{categories} || [] };</span><span class="language-xml">
</span><span class="language-perl">%= <span class="hljs-keyword">join</span> <span class="hljs-string">q{, }</span>, <span class="hljs-keyword">sort</span> @{ <span class="hljs-variable">$item</span>-&gt;{categories} || [] };</span><span class="language-xml">
<span class="hljs-tag">&lt;/<span class="hljs-name">dd</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">dl</span>&gt;</span>
</span><span class="language-perl">% }</span><span class="language-xml">
Expand Down

0 comments on commit 9b1e583

Please sign in to comment.