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

Declarations in between duplicate properties are removed (!) leading to data loss #752

Closed
mathiasbynens opened this issue Mar 25, 2016 · 2 comments

Comments

@mathiasbynens
Copy link

The following CSS is incorrectly minified:

canvas {
    -ms-interpolation-mode: nearest-neighbor; /* IE */
    image-rendering: -webkit-optimize-contrast; /* Old Android, Safari, UC Browser */
    image-rendering: -moz-crisp-edges; /* Firefox */
    image-rendering: crisp-edges; /* Safari */
    -ms-interpolation-mode: nearest-neighbor; /* Note: this is a duplicate! This triggers the bug */
    image-rendering: pixelated; /* Chrome & Opera */
}

Note that -ms-interpolation-mode property appears twice (this is code generated by Autoprefixer — don’t ask). In this case, it seems that everything in between the two duplicate properties is removed while minifying!

Actual minified output:

canvas{-ms-interpolation-mode:nearest-neighbor;image-rendering:pixelated}

Expected minified output:

canvas{-ms-interpolation-mode:nearest-neighbor;image-rendering:-webkit-optimize-contrast;image-rendering:-moz-crisp-edges;image-rendering:crisp-edges;image-rendering:pixelated}

Simple test case to reproduce the issue:

'use strict';
const CleanCSS = require('clean-css');
const source = `
canvas {
    -ms-interpolation-mode: nearest-neighbor; /* IE */
    image-rendering: -webkit-optimize-contrast; /* Old Android, Safari, UC Browser */
    image-rendering: -moz-crisp-edges; /* Firefox */
    image-rendering: crisp-edges; /* Safari */
    -ms-interpolation-mode: nearest-neighbor;
    image-rendering: pixelated; /* Chrome & Opera */
}
`;

const minified = new CleanCSS().minify(source).styles;
console.log(minified);
@mathiasbynens
Copy link
Author

I’ve managed to narrow down the problem some more. It has to do with properties getting several different values (as happens frequently with vendor-prefixed values). Everything works fine if all the properties follow one another directly:

example {
    foo: -webkit-bar;
    foo: -moz-bar;
    foo: -ms-bar;
    foo: bar;
}

This gets minified correctly, as expected.

However, throw in any other property between the sequential foo declarations, and the bug kicks in:

example {
    foo: -webkit-bar;
    foo: -moz-bar;
    foo: -ms-bar;
    c-c-c-combo: breaker;
    foo: bar;
}

This results in example{c-c-c-combo:breaker;foo:bar}, i.e. all the declarations with vendor-prefixed values are gone. 😢


Updated/simplified test case:

'use strict';
const CleanCSS = require('clean-css');
const source = `
example {
    foo: -webkit-bar;
    foo: -moz-bar;
    foo: -ms-bar;
    c-c-c-combo: breaker;
    foo: bar;
}
`;

const minified = new CleanCSS().minify(source).styles;
console.log(minified);

@jakubpawlowicz
Copy link
Collaborator

Currently clean-css expects same properties to be declared in a successive order (which is a good practice anyway), however there's an outstanding bug opened to make it more flexible: #290.

You can use --skip-aggressive-merging in the meantime.
Closing this bug as a duplicate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants