Skip to content

Commit d7b6abb

Browse files
committed
bring formating closer to the browser
1 parent fa6d25f commit d7b6abb

File tree

8 files changed

+51
-48
lines changed

8 files changed

+51
-48
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,16 @@ public CSSFontFaceRuleImpl(final CSSStyleSheetImpl parentStyleSheet, final Abstr
4646
@Override
4747
public String getCssText() {
4848
final StringBuilder sb = new StringBuilder();
49-
sb.append("@font-face {");
49+
sb.append("@font-face { ");
5050

5151
final CSSStyleDeclarationImpl style = getStyle();
5252
if (null != style) {
5353
sb.append(style.getCssText());
54+
if (style.getProperties().size() > 0) {
55+
sb.append(";");
56+
}
5457
}
55-
sb.append("}");
58+
sb.append(" }");
5659
return sb.toString();
5760
}
5861

src/main/java/com/gargoylesoftware/css/parser/LexicalUnitImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ public String getCssText() {
381381
}
382382
break;
383383
case URI:
384-
sb.append("url(").append(getStringValue()).append(")");
384+
sb.append("url(\"").append(getStringValue()).append("\")");
385385
break;
386386
case COUNTER_FUNCTION:
387387
sb.append("counter(");

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ public class CSSFontFaceRuleImplTest {
3636
public void getCssText() throws Exception {
3737
final CSSFontFaceRuleImpl value = parseFontFaceRule("@font-face { font-family: 'Scarface' }");
3838

39-
Assert.assertEquals("@font-face {font-family: \"Scarface\"}", value.getCssText());
40-
Assert.assertEquals("@font-face {font-family: \"Scarface\"}", value.toString());
39+
Assert.assertEquals("@font-face { font-family: \"Scarface\"; }", value.getCssText());
40+
Assert.assertEquals("@font-face { font-family: \"Scarface\"; }", value.toString());
4141
}
4242

4343
/**
@@ -47,12 +47,12 @@ public void getCssText() throws Exception {
4747
public void setCssText() throws Exception {
4848
final CSSFontFaceRuleImpl value = parseFontFaceRule("@font-face { font-family: 'Scarface' }");
4949

50-
Assert.assertEquals("@font-face {font-family: \"Scarface\"}", value.getCssText());
51-
Assert.assertEquals("@font-face {font-family: \"Scarface\"}", value.toString());
50+
Assert.assertEquals("@font-face { font-family: \"Scarface\"; }", value.getCssText());
51+
Assert.assertEquals("@font-face { font-family: \"Scarface\"; }", value.toString());
5252

5353
value.setCssText("@font-face { font-family: 'Ariel'; font-style: 'cute'; }");
54-
Assert.assertEquals("@font-face {font-family: \"Ariel\"; font-style: \"cute\"}", value.getCssText());
55-
Assert.assertEquals("@font-face {font-family: \"Ariel\"; font-style: \"cute\"}", value.toString());
54+
Assert.assertEquals("@font-face { font-family: \"Ariel\"; font-style: \"cute\"; }", value.getCssText());
55+
Assert.assertEquals("@font-face { font-family: \"Ariel\"; font-style: \"cute\"; }", value.toString());
5656

5757
}
5858

@@ -75,7 +75,7 @@ public void getStyle() throws Exception {
7575
public void type() throws Exception {
7676
final CSSFontFaceRuleImpl value = new CSSFontFaceRuleImpl(null, null);
7777

78-
Assert.assertEquals("@font-face {}", value.toString());
78+
Assert.assertEquals("@font-face { }", value.toString());
7979
}
8080

8181
private CSSFontFaceRuleImpl parseFontFaceRule(final String rule) throws Exception {
@@ -93,7 +93,7 @@ private CSSFontFaceRuleImpl parseFontFaceRule(final String rule) throws Exceptio
9393
public void getCssTextFormated() throws Exception {
9494
final CSSFontFaceRuleImpl value = parseFontFaceRule("@font-face { font-family: 'Scarface' }");
9595

96-
Assert.assertEquals("@font-face {font-family: \"Scarface\"}", value.toString());
97-
Assert.assertEquals("@font-face {font-family: \"Scarface\"}", value.getCssText());
96+
Assert.assertEquals("@font-face { font-family: \"Scarface\"; }", value.toString());
97+
Assert.assertEquals("@font-face { font-family: \"Scarface\"; }", value.getCssText());
9898
}
9999
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,10 @@ public void cssTextHasNoCurlyBraces() throws Exception {
196196
public void emptyUrl() throws Exception {
197197
final CSSStyleDeclarationImpl style = parseStyleDeclaration("background: url()");
198198

199-
Assert.assertEquals("background: url()", style.getCssText());
199+
Assert.assertEquals("background: url(\"\")", style.getCssText());
200200
Assert.assertEquals(1, style.getLength());
201201
Assert.assertEquals("background", style.getProperties().get(0).getName());
202-
Assert.assertEquals("url()", style.getPropertyValue("background"));
202+
Assert.assertEquals("url(\"\")", style.getPropertyValue("background"));
203203
}
204204

205205
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@ public void uri() throws Exception {
728728
final LexicalUnit lu = LexicalUnitImpl.createURI(null, "cssparser");
729729
final CSSValueImpl value = new CSSValueImpl(lu, false);
730730

731-
Assert.assertEquals("url(cssparser)", value.getCssText());
731+
Assert.assertEquals("url(\"cssparser\")", value.getCssText());
732732
Assert.assertEquals(CSSPrimitiveValueType.CSS_URI, value.getPrimitiveType());
733733
Assert.assertEquals(LexicalUnit.LexicalUnitType.URI, value.getLexicalUnitType());
734734
Assert.assertEquals(0.0, value.getDoubleValue(), 0.00001);

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

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ public void atRuleFontFace() throws Exception {
583583
Assert.assertEquals(1, rules.getLength());
584584

585585
final AbstractCSSRuleImpl rule = rules.getRules().get(0);
586-
Assert.assertEquals("@font-face {font-family: Gentium; src: url(http://example.com/fonts/Gentium.ttf)}",
586+
Assert.assertEquals("@font-face { font-family: Gentium; src: url(\"http://example.com/fonts/Gentium.ttf\"); }",
587587
rule.getCssText());
588588
}
589589

@@ -600,7 +600,7 @@ public void atRuleFontFaceUnicodeRangeSingleCodepoint() throws Exception {
600600
Assert.assertEquals(1, rules.getLength());
601601

602602
final AbstractCSSRuleImpl rule = rules.getRules().get(0);
603-
Assert.assertEquals("@font-face {font-family: Gentium; unicode-range: U+26}",
603+
Assert.assertEquals("@font-face { font-family: Gentium; unicode-range: U+26; }",
604604
rule.getCssText());
605605
}
606606

@@ -617,7 +617,7 @@ public void atRuleFontFaceUnicodeRangeWildcard() throws Exception {
617617
Assert.assertEquals(1, rules.getLength());
618618

619619
final AbstractCSSRuleImpl rule = rules.getRules().get(0);
620-
Assert.assertEquals("@font-face {font-family: Gentium; unicode-range: U+4?}",
620+
Assert.assertEquals("@font-face { font-family: Gentium; unicode-range: U+4?; }",
621621
rule.getCssText());
622622
}
623623

@@ -634,7 +634,7 @@ public void atRuleFontFaceUnicodeRangeRange() throws Exception {
634634
Assert.assertEquals(1, rules.getLength());
635635

636636
final AbstractCSSRuleImpl rule = rules.getRules().get(0);
637-
Assert.assertEquals("@font-face {font-family: Gentium; unicode-range: U+0-7F}",
637+
Assert.assertEquals("@font-face { font-family: Gentium; unicode-range: U+0-7F; }",
638638
rule.getCssText());
639639
}
640640

@@ -651,7 +651,7 @@ public void atRuleFontFaceUnicodeRangeRange2() throws Exception {
651651
Assert.assertEquals(1, rules.getLength());
652652

653653
final AbstractCSSRuleImpl rule = rules.getRules().get(0);
654-
Assert.assertEquals("@font-face {font-family: Gentium; unicode-range: U+0025-00FF}",
654+
Assert.assertEquals("@font-face { font-family: Gentium; unicode-range: U+0025-00FF; }",
655655
rule.getCssText());
656656
}
657657

@@ -668,7 +668,7 @@ public void atRuleFontFaceUnicodeRangeMultipleValues() throws Exception {
668668
Assert.assertEquals(1, rules.getLength());
669669

670670
final AbstractCSSRuleImpl rule = rules.getRules().get(0);
671-
Assert.assertEquals("@font-face {font-family: Gentium; unicode-range: U+0025-0??F, U+4??}",
671+
Assert.assertEquals("@font-face { font-family: Gentium; unicode-range: U+0025-0??F, U+4??; }",
672672
rule.getCssText());
673673
}
674674

@@ -689,8 +689,8 @@ public void atRuleFontFaceComplex() throws Exception {
689689
Assert.assertEquals(1, rules.getLength());
690690

691691
final AbstractCSSRuleImpl rule = rules.getRules().get(0);
692-
Assert.assertEquals("@font-face {font-family: Headline; "
693-
+ "src: local(Futura-Medium), url(fonts.svg#MyGeometricModern) format(\"svg\")}",
692+
Assert.assertEquals("@font-face { font-family: Headline; "
693+
+ "src: local(Futura-Medium), url(\"fonts.svg#MyGeometricModern\") format(\"svg\"); }",
694694
rule.getCssText());
695695
}
696696

@@ -716,13 +716,13 @@ public void atRuleFontFaceComplex2() throws Exception {
716716
Assert.assertEquals(1, rules.getLength());
717717

718718
final AbstractCSSRuleImpl rule = rules.getRules().get(0);
719-
Assert.assertEquals("@font-face {font-family: Pangolin; "
719+
Assert.assertEquals("@font-face { font-family: Pangolin; "
720720
+ "font-style: normal; font-weight: 400; "
721721
+ "src: local(\"Indie Flower\"), local(\"IndieFlower\"), "
722-
+ "url(https://fonts.gstatic.com/s/indieflower/v9"
723-
+ "/10JVD_humAd5zP2yrFqw6ugdm0LZdjqr5-oayXSOefg.woff2) format(\"woff2\"); "
722+
+ "url(\"https://fonts.gstatic.com/s/indieflower/v9"
723+
+ "/10JVD_humAd5zP2yrFqw6ugdm0LZdjqr5-oayXSOefg.woff2\") format(\"woff2\"); "
724724
+ "unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02C6, U+02DA, "
725-
+ "U+02DC, U+2000-206F, U+2074, U+20AC, U+2212, U+2215}",
725+
+ "U+02DC, U+2000-206F, U+2074, U+20AC, U+2212, U+2215; }",
726726
rule.getCssText());
727727
}
728728

@@ -2060,19 +2060,19 @@ public void strings() throws Exception {
20602060
Assert.assertEquals(6, rules.getLength());
20612061

20622062
AbstractCSSRuleImpl rule = rules.getRules().get(0);
2063-
Assert.assertEquals("h1 { background: url(this is a 'string'); }", rule.getCssText());
2063+
Assert.assertEquals("h1 { background: url(\"this is a 'string'\"); }", rule.getCssText());
20642064

20652065
rule = rules.getRules().get(1);
2066-
Assert.assertEquals("h2 { background: url(this is a \"string\"); }", rule.getCssText());
2066+
Assert.assertEquals("h2 { background: url(\"this is a \"string\"\"); }", rule.getCssText());
20672067

20682068
rule = rules.getRules().get(2);
2069-
Assert.assertEquals("h4 { background: url(this is a \"string\"); }", rule.getCssText());
2069+
Assert.assertEquals("h4 { background: url(\"this is a \"string\"\"); }", rule.getCssText());
20702070

20712071
rule = rules.getRules().get(3);
2072-
Assert.assertEquals("h5 { background: url(this is a 'string'); }", rule.getCssText());
2072+
Assert.assertEquals("h5 { background: url(\"this is a 'string'\"); }", rule.getCssText());
20732073

20742074
rule = rules.getRules().get(4);
2075-
Assert.assertEquals("h6 { background: url(this is a string); }", rule.getCssText());
2075+
Assert.assertEquals("h6 { background: url(\"this is a string\"); }", rule.getCssText());
20762076

20772077
rule = rules.getRules().get(5);
20782078
Assert.assertEquals("h1:before { content: \"chapter\\A hoofdstuk\\A chapitre\"; }", rule.getCssText());
@@ -2094,16 +2094,16 @@ public void emptyUrl() throws Exception {
20942094
Assert.assertEquals(4, rules.getLength());
20952095

20962096
AbstractCSSRuleImpl rule = rules.getRules().get(0);
2097-
Assert.assertEquals("h1 { background: url(); }", rule.getCssText());
2097+
Assert.assertEquals("h1 { background: url(\"\"); }", rule.getCssText());
20982098

20992099
rule = rules.getRules().get(1);
2100-
Assert.assertEquals("h2 { background: url(); }", rule.getCssText());
2100+
Assert.assertEquals("h2 { background: url(\"\"); }", rule.getCssText());
21012101

21022102
rule = rules.getRules().get(2);
2103-
Assert.assertEquals("h4 { background: url(); }", rule.getCssText());
2103+
Assert.assertEquals("h4 { background: url(\"\"); }", rule.getCssText());
21042104

21052105
rule = rules.getRules().get(3);
2106-
Assert.assertEquals("h5 { background: url(); }", rule.getCssText());
2106+
Assert.assertEquals("h5 { background: url(\"\"); }", rule.getCssText());
21072107
}
21082108

21092109
/**

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -241,21 +241,21 @@ public void parseStyleDeclarationWithoutBrace() throws Exception {
241241
@Test
242242
public void urlGreedy() throws Exception {
243243
Assert.assertEquals(
244-
"background: url(images/bottom-angle.png); background-image: url(background.png)",
244+
"background: url(\"images/bottom-angle.png\"); background-image: url(\"background.png\")",
245245
getCssTextFromDeclaration(
246246
new CSS3Parser(),
247247
"background:url('images/bottom-angle.png');background-image:url('background.png');"));
248248
Assert.assertEquals(
249-
"background: url(images/bottom-angle.png); background-image: url(background.png)",
249+
"background: url(\"images/bottom-angle.png\"); background-image: url(\"background.png\")",
250250
getCssTextFromDeclaration(
251251
new CSS3Parser(),
252252
"background:url(\"images/bottom-angle.png\");background-image:url(\"background.png\");"));
253253
Assert.assertEquals(
254-
"background: rgb(60, 90, 118) url(/images/status_bg.png?2) no-repeat center; "
254+
"background: rgb(60, 90, 118) url(\"/images/status_bg.png?2\") no-repeat center; "
255255
+ "font-family: Arial, \"Helvetica Neue\", Helvetica, sans-serif",
256256
getCssTextFromDeclaration(
257257
new CSS3Parser(),
258-
"background:#3c5a76 url('/images/status_bg.png?2') no-repeat center;"
258+
"background:#3c5a76 url(\"/images/status_bg.png?2\") no-repeat center;"
259259
+ "font-family:Arial,'Helvetica Neue',Helvetica,sans-serif"));
260260
}
261261

@@ -286,14 +286,14 @@ public void commaList() throws Exception {
286286
@Test
287287
public void colorFirst() throws Exception {
288288
Assert.assertEquals(
289-
"background: rgb(232, 239, 245) url(images/bottom-angle.png) no-repeat",
289+
"background: rgb(232, 239, 245) url(\"images/bottom-angle.png\") no-repeat",
290290
getCssTextFromDeclaration(new CSS3Parser(),
291291
"background: #e8eff5 url(images/bottom-angle.png) no-repeat"));
292292
Assert.assertEquals(
293-
"background: red url(images/bottom-angle.png) no-repeat",
293+
"background: red url(\"images/bottom-angle.png\") no-repeat",
294294
getCssTextFromDeclaration(new CSS3Parser(), "background: red url(images/bottom-angle.png) no-repeat"));
295295
Assert.assertEquals(
296-
"background: rgb(8, 3, 6) url(images/bottom-angle.png) no-repeat",
296+
"background: rgb(8, 3, 6) url(\"images/bottom-angle.png\") no-repeat",
297297
getCssTextFromDeclaration(new CSS3Parser(),
298298
"background: rgb(8, 3, 6) url(images/bottom-angle.png) no-repeat"));
299299
}
@@ -487,7 +487,7 @@ public void overwriteProperties() throws Exception {
487487

488488
Assert.assertEquals("p { background: rgb(0, 0, 0); "
489489
+ "background-repeat: repeat-y; "
490-
+ "background: url(img/test.png) no-repeat; "
490+
+ "background: url(\"img/test.png\") no-repeat; "
491491
+ "background-size: 190px 48px; }",
492492
sheet.toString().trim());
493493

@@ -501,13 +501,13 @@ public void overwriteProperties() throws Exception {
501501
Assert.assertEquals(4, declImpl.getLength());
502502

503503
Assert.assertEquals("background", declImpl.getProperties().get(0).getName());
504-
Assert.assertEquals("url(img/test.png) no-repeat", declImpl.getPropertyCSSValue("background").getCssText());
504+
Assert.assertEquals("url(\"img/test.png\") no-repeat", declImpl.getPropertyCSSValue("background").getCssText());
505505

506506
Assert.assertEquals("background-repeat", declImpl.getProperties().get(1).getName());
507507
Assert.assertEquals("repeat-y", declImpl.getPropertyCSSValue("background-repeat").getCssText());
508508

509509
Assert.assertEquals("background", declImpl.getProperties().get(2).getName());
510-
Assert.assertEquals("url(img/test.png) no-repeat", declImpl.getPropertyCSSValue("background").getCssText());
510+
Assert.assertEquals("url(\"img/test.png\") no-repeat", declImpl.getPropertyCSSValue("background").getCssText());
511511

512512
Assert.assertEquals("background-size", declImpl.getProperties().get(3).getName());
513513
Assert.assertEquals("190px 48px", declImpl.getPropertyCSSValue("background-size").getCssText());
@@ -525,7 +525,7 @@ public void overwriteProperties() throws Exception {
525525

526526
prop = declImpl.getProperties().get(2);
527527
Assert.assertEquals("background", prop.getName());
528-
Assert.assertEquals("url(img/test.png) no-repeat", prop.getValue().getCssText());
528+
Assert.assertEquals("url(\"img/test.png\") no-repeat", prop.getValue().getCssText());
529529

530530
prop = declImpl.getProperties().get(3);
531531
Assert.assertEquals("background-size", prop.getName());

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1233,7 +1233,7 @@ public void createURI() throws Exception {
12331233
Assert.assertNull(unit.getNextLexicalUnit());
12341234
Assert.assertNull(unit.getPreviousLexicalUnit());
12351235

1236-
Assert.assertEquals("url(www.wetator.org)", unit.toString());
1236+
Assert.assertEquals("url(\"www.wetator.org\")", unit.toString());
12371237
Assert.assertEquals("URI(url(www.wetator.org))", ((LexicalUnitImpl) unit).toDebugString());
12381238
}
12391239

0 commit comments

Comments
 (0)