RazorFormat.MinifyHtml breaks javascript

I am using the RazorFormat minify, e.g.

Plugins.Add(new RazorFormat() { MinifyHtml = true });

But it is changing embedded script code within a page:

  var $$$$name = 1;


  var $name = 1;

I am using angular and so referencing their existing variable “$$childHead”. Re-factoring will move the code out later, but for now it breaks.

The minifiers is using a C# port of Google’s HtmlCompressor and Crockfords JSMin where there is some JS Code it doesn’t like, the only workaround is to change the variable names to make the minifiers happy.

I may be able to fix this. CDMackie, can you post a full file with the failing script?

@Mythz: the JSMin port of flawed is a number of ways, I will try to fix that.

Hi Rob

Here is a working project showing the issue.



The original Java code uses Matcher.quoteReplacement, which deliberately stops / and $ from having any special meaning:

https://code.google.com/p/htmlcompressor/source/browse/trunk/src/main/java/com/googlecode/htmlcompressor/compressor/HtmlCompressor.java (line 469)

The C# version is using match.Result which is doing token replacement, such that “$$” becomes “$”. This would cause similar problems if any variables were named, or started with, “$1”.

C# doesn’t have an equivalent of quoteReplacement, so you could probably resort to using Replace, as you only need to escape / and $ (rather than using Regex.Escape).

So, in ServiceStack/Html/Minifiers.html.cs (forum won’t let me link more than 2 things!)

line 1026


Should become

sb.Append(match.Result(scriptBlocks[i].Replace("\\", "\\\\").Replace("$$", "$$$$")));

Nice Catch! I’ve made the proposed change which is now available on MyGet: https://github.com/ServiceStack/ServiceStack/wiki/MyGet

Please let me know if this resolves the issue.

Have to re-open this old thread because I’ve just been caught out by the fix I had suggested.

I was getting a problem with “” escaped within inline scripts, and it’s my own fix! Oops, sorry.

The original issue with $ is still valid, but it’s the backslash that’s the issue. It doesn’t actually get replaced in Regex.Replace like $ does, so is just causing “” to become “\\”.

sb.Append(match.Result(scriptBlocks[i].Replace("\\", "\\\\").Replace("$$", "$$$$")));

needs to just be

sb.Append(match.Result(scriptBlocks[i].Replace("$$", "$$$$")));

See my updated project showing the issue: jsminbug-20160907

ok thanks, fix is now in this commit, which is now available on MyGet.