GithubHelp home page GithubHelp logo

csstidy's Introduction

CSSTidy

CSSTidy is a CSS minifier

  • css_optimiser.php is the web-interface
  • class.csstidy.php is the parser
  • bin/pcsstidy is the standalone command line executable

This class represents a CSS parser which reads CSS code and saves it in an array. In opposite to most other CSS parsers, it does not use regular expressions and thus has full CSS3 support and a higher reliability. The downside of not using regular expressions is a lower speed though. Additional to that it applies some optimisations and fixes to the CSS code.

Usage

include('class.csstidy.php');
$csstidy = new csstidy();

// Set some options :
$csstidy->set_cfg('optimise_shorthands', 2);
$csstidy->set_cfg('template', 'high');

// Parse the CSS
$csstidy->parse($css_code);

// Get back the optimized CSS Code
$css_code_opt = $csstidy->print->plain();

Changelog

  • v2.0.0 :
    • PHP 7.1 is the minimal version. Compatible PHP 8.0 and 8.1
  • v1.7.3 :
    • fix bug and notice on reverse_left_and_right option
  • v1.7.1 :
    • fix deprecated with PHP 7.4
  • v1.7.0 :
    • provide bin/pcsstidy for command line usage
    • support nested @media and @supports rules
  • v1.6.5 :
    • fix warnings with PHP 7.3
  • v1.6.4 :
    • preserve important comments (starting with !) in the minification /*! Credits/Licence */
  • v1.6.3 :
    • border-radius shorthands optimisation, reverse_left_and_right option
  • v1.5.7 :
    • PHP 7 compatibility, composer update, Travis CI integration
  • v1.5.6 :
    • fixes minor bugs, mainly on CSS3 properties/units
  • v1.5.2 :
  • v1.4 :
    Is the new version coming from master branch (corresponds to the initial trunk of svn repository) after beeing stabilized
  • v1.3 branch corresponds to the last stable relase published by the author.
    It integrates some bugfixes and a 1.3.1 version has been taged Since the original project (http://csstidy.sourceforge.net/index.php) has been suspended here is the import of https://csstidy.svn.sourceforge.net/svnroot/csstidy on 2010-11-14

Only PHP version is here maintained

Licence

Copyright 2005-2007 Florian Schmitz
Copyright 2010-2019 Cedric Morin

CSSTidy is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

CSSTidy is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

History

Original Tracker : http://sourceforge.net/tracker/?group_id=148404&atid=771415

csstidy's People

Contributors

amiga-500 avatar azettl avatar bytestream avatar cerdic avatar cfinke avatar christophwurst avatar claviska avatar dsferruzza avatar flug avatar jaydiablo avatar marcimat avatar need4video avatar nuxodin avatar phenix-factory avatar phpgangsta avatar richeryang avatar sunflowerfuchs avatar weikinhuang avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

csstidy's Issues

Nested @media rules not supported

@media print {
  #navigation { display: none }
  @media (max-width: 12cm) {
    .note { float: none }
  }
}

Produce

@media print {
	#navigation
	{
		display:none
	}
}

@media (max-width: 12cm) {
	.note
	{
		float:none
	}
}

Which is a mistake in CSS 3.0

Notice sur class.csstidy_optimise.php : reverse_left_and_right_background_position( )

En utilisant une fonction de transformation rtl, on obtient :
Notice: Undefined offset: 1 in … class.csstidy_optimise.php on line 1286

Dans la méthode reverse_left_and_right_background_position($value)

  • $value = "0 0"
  • $values = []

La trace indique qu’on arrive avec background: #fff, si ça peut aider...

^ array:27 [▼
  0 => array:7 [▼
    "file" => "class.csstidy_optimise.php"
    "line" => 1234
    "function" => "reverse_left_and_right_background_position"
    "class" => "csstidy_optimise"
    "object" => csstidy_optimise {#151 ▶}
    "type" => "->"
    "args" => array:1 [▼
      0 => "0 0"
    ]
  ]
  1 => array:7 [▼
    "file" => "class.csstidy_optimise.php"
    "line" => 1077
    "function" => "reverse_left_and_right_background"
    "class" => "csstidy_optimise"
    "object" => csstidy_optimise {#151 ▶}
    "type" => "->"
    "args" => array:1 [▼
      0 => "#fff"
    ]
  ]
  2 => array:7 [▼
    "file" => "class.csstidy_optimise.php"
    "line" => 79
    "function" => "reverse_left_and_right"
    "class" => "csstidy_optimise"
    "object" => csstidy_optimise {#151 ▶}
    "type" => "->"
    "args" => array:1 [▼
      0 => array:9 [▼
        "display" => "block"
        "margin" => "0"
        "padding" => "0"
        "min-height" => "3.2em"
        "background" => "#fff"
        "border" => "1px solid #ddd"
        "border-color" => "hsla(0,0%,0%,0.1)"
        "border-bottom-color" => "hsla(0,0%,0%,0.05)"
        "border-radius" => "6px 6px 0 0"
      ]
    ]
  ]

Security

I've sent you a couple of emails at your c****c@y******m [redacted for your privacy] address regarding some possible security issues in the CSSTidy codebase. Did you get them?

Question about minimal optimization

I use cssmin which does too much with certain bootstrap.css'es (and evtl. also others - sometimes strange problems with some css'es)

How can i adjust csstidy , so that it just removes / *.... * / and //... and unecessary spaces and otherwise changes as little as possible - regarding code optimization ? The code may be a little bigger - i use mod_deflate anyway.

Contact information for responsible disclosure

I have found a security vulnerability and would like to inform you about it in a non-public way, but I couldn't find any current contact information anywhere. Is there an e-mail address I can write to?

Methods with the same name as their class will not be constructors in a future version of PHP

CSS Tidy generates these errors when running setting WP_DEBUG: TRUE.

csstidy has a deprecated constructor in /app/public/wp-content/plugins/wp-foft-loader/includes/csstidy/class.csstidy.php on line 61

csstidy_print has a deprecated constructor in /app/public/wp-content/plugins/wp-foft-loader/includes/csstidy/class.csstidy_print.php on line 39

csstidy_optimise has a deprecated constructor in /app/public/wp-content/plugins/wp-foft-loader/includes/csstidy/class.csstidy_optimise.php on line 39

Warning: Cannot modify header information - headers already sent by (output started at /path/to/plugin/includes/csstidy/class.csstidy_print.php:39) in /wp-includes/pluggable.php on line 1251

Warning: Cannot modify header information - headers already sent by (output started at /path/to/plugin/includes/csstidy/class.csstidy_print.php:39) in /wp-includes/pluggable.php on line 1254

center est indépendant de la référence left/right

L'option reverse_left_and_right ajoute un disgracieux autant qu'inutile right en reference quand on a une position center:

.foo {
    background-position: center center;
}

devient :

.foo {
    background-position: center right center;
}

ctype_alpha() is wrong

The replacement definition for the ctype_alpha() function is wrong. In its current state, it will only check for the existence of a letter in the checked string, but it does not check if ALL characters are letters.
It should be something like

function ctype_alpha( $text ) {
    return ( 1 === preg_match( '/^[a-zA-Z]+$/', $text ) );
}

Font-face src using relative URL

In my css files I have relative URLs for background images and these get converted into absolute URL perfectly.

My problem is for font-face src, is it possible to also set this to be converted into absolute URL?

The reason for this is because my minified css file is saved in a different location to the original css files.

License problem

The whole package is licensed with the LGPL, but in data.inc.php the quoted license is the standard GPL, which conflicts and actually makes it impossible for the whole package being LGPL.

Could you clear this up and correct the licensing for the whole package or the file?

Add a script to run it from command line as C++ csstidy drop in replacement

I was using the CSSTidy C++ version to compress my CSS but this version had problem with format in @font-face. After taking a quick look to your up to date PHP version, I was thinking that this problem was fix in it (actually it's not fix, check my next issue).

So, I decided to use it instead of the C++ one. But I needed to call from the command line (like the C++ version) because I'm using it through django_compressor. The solution was to write a script to run it from the command line. I tried to do it in way that it will be mostly a drop in replacement of the C++ version.

You can found my script here: https://gist.github.com/2789689

It's NOT well tested and I don't think I will put more work on it because I can't use your CSSTidy PHP version for the moment. But feel free to do whatever you want with it, just keep my name in the credits please.

N.B.: I should add that the PHP and C++ versions are not totally equivalent. If I'm not wrong, the C++ version remove duplicate properties like this:

.duplicate{
    display: none;
    display: none;
}

The PHP version don't do that. I saw other differences like quotation around @font-face font-family name, etc.

Issue with multiple scripts/runs

I am glad the project is still maintained.

Interesting, that it still has the same bug as the original one, though.
You cannot use the class twice, for 2 css files in a row. the second file would always be empty upon parse():

$this->_Tidy->parse($content);
$css = $this->_Tidy->css;

...
unset($this->_Tidy); // try to avoid the bug by destroying
return $css;

in the second run with another script it returns an empty array.
thing is, If I switch the two files, the other one (now first) works fine.
So its definitly in the csstidy class somewhere
even destroying the object, unsetting the globals supervar etc won't help.

PS: would it be possible to rewrite it to not use $GLOBALS etc? I think the way of doing it this way and jumping outside its clean scope is not best practice anymore and should be avoided. the class can perfectly store the references inside some protected variables.

Bad include path breaks command line executable

I have csstidy 2.0.1 installed via composer. The command line executable is broken. The include path for class.csstidy.php is incorrect on line 42 of cerdic/css-tidy/bin/pcsstidy.

$ vendor/bin/pcsstidy --help
PHP Warning:  include(class.csstidy.php): failed to open stream: No such file or directory in /var/www/html/dev/vendor/cerdic/css-tidy/bin/pcsstidy on line 42
PHP Stack trace:
PHP   1. {main}() /var/www/html/dev/vendor/bin/pcsstidy:0
PHP   2. include() /var/www/html/dev/vendor/bin/pcsstidy:112
PHP Warning:  include(): Failed opening 'class.csstidy.php' for inclusion (include_path='.:/usr/share/php') in /var/www/html/dev/vendor/cerdic/css-tidy/bin/pcsstidy on line 42
PHP Stack trace:
PHP   1. {main}() /var/www/html/dev/vendor/bin/pcsstidy:0
PHP   2. include() /var/www/html/dev/vendor/bin/pcsstidy:112
PHP Fatal error:  Uncaught Error: Class 'csstidy' not found in /var/www/html/dev/vendor/cerdic/css-tidy/bin/pcsstidy:43
Stack trace:
#0 /var/www/html/dev/vendor/bin/pcsstidy(112): include()
#1 {main}
  thrown in /var/www/html/dev/vendor/cerdic/css-tidy/bin/pcsstidy on line 43

Comment in selector partly broken

In following selector

/* ici */
.cartouche, .surlignable,
/*.liste h2,*/
ul.liste-items li.hentry,
p.pagination,
#comments,
p.repondre,
.ps,
.hyperlien,
#descriptif_site_spip,
.page_login .formulaire_spip,
.page_login #formulaire_menu_lang,
.resultats,
.documents_joints,
.chapo,
.page_article .formulaire_ecrire_auteur {
    padding: 30px;
}

/*.liste h2,*/ becomes /.liste h2,*/
and the rule is broken

CSS variables is not supported

Hi there

I use CSS variables in my code and it shows the following log.

{"2":[{"m":"Removed invalid property: --color","t":"Warning"}]}

Is there any way to support CSS variables except turning off discard_invalid_properties? Thanks!

Example

Before

:root {
  --color: red;
}

After

:root {}

font-face src-property

E.g. Bootstrap Glyphicons font-face.

Before:

@font-face {
    font-family: 'Glyphicons Halflings';
    src: url('../fonts/glyphicons-halflings-regular.eot');
    src: url('../fonts/glyphicons-halflings-regular.eot?#iefix') format('embedded-opentype'),
         url('../fonts/glyphicons-halflings-regular.woff') format('woff'),
         url('../fonts/glyphicons-halflings-regular.ttf') format('truetype'),
         url('../fonts/glyphicons-halflings-regular.svg#glyphicons_halflingsregular') format('svg');
}

After (w/o minification for clarity)

@font-face {
    font-family: "Glyphicons Halflings";
    src: url("") format("embedded-opentype"),
         url("../fonts/glyphicons-halflings-regular.woff") format("woff"),
         url("../fonts/glyphicons-halflings-regular.ttf") format("truetype"),
         url("") format("svg");
}

The problems:

  • shows only last src-property
  • url like ...eot?#... and ...svg#... not shows

Remove unnecessary backslashes

First of all, it's great to see this project is still alive. I'm really enjoying the changes made after v1.3 :)

There's however a (new) minor issue with the "Remove unnecessary backslashes" checkbox.

Using this with a "custom layout", this feature simply doesn't work anymore. Also after submitting the "Process CSS" button it will automatically uncheck itself again.

Properties not being supported

As a quick test I've tried using some css properties and notice that they are not in the data.inc.php file (sorry - don't know which CSS they relate to).

grid-area
grid-template-columns
grid-template-rows
grid-template-areas (important note - these require line breaks to be retained)
-ms-grid-columns
-ms-grid-column
-ms-grid-column-span
-ms-grid-rows
-ms-grid-row
-ms-flex-line-pack
align-content
-webkit-transition
-o-transition
-webkit-touch-callout
-webkit-user-select
-khtml-user-select
-moz-user-select
-ms-user-select
user-select
-webkit-appearance

Corrupting rgb() color definition

When using " compress colors" option it does not recognize rgb() notation properly.

considering example :

white10 { color: rgba(255, 255, 255, 10%); }
white20 { color: rgb(255 255 255 / 20%);   }
white30 { color: rgb(255, 255, 255, 30%);  }

Output :

Optimised color: Changed "rgb(255 255 255 / 20%)" to "red"
Optimised color: Changed "rgb(255,255,255,30%)" to "#fff"

white10{color:rgba(255,255,255,10%)}
white20{color:red}
white30{color:#fff}

Should be:

white10{color:#ffffff1a}
white20{color:#fff3}
white30{color:#ffffff4d}

Keyframes doesn't work

Hi,

I'm not sure whether this is the right place to report bugs, so feel free to point me elsewhere if needed. I have a very simple CSS file:

@keyframes blink {
  0% { background: #7e7; }
  50% { background: none; }
  100% { background: #7e7; }
}

When throwing this file at CSSTidy, it gives me this result:

-----------------------------------

50% {
background:none;
}

100% {
background:#7e7;
}
-----------------------------------

1: Invalid @-rule: keyframes (removed)
2: Invalid property in CSS2.1: 0background

-----------------------------------

which is obviously not right, because @keyframes blink is valid syntax, right?

Transition subvalue number compression error

Hi.

The compression changes 0s to 0 which is invalid in transition. for example:
transition: color 0.4s ease 0s;
will become
transition: color 0.4s ease 0;
which doesn't work

php8.2 Compatibility Issues

class.csstidy.php
8192: Creation of dynamic property csstidy::$template is deprecated
Missing
public $template;

class.csstidy_optimise.php
Missing
public $css;
public $sub_value;
public $at;
public $selector;
public $property;
public $value;
class.csstidy_print.php
Missing
public $css;
public $template;
public $tokens;
public $charset;
public $import;
public $namespace;

BUG - @import

Normally the @import looks like this:

@import url("my.css");

When this runs through CSSTidy it changes it to:

@import "my.css";

HOWEVER, if you start with:

@import "my.css";

Then CSSTidy changes it to:

@import my.css;

Notice the double-quotes have been stripped. When this is done the imports no longer work.

This is a bug.

The fix can be found at PR #45

$version still showing v1.5.2

This is not an issue but noticed the $version member variable is still set to "1.5.2" instead of "1.5.5" (current version)

File: class.csstidy.php
Line #: 150

Currently:

public $version = '1.5.2';

Should be:

public $version = '1.5.5';

!important space is vanished

Hi, I found some bug.

the interval between "value" and "!important" vanishes.

For example,
a {color:red !important} becomes a {color:red!important}

Thanks for your great work :)

Indent inside media queries

Hi, I'm using this fork of CSSTidy for DirtyMarkup, as the original was abandoned, doesn't support media queries, etc. So thanks for your work on this project!

Quick feature request for indenting media queries. The current output looks like this:

@media (max-width: 767px) {
h1
    font-size: 2em;
}
}

Desired output is:

@media (max-width: 767px) {
    h1
        font-size: 2em;
    }
}

It looks like this would require a new formatter in the template files, or maybe it could be assumed that whatever the indent level is for properties could simply be doubled.

What are your thoughts on this?

V2.1 .exe

Hello, I love this tool, but don't know how to build for Windows. Is there any way you could upload v2.1.0 (or latest version)?

I found older version on SourceForge: https://sourceforge.net/projects/csstidy/
But it is not supporting CSS3 well.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.