Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CSS Minification: Decline to minify files containing unicode escapes like \f178 #50

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/css-min.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
module.exports = ( body ) => {
const unicodePattern = /\\[0-9a-fA-F]{1,6}/;

// If Unicode escape sequence is found, return original CSS
if ( unicodePattern.test( body ) ) {
return body;
}

const csso = require( 'csso' );
return csso.minify( body, { restructure: false } ).css;
};
3 changes: 0 additions & 3 deletions routes/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ module.exports = async ( request, reply ) => {
if ( do_minify && path.match( /\.(dev|min)\./ ) ) {
do_minify = false;
}
if ( do_minify && path.endsWith( 'dashicons.css' ) ) {
do_minify = false;
}
log.minify = do_minify;
let use_cache = false;

Expand Down
4 changes: 0 additions & 4 deletions routes/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ module.exports = ( request, reply ) => {
if ( typeof request.query.minify === 'string' && request.query.minify === 'false' ) {
do_minify = false;
}
// Skip minification for dashicons.css - workaround for unicode escapes being changed to utf8
if ( url.endsWith( 'dashicons.css' ) ) {
do_minify = false;
}
log.minify = do_minify;

// Go get the original
Expand Down
61 changes: 59 additions & 2 deletions tests/file-css.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,45 @@ const fs = require( 'fs' ).promises;
describe( 'file (css): Minify CSS files', () => {
let request = supertest( `http://localhost:${ getSharedServerPort() }` );

test( 'GET `/file` -- CSS minify bootstrap.css', async () => {
test( 'GET `/file` -- CSS minify bulma.css', async () => {
const target_url = 'tests/files/bulma.css';
const originalContent = await fs.readFile( target_url, 'utf8' );

const startTime = process.hrtime();
const resp = await request
.get( `/file?path=${ target_url }` )
.expect( 200 )
.expect( 'Content-Type', /text\/css/ )
.expect( 'x-minify', 't' );
const [ seconds, nanoseconds ] = process.hrtime( startTime );
const milliseconds = seconds * 1000 + nanoseconds / 1000000;

const minifiedText = resp.text;

// Verify it was actually minified
const originalSize = originalContent.length;
const minifiedSize = minifiedText.length;
expect( minifiedSize ).toBeLessThan( originalSize * 0.91 );
console.info(
`Minimized CSS ${ target_url } to ${ ( ( minifiedSize / originalSize ) * 100 ).toFixed(
2,
) }% of original size in ${ milliseconds.toFixed( 2 ) }ms`,
);
} );

// Skipped - bootstrap.css does not minify because it has escape sequences
test.skip( 'GET `/file` -- CSS minify bootstrap.css', async () => {
const target_url = 'tests/files/bootstrap.css';
const originalContent = await fs.readFile( target_url, 'utf8' );

const startTime = process.hrtime();
const resp = await request
.get( `/file?path=${ target_url }` )
.expect( 200 )
.expect( 'Content-Type', /text\/css/ )
.expect( 'x-minify', 't' );
const [ seconds, nanoseconds ] = process.hrtime( startTime );
const milliseconds = seconds * 1000 + nanoseconds / 1000000;

const minifiedText = resp.text;

Expand All @@ -26,7 +56,34 @@ describe( 'file (css): Minify CSS files', () => {
console.info(
`Minimized CSS ${ target_url } to ${ ( ( minifiedSize / originalSize ) * 100 ).toFixed(
2,
) }% of original size`,
) }% of original size in ${ milliseconds.toFixed( 2 ) }ms`,
);
} );

test( 'GET `/file` -- CSS preserves Unicode escape sequences', async () => {
const target_url = 'tests/files/test-unicode.css';
const originalContent = await fs.readFile( target_url, 'utf8' );

const startTime = process.hrtime();
const resp = await request
.get( `/file?path=${ target_url }` )
.expect( 200 )
.expect( 'Content-Type', /text\/css/ )
.expect( 'x-minify', 't' );
const [ seconds, nanoseconds ] = process.hrtime( startTime );
const milliseconds = seconds * 1000 + nanoseconds / 1000000;

const minifiedText = resp.text;

// Verify Unicode escape sequence is preserved
expect( minifiedText ).toContain( '"\\f148"' );

// expect( minifiedText.length ).toBeLessThan( originalContent.length ); // Do not verify for now
console.info(
`Minimized CSS ${ target_url } to ${ (
( minifiedText.length / originalContent.length ) *
100
).toFixed( 2 ) }% of original size in ${ milliseconds.toFixed( 2 ) }ms`,
);
} );
} );
Loading