Autoformat CSSMin
This commit is contained in:
@@ -53,30 +53,37 @@
|
||||
|
||||
package j2html.utils;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
import java.util.logging.*;
|
||||
import java.util.regex.*;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.StringReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Vector;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.regex.PatternSyntaxException;
|
||||
|
||||
public class CSSMin {
|
||||
|
||||
private CSSMin() {}
|
||||
|
||||
private CSSMin() {
|
||||
}
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(CSSMin.class.getName());
|
||||
|
||||
static boolean debugLogging = false;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Minify supplied CSS.
|
||||
*
|
||||
* @param input the CSS
|
||||
* @return the compressed version
|
||||
*/
|
||||
public static String compress(String input) {
|
||||
try {
|
||||
int k,
|
||||
j, // Number of open braces
|
||||
n; // Current position in stream
|
||||
j, // Number of open braces
|
||||
n; // Current position in stream
|
||||
char curr;
|
||||
|
||||
BufferedReader br = new BufferedReader(new StringReader(input));
|
||||
@@ -87,7 +94,9 @@ public class CSSMin {
|
||||
}
|
||||
String s;
|
||||
while ((s = br.readLine()) != null) {
|
||||
if (s.trim().equals("")) continue;
|
||||
if (s.trim().equals("")) {
|
||||
continue;
|
||||
}
|
||||
sb.append(s);
|
||||
}
|
||||
|
||||
@@ -192,7 +201,6 @@ class Selector {
|
||||
// Simplify combinators
|
||||
this.selector = this.selector.replaceAll("\\s?(\\+|~|,|=|~=|\\^=|\\$=|\\*=|\\|=|>)\\s?", "$1");
|
||||
|
||||
|
||||
// We're dealing with a nested property, eg @-webkit-keyframes
|
||||
if (parts.length > 2) {
|
||||
this.subSelectors = new Vector<>();
|
||||
@@ -269,15 +277,21 @@ class Selector {
|
||||
} else if (contents.charAt(i) == '"') {
|
||||
bInsideString = true;
|
||||
} else if (contents.charAt(i) == '(') {
|
||||
if ((i - 3) > 0 && "url".equals(contents.substring(i - 3, i))) bInsideURL = true;
|
||||
if ((i - 3) > 0 && "url".equals(contents.substring(i - 3, i))) {
|
||||
bInsideURL = true;
|
||||
}
|
||||
} else if (contents.charAt(i) == ';') {
|
||||
substr = contents.substring(j, i);
|
||||
if (!(substr.trim().equals(""))) parts.add(substr);
|
||||
if (!(substr.trim().equals(""))) {
|
||||
parts.add(substr);
|
||||
}
|
||||
j = i + 1;
|
||||
}
|
||||
}
|
||||
substr = contents.substring(j, contents.length());
|
||||
if (!(substr.trim().equals(""))) parts.add(substr);
|
||||
if (!(substr.trim().equals(""))) {
|
||||
parts.add(substr);
|
||||
}
|
||||
|
||||
ArrayList<Property> results = new ArrayList<>();
|
||||
for (String part : parts) {
|
||||
@@ -331,12 +345,16 @@ class Property implements Comparable<Property> {
|
||||
bCanSplit = false;
|
||||
} else if (property.charAt(i) == ':' && parts.isEmpty()) {
|
||||
substr = property.substring(j, i);
|
||||
if (!(substr.trim().equals(""))) parts.add(substr);
|
||||
if (!(substr.trim().equals(""))) {
|
||||
parts.add(substr);
|
||||
}
|
||||
j = i + 1;
|
||||
}
|
||||
}
|
||||
substr = property.substring(j, property.length());
|
||||
if (!(substr.trim().equals(""))) parts.add(substr);
|
||||
if (!(substr.trim().equals(""))) {
|
||||
parts.add(substr);
|
||||
}
|
||||
if (parts.size() < 2) {
|
||||
throw new IncompletePropertyException(property);
|
||||
}
|
||||
@@ -482,9 +500,15 @@ class Part {
|
||||
this.contents = this.contents.trim();
|
||||
|
||||
// Simplify multiple zeroes
|
||||
if (this.contents.equals("0 0 0 0")) this.contents = "0";
|
||||
if (this.contents.equals("0 0 0")) this.contents = "0";
|
||||
if (this.contents.equals("0 0")) this.contents = "0";
|
||||
if (this.contents.equals("0 0 0 0")) {
|
||||
this.contents = "0";
|
||||
}
|
||||
if (this.contents.equals("0 0 0")) {
|
||||
this.contents = "0";
|
||||
}
|
||||
if (this.contents.equals("0 0")) {
|
||||
this.contents = "0";
|
||||
}
|
||||
|
||||
// Simplify multiple-parameter properties
|
||||
simplifyParameters();
|
||||
@@ -501,7 +525,9 @@ class Part {
|
||||
}
|
||||
|
||||
private void simplifyParameters() {
|
||||
if (this.property.equals("background-size")) return;
|
||||
if (this.property.equals("background-size")) {
|
||||
return;
|
||||
}
|
||||
|
||||
StringBuilder newContents = new StringBuilder();
|
||||
|
||||
@@ -531,7 +557,9 @@ class Part {
|
||||
}
|
||||
|
||||
private void simplifyFontWeights() {
|
||||
if (!this.property.equals("font-weight")) return;
|
||||
if (!this.property.equals("font-weight")) {
|
||||
return;
|
||||
}
|
||||
|
||||
String lcContents = this.contents.toLowerCase();
|
||||
|
||||
@@ -660,8 +688,8 @@ class IncompleteSelectorException extends Exception {
|
||||
}
|
||||
|
||||
class Constants {
|
||||
static final String[] htmlColourNames = {"aliceblue", "antiquewhite", "aqua", "aquamarine", "azure", "beige", "bisque", "black", "blanchedalmond", "blue", "blueviolet", "brown", "burlywood", "cadetblue", "chartreuse", "chocolate", "coral", "cornflowerblue", "cornsilk", "crimson", "cyan", "darkblue", "darkcyan", "darkgoldenrod", "darkgray", "darkgreen", "darkkhaki", "darkmagenta", "darkolivegreen", "darkorange", "darkorchid", "darkred", "darksalmon", "darkseagreen", "darkslateblue", "darkslategray", "darkturquoise", "darkviolet", "deeppink", "deepskyblue", "dimgray", "dodgerblue", "firebrick", "floralwhite", "forestgreen", "fuchsia", "gainsboro", "ghostwhite", "gold", "goldenrod", "gray", "green", "greenyellow", "honeydew", "hotpink", "indianred ", "indigo ", "ivory", "khaki", "lavender", "lavenderblush", "lawngreen", "lemonchiffon", "lightblue", "lightcoral", "lightcyan", "lightgoldenrodyellow", "lightgrey", "lightgreen", "lightpink", "lightsalmon", "lightseagreen", "lightskyblue", "lightslategray", "lightsteelblue", "lightyellow", "lime", "limegreen", "linen", "magenta", "maroon", "mediumaquamarine", "mediumblue", "mediumorchid", "mediumpurple", "mediumseagreen", "mediumslateblue", "mediumspringgreen", "mediumturquoise", "mediumvioletred", "midnightblue", "mintcream", "mistyrose", "moccasin", "navajowhite", "navy", "oldlace", "olive", "olivedrab", "orange", "orangered", "orchid", "palegoldenrod", "palegreen", "paleturquoise", "palevioletred", "papayawhip", "peachpuff", "peru", "pink", "plum", "powderblue", "purple", "red", "rosybrown", "royalblue", "saddlebrown", "salmon", "sandybrown", "seagreen", "seashell", "sienna", "silver", "skyblue", "slateblue", "slategray", "snow", "springgreen", "steelblue", "tan", "teal", "thistle", "tomato", "turquoise", "violet", "wheat", "white", "whitesmoke", "yellow", "yellowgreen"};
|
||||
static final String[] htmlColourNames = {"aliceblue", "antiquewhite", "aqua", "aquamarine", "azure", "beige", "bisque", "black", "blanchedalmond", "blue", "blueviolet", "brown", "burlywood", "cadetblue", "chartreuse", "chocolate", "coral", "cornflowerblue", "cornsilk", "crimson", "cyan", "darkblue", "darkcyan", "darkgoldenrod", "darkgray", "darkgreen", "darkkhaki", "darkmagenta", "darkolivegreen", "darkorange", "darkorchid", "darkred", "darksalmon", "darkseagreen", "darkslateblue", "darkslategray", "darkturquoise", "darkviolet", "deeppink", "deepskyblue", "dimgray", "dodgerblue", "firebrick", "floralwhite", "forestgreen", "fuchsia", "gainsboro", "ghostwhite", "gold", "goldenrod", "gray", "green", "greenyellow", "honeydew", "hotpink", "indianred ", "indigo ", "ivory", "khaki", "lavender", "lavenderblush", "lawngreen", "lemonchiffon", "lightblue", "lightcoral", "lightcyan", "lightgoldenrodyellow", "lightgrey", "lightgreen", "lightpink", "lightsalmon", "lightseagreen", "lightskyblue", "lightslategray", "lightsteelblue", "lightyellow", "lime", "limegreen", "linen", "magenta", "maroon", "mediumaquamarine", "mediumblue", "mediumorchid", "mediumpurple", "mediumseagreen", "mediumslateblue", "mediumspringgreen", "mediumturquoise", "mediumvioletred", "midnightblue", "mintcream", "mistyrose", "moccasin", "navajowhite", "navy", "oldlace", "olive", "olivedrab", "orange", "orangered", "orchid", "palegoldenrod", "palegreen", "paleturquoise", "palevioletred", "papayawhip", "peachpuff", "peru", "pink", "plum", "powderblue", "purple", "red", "rosybrown", "royalblue", "saddlebrown", "salmon", "sandybrown", "seagreen", "seashell", "sienna", "silver", "skyblue", "slateblue", "slategray", "snow", "springgreen", "steelblue", "tan", "teal", "thistle", "tomato", "turquoise", "violet", "wheat", "white", "whitesmoke", "yellow", "yellowgreen"};
|
||||
static final String[] htmlColourValues = {"#f0f8ff", "#faebd7", "#00ffff", "#7fffd4", "#f0ffff", "#f5f5dc", "#ffe4c4", "#000", "#ffebcd", "#00f", "#8a2be2", "#a52a2a", "#deb887", "#5f9ea0", "#7fff00", "#d2691e", "#ff7f50", "#6495ed", "#fff8dc", "#dc143c", "#0ff", "#00008b", "#008b8b", "#b8860b", "#a9a9a9", "#006400", "#bdb76b", "#8b008b", "#556b2f", "#ff8c00", "#9932cc", "#8b0000", "#e9967a", "#8fbc8f", "#483d8b", "#2f4f4f", "#00ced1", "#9400d3", "#ff1493", "#00bfff", "#696969", "#1e90ff", "#b22222", "#fffaf0", "#228b22", "#f0f", "#dcdcdc", "#f8f8ff", "#ffd700", "#daa520", "#808080", "#008000", "#adff2f", "#f0fff0", "#ff69b4", "#cd5c5c", "#4b0082", "#fffff0", "#f0e68c", "#e6e6fa", "#fff0f5", "#7cfc00", "#fffacd", "#add8e6", "#f08080", "#e0ffff", "#fafad2", "#d3d3d3", "#90ee90", "#ffb6c1", "#ffa07a", "#20b2aa", "#87cefa", "#789", "#b0c4de", "#ffffe0", "#0f0", "#32cd32", "#faf0e6", "#f0f", "#800000", "#66cdaa", "#0000cd", "#ba55d3", "#9370d8", "#3cb371", "#7b68ee", "#00fa9a", "#48d1cc", "#c71585", "#191970", "#f5fffa", "#ffe4e1", "#ffe4b5", "#ffdead", "#000080", "#fdf5e6", "#808000", "#6b8e23", "#ffa500", "#ff4500", "#da70d6", "#eee8aa", "#98fb98", "#afeeee", "#d87093", "#ffefd5", "#ffdab9", "#cd853f", "#ffc0cb", "#dda0dd", "#b0e0e6", "#800080", "#f00", "#bc8f8f", "#4169e1", "#8b4513", "#fa8072", "#f4a460", "#2e8b57", "#fff5ee", "#a0522d", "#c0c0c0", "#87ceeb", "#6a5acd", "#708090", "#fffafa", "#00ff7f", "#4682b4", "#d2b48c", "#008080", "#d8bfd8", "#ff6347", "#40e0d0", "#ee82ee", "#f5deb3", "#fff", "#f5f5f5", "#ff0", "#9acd32"};
|
||||
static final String[] fontWeightNames = {"normal", "bold", "bolder", "lighter"};
|
||||
static final String[] fontWeightNames = {"normal", "bold", "bolder", "lighter"};
|
||||
static final String[] fontWeightValues = {"400", "700", "900", "100"};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user