John Griffiths
John Griffiths

Reputation: 3423

Can you strip carriage returns from CSS files?

Can you strip carriage returns from CSS files safely?

I have an PHP example in in this case, but the question is for any programming language.

Given the following sequence of tasks in minifying CSS.

START prepare CSS
# removeComments( $this->rawCSS );
# trimLines( $css );
# stripSpaces( $css );
$css = str_replace( array( "\r\n", "\r", "\n", "\t" ) , '' , $css );
# removeDoubleSpaces( $css );
# removePunctuationSpaces( $css );
# enforceInternetExplorerLineLengthLimit( $css );
# migrateImportsToTop( $css );
DONE prepare CSS

Will the removal of characters HT (9), LF (10), FF (12), CR (13) ever leave the CSS in an invalid condition?

Upvotes: 2

Views: 365

Answers (4)

gerryino
gerryino

Reputation: 378

EDIT: you can surely change the meaning of some property:

.someClass::after{    
    content: 'Some content here';
}

if the words are separated by TAB HT (9) you will end up with

content: 'Somecontenthere';

It can also broke some rule i think

.someClass
A{
color:white
}

it's perfectly valid css but it will end up as

.someClassA{color:white}

which is a different rule.

Upvotes: 1

ecc
ecc

Reputation: 1510

Yes, because of some comments.

// This is my precious class
.preciousClass {
  color: yellow;
}

When you remove all whitespace, though, this happens. It's not syntactically invalid, but it is not working any longer.

// This is my precious class.preciousClass {  color: yellow;}

But otherwise, your CSS won't be invalid just by removing non-space whitespace.

Upvotes: 0

Alex Shesterov
Alex Shesterov

Reputation: 27525

Yes, removing line breaks/tabs could make CSS invalid.

An example:

@import
url("imported.css");

would become invalid:

@importurl("imported.css");

Upvotes: 2

Quentin
Quentin

Reputation: 943569

Lists of values for properties are often separated by white space, which can be a new line. Given this code:

div { border: solid
red
1px;
  }
<div>content</div>

… stripping new lines would result in:

border: solidred1px;

… which would be invalid.

Upvotes: 2

Related Questions