Login   Register  
PHP Classes

File: index.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Corey Hart  >  CSS Compressor  >  index.php  >  Download  
File: index.php
Role: Example script
Content type: text/plain
Description: Testing script
Class: CSS Compressor
Compact the size of CSS definitions
Author: By
Last change: -Better options handling
-Look behind regexs are used to not break urls
-Singleton access added
-Exposing byte unit conversion display
-The compressor no longer self instantiates itself, you must explicitly initialize it.
-Most methods that were never meant to be public, have been turned into private/protected based on accessibility requirements.
Date: 4 years ago
Size: 8,679 bytes


Class file image Download
<!DOCTYPE html>
    <title>CSS Compressor [VERSION]</title>
    <link rel='stylesheet' type='text/css' href='styles.css' />
    <script src='js.js'></script>

CSS Compressor [VERSION]
Corey Hart @ http://www.codenothing.com

<h2>CSS Compressor [VERSION]</h2>


if ( isset( $_GET['view'] ) && $_GET['view'] == "compress" ) {
// Run compression on passed script
$CSSC = new CSSCompression$_POST['css'], $_POST );
$height = ( $CSSC->media || $CSSC->readability CSSCompression::READ_NONE ) ? "400px" "12px";

// Add results above the form
echo "<div id='results'>";
"<textarea style='height:$height;' onclick='this.select()'>".$CSSC->css."</textarea><br><br>";

// Form Saving
foreach ( $_POST as $k => $v ) {
        if ( 
$k != 'css' ) {
$checked$k ] = $v "checked='checked'" '';

// Readability is a select, not a checkbox
$checked['readability'] = $_POST['readability'];

// Set options by default
$opts = array(

    foreach ( 
$opts as $key ) {
$checked$key ] = "checked='checked'";
$checked['color-hex2shortcolor'] = '';


<form action='index.php?view=compress' method='POST'>
<tr valign='top'>
    <td width='50%'><textarea name='css'><?php if ( isset( $_POST['css'] ) ) echo $_POST['css'];?></textarea></td>
        <label class='order-important'>
            <input type='checkbox' name='order-important' onChange='forceOrder(this);' <?php if ( isset( $checked['order-important'] ) ) echo $checked['order-important']; ?> />
            Order Important Stylesheet (Won't mess with the order of selectors)
        <div class='options'>
                <input type='checkbox' name='color-long2hex' <?php if ( isset( $checked['color-long2hex'] ) ) echo $checked['color-long2hex']; ?> />
                Convert long color names to short hex names (aliceblue -&gt; #f0f8ff)
            <label class='odd'>
                <input type='checkbox' name='color-rgb2hex' <?php  if ( isset( $checked['color-rgb2hex'] ) ) echo $checked['color-rgb2hex']; ?> />
                Convert rgb colors to hex (rgb(159,80,98) -&gt; #9F5062, rgb(100%) -&gt; #FFFFFF)
                <input type='checkbox' name='color-hex2shortcolor' <?php if ( isset( $checked['color-hex2shortcolor'] ) ) echo $checked['color-hex2shortcolor']; ?> />
                Convert long hex codes to short color names (#f5f5dc -&gt; beige)<br>
                <i>(Short colornames are only supported by newer browsers)</i>
            <label class='odd'>
                <input type='checkbox' name='color-hex2shorthex' <?php if ( isset( $checked['color-hex2shorthex'] ) ) echo $checked['color-hex2shorthex']; ?> />
                Convert long hex codes to short hex codes (#44ff11 -&gt; #4f1)
                <input type='checkbox' name='fontweight2num' <?php if ( isset( $checked['fontweight2num'] ) ) echo $checked['fontweight2num']; ?> />
                Convert font-weight names to numbers (bold -&gt; 700)
            <label class='odd'>
                <input type='checkbox' name='format-units' <?php if ( isset( $checked['format-units'] ) ) echo $checked['format-units']; ?> />
                Remove zero decimals and 0 units (15.0px -&gt; 15px || 0px -&gt; 0)
                <input type='checkbox' name='lowercase-selectors' <?php if ( isset( $checked['lowercase-selectors'] ) ) echo $checked['lowercase-selectors']; ?> />
                Lowercase html tags from list (BODY -&gt; body)
            <label class='odd'>
                <input type='checkbox' name='directional-compress' <?php if ( isset( $checked['directional-compress'] ) ) echo $checked['directional-compress']; ?> />
                Compress single defined multi-directional properties (margin:15px 25px 15px 25px -&gt; margin:15px 25px)
                <input type='checkbox' id='orderimportant1' name='multiple-selectors' <?php echo ( isset( $checked['order-important'] ) ? "disabled='disabled'" '')?> <?php if ( isset( $checked['multiple-selectors'] ) ) echo $checked['multiple-selectors']; ?> />
                Combine multiply defined selectors (p{color:blue;} p{font-size:12pt} -&gt; p{color:blue;font-size:12pt;})
            <label class='odd'>
                <input type='checkbox' id='orderimportant2' name='multiple-details' <?php echo ( isset( $checked['order-important'] ) ? "disabled='disabled'" '')?> <?php if ( isset( $checked['multiple-details'] ) ) echo $checked['multiple-details']; ?> />
                Combine selectors with same details (p{color:blue;} a{color:blue;} -&gt; p,a{color:blue;})
                <input type='checkbox' name='csw-combine' <?php if ( isset( $checked['csw-combine'] ) ) echo $checked['csw-combine']; ?> />
                Combine color/style/width properties (border-style:dashed;border-color:black;border-width:4px; -&gt; border:4px dashed black)
            <label class='odd'>
                <input type='checkbox' name='auralcp-combine' <?php if ( isset( $checked['auralcp-combine'] ) ) echo $checked['auralcp-combine']; ?> />
                Combines cue/pause properties (cue-before: url(before.au); cue-after: url(after.au) -&gt; cue:url(before.au) url(after.au))
                <input type='checkbox' name='mp-combine' <?php if ( isset( $checked['mp-combine'] ) ) echo $checked['mp-combine']; ?> />
                Combine margin/padding directionals (margin-top:10px;margin-right:5px;margin-bottom:4px;margin-left:1px; -&gt; margin:10px 5px 4px 1px;)
            <label class='odd'>
                <input type='checkbox' name='border-combine' <?php if ( isset( $checked['border-combine'] ) ) echo $checked['border-combine']; ?> />
                Combine border directionals (border-top|right|bottom|left:1px solid black -&gt; border:1px solid black)
                <input type='checkbox' name='font-combine' <?php if ( isset( $checked['font-combine'] ) ) echo $checked['font-combine']; ?> />
                Combine font properties (font-size:12pt; font-family: arial; -&gt; font:12pt arial)
            <label class='odd'>
                <input type='checkbox' name='background-combine' <?php if ( isset( $checked['background-combine'] ) ) echo $checked['background-combine']; ?> />
                Combine background properties (background-color: black; background-image: url(bgimg.jpeg); -&gt; background:black url(bgimg.jpeg))
                <input type='checkbox' name='list-combine' <?php if ( isset( $checked['list-combine'] ) ) echo $checked['list-combine']; ?> />
                Combine list-style properties (list-style-type: round; list-style-position: outside -&gt; list-style:round outside
            <label class='odd'>
                <input type='checkbox' name='unnecessary-semicolons' <?php if ( isset( $checked['unnecessary-semicolons'] ) ) echo $checked['unnecessary-semicolons']; ?> />
                Removes the last semicolon of a property set ({margin: 2px; color: blue;} -&gt; {margin: 2px; color: blue})
                <input type='checkbox' name='rm-multi-define' <?php if ( isset( $checked['rm-multi-define'] ) ) echo $checked['rm-multi-define']; ?> />
                Remove multiply defined properties, STRONGLY SUGGESTED TO KEEP THIS ONE TRUE
            <label class='odd'>
                <select name='readability'>
                    <option value='0' <?php echo isset( $checked['readability'] ) && $checked['readability'] == "selected='selected'" '';?> >None</option>
                    <option value='1' <?php echo isset( $checked['readability'] ) && $checked['readability'] == "selected='selected'" '';?> >Minimal</option>
                    <option value='2' <?php echo isset( $checked['readability'] ) && $checked['readability'] == "selected='selected'" '';?> >Average</option>
                    <option value='3' <?php echo isset( $checked['readability'] ) && $checked['readability'] == "selected='selected'" '';?> >Maximum</option>
                <b>Readability</b> after compression (None == single line)
    <td align='center'><input type='submit' value=' Compress ' /></td>

<p style='margin-top:60px;font-size:9pt;'>
Have a question? Found a bug? Test it using the 
<a href='sandbox/'>sandbox</a> or 
<a href='mailto:corey@codenothing.com?Subject=CSSC Question/Bug'>mail me</a>.

<div style='margin-top:50px;'>
    <a href='http://www.codenothing.com/archives/php/css-compressor/'>Back to Original Article</a>