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);
        }
        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(
                "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]);}});}\nvar blah=1;blah++;blah=blah+2;var newBlah=++blah;newBlah+=234+4;};",
                output);
        }
Beispiel #3
0
        /// <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;
        }
Beispiel #4
0
    public static string ScriptMinify(string script)
    {
        string openTag, jsContent;
        int    si, ei;

        si = script.IndexOf(S_TAG, 0, StringComparison.OrdinalIgnoreCase);
        if (si == -1)
        {
            return(script);
        }

        ei = script.IndexOf(">", si);
        if (ei == -1)
        {
            return(script);
        }

        openTag = script.Substring(0, ei);

        si = script.IndexOf(E_TAG, ei, StringComparison.OrdinalIgnoreCase);

        if (si == -1)
        {
            return(script);
        }

        ei++;
        IMinify min = new JSMin();

        jsContent = min.Minify(script.Substring(ei, si - ei));
        return(string.Concat(openTag, jsContent, script.Substring(si)));
    }
        public void JsMinify_TypeScript_Enum()
        {
            var script = @"$(""#TenderListType"").val(1 /* Calendar */.toString());";

            var minifier = new JSMin();
            var output   = minifier.Minify(script);

            Assert.AreEqual("$(\"#TenderListType\").val(1..toString());", output);
        }
        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);
        }
        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("function Test(){jQuery(this).append('<div>  <div>   <a href=\"http://google.com\" /></a>  </div> </div>');}", output);
        }
        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("var c={};var c.name=0;var i=1;c.name=i+ +new Date;alert(c.name);", output);
        }
Beispiel #9
0
    public static string HtmlTidy(string html,
                                  bool minifyHtml         = true,
                                  bool minifyJs           = true,
                                  bool minifyCss          = true,
                                  bool optimizeJsPostion  = true,
                                  bool optimizeCssPostion = true)
    {
        //return html;
        const string SCRIPT_TAG_START = "<script";
        const string SCRIPT_TAG_END   = "</script>";

        const string STYLE_TAG_START = "<style";
        const string STYLE_TAG_END   = "</style>";

        const string HEAD_TAG_END = "</head>";
        const string BODY_TAG_END = "</body>";

        int startLength = STYLE_TAG_START.Length;
        int endLength = SCRIPT_TAG_END.Length;
        int startIdx, endIdx, offset = 0;
        int headIdx    = html.IndexOf(HEAD_TAG_END, StringComparison.OrdinalIgnoreCase);
        int bodyEndIdx = -1;

        string          scriptStr = "";
        List <CodeItem> lsParts = new List <CodeItem>();
        List <CodeItem> lsResult = new List <CodeItem>();
        int             close, endblock;

        /*Javascript*/
        while ((startIdx = html.IndexOf(SCRIPT_TAG_START, offset, StringComparison.OrdinalIgnoreCase)) != -1)
        {
            if ((endIdx = html.IndexOf(SCRIPT_TAG_END, startIdx + startLength, StringComparison.OrdinalIgnoreCase)) == -1)
            {
                break;
            }

            lsParts.Add(CodeItem.NewItem(CodeType.Html, "", html.Substring(offset, startIdx - offset), "", offset, startIdx));

            scriptStr = html.Substring(startIdx, endIdx + endLength - startIdx);
            close     = scriptStr.IndexOf('>');
            if (close != -1)
            {
                endblock = endIdx - startIdx;
                lsParts.Add(CodeItem.NewItem(CodeType.Js,
                                             scriptStr.Substring(0, close + 1),
                                             scriptStr.Substring(close + 1, endblock - (close + 1)),
                                             scriptStr.Substring(endblock),
                                             startIdx,
                                             endIdx + endLength
                                             ));
            }
            offset = endIdx + endLength;
        }

        //if ((endIdx = html.IndexOf(BODY_TAG_END, offset, StringComparison.OrdinalIgnoreCase)) != -1)
        //{
        //    bodyEndIdx = endIdx;
        //    lsParts.Add(CodeItem.NewItem(CodeType.Html, "", html.Substring(offset, endIdx - offset), "", offset, endIdx));
        //    lsParts.Add(CodeItem.NewItem(CodeType.Html, "", html.Substring(endIdx), "", endIdx, html.Length - 1));  //tail
        //}
        //else
        //{
        lsParts.Add(CodeItem.NewItem(CodeType.Html, "", html.Substring(offset), "", offset, html.Length - 1));
        //}


        startLength = STYLE_TAG_START.Length;
        endLength   = STYLE_TAG_END.Length;
        int i;

        for (i = 0; i < lsParts.Count; i++)
        {
            var code = lsParts[i];
            if (code.ItemType == CodeType.Js)
            {
                lsResult.Add(code);
            }
            else
            {
                offset = 0;
                while ((startIdx = code.Content.IndexOf(STYLE_TAG_START, offset, StringComparison.OrdinalIgnoreCase)) != -1)
                {
                    if ((endIdx = code.Content.IndexOf(STYLE_TAG_END, startIdx + startLength, StringComparison.OrdinalIgnoreCase)) == -1)
                    {
                        break;
                    }

                    lsResult.Add(CodeItem.NewItem(CodeType.Html, "", code.Content.Substring(offset, startIdx - offset), "", code.StartIndex + offset, code.StartIndex + startIdx));

                    scriptStr = code.Content.Substring(startIdx, endIdx + endLength - startIdx);
                    close     = scriptStr.IndexOf('>');
                    if (close != -1)
                    {
                        endblock = endIdx - startIdx;
                        lsResult.Add(CodeItem.NewItem(CodeType.Css,
                                                      scriptStr.Substring(0, close + 1),
                                                      scriptStr.Substring(close + 1, endblock - (close + 1)),
                                                      scriptStr.Substring(endblock),
                                                      code.StartIndex + startIdx,
                                                      code.StartIndex + (endIdx + endLength)
                                                      ));
                    }
                    offset = endIdx + endLength;
                }
                lsResult.Add(CodeItem.NewItem(CodeType.Html, "", code.Content.Substring(offset), "", code.StartIndex + offset, code.StartIndex + code.Content.Length - 1));
            }
        }
        if (headIdx == -1 || bodyEndIdx == -1)
        {
            optimizeCssPostion = optimizeJsPostion = false;
        }

        StringBuilder sb       = new StringBuilder();
        HTMLMin       _htmlMin = new HTMLMin();
        CSSMin        _cssMin  = new CSSMin();
        JSMin         _jsMin   = new JSMin();

        for (i = 0; i < lsResult.Count; i++)
        {
            var code = lsResult[i];
            if (code.ItemType == CodeType.Html)
            {
                if (minifyHtml)
                {
                    code.Content = _htmlMin.Minify(code.Content);
                }
            }
            else if (code.ItemType == CodeType.Js)
            {
                if (minifyJs)
                {
                    code.Content = _jsMin.Minify(code.Content);
                }
            }
            if (code.ItemType == CodeType.Css)
            {
                if (minifyCss)
                {
                    code.Content = _cssMin.Minify(code.Content);
                }
            }
            //sb.Append("<hr>");
            //sb.AppendFormat("tt: {0} ({1}-{2})", i, code.StartIndex, code.EndIndex);
            //sb.Append("<hr>");
            sb.Append(code.OpenTag);
            sb.Append(code.Content);
            sb.Append(code.CloseTag);
        }
        return(sb.ToString());
    }
Beispiel #10
0
    public static string JsMinify(string rawJs)
    {
        IMinify min = new JSMin();

        return(min.Minify(rawJs));
    }