public MinifyHtmlCodeGenerator(MinifyHtmlMinifier minifier)
 {
     _minifier = minifier;
     _previousIsWhiteSpace = true;
     _previousTokenEndsWithBlockElement = true;
 }
Esempio n. 2
0
        public MinifyHtmlWebRazorHostFactory()
        {
            Func <string, string> minifyJS  = null;
            Func <string, string> minifyCSS = null;

            // We initialize the JS and CSS minification by reflexion to remove a DLL dependency
            try
            {
                if ((minifyJS == null) || (minifyCSS == null))
                {
                    // Minification using the SWO transforms
                    var swo = Assembly.Load("System.Web.Optimization");
                    if (swo != null)
                    {
                        var bundleCollectionType         = swo.GetType("System.Web.Optimization.BundleCollection");
                        var bundleContextType            = swo.GetType("System.Web.Optimization.BundleContext");
                        var bundleContextConstructor     = bundleContextType.GetConstructor(new Type[] { typeof(HttpContextBase), bundleCollectionType, typeof(string) });
                        var bundleReponseType            = swo.GetType("System.Web.Optimization.BundleResponse");
                        var bundleReponseConstructor     = bundleReponseType.GetConstructor(Type.EmptyTypes);
                        var bundleReponseContentProperty = bundleReponseType.GetProperty("Content");
                        var bundleTransformType          = swo.GetType("System.Web.Optimization.IBundleTransform");
                        var bundleTransformProcessMethod = bundleTransformType.GetMethod("Process");

                        var httpContext      = new EmptyHttpContext();
                        var bundleCollection = bundleCollectionType.GetConstructor(Type.EmptyTypes).Invoke(null);
                        var bundleContext    = bundleContextConstructor.Invoke(new object[] { httpContext, bundleCollection, "" });

                        if (minifyCSS == null)
                        {
                            var cssMinifyType = swo.GetType("System.Web.Optimization.CssMinify");
                            var cssMinify     = cssMinifyType.GetConstructor(Type.EmptyTypes).Invoke(null);

                            minifyCSS = delegate(string code)
                            {
                                var bundleReponse = bundleReponseConstructor.Invoke(null);
                                bundleReponseContentProperty.SetValue(bundleReponse, code, null);

                                bundleTransformProcessMethod.Invoke(cssMinify, new object[] { bundleContext, bundleReponse });

                                var minifiedCode = (string)bundleReponseContentProperty.GetValue(bundleReponse, null);
                                return(minifiedCode);
                            };
                        }
                        if (minifyJS == null)
                        {
                            var jsMinifyType = swo.GetType("System.Web.Optimization.JsMinify");
                            var jsMinify     = jsMinifyType.GetConstructor(Type.EmptyTypes).Invoke(null);

                            minifyJS = delegate(string code)
                            {
                                var bundleReponse = bundleReponseConstructor.Invoke(null);
                                bundleReponseContentProperty.SetValue(bundleReponse, code, null);

                                bundleTransformProcessMethod.Invoke(jsMinify, new object[] { bundleContext, bundleReponse });

                                var minifiedCode = (string)bundleReponseContentProperty.GetValue(bundleReponse, null);
                                return(minifiedCode);
                            };
                        }
                    }
                }

                if ((minifyJS == null) || (minifyCSS == null))
                {
                    // Minification using Microsoft AjaxMin
                    var ajaxmin = Assembly.Load("ajaxmin");
                    if (ajaxmin != null)
                    {
                        var scriptCruncherType = ajaxmin.GetType("Microsoft.Ajax.Utilities.ScriptCruncher");
                        var sc = scriptCruncherType.GetConstructor(Type.EmptyTypes).Invoke(null);

                        if (minifyJS == null)
                        {
                            // JS
                            var codeSettingsType      = ajaxmin.GetType("Microsoft.Ajax.Utilities.CodeSettings");
                            var localRenamingProperty = codeSettingsType.GetProperty("LocalRenaming");
                            var crunchMethod          = scriptCruncherType.GetMethod("Crunch", new Type[] { typeof(string), codeSettingsType });

                            var scsettings = codeSettingsType.GetConstructor(Type.EmptyTypes).Invoke(null);
                            localRenamingProperty.SetValue(scsettings, 1, null);

                            minifyJS = delegate(string code)
                            {
                                var minifiedCode = (string)crunchMethod.Invoke(sc, new object[] { code, scsettings });
                                return(minifiedCode);
                            };
                        }

                        if (minifyCSS == null)
                        {
                            // CSS
                            var cssSettingsType        = ajaxmin.GetType("Microsoft.Ajax.Utilities.CssSettings");
                            var commentModeProperty    = cssSettingsType.GetProperty("CommentMode");
                            var minifyStyleSheetMethod = scriptCruncherType.GetMethod("MinifyStyleSheet", new Type[] { typeof(string), cssSettingsType });

                            var scsettings2 = cssSettingsType.GetConstructor(Type.EmptyTypes).Invoke(null);
                            commentModeProperty.SetValue(scsettings2, 2, null);

                            minifyCSS = delegate(string code)
                            {
                                var minifiedCode = (string)minifyStyleSheetMethod.Invoke(sc, new object[] { code, scsettings2 });
                                return(minifiedCode);
                            };
                        }
                    }
                }
            }
            catch
            {
            }

            var confAggressive = ConfigurationManager.AppSettings["meleze-minifier:Aggressive"];
            var confComments   = ConfigurationManager.AppSettings["meleze-minifier:Comments"];
            var confJavascript = ConfigurationManager.AppSettings["meleze-minifier:Javascript"];
            var confCSS        = ConfigurationManager.AppSettings["meleze-minifier:CSS"];

            _minifier            = new MinifyHtmlMinifier();
            _minifier.MinifyJS   = minifyJS;
            _minifier.MinifyCSS  = minifyCSS;
            _minifier.Aggressive = confAggressive == null || confAggressive.ToLower() == "true";
            _minifier.Comments   = confComments == null || confComments.ToLower() == "true";
            _minifier.Javascript = confJavascript == null || confJavascript.ToLower() == "true";
            _minifier.CSS        = confCSS == null || confCSS.ToLower() == "true";
        }
Esempio n. 3
0
 public MinifyHtmlCodeGenerator(MinifyHtmlMinifier minifier)
 {
     _minifier             = minifier;
     _previousIsWhiteSpace = true;
     _previousTokenEndsWithBlockElement = true;
 }
Esempio n. 4
0
 public MinifyHtmlParser(ParserBase other, MinifyHtmlMinifier minifier)
 {
     _other    = other;
     _minifier = minifier;
 }
Esempio n. 5
0
 public MinifyHtmlParser(ParserBase other, MinifyHtmlMinifier minifier)
 {
     _other = other;
     _minifier = minifier;
 }
        public MinifyHtmlWebRazorHostFactory()
        {
            Func<string, string> minifyJS = null;
            Func<string, string> minifyCSS = null;

            // We initialize the JS and CSS minification by reflexion to remove a DLL dependency
            try
            {
                if ((minifyJS == null) || (minifyCSS == null))
                {
                    // Minification using the SWO transforms
                    var swo = Assembly.Load("System.Web.Optimization");
                    if (swo != null)
                    {
                        var bundleCollectionType = swo.GetType("System.Web.Optimization.BundleCollection");
                        var bundleContextType = swo.GetType("System.Web.Optimization.BundleContext");
                        var bundleContextConstructor = bundleContextType.GetConstructor(new Type[] { typeof(HttpContextBase), bundleCollectionType, typeof(string) });
                        var bundleReponseType = swo.GetType("System.Web.Optimization.BundleResponse");
                        var bundleReponseConstructor = bundleReponseType.GetConstructors()[0];
                        var bundleReponseContentProperty = bundleReponseType.GetProperty("Content");
                        var bundleTransformType = swo.GetType("System.Web.Optimization.IBundleTransform");
                        var bundleTransformProcessMethod = bundleTransformType.GetMethod("Process");

                        var httpContext = new EmptyHttpContext();
                        var bundleCollection = bundleCollectionType.GetConstructor(Type.EmptyTypes).Invoke(null);
                        var bundleContext = bundleContextConstructor.Invoke(new object[] { httpContext, bundleCollection, "" });

                        if (minifyCSS == null)
                        {
                            var cssMinifyType = swo.GetType("System.Web.Optimization.CssMinify");
                            var cssMinify = cssMinifyType.GetConstructor(Type.EmptyTypes).Invoke(null);

                            minifyCSS = delegate(string code)
                            {
                                var bundleReponse = bundleReponseConstructor.Invoke(new object[] { null, null });
                                bundleReponseContentProperty.SetValue(bundleReponse, code, null);

                                bundleTransformProcessMethod.Invoke(cssMinify, new object[] { bundleContext, bundleReponse });

                                var minifiedCode = (string)bundleReponseContentProperty.GetValue(bundleReponse, null);
                                return minifiedCode;
                            };
                        }
                        if (minifyJS == null)
                        {
                            var jsMinifyType = swo.GetType("System.Web.Optimization.JsMinify");
                            var jsMinify = jsMinifyType.GetConstructor(Type.EmptyTypes).Invoke(null);

                            minifyJS = delegate(string code)
                            {
                                var bundleReponse = bundleReponseConstructor.Invoke(new object[] { null, null });
                                bundleReponseContentProperty.SetValue(bundleReponse, code, null);

                                bundleTransformProcessMethod.Invoke(jsMinify, new object[] { bundleContext, bundleReponse });

                                var minifiedCode = (string)bundleReponseContentProperty.GetValue(bundleReponse, null);
                                return minifiedCode;
                            };
                        }
                    }
                }

                if ((minifyJS == null) || (minifyCSS == null))
                {
                    // Minification using Microsoft AjaxMin
                    var ajaxmin = Assembly.Load("ajaxmin");
                    if (ajaxmin != null)
                    {
                        var minifierType = ajaxmin.GetType("Microsoft.Ajax.Utilities.Minifier");
                        var min = minifierType.GetConstructor(Type.EmptyTypes).Invoke(null);

                        if (minifyJS == null)
                        {
                            // JS
                            var codeSettingsType = ajaxmin.GetType("Microsoft.Ajax.Utilities.CodeSettings");
                            var localRenamingProperty = codeSettingsType.GetProperty("LocalRenaming");
                            var minifyJavaScriptMethod = minifierType.GetMethod("MinifyJavaScript", new Type[] { typeof(string), codeSettingsType });

                            var scsettings = codeSettingsType.GetConstructor(Type.EmptyTypes).Invoke(null);
                            localRenamingProperty.SetValue(scsettings, 1, null);

                            minifyJS = delegate(string code)
                            {
                                var minifiedCode = (string)minifyJavaScriptMethod.Invoke(min, new object[] { code, scsettings });
                                return minifiedCode;
                            };
                        }

                        if (minifyCSS == null)
                        {
                            // CSS
                            var cssSettingsType = ajaxmin.GetType("Microsoft.Ajax.Utilities.CssSettings");
                            var commentModeProperty = cssSettingsType.GetProperty("CommentMode");
                            var minifyStyleSheetMethod = minifierType.GetMethod("MinifyStyleSheet", new Type[] { typeof(string), cssSettingsType });

                            var scsettings2 = cssSettingsType.GetConstructor(Type.EmptyTypes).Invoke(null);
                            commentModeProperty.SetValue(scsettings2, 2, null);

                            minifyCSS = delegate(string code)
                            {
                                var minifiedCode = (string)minifyStyleSheetMethod.Invoke(min, new object[] { code, scsettings2 });
                                return minifiedCode;
                            };
                        }
                    }
                }
            }
            catch
            {
            }

            var confAggressive = ConfigurationManager.AppSettings["meleze-minifier:Aggressive"];
            var confComments = ConfigurationManager.AppSettings["meleze-minifier:Comments"];
            var confJavascript = ConfigurationManager.AppSettings["meleze-minifier:Javascript"];
            var confCSS = ConfigurationManager.AppSettings["meleze-minifier:CSS"];

            _minifier = new MinifyHtmlMinifier();
            _minifier.MinifyJS = minifyJS;
            _minifier.MinifyCSS = minifyCSS;
            _minifier.Aggressive = confAggressive == null || confAggressive.ToLower() == "true";
            _minifier.Comments = confComments == null || confComments.ToLower() == "true";
            _minifier.Javascript = confJavascript == null || confJavascript.ToLower() == "true";
            _minifier.CSS = confCSS == null || confCSS.ToLower() == "true";
        }
 internal MinifyHtmlMvcWebPageRazorHost(WebPageRazorHost host, MinifyHtmlMinifier minifier, string virtualPath, string physicalPath)
     : base(virtualPath, physicalPath)
 {
     _host = host;
     _minifier = minifier;
 }
 internal MinifyHtmlMvcWebPageRazorHost(WebPageRazorHost host, MinifyHtmlMinifier minifier, string virtualPath, string physicalPath)
     : base(virtualPath, physicalPath)
 {
     _host     = host;
     _minifier = minifier;
 }
        public MinifyHtmlWebRazorHostFactory()
        {
            // Javascript minification

            Func<string, string> minifyJS = null;
            Func<string, string> minifyCSS = null;
            //minifyJS = delegate(string code)
            //{
            //    var sc = new Microsoft.Ajax.Utilities.ScriptCruncher();
            //    var scsettings = new Microsoft.Ajax.Utilities.CodeSettings() { LocalRenaming = Microsoft.Ajax.Utilities.LocalRenaming.KeepLocalizationVars };
            //    var minifiedCode = sc.Crunch(code, scsettings);
            //    return minifiedCode;
            //};
            //minifyCSS = delegate(string code)
            //{
            //    var sc = new Microsoft.Ajax.Utilities.ScriptCruncher();
            //    var scsettings = new Microsoft.Ajax.Utilities.CssSettings() { CommentMode = Microsoft.Ajax.Utilities.CssComment.Hacks };
            //    var minifiedCode = sc.MinifyStyleSheet(code, scsettings);
            //    return minifiedCode;
            //};

            // We initialize the JS and CSS minification by reflexion to remove a DLL dependency
            try
            {
                var ajaxmin = Assembly.Load("ajaxmin");
                if (ajaxmin != null)
                {
                    var scriptCruncherType = ajaxmin.GetType("Microsoft.Ajax.Utilities.ScriptCruncher");

                    // JS
                    var codeSettingsType = ajaxmin.GetType("Microsoft.Ajax.Utilities.CodeSettings");
                    var localRenamingProperty = codeSettingsType.GetProperty("LocalRenaming");
                    var crunchMethod = scriptCruncherType.GetMethod("Crunch", new Type[] { typeof(string), codeSettingsType });

                    var sc = scriptCruncherType.GetConstructor(Type.EmptyTypes).Invoke(null);
                    var scsettings = codeSettingsType.GetConstructor(Type.EmptyTypes).Invoke(null);
                    localRenamingProperty.SetValue(scsettings, 1, null);

                    minifyJS = delegate(string code)
                    {
                        var minifiedCode = (string)crunchMethod.Invoke(sc, new object[] { code, scsettings });
                        return minifiedCode;
                    };

                    // CSS
                    var cssSettingsType = ajaxmin.GetType("Microsoft.Ajax.Utilities.CssSettings");
                    var commentModeProperty = cssSettingsType.GetProperty("CommentMode");
                    var minifyStyleSheetMethod = scriptCruncherType.GetMethod("MinifyStyleSheet", new Type[] { typeof(string), cssSettingsType });

                    var scsettings2 = cssSettingsType.GetConstructor(Type.EmptyTypes).Invoke(null);
                    commentModeProperty.SetValue(scsettings2, 2, null);

                    minifyCSS = delegate(string code)
                    {
                        var minifiedCode = (string)minifyStyleSheetMethod.Invoke(sc, new object[] { code, scsettings2 });
                        return minifiedCode;
                    };
                }
            }
            catch
            {
            }

            var confAggressive = ConfigurationManager.AppSettings["meleze-minifier:Aggressive"];
            var confComments = ConfigurationManager.AppSettings["meleze-minifier:Comments"];
            var confJavascript = ConfigurationManager.AppSettings["meleze-minifier:Javascript"];
            var confCSS = ConfigurationManager.AppSettings["meleze-minifier:CSS"];

            _minifier = new MinifyHtmlMinifier();
            _minifier.MinifyJS = minifyJS;
            _minifier.MinifyCSS = minifyCSS;
            _minifier.Aggressive = confAggressive == null || confAggressive.ToLower() == "true";
            _minifier.Comments = confComments == null || confComments.ToLower() == "true";
            _minifier.Javascript = confJavascript == null || confJavascript.ToLower() == "true";
            _minifier.CSS = confCSS == null || confCSS.ToLower() == "true";
        }
        public MinifyHtmlWebRazorHostFactory()
        {
            // Javascript minification

            Func <string, string> minifyJS  = null;
            Func <string, string> minifyCSS = null;

            //minifyJS = delegate(string code)
            //{
            //    var sc = new Microsoft.Ajax.Utilities.ScriptCruncher();
            //    var scsettings = new Microsoft.Ajax.Utilities.CodeSettings() { LocalRenaming = Microsoft.Ajax.Utilities.LocalRenaming.KeepLocalizationVars };
            //    var minifiedCode = sc.Crunch(code, scsettings);
            //    return minifiedCode;
            //};
            //minifyCSS = delegate(string code)
            //{
            //    var sc = new Microsoft.Ajax.Utilities.ScriptCruncher();
            //    var scsettings = new Microsoft.Ajax.Utilities.CssSettings() { CommentMode = Microsoft.Ajax.Utilities.CssComment.Hacks };
            //    var minifiedCode = sc.MinifyStyleSheet(code, scsettings);
            //    return minifiedCode;
            //};

            // We initialize the JS and CSS minification by reflexion to remove a DLL dependency
            try
            {
                var ajaxmin = Assembly.Load("ajaxmin");
                if (ajaxmin != null)
                {
                    var scriptCruncherType = ajaxmin.GetType("Microsoft.Ajax.Utilities.ScriptCruncher");

                    // JS
                    var codeSettingsType      = ajaxmin.GetType("Microsoft.Ajax.Utilities.CodeSettings");
                    var localRenamingProperty = codeSettingsType.GetProperty("LocalRenaming");
                    var crunchMethod          = scriptCruncherType.GetMethod("Crunch", new Type[] { typeof(string), codeSettingsType });

                    var sc         = scriptCruncherType.GetConstructor(Type.EmptyTypes).Invoke(null);
                    var scsettings = codeSettingsType.GetConstructor(Type.EmptyTypes).Invoke(null);
                    localRenamingProperty.SetValue(scsettings, 1, null);

                    minifyJS = delegate(string code)
                    {
                        var minifiedCode = (string)crunchMethod.Invoke(sc, new object[] { code, scsettings });
                        return(minifiedCode);
                    };

                    // CSS
                    var cssSettingsType        = ajaxmin.GetType("Microsoft.Ajax.Utilities.CssSettings");
                    var commentModeProperty    = cssSettingsType.GetProperty("CommentMode");
                    var minifyStyleSheetMethod = scriptCruncherType.GetMethod("MinifyStyleSheet", new Type[] { typeof(string), cssSettingsType });

                    var scsettings2 = cssSettingsType.GetConstructor(Type.EmptyTypes).Invoke(null);
                    commentModeProperty.SetValue(scsettings2, 2, null);

                    minifyCSS = delegate(string code)
                    {
                        var minifiedCode = (string)minifyStyleSheetMethod.Invoke(sc, new object[] { code, scsettings2 });
                        return(minifiedCode);
                    };
                }
            }
            catch
            {
            }

            var confAggressive = ConfigurationManager.AppSettings["meleze-minifier:Aggressive"];
            var confComments   = ConfigurationManager.AppSettings["meleze-minifier:Comments"];
            var confJavascript = ConfigurationManager.AppSettings["meleze-minifier:Javascript"];
            var confCSS        = ConfigurationManager.AppSettings["meleze-minifier:CSS"];

            _minifier            = new MinifyHtmlMinifier();
            _minifier.MinifyJS   = minifyJS;
            _minifier.MinifyCSS  = minifyCSS;
            _minifier.Aggressive = confAggressive == null || confAggressive.ToLower() == "true";
            _minifier.Comments   = confComments == null || confComments.ToLower() == "true";
            _minifier.Javascript = confJavascript == null || confJavascript.ToLower() == "true";
            _minifier.CSS        = confCSS == null || confCSS.ToLower() == "true";
        }