-
-
Save mjclemente/477dd7d0e9b43102104e5892f46a4949 to your computer and use it in GitHub Desktop.
ColdFusion Compress HTML
This file contains 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
/** | |
* @hint Removes whitespace from HTML code | |
Originally authored by Jordan Clark (JordanClark@telus.net) | |
*/ | |
public string function compressHtml( | |
required string html, | |
numeric level = 2 | |
){ | |
local.response = this.trim(arguments.html); | |
if (arguments.level == 1) { | |
// Remove whitespace at beginning of line | |
local.response = reReplaceNoCase(local.response, "\n+\s+", chr(10), "all"); | |
} | |
if (arguments.level >= 2) { | |
// Replace multiple whitespace characters with a single space | |
local.response = reReplaceNoCase(local.response, "\s{2,}", " ", "all"); | |
} | |
if (arguments.level >= 3) { | |
// Remove whitespace in between angle brackets | |
local.response = reReplaceNoCase(local.response, ">\s+<", "><", "all"); | |
// Remove HTML and CF comments | |
local.response = reReplaceNoCase(local.response, "<!--[^>]+>", "", "all"); | |
} | |
// Remove whitespace in between paired punctuation | |
if (arguments.level >= 4) { | |
local.response = reReplaceNoCase(local.response, "([>\(\{\[])\s+", "\1", "all"); | |
local.response = reReplaceNoCase(local.response, "\s+([<\)\}\]])", "\1", "all"); | |
} | |
return local.response; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment