8
\$\begingroup\$

I've noticed that my Red vs. Blue Sandbox controller snippet here has stopped working (at least in Chrome).

This is incredibly strange because the JSFiddle that is exactly identical is working fine. More importantly, if you go to the edit mode for the meta question, the snippet works fine when you click Run code snippet in the render preview, or if you click edit the above snippetRun.

The error that the browser gives is

Uncaught SyntaxError: Unexpected token ILLEGAL

this makes perfect sense when you look at the JavaScript causing it:

var battle, redTeam, blueTeam, interval

1385d3a1-d76e-4049-926f-d58c6ff78ba5('#showCircles')
    loadEntries()
})

I have no idea where the 1385d3a1-d76e-4049-926f-d58c6ff78ba5 came from. The code should look something like this:

var battle, redTeam, blueTeam, interval

$(document).ready(function() {
    ctx = $('#canvas')[0].getContext('2d')
    moveCounter = $('#moveCounter')
    showNames = $('#showNames')
    showCircles = $('#showCircles')
    loadEntries()
})

The edit page has the correct code but the real question doesn't.

I'm guessing it has something to do with jQuery or MathJax because of the $ involved.

Interestingly, the official question snippet works fine. The code there is minified though so this may not be that surprising.

Any clue what's going on?

(Not sure but a similar error may be happening here.)

\$\endgroup\$
3
  • 2
    \$\begingroup\$ In case it helps, I can reproduce this problem (even the odd hash-looking thing is identical) \$\endgroup\$ Commented Apr 8, 2015 at 20:48
  • \$\begingroup\$ Definitely a MathJax-related problem (I've seen a similar strange effect on CR). Looking into it... \$\endgroup\$
    – Doorknob Mod
    Commented Apr 8, 2015 at 20:50
  • 1
    \$\begingroup\$ This is definitely what broke my snippet. \$\endgroup\$
    – orlp
    Commented Apr 9, 2015 at 0:10

3 Answers 3

9
\$\begingroup\$

This is a MathJax-related issue. Here are some very short snippets that reproduce the bug, which appears to occur whenever multiple dollar signs are placed in a single code block:

$foo
$bar
$baz

and

$
foo
bar
baz
$

Note that the original source of this answer does not include any extra indentation, yet the rendered HTML does show it.

According to a comment by @m0sa, Stack Exchange developer:

I looked into it, turned out to be a deep rabbit hole requiring at major refactoring of MarkdownSharp (we'd need to make the pipeline extensible, so we could inject stuff before DoCodeBlocks in RunBlockGamut - currently all the processing happens before we ever hit MarkdownSharp, but we'd need to leverage it's block processing)

So, it's unlikely that this will be fixed any time soon (ugh).

Unfortunately, the answer is just "use jQuery or some other alias in your code instead of $" until this is fixed.

\$\endgroup\$
8
  • 7
    \$\begingroup\$ Now I'm not so happy we got MathJax. I think bug-free stack snippets are much more important for PPCG. :( \$\endgroup\$ Commented Apr 8, 2015 at 21:24
  • \$\begingroup\$ I was actually going to report this as a bug for code block rendering... \$\endgroup\$
    – Martin Ender Mod
    Commented Apr 8, 2015 at 21:31
  • \$\begingroup\$ And I don't get how it can render correctly in the edit preview but not in the actual question. You'd think it'd be the exact same procedure for rendering both of them. \$\endgroup\$ Commented Apr 8, 2015 at 21:32
  • 1
    \$\begingroup\$ @Calvin'sHobbies It's not. Client-side for preview, server-side for actual post. \$\endgroup\$
    – Doorknob Mod
    Commented Apr 8, 2015 at 21:33
  • \$\begingroup\$ @Calvin'sHobbies It doesn't. Try editing this answer. \$\endgroup\$
    – Martin Ender Mod
    Commented Apr 8, 2015 at 21:33
  • \$\begingroup\$ I see. @MartinBüttner The JS in the RvB Sandbox was rendered correctly in the edit preview (there was no hash thing). \$\endgroup\$ Commented Apr 8, 2015 at 21:37
  • 6
    \$\begingroup\$ Are the developers aware that dollar signs break the search as well? \$\endgroup\$
    – grc
    Commented Apr 9, 2015 at 1:24
  • \$\begingroup\$ @MartinBüttner, Doorknob, there is a similar issue on Music stackexchange and there is a very simple workaround. See my answer and check if it works for you. \$\endgroup\$ Commented Sep 29, 2015 at 18:32
0
\$\begingroup\$

On the meta only, I have edited the MathJax delimiter to use \\( and \\). Let me know if this allows your snippets to work better!

Testing alternative MathJax invocation syntax: \(\sqrt{x^2+y^2}\)

Display mode still works:

\[\sqrt{x^2+y^2}\]

Another test from Martin:

abc$
    $def
$$ab
    de$$
\(ab
de\)
\[ab
de\]
\$\endgroup\$
3
  • \$\begingroup\$ I tried using $ again instead of jQuery in my meta snippet but the same exact issue (strangely?) still occurred. \$\endgroup\$ Commented Apr 10, 2015 at 19:01
  • \$\begingroup\$ On your test from Martin, was it supposed to be formatted as code? \$\endgroup\$ Commented Apr 10, 2015 at 19:11
  • \$\begingroup\$ @ASCIIThenANSI Yes. \$\endgroup\$
    – C. K. Young StaffMod
    Commented Apr 10, 2015 at 20:36
0
\$\begingroup\$

Not sure if this has been fixed, it's not showing up in the edit box

$foo
        $bar
$baz

(no, it's not fixed, $bar has additional indentation than what I saw in the edit!)

There is a similar issue on Music Stack exchange, where unwanted guitar chords pop up. The workaround is to use <pre> and </pre> tags around content you don't want mangled. This also means you don't need to include 4 leading spaces in front of your code.

(Yes, works!)

$foo
    $bar
$baz

(Please click the edit button to see exactly what I have done.)

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Having to escape &, <, and other HTML characters isn't fun... nor is the lack of syntax highlighting. (Not to mention the potential to break several of the Stack Snippets that we commonly use.) \$\endgroup\$
    – Doorknob Mod
    Commented Sep 29, 2015 at 19:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .