Skip to content

Commit 26c9439

Browse files
committed
bring formating closer to the browser
1 parent a0f627c commit 26c9439

File tree

6 files changed

+86
-73
lines changed

6 files changed

+86
-73
lines changed

src/main/java/com/gargoylesoftware/css/dom/CSSMediaRuleImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ public String getCssText() {
6060
sb.append(" {");
6161
for (int i = 0; i < getCssRules().getLength(); i++) {
6262
final AbstractCSSRuleImpl rule = getCssRules().getRules().get(i);
63-
sb.append(rule.getCssText()).append(" ");
63+
sb.append("\n ").append(rule.getCssText());
6464
}
65-
sb.append("}");
65+
sb.append("\n}");
6666
return sb.toString();
6767
}
6868

src/main/java/com/gargoylesoftware/css/parser/media/MediaQuery.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,19 +92,32 @@ public boolean isNot() {
9292
public String toString() {
9393
final StringBuilder sb = new StringBuilder();
9494

95+
boolean hasMedia = false;
9596
if (isOnly_) {
9697
sb.append("only ");
98+
sb.append(getMedia());
99+
hasMedia = true;
97100
}
98-
if (isNot_) {
101+
else if (isNot_) {
99102
sb.append("not ");
103+
sb.append(getMedia());
104+
hasMedia = true;
105+
}
106+
else if (!"all".equals(getMedia())) {
107+
sb.append(getMedia());
108+
hasMedia = true;
100109
}
101-
102-
sb.append(getMedia());
103110

104111
for (final Property prop : properties_) {
105-
sb.append(" and (")
106-
.append(prop.toString())
107-
.append(')');
112+
if (hasMedia) {
113+
sb.append(" and ");
114+
}
115+
else {
116+
hasMedia = true;
117+
}
118+
sb.append("(");
119+
sb.append(prop.toString());
120+
sb.append(')');
108121
}
109122
return sb.toString();
110123
}

src/test/java/com/gargoylesoftware/css/TestException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public void test() throws Exception {
7676

7777
Assert.assertEquals(3, rules.getLength());
7878
Assert.assertEquals("@charset \"US-ASCII\";", rules.getRules().get(0).getCssText());
79-
Assert.assertEquals("@media speech {h1 { voice: male } }", rules.getRules().get(1).getCssText());
79+
Assert.assertEquals("@media speech {\n h1 { voice: male }\n}", rules.getRules().get(1).getCssText());
8080
Assert.assertEquals("h2 { smell: strong }", rules.getRules().get(2).getCssText());
8181

8282
rule = rules.getRules().get(1);

src/test/java/com/gargoylesoftware/css/dom/CSSMediaRuleImplTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void getCssText() throws Exception {
4040
final CSSStyleSheetImpl ss = parser.parseStyleSheet(source, null);
4141
final CSSMediaRuleImpl mediaRule = (CSSMediaRuleImpl) ss.getCssRules().getRules().get(0);
4242

43-
Assert.assertEquals("@media print {body { font-size: 10pt } }", mediaRule.getCssText());
43+
Assert.assertEquals("@media print {\n body { font-size: 10pt }\n}", mediaRule.getCssText());
4444
}
4545

4646
/**
@@ -188,6 +188,6 @@ public void asString() throws Exception {
188188
final CSSStyleSheetImpl ss = parser.parseStyleSheet(source, null);
189189
final CSSMediaRuleImpl mediaRule = (CSSMediaRuleImpl) ss.getCssRules().getRules().get(0);
190190

191-
Assert.assertEquals("@media print {body { font-size: 10pt } }", mediaRule.toString());
191+
Assert.assertEquals("@media print {\n body { font-size: 10pt }\n}", mediaRule.toString());
192192
}
193193
}

src/test/java/com/gargoylesoftware/css/parser/CSS3ParserTest.java

Lines changed: 59 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ public void mediaRulePrint() throws Exception {
505505

506506
Assert.assertEquals(1, rules.getLength());
507507
final AbstractCSSRuleImpl cssRule = rules.getRules().get(0);
508-
Assert.assertEquals("@media print {h1 { color: red } }", cssRule.getCssText());
508+
Assert.assertEquals("@media print {\n h1 { color: red }\n}", cssRule.getCssText());
509509

510510
final MediaListImpl mediaList = ((CSSMediaRuleImpl) cssRule).getMediaList();
511511

@@ -525,7 +525,7 @@ public void mediaRulePrintAndScreen() throws Exception {
525525

526526
Assert.assertEquals(1, rules.getLength());
527527
final AbstractCSSRuleImpl cssRule = rules.getRules().get(0);
528-
Assert.assertEquals("@media print, screen {h1 { color: red } }", cssRule.getCssText());
528+
Assert.assertEquals("@media print, screen {\n h1 { color: red }\n}", cssRule.getCssText());
529529

530530
final MediaListImpl mediaList = ((CSSMediaRuleImpl) cssRule).getMediaList();
531531

@@ -547,8 +547,8 @@ public void nestedMediaRule() throws Exception {
547547

548548
Assert.assertEquals(1, rules.getLength());
549549
AbstractCSSRuleImpl cssRule = rules.getRules().get(0);
550-
Assert.assertEquals("@media print {*#navigation { display: none } "
551-
+ "@media all and (max-width: 12cm) {*.note { float: none } } }", cssRule.getCssText());
550+
Assert.assertEquals("@media print {\n *#navigation { display: none }"
551+
+ "\n @media (max-width: 12cm) {\n *.note { float: none }\n}\n}", cssRule.getCssText());
552552

553553
MediaListImpl mediaList = ((CSSMediaRuleImpl) cssRule).getMediaList();
554554
Assert.assertEquals(1, mediaList.getLength());
@@ -561,11 +561,11 @@ public void nestedMediaRule() throws Exception {
561561
Assert.assertEquals("*#navigation { display: none }", cssRule.getCssText());
562562

563563
cssRule = innerRules.getRules().get(1);
564-
Assert.assertEquals("@media all and (max-width: 12cm) {*.note { float: none } }", cssRule.getCssText());
564+
Assert.assertEquals("@media (max-width: 12cm) {\n *.note { float: none }\n}", cssRule.getCssText());
565565

566566
mediaList = ((CSSMediaRuleImpl) cssRule).getMediaList();
567567
Assert.assertEquals(1, mediaList.getLength());
568-
Assert.assertEquals("all and (max-width: 12cm)", mediaList.getMediaText());
568+
Assert.assertEquals("(max-width: 12cm)", mediaList.getMediaText());
569569
}
570570

571571
/**
@@ -810,7 +810,7 @@ public void atRules2() throws Exception {
810810
Assert.assertEquals("@import url(\"subs.css\");", rule.getCssText());
811811

812812
rule = rules.getRules().get(1);
813-
Assert.assertEquals("@media print {body { font-size: 10pt } }", rule.getCssText());
813+
Assert.assertEquals("@media print {\n body { font-size: 10pt }\n}", rule.getCssText());
814814

815815
rule = rules.getRules().get(2);
816816
Assert.assertEquals("h1 { color: blue }", rule.getCssText());
@@ -855,7 +855,7 @@ public void atRules2b() throws Exception {
855855
Assert.assertEquals("@import url(\"subs.css\");", rule.getCssText());
856856

857857
rule = rules.getRules().get(1);
858-
Assert.assertEquals("@media print {body { font-size: 10pt } }", rule.getCssText());
858+
Assert.assertEquals("@media print {\n body { font-size: 10pt }\n}", rule.getCssText());
859859

860860
rule = rules.getRules().get(2);
861861
Assert.assertEquals("h1 { color: blue }", rule.getCssText());
@@ -1834,7 +1834,7 @@ public void unexpectedEndOfStyleSheet() throws Exception {
18341834
Assert.assertEquals(1, rules.getLength());
18351835

18361836
final AbstractCSSRuleImpl rule = rules.getRules().get(0);
1837-
Assert.assertEquals("@media screen {p:before { content: Hello } }", rule.getCssText());
1837+
Assert.assertEquals("@media screen {\n p:before { content: Hello }\n}", rule.getCssText());
18381838
}
18391839

18401840
/**
@@ -1870,7 +1870,7 @@ public void unexpectedEndOfMediaRule() throws Exception {
18701870
Assert.assertEquals(1, rules.getLength());
18711871

18721872
final AbstractCSSRuleImpl rule = rules.getRules().get(0);
1873-
Assert.assertEquals("@media screen {p:before { content: Hello } }", rule.getCssText());
1873+
Assert.assertEquals("@media screen {\n p:before { content: Hello }\n}", rule.getCssText());
18741874
}
18751875

18761876
/**
@@ -3457,7 +3457,7 @@ public void unicode() throws Exception {
34573457
*/
34583458
@Test
34593459
public void unicodeEscaping() throws Exception {
3460-
unicode("@media paper\\7b { }", "@media paper{ {}");
3460+
unicode("@media paper\\7b { }", "@media paper{ {\n}");
34613461
unicode(".class\\7b { color: blue }", "*.class{ { color: blue }");
34623462
unicode("@page :pseu\\64o { color: blue }", "@page :pseudo {color: blue}");
34633463
unicode("h1:first-l\\69ne { color: blue }", "h1:first-line { color: blue }");
@@ -3647,26 +3647,26 @@ public void realWorldMicrosoft() throws Exception {
36473647
@Test
36483648
public void realWorldOracle() throws Exception {
36493649
realWorld("realworld/compass-homestyle.css", 735, 2160,
3650-
"all and (max-height: 600px);"
3651-
+ "all and (max-width: 600px);"
3652-
+ "all and (max-width: 770px);"
3653-
+ "all and (min-width: 0) and (max-width: 1012px);"
3654-
+ "all and (min-width: 0) and (max-width: 1018px);"
3655-
+ "all and (min-width: 0) and (max-width: 1111px);"
3656-
+ "all and (min-width: 0) and (max-width: 1312px);"
3657-
+ "all and (min-width: 0) and (max-width: 390px);"
3658-
+ "all and (min-width: 0) and (max-width: 400px);"
3659-
+ "all and (min-width: 0) and (max-width: 410px);"
3660-
+ "all and (min-width: 0) and (max-width: 450px);"
3661-
+ "all and (min-width: 0) and (max-width: 600px);"
3662-
+ "all and (min-width: 0) and (max-width: 640px);"
3663-
+ "all and (min-width: 0) and (max-width: 680px);"
3664-
+ "all and (min-width: 0) and (max-width: 720px);"
3665-
+ "all and (min-width: 0) and (max-width: 770px);"
3666-
+ "all and (min-width: 0) and (max-width: 870px);"
3667-
+ "all and (min-width: 0) and (max-width: 974px);"
3668-
+ "all and (min-width: 601px);"
3669-
+ "all and (min-width: 771px) and (max-width: 990px);"
3650+
"(max-height: 600px);"
3651+
+ "(max-width: 600px);"
3652+
+ "(max-width: 770px);"
3653+
+ "(min-width: 0) and (max-width: 1012px);"
3654+
+ "(min-width: 0) and (max-width: 1018px);"
3655+
+ "(min-width: 0) and (max-width: 1111px);"
3656+
+ "(min-width: 0) and (max-width: 1312px);"
3657+
+ "(min-width: 0) and (max-width: 390px);"
3658+
+ "(min-width: 0) and (max-width: 400px);"
3659+
+ "(min-width: 0) and (max-width: 410px);"
3660+
+ "(min-width: 0) and (max-width: 450px);"
3661+
+ "(min-width: 0) and (max-width: 600px);"
3662+
+ "(min-width: 0) and (max-width: 640px);"
3663+
+ "(min-width: 0) and (max-width: 680px);"
3664+
+ "(min-width: 0) and (max-width: 720px);"
3665+
+ "(min-width: 0) and (max-width: 770px);"
3666+
+ "(min-width: 0) and (max-width: 870px);"
3667+
+ "(min-width: 0) and (max-width: 974px);"
3668+
+ "(min-width: 601px);"
3669+
+ "(min-width: 771px) and (max-width: 990px);"
36703670
+ "only screen and (max-width: 974px);"
36713671
+ "only screen and (min-width: 0) and (max-width: 1024px);"
36723672
+ "only screen and (min-width: 0) and (max-width: 320px);"
@@ -3766,14 +3766,14 @@ public void realWorldBlueprint() throws Exception {
37663766
*/
37673767
@Test
37683768
public void realWorldBootstrap337() throws Exception {
3769-
final String media = "all and (-webkit-transform-3d);"
3770-
+ "all and (max-device-width: 480px) and (orientation: landscape);"
3771-
+ "all and (max-width: 767px);"
3772-
+ "all and (min-width: 1200px);"
3773-
+ "all and (min-width: 768px);"
3774-
+ "all and (min-width: 768px) and (max-width: 991px);all and (min-width: 992px);"
3775-
+ "all and (min-width: 992px) and (max-width: 1199px);"
3776-
+ "all and (transform-3d);print;"
3769+
final String media = "(-webkit-transform-3d);"
3770+
+ "(max-device-width: 480px) and (orientation: landscape);"
3771+
+ "(max-width: 767px);"
3772+
+ "(min-width: 1200px);"
3773+
+ "(min-width: 768px);"
3774+
+ "(min-width: 768px) and (max-width: 991px);(min-width: 992px);"
3775+
+ "(min-width: 992px) and (max-width: 1199px);"
3776+
+ "(transform-3d);print;"
37773777
+ "screen and (-webkit-min-device-pixel-ratio: 0);"
37783778
+ "screen and (max-width: 767px);"
37793779
+ "screen and (min-width: 768px);";
@@ -3785,14 +3785,14 @@ public void realWorldBootstrap337() throws Exception {
37853785
*/
37863786
@Test
37873787
public void realWorldBootstrap400() throws Exception {
3788-
final String media = "all and (max-width: 1199.98px);"
3789-
+ "all and (max-width: 575.98px);"
3790-
+ "all and (max-width: 767.98px);"
3791-
+ "all and (max-width: 991.98px);"
3792-
+ "all and (min-width: 1200px);"
3793-
+ "all and (min-width: 576px);"
3794-
+ "all and (min-width: 768px);"
3795-
+ "all and (min-width: 992px);"
3788+
final String media = "(max-width: 1199.98px);"
3789+
+ "(max-width: 575.98px);"
3790+
+ "(max-width: 767.98px);"
3791+
+ "(max-width: 991.98px);"
3792+
+ "(min-width: 1200px);"
3793+
+ "(min-width: 576px);"
3794+
+ "(min-width: 768px);"
3795+
+ "(min-width: 992px);"
37963796
+ "print;";
37973797
realWorld("realworld/bootstrap_4_0_0.css", 1033, 2470, media, 0, 0);
37983798
realWorld("realworld/bootstrap_4_0_0_min.css", 1033, 2470, media, 0, 0);
@@ -3825,18 +3825,18 @@ public void realStackoverflow() throws Exception {
38253825
*/
38263826
@Test
38273827
public void realMui() throws Exception {
3828-
final String media = "all and (-ms-high-contrast: active);"
3829-
+ "all and (-ms-high-contrast: none);"
3830-
+ "all and (max-width: 543px);"
3831-
+ "all and (min-width: 1200px);"
3832-
+ "all and (min-width: 480px);"
3833-
+ "all and (min-width: 544px);"
3834-
+ "all and (min-width: 544px) and (max-width: 767px);"
3835-
+ "all and (min-width: 768px);"
3836-
+ "all and (min-width: 768px) and (max-width: 991px);"
3837-
+ "all and (min-width: 992px);"
3838-
+ "all and (min-width: 992px) and (max-width: 1199px);"
3839-
+ "all and (orientation: landscape) and (max-height: 480px);";
3828+
final String media = "(-ms-high-contrast: active);"
3829+
+ "(-ms-high-contrast: none);"
3830+
+ "(max-width: 543px);"
3831+
+ "(min-width: 1200px);"
3832+
+ "(min-width: 480px);"
3833+
+ "(min-width: 544px);"
3834+
+ "(min-width: 544px) and (max-width: 767px);"
3835+
+ "(min-width: 768px);"
3836+
+ "(min-width: 768px) and (max-width: 991px);"
3837+
+ "(min-width: 992px);"
3838+
+ "(min-width: 992px) and (max-width: 1199px);"
3839+
+ "(orientation: landscape) and (max-height: 480px);";
38403840
realWorld("realworld/mui.css", 342, 752, media, 0, 0);
38413841
}
38423842

src/test/java/com/gargoylesoftware/css/parser/media/CSS3MediaTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,16 +241,16 @@ public void allShorthand() throws Exception {
241241
Assert.assertEquals(1, sheet.getCssRules().getLength());
242242

243243
Assert.assertEquals(1, sheet.getMedia().getLength());
244-
Assert.assertEquals("all", sheet.getMedia().toString());
244+
Assert.assertEquals("", sheet.getMedia().toString());
245245

246246
final AbstractCSSRuleImpl cssRule = sheet.getCssRules().getRules().get(0);
247247
Assert.assertTrue(cssRule instanceof CSSMediaRuleImpl);
248248

249249
final MediaListImpl mediaListImpl = ((CSSMediaRuleImpl) cssRule).getMediaList();
250-
Assert.assertEquals("all and (color)", mediaListImpl.getMediaText());
250+
Assert.assertEquals("(color)", mediaListImpl.getMediaText());
251251
Assert.assertEquals(1, mediaListImpl.getLength());
252252
Assert.assertEquals("all", mediaListImpl.mediaQuery(0).getMedia());
253-
Assert.assertEquals("all and (color)", mediaListImpl.mediaQuery(0).toString());
253+
Assert.assertEquals("(color)", mediaListImpl.mediaQuery(0).toString());
254254
Assert.assertEquals(1, mediaListImpl.mediaQuery(0).getProperties().size());
255255
Assert.assertEquals("color", mediaListImpl.mediaQuery(0).getProperties().get(0).toString());
256256
}

0 commit comments

Comments
 (0)