Ejemplo n.º 1
0
        public void JsMinify_Minify()
        {
            //Arrange

            var script =
                @"var Messaging = {
    GetMessage: function(callback) {
        $.ajax({
            type: ""POST"",
            url: ""/Services/MessageService.asmx/HelloWorld"",
            data: ""{}"",
            contentType: ""application/json; charset=utf-8"",
            dataType: ""json"",
            success: function(msg) {
                callback.apply(this, [msg.d]);
            }
        });
    }
    var blah = 1;
    blah++;
    blah = blah + 2;
    var newBlah = ++blah;
    newBlah += 234 +4;
};";

            var minifier = new JSMin();

            //Act

            var output = minifier.Minify(script);

            Assert.AreEqual(
                "\nvar Messaging={GetMessage:function(callback){$.ajax({type:\"POST\",url:\"/Services/MessageService.asmx/HelloWorld\",data:\"{}\",contentType:\"application/json; charset=utf-8\",dataType:\"json\",success:function(msg){callback.apply(this,[msg.d]);}});}\nvar blah=1;blah++;blah=blah+2;var newBlah=++blah;newBlah+=234+4;};",
                output);
        }
Ejemplo n.º 2
0
 public static string CompressJS(Stream stream)
 {
     var jsMin = new JSMin();
     if (!stream.CanRead) throw new InvalidOperationException("Cannot read input stream");
     if (stream.CanSeek)
     {
         stream.Position = 0;
     }
     return jsMin.Minify(new StreamReader(stream));
 }
Ejemplo n.º 3
0
        public void JsMinify_Escaped_Quotes_In_String_Literal()
        {
            var script = "var asdf=\"Some string\\\'s with \\\"quotes\\\" in them\"";

            var minifier = new JSMin();

            //Act

            var output = minifier.Minify(script);

            Assert.AreEqual("var asdf=\"Some string\\\'s with \\\"quotes\\\" in them\"", output);
        }
Ejemplo n.º 4
0
        public static string CompressJS(Stream stream)
        {
            var jsMin = new JSMin();

            if (!stream.CanRead)
            {
                throw new InvalidOperationException("Cannot read input stream");
            }
            if (stream.CanSeek)
            {
                stream.Position = 0;
            }
            return(jsMin.Minify(new StreamReader(stream)));
        }
        /// <summary>
        /// Minify the result if in release mode
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnResultExecuting(ResultExecutingContext filterContext)
        {
            base.OnResultExecuting(filterContext);

            if (filterContext.Result == null) return;
            var jsResult = filterContext.Result as JavaScriptResult;
            if (jsResult == null) return;
            if (filterContext.HttpContext.IsDebuggingEnabled) return;
            
            //minify the result
            var result = jsResult.Script;
            var minifier = new JSMin();
            var minified = minifier.Minify(result);            
            jsResult.Script = minified;
        }
Ejemplo n.º 6
0
        public void JsMinify_Backslash_Line_Escapes()
        {
            var script = @"function Test() {
            jQuery(this).append('<div>\
            <div>\
            <a href=""http://google.com"" /></a>\
            </div>\
            </div>');
            }";

            var minifier = new JSMin();

            //Act

            var output = minifier.Minify(script);

            //Assert

            Assert.AreEqual("\nfunction Test(){jQuery(this).append('<div>\\\n\n  <div>\\\n\n   <a href=\"http://google.com\" /></a>\\\n\n  </div>\\\n\n </div>');}", output);
        }
Ejemplo n.º 7
0
        public void JsMinify_Handles_Regex()
        {
            var script1 = @"b.prototype._normalizeURL=function(a){return/^https?:\/\//.test(a)||(a=""http://""+a),a}";
            var script2 = @"var ex = +  /w$/.test(resizing),
            ey = +/^ n /.test(resizing); ";
            var script3 = @"return /["",\n]/.test(text)
              ? ""\"""" + text.replace(/\"" / g, ""\""\"""") + ""\""""
              : text; ";

            var minifier = new JSMin();

            //Act

            var output1 = minifier.Minify(script1);
            Assert.AreEqual("" + script1, output1);

            var output2 = minifier.Minify(script2);
            Assert.AreEqual("" + @"var ex=+/w$/.test(resizing),ey=+/^ n /.test(resizing);", output2);

            var output3 = minifier.Minify(script3);
            Assert.AreEqual("" + @"return /["",\n]/.test(text)?""\""""+text.replace(/\"" /g,""\""\"""")+""\"""":text;", output3);
        }
Ejemplo n.º 8
0
        public void JsMinify_Minify_With_Unary_Operator()
        {
            //see: http://clientdependency.codeplex.com/workitem/13162

            //Arrange

            var script = 
@"var c = {};
var c.name = 0;
var i = 1;
c.name=i+ +new Date;
alert(c.name);";

            var minifier = new JSMin();
            
            //Act

            var output = minifier.Minify(script);

            //Assert

            Assert.AreEqual("\nvar c={};var c.name=0;var i=1;c.name=i+ +new Date;alert(c.name);", output);
        }
Ejemplo n.º 9
0
        public void JsMinify_TypeScript_Enum()
        {
            var script = @"$(""#TenderListType"").val(1 /* Calendar */.toString());";

            var minifier = new JSMin();
            var output = minifier.Minify(script);
            Assert.AreEqual("\n$(\"#TenderListType\").val(1..toString());", output);
        }