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

Alphabetically arrange properties in the declaration block #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
30 changes: 30 additions & 0 deletions src/com/yahoo/platform/yui/compressor/CssCompressor.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.ArrayList;
import java.util.Arrays;

public class CssCompressor {

Expand Down Expand Up @@ -294,7 +295,36 @@ public void compress(Writer out, int linebreakpos)
for(i = 0, max = preservedTokens.size(); i < max; i++) {
css = css.replace("___YUICSSMIN_PRESERVED_TOKEN_" + i + "___", preservedTokens.get(i).toString());
}

// Alphabetically arrange properties in declaration block
sb = new StringBuffer(css);
startIndex = 0;
while ((startIndex = sb.indexOf("{", startIndex)) >= 0) {
endIndex = sb.indexOf("}", startIndex + 1);
if (endIndex < 0) {
endIndex = totallen;
}

token = sb.substring(startIndex + 1, endIndex);

// Split string into an array and sort
String[] arr = token.split(";");
Arrays.sort(arr);

// And glue it back
token = "";
for(i = 0, max = arr.length; i < max; i++) {
if (token != "") {
token += ";";
}
token += arr[i];
}

sb.replace(startIndex + 1, endIndex, token);
startIndex++;
}
css = sb.toString();

// Trim the final string (for any leading or trailing white spaces)
css = css.trim();

Expand Down