diff --git a/distro/index.html b/distro/index.html index 103ec49..7db3ed0 100644 --- a/distro/index.html +++ b/distro/index.html @@ -1,249 +1 @@ -HtmlHelper.cfc: minifyHtml() & encodeTrustedHtml() in your CFML Projects Fork me on GitHub

HtmlHelper.cfc


Minify your CFML generated HTML on runtime!

A simple basic CFML component delivering lambda functions to

1. minifyHtml( string html required )

Converts a whitespace poluted HTML block like this ...

        <!DOCTYPE html>
-
-
-<html>
-
-                   <head>        <link rel="stylesheet" href="/libs/markdowncss/retro.css">
-
-          <link rel="stylesheet" 
-                 href="/libs/highlightjs/highlightjs_styles_default.min.css">
-
-
-
-  <link href="/libs/highlightjs/highlightjs_atom-one-dark.min.css" rel="stylesheet">
-
-
-            <style>
-
-
-    html {
-                        background: transparent;
-
-      }
-
-
-                    body {
-                     background: linear-gradient(92deg, #002e34 50%, #017f8e 76%);
-                               max-width: 992px;
-                          padding: 0 4rem 4rem;
-
-
-
-                }
-
-
-
-                h1,    h2,     h3,h4,    h5{
-                    margin-top:4rem;
-            }
-               h1{
-                    font-weight: 600;
-       font-size: 4.5rem;
-      }
-
-                h2{
-                    font-size:1.5rem;
-                    color:#96dce4;
-                            font-weight: 600;
-
-                }
-
-                    h3{
-                    font-size:1.2rem;
-                    color: #72cb13;
-                    font-weight: 600;
-                } 
-
-                .hljs-punctuation, .hljs-tag {
-                    color: #4cbac7;
-                }
-                                    .hljs-tag .hljs-attr, .hljs-tag .hljs-name {
-                    color: #4cbac7;
-                }
-                .hljs{
-                    background: #314161;
-                }
-                                pre { max-height: 10rem; overflow: auto; padding:0;}</style>
-        </head>
-                        <body>
-<h1>HtmlHelper.cfc</h1>
-
-            <hr />
-                        <p>A simple CFML component that helps to:</p>
-<ul>
-<li><strong>minify CFML generated html</strong> content, or</li>
-                <li>
-
-                <strong>
-                encode 
-
-
-                trusted HTML    
-
-           </strong> content containing non-compliant/unescaped characters in the inner HTML (such as &euro;,&uuml;,&ouml;,&szlig;, etc.).</li>
-</ul>
-                                        <p>               <a href="http://example.com">See at Github</a>
-
-
-                                                    </p>
-<hr />
-
-
-                                   <h2><em>Convert a whitespace poluted HTML block like this</em></h2>
-<h2><em>Into a minified HTML for the browser</em></h2>
-<hr />
-                <h3>minifyHtml( string html required )</h3>
-<p>The function <strong>minifyHtml()</strong> minifies CFML generated html content by:</p>
-<ul>
-
-
-
-
-
-<li>stripping HTML comments</li>
-                <li>stripping JavaScript multline comments</li>
-<li>stripping JavaScript singleline comments</li>
-                      <li>stripping whitespaces, such as all double tabs/spaces/newlines</li>
-                        <li>honouring and preserving the content within <code>&lt;pre&gt;&lt;/pre&gt;</code> or <code>&lt;code&gt;&lt;/code&gt;</code> tags as submitted</li>
-</ul>
-
-
-
-
-                <h3>encodeTrustedHTML( string trustedHtml required )
-
-    </h3>
-                    <p>A simple basic function that detects and encodes unencoded characters, but preserves valid HTML-Tags and HTML-Entities.</p>
-<h3><em>Convert HTML from a 
-
-
-                trusted source like this:</em></h3>
-
-
-
-
-
-<pre><code>&lt;div class="someClass"&gt;
-    I tend to add "€, ä, &amp;, é, ß"
-    directly into my html, because I'm
-    a lazy content writer and I don't like 
-    adding those as html encoded characters manually!
-&lt;/div&gt;
-</code></pre>
-<h3><em>Into proper html like this:</em></h3>
-<pre><code>&lt;div class="someClass"&gt;
-    I tend to add &amp;quot;&amp;euro;, &amp;auml;, &amp;amp;, &amp;eacute;, &amp;szlig;&amp;quot; 
-    directly into my html because I&amp;#x27;m
-    a lazy content writer and I don&amp;#x27;t like 
-    adding those as html encoded characters manually&amp;#x21;
-&lt;/div&gt;
-</code></pre>
-
-
-            <script src="/libs/highlightjs/highlight.min.js"></script>
-    <script>
-            const elements = document.querySelectorAll('code');
-
-
-        elements.forEach((element) => {
-
-
-                    element.classList.add('language-html');
-     });
-
-                                hljs.highlightAll();
-                                /* 
-                                *  this is just a JavaScript comment 
-                                */
-                                console.log( 'ready' );
-  </script>
-        </body>
-                      </html>
-

... into a minified HTML for the browser like this ...

<!DOCTYPE html><html><head><link rel="stylesheet" href="/libs/markdowncss/retro.css"><link rel="stylesheet" href="/libs/highlightjs/highlightjs_styles_default.min.css"><link href="/libs/highlightjs/highlightjs_atom-one-dark.min.css" rel="stylesheet"><style>html { background: transparent; } body { background: linear-gradient(92deg, #002e34 50%, #017f8e 76%); max-width: 992px; padding: 0 4rem 4rem; } h1,h2,h3,h4,h5{ margin-top:4rem; } h1{ font-weight: 600; font-size: 4.5rem; } h2{ font-size:1.5rem; color:#96dce4; font-weight: 600; } h3{ font-size:1.2rem; color: #72cb13; font-weight: 600; } .hljs-punctuation, .hljs-tag { color: #4cbac7; } .hljs-tag .hljs-attr, .hljs-tag .hljs-name { color: #4cbac7; } .hljs{ background: #314161; } pre { max-height: 10rem; overflow: auto; padding:0;}</style></head><body><h1>HtmlHelper.cfc</h1><hr /><p>AsimpleCFMLcomponentthathelpsto&#x3a;</p><ul><li><strong>minifyCFMLgeneratedhtml</strong>content,or</li><li><strong>encodetrustedHTML</strong>contentcontainingnon-compliant&#x2f;unescapedcharactersintheinnerHTML&#x28;suchas&euro;,&uuml;,&ouml;,&szlig;,etc.&#x29;.</li></ul><p><a href="http://example.com">SeeatGithub</a></p><hr /><h2><em>ConvertawhitespacepolutedHTMLblocklikethis</em></h2><h2><em>IntoaminifiedHTMLforthebrowser</em></h2><hr /><h3>minifyHtml&#x28;stringhtmlrequired&#x29;</h3><p>Thefunction<strong>minifyHtml&#x28;&#x29;</strong>minifiesCFMLgeneratedhtmlcontentby&#x3a;</p><ul><li>strippingHTMLcomments</li><li>strippingJavaScriptmultlinecomments</li><li>strippingridofJavaScriptsinglelinecomments</li><li>strippingwhitespaces,suchasalldoubletabs&#x2f;spaces&#x2f;newlines</li><li>honouringandpreservingthecontentwithin<code>&lt;pre&gt;&lt;/pre&gt;</code>or<code>&lt;code&gt;&lt;/code&gt;</code>tagsassubmitted</li></ul><h3>encodeTrustedHTML&#x28;stringtrustedHtmlrequired&#x29;</h3><p>AsimplebasicfunctionthatdetectsandencodesunencodedcharactersbutpreservesvalidHTML-TagsandHTML-Entities.</p><h3><em>ConvertHTMLfromatrustedsourcelikethis&#x3a;</em></h3><pre><code>&lt;div class="someClass"&gt;
-        I tend to add "€, ä, &amp;, é, ß"
-        directly into my html, because I'm
-        a lazy content writer and I don't like 
-        adding those as html encoded characters manually!
-    &lt;/div&gt;
-    </code></pre><h3><em>Intoproperhtmllikethis&#x3a;</em></h3><pre><code>&lt;div class="someClass"&gt;
-        I tend to add &amp;quot;&amp;euro;, &amp;auml;, &amp;amp;, &amp;eacute;, &amp;szlig;&amp;quot; 
-        directly into my html because I&amp;#x27;m
-        a lazy content writer and I don&amp;#x27;t like 
-        adding those as html encoded characters manually&amp;#x21;
-    &lt;/div&gt;
-    </code></pre><script src="/libs/highlightjs/highlight.min.js"></script><script> const elements = document.querySelectorAll('code'); elements.forEach((element) => { element.classList.add('language-html'); }); hljs.highlightAll(); console.log( 'ready' ); </script></body></html>
-

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

CFML example: How to use minifyHtml():
  1. Download the HtmlHelper.cfc at
  2. Save the HtmlHelper.cfc component to your application, e.g /wwwroot/components/HtmlHelper.cfc
  3. Create a minifyHtml.cfm at /wwwroot/minifyHtml.cfm

Add the following code and run it:

<!--- /examples/minifyHtml.cfm: minifyHtml() --->
-
-<!--- Make sure to override admin setting and read template with correct charset(UTF-8) --->
-<cfprocessingdirective pageEncoding="UTF-8">
-<cfsavecontent variable="someHtmlBlock">
-    <!DOCTYPE html>
-    <html lang="en">
-    <head>
-        <meta charset="UTF-8">
-        <meta name="viewport" content="width=device-width, initial-scale=1.0">
-        <meta http-equiv="X-UA-Compatible" content="ie=edge">
-        <title>Some Html</title>
-        <link rel="stylesheet" href="./style.css">
-        <link rel="icon" href="./favicon.ico" type="image/x-icon">
-    </head>
-    <body>
-        <main>
-            <h1>This is just a sample page!</h1>  
-        </main>
-        <script src="index.js"></script>
-    </body>
-    </html>
-    </cfsavecontent>
-    <cfscript>
-    htmlHelperService=new components.HtmlHelper();
-    cfcontent( reset = "true" );
-    writeoutput( 
-        htmlHelperService.minifyHtml( someHtmlBlock )
-    );
-    </cfscript>
-

Another example with cfhttp by fetching a whitespace overfilled wordpress page:

<!--- /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>
-

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 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-Tags and HTML-Entities.

CFML example: How to use encodeTrustedHtml():
  1. Download the HtmlHelper.cfc at
  2. Save the HtmlHelper.cfc component to your application, e.g /wwwroot/components/HtmlHelper.cfc
  3. Create a encodeTrustedHtml.cfm at /wwwroot/encodeTrustedHtml.cfm

Add the following code and run it:

<!--- /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>
-        Just adding some characters like " &amp; or & " ... both will create the same source.
-        But you can add ä, ö, Ä, ü, é, ~ > or other characters here.
-    </div>
-</cfsavecontent>
-<cfscript>
-    htmlHelperService=new components.HtmlHelper();
-    cfcontent( reset = "true" );
-    writeoutput( 
-        htmlHelperService.encodeTrustedHtml ( someHtmlBlock )
-    );
-</cfscript>
-

Lambda function expression

HtmlHelper.cfc delivers Lambda function expressions, keeping the working data in the inner function scope:

cfml html minifier

Tips & Security Advisory

Downloads

Run 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

Donations

I'm not taking anything for this but sharing with you. If you like/are using it, I kindly ask you to donate to the Lucee Organization to make this awesome cfengine even better:

❤️ Lucee Open Collective Donation ❤️

About

\ No newline at end of file +Connection Failure \ No newline at end of file diff --git a/package.json b/package.json index 07c25dc..1742849 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "repository": { "type": "git", - "url": "git+https://github.com/andreasRu/cfml-HtmlHelper.git" + "url": "git+https://github.com/andreasRu/cfml-Htmlhelper.git" }, "keywords": [ "cfml", @@ -24,7 +24,7 @@ "author": "Claudio Andreas Rüger", "license": "MIT", "bugs": { - "url": "https://github.com/andreasRu/cfml-HtmlHelper/issues" + "url": "https://github.com/andreasRu/cfml-htmlhelper/issues" }, - "homepage": "https://github.com/andreasRu/cfml-HtmlHelper#readme" + "homepage": "https://github.com/andreasRu/cfml-htmlhelper#readme" } diff --git a/serve.bat b/serve.bat index 634c8fa..6f1e1cf 100644 --- a/serve.bat +++ b/serve.bat @@ -1,3 +1,3 @@ set CWD=%cd% -box stop name=cfml-HtmlHelper -box start name=cfml-HtmlHelper \ No newline at end of file +box stop name=cfml-htmlhelper +box start name=cfml-htmlhelper \ No newline at end of file diff --git a/serve.sh b/serve.sh index a18d536..d67d9f6 100644 --- a/serve.sh +++ b/serve.sh @@ -3,4 +3,4 @@ cd `dirname $0` CWD="`pwd`" -box start name=cfml-HtmlHelper +box start name=cfml-htmlhelper diff --git a/server-cfml-HtmlHelper.json b/server-cfml-HtmlHelper.json index 4288e35..28fe9ba 100644 --- a/server-cfml-HtmlHelper.json +++ b/server-cfml-HtmlHelper.json @@ -1,5 +1,5 @@ { - "name":"cfml-HtmlHelper", + "name":"cfml-htmlhelper", "force":"true", "JVM":{ "heapSize":"2048" @@ -13,7 +13,7 @@ }, "trayOptions":[ { - "label":"cfml-HtmlHelper", + "label":"cfml-htmlhelper", "action":"openbrowser", "url":"http://${runwar.host}:${runwar.port}/", "disabled":false