Created
August 11, 2014 12:34
-
-
Save tompipe/4717f6354b92534a3b45 to your computer and use it in GitHub Desktop.
Enable HTML Minification
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Global : UmbracoApplication | |
{ | |
protected override void OnApplicationStarting(object sender, EventArgs e) | |
{ | |
GlobalFilters.Filters.Add(new RemoveHtmlWhitespaceFilterAttribute()); | |
base.OnApplicationStarting(sender, e); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RemoveHtmlWhitespaceFilterAttribute : ActionFilterAttribute | |
{ | |
public override void OnActionExecuting(ActionExecutingContext filterContext) | |
{ | |
var response = filterContext.HttpContext.Response; | |
response.Filter = new ModifyOutputFilter(response.Filter, s => | |
{ | |
if (I2GSettings.Instance.EnableMinification) | |
{ | |
s = Regex.Replace(s, @"\s+", " "); | |
s = Regex.Replace(s, @"\s*\n\s*", "\n"); | |
s = Regex.Replace(s, @"\s*\>\s*\<\s*", "><"); | |
s = Regex.Replace(s, @"<!--(.*?)-->", ""); //Remove comments | |
// single-line doctype must be preserved | |
var firstEndBracketPosition = s.IndexOf(">", StringComparison.Ordinal); | |
if (firstEndBracketPosition >= 0) | |
{ | |
s = s.Remove(firstEndBracketPosition, 1); | |
s = s.Insert(firstEndBracketPosition, ">"); | |
} | |
} | |
return s; | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment