Skip to content

Commit 0ca410e

Browse files
committed
parser resync with cssparser
1 parent ef16ead commit 0ca410e

File tree

2 files changed

+101
-44
lines changed

2 files changed

+101
-44
lines changed

src/main/javacc/CSS3Parser.jj

Lines changed: 49 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1655,16 +1655,16 @@ LexicalUnit function(LexicalUnit prev) :
16551655
LexicalUnit calc(LexicalUnit prev) :
16561656
{
16571657
Token t;
1658-
LexicalUnit sum = null;
1658+
LexicalUnit head = LexicalUnitImpl.createIdent(null, "");
16591659
String funct = "";
16601660
}
16611661
{
16621662
t = <FUNCTION_CALC> { funct = unescape(t.image, false); }
16631663
( <S> )*
1664-
sum = calcSum(null)
1664+
calcSum(head)
16651665
<RROUND>
16661666
{
1667-
return functionInternal(prev, funct, sum);
1667+
return functionInternal(prev, funct, head.getNextLexicalUnit());
16681668
}
16691669
}
16701670

@@ -1674,11 +1674,10 @@ LexicalUnit calc(LexicalUnit prev) :
16741674
//
16751675
LexicalUnit calcSum(LexicalUnit prev) :
16761676
{
1677-
LexicalUnit sum = null;
16781677
}
16791678
{
16801679
(
1681-
sum = calcProduct(prev) { prev = sum; }
1680+
prev = calcProduct(prev)
16821681
(
16831682
(
16841683
<PLUS> { prev = LexicalUnitImpl.createPlus(prev); }
@@ -1689,11 +1688,11 @@ LexicalUnit calcSum(LexicalUnit prev) :
16891688
)*
16901689
)
16911690
{
1692-
if (sum != null)
1691+
if (prev != null)
16931692
{
1694-
sum.setLocator(createLocator(token));
1693+
prev.setLocator(createLocator(token));
16951694
}
1696-
return sum;
1695+
return prev;
16971696
}
16981697
}
16991698

@@ -1707,7 +1706,7 @@ LexicalUnit calcProduct(LexicalUnit prev) :
17071706
}
17081707
{
17091708
(
1710-
prod = calcValue(prev) { prev = prod; }
1709+
prev = calcValue(prev)
17111710
(
17121711
(
17131712
<ASTERISK> { prev = LexicalUnitImpl.createMultiply(prev); }
@@ -1723,11 +1722,11 @@ LexicalUnit calcProduct(LexicalUnit prev) :
17231722
)*
17241723
)
17251724
{
1726-
if (prod != null)
1725+
if (prev != null)
17271726
{
1728-
prod.setLocator(createLocator(token));
1727+
prev.setLocator(createLocator(token));
17291728
}
1730-
return prod;
1729+
return prev;
17311730
}
17321731
}
17331732

@@ -1739,35 +1738,42 @@ LexicalUnit calcValue(LexicalUnit prev) :
17391738
{
17401739
Token t;
17411740
char op = ' ';
1742-
LexicalUnit value = null;
1741+
String funct = "(";
1742+
LexicalUnit head = LexicalUnitImpl.createIdent(null, "");
17431743
}
17441744
{
17451745
(
17461746
(
17471747
(
17481748
(op = unaryOperator() )?
1749-
value = number(prev, op)
1749+
(
1750+
prev = number(prev, op)
1751+
| prev = dimension(prev, op)
1752+
| prev = percentage(prev, op)
1753+
)
17501754
)
1751-
| value = dimension(prev, op)
1752-
| value = percentage(prev, op)
17531755
|
17541756
(
1755-
<LROUND>
1756-
value = calcSum(prev) // TODO
1757+
(
1758+
t = <FUNCTION_CALC> { funct = unescape(t.image, false); }
1759+
| t = <LROUND> { funct = unescape(t.image, false); }
1760+
)
1761+
calcSum(head)
17571762
<RROUND>
17581763
)
17591764
{
1760-
value = functionInternal(prev, "calc(", value);
1765+
// use an empty function as block scope
1766+
prev = functionInternal(prev, "(", head.getNextLexicalUnit());
17611767
}
17621768
)
17631769
( <S> )*
17641770
)
17651771
{
1766-
if (value != null)
1772+
if (prev != null)
17671773
{
1768-
value.setLocator(createLocator(token));
1774+
prev.setLocator(createLocator(token));
17691775
}
1770-
return value;
1776+
return prev;
17711777
}
17721778
}
17731779

@@ -1777,11 +1783,10 @@ LexicalUnit calcValue(LexicalUnit prev) :
17771783
//
17781784
LexicalUnit calcNumberSum(LexicalUnit prev) :
17791785
{
1780-
LexicalUnit sum = null;
17811786
}
17821787
{
17831788
(
1784-
sum = calcNumberProduct(prev) { prev = sum; }
1789+
prev = calcNumberProduct(prev)
17851790
(
17861791
(
17871792
<PLUS> { prev = LexicalUnitImpl.createPlus(prev); }
@@ -1792,11 +1797,11 @@ LexicalUnit calcNumberSum(LexicalUnit prev) :
17921797
)*
17931798
)
17941799
{
1795-
if (sum != null)
1800+
if (prev != null)
17961801
{
1797-
sum.setLocator(createLocator(token));
1802+
prev.setLocator(createLocator(token));
17981803
}
1799-
return sum;
1804+
return prev;
18001805
}
18011806
}
18021807

@@ -1806,11 +1811,10 @@ LexicalUnit calcNumberSum(LexicalUnit prev) :
18061811
//
18071812
LexicalUnit calcNumberProduct(LexicalUnit prev) :
18081813
{
1809-
LexicalUnit prod = null;
18101814
}
18111815
{
18121816
(
1813-
prod = calcNumberValue(prev) { prev = prod; }
1817+
prev = calcNumberValue(prev)
18141818
(
18151819
(
18161820
<ASTERISK> { prev = LexicalUnitImpl.createMultiply(prev); }
@@ -1826,11 +1830,11 @@ LexicalUnit calcNumberProduct(LexicalUnit prev) :
18261830
)*
18271831
)
18281832
{
1829-
if (prod != null)
1833+
if (prev != null)
18301834
{
1831-
prod.setLocator(createLocator(token));
1835+
prev.setLocator(createLocator(token));
18321836
}
1833-
return prod;
1837+
return prev;
18341838
}
18351839
}
18361840

@@ -1842,33 +1846,38 @@ LexicalUnit calcNumberValue(LexicalUnit prev) :
18421846
{
18431847
Token t;
18441848
char op = ' ';
1845-
LexicalUnit value = null;
1849+
String funct = "(";
1850+
LexicalUnit head = LexicalUnitImpl.createIdent(null, "");
18461851
}
18471852
{
18481853
(
18491854
(
18501855
(
18511856
(op = unaryOperator() )?
1852-
value = number(prev, op)
1857+
prev = number(prev, op)
18531858
)
18541859
|
18551860
(
1856-
<LROUND>
1857-
value = calcNumberSum(prev) // TODO
1861+
(
1862+
t = <FUNCTION_CALC> { funct = unescape(t.image, false); }
1863+
| t = <LROUND> { funct = unescape(t.image, false); }
1864+
)
1865+
calcNumberSum(head)
18581866
<RROUND>
18591867
)
18601868
{
1861-
value = functionInternal(prev, "calc(", value);
1869+
// use an empty function as block scope
1870+
prev = functionInternal(prev, funct, head.getNextLexicalUnit());
18621871
}
18631872
)
18641873
( <S> )*
18651874
)
18661875
{
1867-
if (value != null)
1876+
if (prev != null)
18681877
{
1869-
value.setLocator(createLocator(token));
1878+
prev.setLocator(createLocator(token));
18701879
}
1871-
return value;
1880+
return prev;
18721881
}
18731882
}
18741883

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

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,7 +1175,7 @@ public void calcCalc() throws Exception {
11751175
Assert.assertEquals(1, style.getLength());
11761176

11771177
final String name = style.getProperties().get(0).getName();
1178-
Assert.assertEquals("width : calc(14.1pc * calc(40mm / 1.2))",
1178+
Assert.assertEquals("width : calc(14.1pc * (40mm / 1.2))",
11791179
name + " : " + style.getPropertyValue(name));
11801180

11811181
final CSSValueImpl value = (CSSValueImpl) style.getPropertyCSSValue(name);
@@ -1192,7 +1192,7 @@ public void calcCalc() throws Exception {
11921192

11931193
unit = (LexicalUnitImpl) unit.getNextLexicalUnit();
11941194
Assert.assertEquals(LexicalUnitType.FUNCTION, unit.getLexicalUnitType());
1195-
Assert.assertEquals("calc", unit.getFunctionName());
1195+
Assert.assertEquals("", unit.getFunctionName());
11961196

11971197
Assert.assertNull(unit.getNextLexicalUnit());
11981198

@@ -1210,6 +1210,54 @@ public void calcCalc() throws Exception {
12101210
Assert.assertNull(unit.getNextLexicalUnit());
12111211
}
12121212

1213+
@Test
1214+
public void calcExpressions() throws Exception {
1215+
calc("#c { top: calc() }", 1, 0, 0);
1216+
1217+
calc("#c { top: calc(14px) }");
1218+
1219+
calc("#c { top: calc(0.875em + 0.1875em) }");
1220+
calc("#c { top: calc(0.875em + -0.1875em) }");
1221+
calc("#c { top: calc(-0.875em + 0.1875em) }");
1222+
calc("#c { top: calc(0.875em - -0.1875em) }");
1223+
1224+
calc("#c { top: calc(1px + 2px) }");
1225+
calc("#c { top: calc(((1px + 2px) + 3px) + 4px) }");
1226+
1227+
calc("#c { top: calc(1px * 2px) }");
1228+
calc("#c { top: calc(((1px * 2px) * 3px) * 4px) }");
1229+
1230+
calc("#c { top: calc(1px / 2px) }", 1, 0, 0);
1231+
calc("#c { top: calc(1px / (1 + 2px)) }", 1, 0, 0);
1232+
calc("#c { top: calc(1px / (1px + 2)) }", 1, 0, 0);
1233+
calc("#c { top: calc(1px / (1 + 2)) }");
1234+
1235+
calc("#c { top: calc(1px / calc(1 + 2) * (7em * 3)) }");
1236+
1237+
calc("#c { top: calc(14) }");
1238+
calc("#c { top: calc(14; }", 1, 0, 0);
1239+
calc("#c { top: calc(14 + (7)) }");
1240+
calc("#c { top: calc(14 + (7); }", 1, 0, 0);
1241+
calc("#c { top: calc(14 + (7 + 3) - 1) }");
1242+
}
1243+
1244+
private void calc(String cssText) throws Exception {
1245+
calc(cssText, 0, 0, 0);
1246+
}
1247+
1248+
private void calc(String cssText, final int err, final int fatal, final int warn) throws Exception {
1249+
final CSSStyleSheetImpl sheet = parse(cssText, err, fatal, warn);
1250+
1251+
if (err == 0) {
1252+
final CSSRuleListImpl rules = sheet.getCssRules();
1253+
1254+
Assert.assertEquals(1, rules.getLength());
1255+
1256+
final CSSStyleRuleImpl rule = (CSSStyleRuleImpl) rules.getRules().get(0);
1257+
Assert.assertEquals("*" + cssText, rule.getCssText());
1258+
}
1259+
}
1260+
12131261
/**
12141262
* @throws Exception in case of failure
12151263
*/
@@ -3549,8 +3597,8 @@ public void realWorldBootstrap400() throws Exception {
35493597
+ "all and (min-width: 768px);"
35503598
+ "all and (min-width: 992px);"
35513599
+ "print;";
3552-
realWorld("realworld/bootstrap_4_0_0.css", 1033, 2441, media, 2, 1);
3553-
realWorld("realworld/bootstrap_4_0_0_min.css", 1033, 2441, media, 2, 1);
3600+
realWorld("realworld/bootstrap_4_0_0.css", 1033, 2442, media, 1, 1);
3601+
realWorld("realworld/bootstrap_4_0_0_min.css", 1033, 2442, media, 1, 1);
35543602
}
35553603

35563604
/**

0 commit comments

Comments
 (0)