diff --git a/components/HtmlHelper.cfc b/components/HtmlHelper.cfc index eebf9f5..a9be43b 100644 --- a/components/HtmlHelper.cfc +++ b/components/HtmlHelper.cfc @@ -118,15 +118,23 @@ component { result = result.reReplace( "", "", "all" ); // remove hardcoded html(multiline/singleline) comments } + + if( stripScriptAndCssComments ) { + result = result - .reReplace( "(\s*)\/\*(.|\n)*?\*\/", "", "all" ) // remove hardcoded javascript/css multiline comments; - .reReplace( "\s?(\/\/)(.*?)(\n|<\/script)", "\3", "all" ) // remove hardcoded javascript singleline comments - } + .reReplace( "(\s*)\/\*(.|\n)*?\*\/", " ", "all" ) // remove hardcoded javascript/css multiline comments; + .reReplace( "([\n|\r]+)\/\/.*?(\n|<\/script)", "\2", "all" ) // remove hardcoded javascript inline comments + .reReplace( "\s+\/\/.*?(\n|<\/script)", "\1", "all" ) // remove hardcoded javascript inline comments + } + + elseif( compressWhitespaces ){ result = result.reReplace( "\s?(\/\/)(.*?)(\n|<\/script)", "/*\2*/\3", "all" ) // convert single line comments to multiline, otherwise it will break javascript } + + if( compressWhitespaces ) { result = result.reReplace( "\s+", " ", "all" ); // compress all double tabs/spaces/newlines to single spaces } diff --git a/docs/index.html b/docs/index.html index f1e2f9c..f1bc192 100644 --- a/docs/index.html +++ b/docs/index.html @@ -1 +1,62 @@ -HtmlHelper.cfc: minifyHtml() & encodeTrustedHtml() in your CFML Projects Fork me on GitHub

HtmlHelper.cfc (Vers. 0.9.2)


Minify your CFML generated HTML at runtime!

A simple basic CFML component to

1. minifyHtml( string html required )

Converts a whitespace poluted HTML block like this

        <!DOCTYPE html>
+        <head>
+<title>Hot CFML Page & content</title>
+                    <meta charset="utf-8">
+        <meta http-equiv="X-UA-Compatible" content="IE=edge">
+    <meta name="viewport" content="width=device-width, initial-scale=1">
+<style> /* some comment 
+    */ 
+    .someClass{ 
+        font-size: 1rem; /* set font size */        } </style>
+
+    <meta name="description" content="Just some Example">
+<link rel="stylesheet" href="css/main.css">
+            </head> <body>
+<!--     Say "Hello" to the CFML coders! --> <p>Hello to all CFML devs 😀 🤩 around the 🌎!!! </p>
+<script>    /*    this is just some 
+            embedded JavaScript          */
+console.log('Log Something'); // this is just some Javascript </script>        </body> </html>
+

... into a minified HTML version like this

<!DOCTYPE html><head><title>Hot CFML Page &amp; content</title><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1"><style> .someClass{ font-size: 1rem; } </style><meta name="description" content="Just some Example"><link rel="stylesheet" href="css/main.css"></head><body><p>Hello to all CFML devs &#x1f600; &#x1f929; around the &#x1f30e;&#x21;&#x21;&#x21; </p><script> console.log('Log Something');</script></body></html>
+

The function minifyHtml() minifies CFML generated html content by:

Passing arguments to minifyHtml() is possible (e.g minifyHtml( htmlString=htmlBlock, stripHtmlComments=true ))

2. encodeTrustedHTML( string trustedHtml required );

Converts HTML from a trusted source with unencoded characters like this

<div class="someClass">
+    I tend to add "€, ä, &, é, ß" and I 
+    even might tend to add a > (greater sign)
+    directly into my html 😲, because I'm
+    a lazy content writer and I don't like 
+    adding those as html encoded characters manually!
+</div>
+

... into proper html like this

<div class="someClass"> I tend to add &quot;&euro;, &auml;, &amp;, &eacute;, &szlig;&quot; and I even might tend to add a &gt; &#x28;greater sign&#x29; directly into my html &#x1f632;, because I&#x27;m a lazy content writer and I don&#x27;t like adding those as html encoded characters manually&#x21; </div>
+

The function encodeTrustedHTML() detects and encodes unencoded characters, but preserves valid HTML-Entities and HTML-Tags already present in the submitted HTML.

3. Examples

Example of minifying a whitespace overfilled WordPress page with minifyHtml():

<!--- /examples/cfhttpMinifyHtml.cfm: minifyHtml() --->
+<!--- Make sure to override admin setting and read template with correct charset(UTF-8) --->
+<cfprocessingdirective pageEncoding="UTF-8">
+<cfscript>
+    // get whitespace polluted wordpress page!
+    cfhttp(method="GET", charset="utf-8", url="https://news.microsoft.com/source/", result="result" ) {};
+
+    htmlHelperService=new components.HtmlHelper();
+    cfcontent( reset = "true" );
+    writeoutput( 
+        htmlHelperService.minifyHtml( result.filecontent )
+    );
+</cfscript>
+

Example of encoding a trusted HTML block to escape unescaped characters with encodeTrustedHtml():

<!--- /examples/encodeTrustedHtml.cfm: encodeTrustedHtml() --->
+<!--- Make sure to override admin setting and read template with correct charset(UTF-8) --->
+<cfprocessingdirective pageEncoding="UTF-8">
+<cfsavecontent variable="someHtmlBlock">
+    <div>
+        I'm adding some unescaped HTML directly into my HTML! 
+        I't doesn't matter if they have already been encoded or not. 
+
+        E.g " &amp; or & " will both create the same source. Add
+        letters like ä, ö, Ä, ü, é, punctuations like ~, >, ⁋, ※, 
+        currencies like $, €, £, ¥, ₹, symbols like ©, ®, ™, Ω, 
+        arrows like →, ↖, ↳, ⇗, emojis like 👋 🤚 🖐 ✋ and they will be encoded properly.
+    </div>
+</cfsavecontent>
+<cfscript>
+    htmlHelperService=new components.HtmlHelper();
+    cfcontent( reset = "true" );
+    writeoutput( 
+        htmlHelperService.encodeTrustedHtml ( someHtmlBlock )
+    );
+</cfscript>
+

4. Service Functions as Lambda Expression

HtmlHelper.cfc passes the functions minifyHtml() and encodeTrustedHtml() as Lambda Expressions to enhance inner local scoping (see code here at GitHub):

cfml html minifier

5. Tips & Security Advisory

6. Downloads

7. How to run the repository locally

To test or watch the code running locally, you'll need CommandBox as dependency:

  1. Download the Repository as ZIP-File
  2. Unzip it
  3. Run server.bat on Windows or server.shon MacOs/Linux
  4. Wait for commandBox open the browser and load the page

8. Remarks

I'm not taking anything for this but if you like or you're using it, I kindly ask you to donate to the Lucee Organization to make this awesome cfengine even better:

Lucee Open Collective Donation ❤️

9. About

\ No newline at end of file