Skip to content
This repository was archived by the owner on Nov 25, 2022. It is now read-only.

Commit d87ce90

Browse files
committed
All HTML is compressed except for <pre> tags content.
1 parent 351f1cd commit d87ce90

1 file changed

Lines changed: 104 additions & 3 deletions

File tree

src/skeleton/libraries/Theme.php

Lines changed: 104 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@
5353
* @since 1.3.3 changed "metadata" to "meta_tags" to avoid conflict with
5454
* the "Kbcore_metadata" library and added themes count.
5555
* @since 1.4.0 Added "the_doctype" filter and moved down body class filters.
56-
* @version 1.4.0
56+
* @since 1.4.1 All HTML is compressed except for <pre> tags content.
57+
* @version 1.4.1
5758
*/
5859
class Theme
5960
{
@@ -3329,10 +3330,16 @@ function($lang)
33293330
$this->_client['platform'] = $this->ci->agent->platform();
33303331
}
33313332

3333+
// --------------------------------------------------------------------
3334+
// Output Compression.
33323335
// --------------------------------------------------------------------
33333336

33343337
/**
33353338
* Compresses the HTML output
3339+
*
3340+
* @since 1.0.0
3341+
* @since 1.4.1 All HTML is compressed except for <pre> tags content.
3342+
*
33363343
* @access private
33373344
* @param string $output the html output to compress
33383345
* @return string the minified version of $output
@@ -3341,15 +3348,86 @@ public function compress_output($output)
33413348
{
33423349
// Make sure $output is always a string
33433350
(is_string($output)) OR $output = (string) $output;
3351+
3352+
// Nothing? Don't process.
3353+
if ('' === trim($output))
3354+
{
3355+
return '';
3356+
}
3357+
3358+
// Conserve <pre> tags.
3359+
$pre_tags = array();
3360+
3361+
if (false !== strpos($output, '<pre'))
3362+
{
3363+
// We explode the output and always keep the last part.
3364+
$parts = explode('</pre>', $output);
3365+
$last_part = array_pop($parts);
3366+
3367+
// Reset output.
3368+
$output = '';
3369+
3370+
// Marker used to identify <pre> tags.
3371+
$i = 0;
3372+
3373+
foreach ($parts as $part)
3374+
{
3375+
$start = strpos($part, '<pre');
33443376

3377+
// Malformed? Add it as it is.
3378+
if (false === $start)
3379+
{
3380+
$output .= $part;
3381+
continue;
3382+
}
3383+
3384+
// Identify the pre tag and keep it.
3385+
$name = "<pre csk-pre-tag-{$i}></pre>";
3386+
$pre_tags[$name] = substr($part, $start).'</pre>';
3387+
$output .= substr($part, 0, $start).$name;
3388+
$i++;
3389+
}
3390+
3391+
// Always add the last part.
3392+
$output .= $last_part;
3393+
}
3394+
3395+
// Compress the final output.
3396+
$output = $this->_compress_output($output);
3397+
3398+
// If we have <pre> tags, add them.
3399+
if ( ! empty($pre_tags))
3400+
{
3401+
$output = str_replace(array_keys($pre_tags), array_values($pre_tags), $output);
3402+
}
3403+
3404+
// Return the final output.
3405+
return $output;
3406+
}
3407+
3408+
// ------------------------------------------------------------------------
3409+
3410+
/**
3411+
* _compress_output
3412+
*
3413+
* The real method behind final output compression.
3414+
*
3415+
* @author Kader Bouyakoub
3416+
* @link https://github.com/bkader
3417+
* @since 1.4.1
3418+
*
3419+
* @access private
3420+
* @param string $output The final output.
3421+
* @return string The final output after compression.
3422+
*/
3423+
private function _compress_output($output)
3424+
{
33453425
// In orders, we are searching for
33463426
// 1. White-spaces after tags, except space.
33473427
// 2. White-spaces before tags, except space.
33483428
// 3. Multiple white-spaces sequences.
33493429
// 4. HTML comments
33503430
// 5. CDATA
3351-
3352-
// We return the minified $output
33533431
$output = preg_replace(array(
33543432
'/\>[^\S ]+/s',
33553433
'/[^\S ]+\</s',
@@ -3364,6 +3442,7 @@ public function compress_output($output)
33643442
"//&lt;![CDATA[\n".'\1'."\n//]]>"
33653443
), $output);
33663444

3445+
// We return the minified $output
33673446
return $output;
33683447
}
33693448

@@ -4626,3 +4705,25 @@ function array_clean(&$array)
46264705
}
46274706
}
46284707
}
4708+
4709+
// ------------------------------------------------------------------------
4710+
4711+
if ( ! function_exists('compress_html'))
4712+
{
4713+
/**
4714+
* compress_html
4715+
*
4716+
* Uses the Theme::compress_output method to compress the given string.
4717+
*
4718+
* @author Kader Bouyakoub
4719+
* @link https://github.com/bkader
4720+
* @since 1.4.1
4721+
*
4722+
* @param string $html The HTML string to compress.
4723+
* @return string The HTML string after being compressed.
4724+
*/
4725+
function compress_html($html)
4726+
{
4727+
return get_instance()->theme->compress_output($html);
4728+
}
4729+
}

0 commit comments

Comments
 (0)