public override string MinifyString(string source) { var settings = new HtmlMinificationSettings { RemoveOptionalEndTags = false, AttributeQuotesRemovalMode = HtmlAttributeQuotesRemovalMode.KeepQuotes, RemoveRedundantAttributes = false, EmptyTagRenderMode = HtmlEmptyTagRenderMode.Slash, }; var minifier = new HtmlMinifier(settings); MarkupMinificationResult result = minifier.Minify(source, generateStatistics: true); if (result.Errors.Count == 0) { EditorExtensionsPackage.DTE.StatusBar.Text = "Web Essentials: HTML minified by " + result.Statistics.SavedInPercent + "%"; return result.MinifiedContent; } else { EditorExtensionsPackage.DTE.StatusBar.Text = "Web Essentials: Cannot minify the current selection. See Output Window for details."; Logger.ShowMessage("Cannot minify the selection:\r\n\r\n" + String.Join(Environment.NewLine, result.Errors.Select(e => e.Message))); return null; } }
public static HtmlMinificationSettings GetSettings(Bundle bundle) { HtmlMinificationSettings settings = new HtmlMinificationSettings(); settings.RemoveOptionalEndTags = GetValue(bundle, "removeOptionalEndTags") == "True"; settings.RemoveRedundantAttributes = GetValue(bundle, "removeRedundantAttributes") == "True"; settings.CollapseBooleanAttributes = GetValue(bundle, "collapseBooleanAttributes") == "True"; settings.CustomAngularDirectiveList = GetValue(bundle, "customAngularDirectiveList"); settings.MinifyAngularBindingExpressions = GetValue(bundle, "minifyAngularBindingExpressions") == "True"; settings.MinifyEmbeddedCssCode = GetValue(bundle, "minifyEmbeddedCssCode") == "True"; settings.MinifyEmbeddedJsCode= GetValue(bundle, "minifyEmbeddedJsCode") == "True"; settings.MinifyInlineCssCode = GetValue(bundle, "minifyInlineCssCode") == "True"; settings.MinifyInlineJsCode = GetValue(bundle, "minifyInlineJsCode") == "True"; settings.MinifyKnockoutBindingExpressions = GetValue(bundle, "minifyKnockoutBindingExpressions") == "True"; settings.ProcessableScriptTypeList = GetValue(bundle, "processableScriptTypeList"); settings.RemoveHtmlComments = GetValue(bundle, "removeHtmlComments") == "True"; settings.RemoveTagsWithoutContent = GetValue(bundle, "removeTagsWithoutContent") == "True"; string quotes = GetValue(bundle, "attributeQuotesRemovalMode"); if (quotes == "html4") settings.AttributeQuotesRemovalMode = HtmlAttributeQuotesRemovalMode.Html4; else if (quotes == "html5") settings.AttributeQuotesRemovalMode = HtmlAttributeQuotesRemovalMode.Html5; else if (quotes == "keepQuotes") settings.AttributeQuotesRemovalMode = HtmlAttributeQuotesRemovalMode.KeepQuotes; return settings; }
/// <summary> /// Creates a instance of HTML minifier based on the settings /// that specified in configuration files (App.config or Web.config) /// </summary> /// <param name="settings">HTML minification settings</param> /// <param name="cssMinifier">CSS minifier</param> /// <param name="jsMinifier">JS minifier</param> /// <param name="logger">logger</param> /// <returns>HTML minifier</returns> public HtmlMinifier CreateHtmlMinifierInstance(HtmlMinificationSettings settings = null, ICssMinifier cssMinifier = null, IJsMinifier jsMinifier = null, ILogger logger = null) { HtmlMinificationSettings innerSettings = settings ?? GetHtmlMinificationSettings(); ICssMinifier innerCssMinifier = cssMinifier ?? _wmmContext.Code.CreateDefaultCssMinifierInstance(); IJsMinifier innerJsMinifier = jsMinifier ?? _wmmContext.Code.CreateDefaultJsMinifierInstance(); ILogger innerLogger = logger ?? _wmmContext.GetDefaultLoggerInstance(); var htmlMinifier = new HtmlMinifier(innerSettings, innerCssMinifier, innerJsMinifier, innerLogger); return(htmlMinifier); }
/// <summary> /// Constructs a instance of HTML minification manager /// </summary> /// <param name="settings">HTML minification settings</param> /// <param name="cssMinifierFactory">CSS minifier factory</param> /// <param name="jsMinifierFactory">JS minifier factory</param> /// <param name="logger">Logger</param> public HtmlMinificationManager(HtmlMinificationSettings settings, ICssMinifierFactory cssMinifierFactory, IJsMinifierFactory jsMinifierFactory, ILogger logger) { MinificationSettings = settings; SupportedMediaTypes = new HashSet<string>(MediaTypeGroupConstants.Html); IncludedPages = new List<IUrlMatcher>(); ExcludedPages = new List<IUrlMatcher>(); CssMinifierFactory = cssMinifierFactory; JsMinifierFactory = jsMinifierFactory; Logger = logger; }
/// <summary> /// Gets a HTML minification settings based on data from /// configuration files (App.config or Web.config) /// </summary> /// <returns>HTML minification settings</returns> public HtmlMinificationSettings GetHtmlMinificationSettings() { HtmlMinificationConfiguration htmlConfig = _wmmContext.GetCoreConfiguration().Html; var htmlSettings = new HtmlMinificationSettings(); MapCommonHtmlSettings(htmlSettings, htmlConfig); htmlSettings.RemoveCdataSectionsFromScriptsAndStyles = htmlConfig.RemoveCdataSectionsFromScriptsAndStyles; htmlSettings.EmptyTagRenderMode = htmlConfig.EmptyTagRenderMode; htmlSettings.RemoveOptionalEndTags = htmlConfig.RemoveOptionalEndTags; htmlSettings.CollapseBooleanAttributes = htmlConfig.CollapseBooleanAttributes; htmlSettings.AttributeQuotesRemovalMode = htmlConfig.AttributeQuotesRemovalMode; htmlSettings.RemoveJsTypeAttributes = htmlConfig.RemoveJsTypeAttributes; htmlSettings.RemoveCssTypeAttributes = htmlConfig.RemoveCssTypeAttributes; return(htmlSettings); }
/// <summary> /// Constructs instance of HTML minifier /// </summary> /// <param name="settings">HTML minification settings</param> /// <param name="cssMinifier">CSS minifier</param> /// <param name="jsMinifier">JS minifier</param> /// <param name="logger">Logger</param> public HtmlMinifier(HtmlMinificationSettings settings = null, ICssMinifier cssMinifier = null, IJsMinifier jsMinifier = null, ILogger logger = null) { settings = settings ?? new HtmlMinificationSettings(); _genericHtmlMinifier = new GenericHtmlMinifier( new GenericHtmlMinificationSettings { WhitespaceMinificationMode = settings.WhitespaceMinificationMode, RemoveHtmlComments = settings.RemoveHtmlComments, RemoveHtmlCommentsFromScriptsAndStyles = settings.RemoveHtmlCommentsFromScriptsAndStyles, RemoveCdataSectionsFromScriptsAndStyles = settings.RemoveCdataSectionsFromScriptsAndStyles, UseShortDoctype = settings.UseShortDoctype, CustomShortDoctype = settings.CustomShortDoctype, PreserveCase = settings.PreserveCase, UseMetaCharsetTag = settings.UseMetaCharsetTag, EmptyTagRenderMode = settings.EmptyTagRenderMode, RemoveOptionalEndTags = settings.RemoveOptionalEndTags, PreservableOptionalTagList = settings.PreservableOptionalTagList, RemoveTagsWithoutContent = settings.RemoveTagsWithoutContent, CollapseBooleanAttributes = settings.CollapseBooleanAttributes, AttributeQuotesRemovalMode = settings.AttributeQuotesRemovalMode, RemoveEmptyAttributes = settings.RemoveEmptyAttributes, RemoveRedundantAttributes = settings.RemoveRedundantAttributes, RemoveJsTypeAttributes = settings.RemoveJsTypeAttributes, RemoveCssTypeAttributes = settings.RemoveCssTypeAttributes, PreservableAttributeList = settings.PreservableAttributeList, RemoveHttpProtocolFromAttributes = settings.RemoveHttpProtocolFromAttributes, RemoveHttpsProtocolFromAttributes = settings.RemoveHttpsProtocolFromAttributes, RemoveJsProtocolFromAttributes = settings.RemoveJsProtocolFromAttributes, MinifyEmbeddedCssCode = settings.MinifyEmbeddedCssCode, MinifyInlineCssCode = settings.MinifyInlineCssCode, MinifyEmbeddedJsCode = settings.MinifyEmbeddedJsCode, MinifyInlineJsCode = settings.MinifyInlineJsCode, MinifyEmbeddedJsonData = settings.MinifyEmbeddedJsonData, ProcessableScriptTypeList = settings.ProcessableScriptTypeList, MinifyKnockoutBindingExpressions = settings.MinifyKnockoutBindingExpressions, MinifyAngularBindingExpressions = settings.MinifyAngularBindingExpressions, CustomAngularDirectiveList = settings.CustomAngularDirectiveList, UseXhtmlSyntax = false }, cssMinifier, jsMinifier, logger ); }
/// <summary> /// Constructs instance of HTML minifier /// </summary> /// <param name="settings">HTML minification settings</param> /// <param name="cssMinifier">CSS minifier</param> /// <param name="jsMinifier">JS minifier</param> /// <param name="logger">Logger</param> public HtmlMinifier(HtmlMinificationSettings settings = null, ICssMinifier cssMinifier = null, IJsMinifier jsMinifier = null, ILogger logger = null) { settings = settings ?? new HtmlMinificationSettings(); _genericHtmlMinifier = new GenericHtmlMinifier( new GenericHtmlMinificationSettings { WhitespaceMinificationMode = settings.WhitespaceMinificationMode, RemoveHtmlComments = settings.RemoveHtmlComments, RemoveHtmlCommentsFromScriptsAndStyles = settings.RemoveHtmlCommentsFromScriptsAndStyles, RemoveCdataSectionsFromScriptsAndStyles = settings.RemoveCdataSectionsFromScriptsAndStyles, UseShortDoctype = settings.UseShortDoctype, PreserveCase = settings.PreserveCase, UseMetaCharsetTag = settings.UseMetaCharsetTag, EmptyTagRenderMode = settings.EmptyTagRenderMode, RemoveOptionalEndTags = settings.RemoveOptionalEndTags, PreservableOptionalTagList = settings.PreservableOptionalTagList, RemoveTagsWithoutContent = settings.RemoveTagsWithoutContent, CollapseBooleanAttributes = settings.CollapseBooleanAttributes, AttributeQuotesRemovalMode = settings.AttributeQuotesRemovalMode, RemoveEmptyAttributes = settings.RemoveEmptyAttributes, RemoveRedundantAttributes = settings.RemoveRedundantAttributes, RemoveJsTypeAttributes = settings.RemoveJsTypeAttributes, RemoveCssTypeAttributes = settings.RemoveCssTypeAttributes, PreservableAttributeList = settings.PreservableAttributeList, RemoveHttpProtocolFromAttributes = settings.RemoveHttpProtocolFromAttributes, RemoveHttpsProtocolFromAttributes = settings.RemoveHttpsProtocolFromAttributes, RemoveJsProtocolFromAttributes = settings.RemoveJsProtocolFromAttributes, MinifyEmbeddedCssCode = settings.MinifyEmbeddedCssCode, MinifyInlineCssCode = settings.MinifyInlineCssCode, MinifyEmbeddedJsCode = settings.MinifyEmbeddedJsCode, MinifyInlineJsCode = settings.MinifyInlineJsCode, ProcessableScriptTypeList = settings.ProcessableScriptTypeList, MinifyKnockoutBindingExpressions = settings.MinifyKnockoutBindingExpressions, MinifyAngularBindingExpressions = settings.MinifyAngularBindingExpressions, CustomAngularDirectiveList = settings.CustomAngularDirectiveList, UseXhtmlSyntax = false }, cssMinifier, jsMinifier, logger ); }
public override string MinifyString(string source) { var weHtmlSettings = WESettings.Instance.Html; var settings = new HtmlMinificationSettings { PreserveCase = weHtmlSettings.PreserveCase, // Tags RemoveOptionalEndTags = false, //EmptyTagRenderMode = HtmlEmptyTagRenderMode.Slash, // Attributes AttributeQuotesRemovalMode = weHtmlSettings.AttributeQuotesRemovalMode, RemoveRedundantAttributes = false, // JavaScript templating ProcessableScriptTypeList = weHtmlSettings.ProcessableScriptTypeList, MinifyKnockoutBindingExpressions = weHtmlSettings.MinifyKnockoutBindingExpressions, MinifyAngularBindingExpressions = weHtmlSettings.MinifyAngularBindingExpressions, CustomAngularDirectiveList = weHtmlSettings.CustomAngularDirectiveList }; var minifier = new HtmlMinifier(settings); MarkupMinificationResult result = minifier.Minify(source, generateStatistics: true); if (result.Errors.Count == 0) { WebEssentialsPackage.DTE.StatusBar.Text = "Web Essentials: HTML minified by " + result.Statistics.SavedInPercent + "%"; return result.MinifiedContent; } else { WebEssentialsPackage.DTE.StatusBar.Text = "Web Essentials: Cannot minify the current selection. See Output Window for details."; Logger.ShowMessage("Cannot minify the selection:\r\n\r\n" + String.Join(Environment.NewLine, result.Errors.Select(e => e.Message))); return null; } }
public static string MinifyString(string extension, string content) { if (extension == ".css") { Minifier minifier = new Minifier(); CssSettings settings = new CssSettings(); settings.CommentMode = CssComment.None; if (WESettings.GetBoolean(WESettings.Keys.KeepImportantComments)) { settings.CommentMode = CssComment.Important; } return minifier.MinifyStyleSheet(content, settings); } else if (extension == ".js") { Minifier minifier = new Minifier(); CodeSettings settings = new CodeSettings() { EvalTreatment = EvalTreatment.MakeImmediateSafe, PreserveImportantComments = WESettings.GetBoolean(WESettings.Keys.KeepImportantComments) }; return minifier.MinifyJavaScript(content, settings); } else if (_htmlExt.Contains(extension.ToLowerInvariant())){ var settings = new HtmlMinificationSettings { RemoveOptionalEndTags = false, AttributeQuotesRemovalMode = HtmlAttributeQuotesRemovalMode.KeepQuotes }; var minifier = new HtmlMinifier(settings); MarkupMinificationResult result = minifier.Minify(content, generateStatistics: true); if (result.Errors.Count == 0) { EditorExtensionsPackage.DTE.StatusBar.Text = "Web Essentials: HTML minified by " + result.Statistics.SavedInPercent + "%"; return result.MinifiedContent; } else { EditorExtensionsPackage.DTE.StatusBar.Text = "Web Essentials: Cannot minify the current selection"; return content; } } return null; }
/// <summary> /// Minifies the HTML content. /// </summary> /// <param name="useEmptyMinificationSettings"> /// Boolean to specify whether to use empty minification settings. /// Default value is <c>false</c>, this will use commonly accepted settings. /// </param> public MinifyHtml(bool useEmptyMinificationSettings = false) { // https://github.com/Taritsyn/WebMarkupMin/wiki/HTML-Minifier _minificationSettings = new HtmlMinificationSettings(useEmptyMinificationSettings); }