Skip to content

Commit 5669267

Browse files
committed
add support for 'rem' unit
1 parent 66b0007 commit 5669267

File tree

6 files changed

+112
-19
lines changed

6 files changed

+112
-19
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,8 @@ public short getPrimitiveType() {
196196
return CSS_NUMBER;
197197
case EM:
198198
return CSS_EMS;
199+
case REM:
200+
return CSS_UNKNOWN;
199201
case EX:
200202
return CSS_EXS;
201203
case PIXEL:

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ enum LexicalUnitType {
4141
INTEGER,
4242
REAL,
4343
EM,
44+
REM,
4445
EX,
4546
PIXEL,
4647
INCH,

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ public String getDimensionUnitText() {
224224
switch (lexicalUnitType_) {
225225
case EM:
226226
return "em";
227+
case REM:
228+
return "rem";
227229
case EX:
228230
return "ex";
229231
case PIXEL:
@@ -337,6 +339,7 @@ public String getCssText() {
337339
sb.append(getTrimedFloatValue());
338340
break;
339341
case EM:
342+
case REM:
340343
case EX:
341344
case PIXEL:
342345
case INCH:
@@ -494,6 +497,12 @@ public String toDebugString() {
494497
.append(getDimensionUnitText())
495498
.append(")");
496499
break;
500+
case REM:
501+
sb.append("REM(")
502+
.append(getTrimedFloatValue())
503+
.append(getDimensionUnitText())
504+
.append(")");
505+
break;
497506
case EX:
498507
sb.append("EX(")
499508
.append(getTrimedFloatValue())
@@ -772,6 +781,15 @@ public static LexicalUnit createEm(final LexicalUnit prev, final float f) {
772781
return new LexicalUnitImpl(prev, LexicalUnitType.EM, f);
773782
}
774783

784+
/**
785+
* @param prev the previous LexicalUnit
786+
* @param f the float value
787+
* @return lexical unit with type rem
788+
*/
789+
public static LexicalUnit createRem(final LexicalUnit prev, final float f) {
790+
return new LexicalUnitImpl(prev, LexicalUnitType.REM, f);
791+
}
792+
775793
/**
776794
* @param prev the previous LexicalUnit
777795
* @param f the float value

src/main/javacc/CSS3Parser.jj

Lines changed: 55 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,9 @@ TOKEN_MGR_DECLS :
323323
// {num}{E}{M} {return EMS;}
324324
| < EMS: <NUM> <E_LETTER> <M_LETTER> > { matchedToken.image = ParserUtils.trimBy(image, 0, 2); }
325325

326+
// {num}{R}{E}{M} {return REM;}
327+
| < REM: <NUM> <R_LETTER> <E_LETTER> <M_LETTER> > { matchedToken.image = ParserUtils.trimBy(image, 0, 3); }
328+
326329
// {num}{E}{X} {return EXS;}
327330
| < EXS: <NUM> <E_LETTER> <X_LETTER> > { matchedToken.image = ParserUtils.trimBy(image, 0, 2); }
328331

@@ -438,7 +441,9 @@ void styleSheetRuleList() :
438441
( <S> | <CDO> | <CDC> )*
439442
)?
440443
(
441-
( ( importRule(ruleFound)
444+
(
445+
(
446+
importRule(ruleFound)
442447
|
443448
(
444449
styleRule() | mediaRule() | pageRule() | fontFaceRule() | unknownAtRule()
@@ -451,7 +456,8 @@ void styleSheetRuleList() :
451456
(
452457
// skip until the next RBRACE
453458
{ ParseException e = generateParseException(); }
454-
invalidRule() {
459+
invalidRule()
460+
{
455461
Token t = getNextToken();
456462

457463
boolean charsetProcessed = false;
@@ -692,9 +698,12 @@ MediaQuery mediaQuery() :
692698
boolean not = false;
693699
}
694700
{
695-
( ( (
701+
(
702+
(
703+
(
696704
(
697-
<ONLY> { only = true; } |
705+
<ONLY> { only = true; }
706+
|
698707
<NOT> { not = true; }
699708
) ( <S> )*
700709
)?
@@ -703,13 +712,29 @@ MediaQuery mediaQuery() :
703712
(
704713
<AND> ( <S> )*
705714
p = mediaExpression()
706-
{ mq.addMediaProperty(p);
715+
{
716+
mq.addMediaProperty(p);
717+
}
718+
)*
719+
)
720+
|
721+
(
722+
p = mediaExpression()
723+
{
724+
s = "all";
725+
handleMedium(s, null);
726+
mq = new MediaQuery(s, only, not);
727+
mq.setLocator(createLocator(token));
728+
mq.addMediaProperty(p);
729+
}
730+
(
731+
<AND> ( <S> )*
732+
p = mediaExpression()
733+
{
734+
mq.addMediaProperty(p);
707735
}
708736
)*
709737
)
710-
| ( p = mediaExpression()
711-
{ s = "all"; handleMedium(s, null); mq = new MediaQuery(s, only, not);
712-
mq.setLocator(createLocator(token)); mq.addMediaProperty(p); } ( <AND> ( <S> )* p = mediaExpression() { mq.addMediaProperty(p); } )* )
713738
)
714739
{ return mq; }
715740
}
@@ -729,16 +754,20 @@ Property mediaExpression() :
729754
<LROUND>
730755
( <S> )*
731756
p = property()
732-
( <COLON> ( <S> )*
757+
(
758+
<COLON> ( <S> )*
733759
e = expr()
734760
)?
735761
<RROUND>
736762
( <S> )*
737763
{
738764
if(e==null)
739-
{ prop = new Property(p, null, false);
765+
{
766+
prop = new Property(p, null, false);
740767
}
741-
else { prop = new Property(p, new CSSValueImpl(e), false);
768+
else
769+
{
770+
prop = new Property(p, new CSSValueImpl(e), false);
742771
}
743772
return prop;
744773
}
@@ -831,7 +860,8 @@ String pageSelectorList() :
831860
}
832861
{
833862
sel = pageSelector() { selectors.add(sel); }
834-
( <COMMA> ( <S> )* sel = pageSelector() { selectors.add(sel); }
863+
(
864+
<COMMA> ( <S> )* sel = pageSelector() { selectors.add(sel); }
835865
)*
836866

837867
{ return String.join(", ", selectors); }
@@ -1450,7 +1480,9 @@ void declaration() :
14501480
)?
14511481

14521482
{
1453-
if (starHack != null) { if (isIeStarHackAccepted()) {
1483+
if (starHack != null)
1484+
{
1485+
if (isIeStarHackAccepted()) {
14541486
handleProperty("*" + p, e, priority, locator);
14551487
return;
14561488
}
@@ -1514,7 +1546,7 @@ LexicalUnit expr() :
15141546
//
15151547
// term
15161548
// : unary_operator?
1517-
// [ NUMBER | PERCENTAGE | LENGTH | EMS | EXS | ANGLE | TIME | FREQ | function ]
1549+
// [ NUMBER | PERCENTAGE | LENGTH | EMS | REM | EXS | ANGLE | TIME | FREQ | function ]
15181550
// | STRING | IDENT | URI | hexcolor | DIMENSION
15191551
// S*
15201552
// ;
@@ -1554,6 +1586,7 @@ LexicalUnit term(LexicalUnit prev) :
15541586
| t = <LENGTH_PT> { value = LexicalUnitImpl.createPoint(prev, floatValue(op, t.image)); }
15551587
| t = <LENGTH_PC> { value = LexicalUnitImpl.createPica(prev, floatValue(op, t.image)); }
15561588
| t = <EMS> { value = LexicalUnitImpl.createEm(prev, floatValue(op, t.image)); }
1589+
| t = <REM> { value = LexicalUnitImpl.createRem(prev, floatValue(op, t.image)); }
15571590
| t = <EXS> { value = LexicalUnitImpl.createEx(prev, floatValue(op, t.image)); }
15581591
| t = <ANGLE_DEG> { value = LexicalUnitImpl.createDegree(prev, floatValue(op, t.image)); }
15591592
| t = <ANGLE_RAD> { value = LexicalUnitImpl.createRadian(prev, floatValue(op, t.image)); }
@@ -1569,7 +1602,8 @@ LexicalUnit term(LexicalUnit prev) :
15691602
| t = <STRING> { value = LexicalUnitImpl.createString(prev, unescape(t.image, false)); }
15701603
| t = "progid:" { value = LexicalUnitImpl.createIdent(prev, skipUnit().trim()); }
15711604
|
1572-
( t = <IDENT>
1605+
(
1606+
t = <IDENT>
15731607
( <COLON> { throw toCSSParseException("invalidExprColon", new String[]{ unescape(t.image, false) }, createLocator(t)); } )?
15741608
)
15751609
{ value = LexicalUnitImpl.createIdent(prev, unescape(t.image, false)); }
@@ -1621,7 +1655,8 @@ LexicalUnit function(LexicalUnit prev) :
16211655
param = term(null) { body = param; }
16221656
(
16231657
(
1624-
( t = <COMMA> { body = LexicalUnitImpl.createComma(body); }
1658+
(
1659+
t = <COMMA> { body = LexicalUnitImpl.createComma(body); }
16251660
| t = <EQUALS> { body = LexicalUnitImpl.createIdent(body, t.image); }
16261661
)
16271662
( <S> )*
@@ -1743,6 +1778,10 @@ void appendUnit(Token t, StringBuilder sb) {
17431778
sb.append("ems");
17441779
return;
17451780
}
1781+
if (t.kind == REM) {
1782+
sb.append("rem");
1783+
return;
1784+
}
17461785
if (t.kind == EXS) {
17471786
sb.append("ex");
17481787
return;

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,13 +1292,13 @@ public void malformedDeclaration() throws Exception {
12921292
final String expected = "Error in declaration. (Invalid token \"}\". Was expecting one of: <S>, \":\".)"
12931293
+ " Error in declaration. (Invalid token \";\". Was expecting one of: <S>, \":\".)"
12941294
+ " Error in expression. (Invalid token \"}\". Was expecting one of: <S>, <NUMBER>, \"inherit\", "
1295-
+ "<IDENT>, <STRING>, \"-\", <PLUS>, <HASH>, <EMS>, <EXS>, "
1295+
+ "<IDENT>, <STRING>, \"-\", <PLUS>, <HASH>, <EMS>, <REM>, <EXS>, "
12961296
+ "<LENGTH_PX>, <LENGTH_CM>, <LENGTH_MM>, "
12971297
+ "<LENGTH_IN>, <LENGTH_PT>, <LENGTH_PC>, <ANGLE_DEG>, <ANGLE_RAD>, <ANGLE_GRAD>, <TIME_MS>, "
12981298
+ "<TIME_S>, <FREQ_HZ>, <FREQ_KHZ>, <RESOLUTION_DPI>, <RESOLUTION_DPCM>, <PERCENTAGE>, "
12991299
+ "<DIMENSION>, <UNICODE_RANGE>, <URI>, <FUNCTION>, \"progid:\".)"
13001300
+ " Error in expression. (Invalid token \";\". Was expecting one of: <S>, <NUMBER>, \"inherit\", "
1301-
+ "<IDENT>, <STRING>, \"-\", <PLUS>, <HASH>, <EMS>, <EXS>, "
1301+
+ "<IDENT>, <STRING>, \"-\", <PLUS>, <HASH>, <EMS>, <REM>, <EXS>, "
13021302
+ "<LENGTH_PX>, <LENGTH_CM>, <LENGTH_MM>, "
13031303
+ "<LENGTH_IN>, <LENGTH_PT>, <LENGTH_PC>, <ANGLE_DEG>, <ANGLE_RAD>, <ANGLE_GRAD>, <TIME_MS>, "
13041304
+ "<TIME_S>, <FREQ_HZ>, <FREQ_KHZ>, <RESOLUTION_DPI>, <RESOLUTION_DPCM>, <PERCENTAGE>, "
@@ -1593,7 +1593,7 @@ public void unexpectedEndOfString() throws Exception {
15931593
Assert.assertEquals(1, errorHandler.getErrorCount());
15941594
final String expected = "Error in expression. "
15951595
+ "(Invalid token \"\\'\". Was expecting one of: <S>, <NUMBER>, \"inherit\", "
1596-
+ "<IDENT>, <STRING>, \"-\", <PLUS>, <HASH>, <EMS>, <EXS>, "
1596+
+ "<IDENT>, <STRING>, \"-\", <PLUS>, <HASH>, <EMS>, <REM>, <EXS>, "
15971597
+ "<LENGTH_PX>, <LENGTH_CM>, <LENGTH_MM>, "
15981598
+ "<LENGTH_IN>, <LENGTH_PT>, <LENGTH_PC>, <ANGLE_DEG>, <ANGLE_RAD>, <ANGLE_GRAD>, <TIME_MS>, "
15991599
+ "<TIME_S>, <FREQ_HZ>, <FREQ_KHZ>, <RESOLUTION_DPI>, <RESOLUTION_DPCM>, <PERCENTAGE>, "
@@ -1992,6 +1992,14 @@ public void dimensionEMS() throws Exception {
19921992
final CSSValueImpl value = dimension("17em");
19931993
Assert.assertEquals(CSSPrimitiveValue.CSS_EMS, value.getPrimitiveType());
19941994
}
1995+
/**
1996+
* @throws Exception if any error occurs
1997+
*/
1998+
@Test
1999+
public void dimensionREM() throws Exception {
2000+
final CSSValueImpl value = dimension("17rem");
2001+
Assert.assertEquals(CSSPrimitiveValue.CSS_UNKNOWN, value.getPrimitiveType());
2002+
}
19952003

19962004
/**
19972005
* @throws Exception if any error occurs

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,9 @@ public void getDimensionUnitText() throws Exception {
271271
LexicalUnitImpl unit = new LexicalUnitImpl(null, LexicalUnitType.EM);
272272
Assert.assertEquals("em", unit.getDimensionUnitText());
273273

274+
unit = new LexicalUnitImpl(null, LexicalUnitType.REM);
275+
Assert.assertEquals("rem", unit.getDimensionUnitText());
276+
274277
unit = new LexicalUnitImpl(null, LexicalUnitType.EX);
275278
Assert.assertEquals("ex", unit.getDimensionUnitText());
276279

@@ -563,6 +566,28 @@ public void createEm() throws Exception {
563566
Assert.assertEquals("EM(1.7em)", ((LexicalUnitImpl) unit).toDebugString());
564567
}
565568

569+
/**
570+
* @throws Exception if any error occurs
571+
*/
572+
@Test
573+
public void createRem() throws Exception {
574+
final LexicalUnit unit = LexicalUnitImpl.createRem(null, 1.7f);
575+
576+
Assert.assertEquals(LexicalUnitType.REM, unit.getLexicalUnitType());
577+
Assert.assertEquals(1.7f, unit.getFloatValue(), 0.0001f);
578+
Assert.assertEquals(1, unit.getIntegerValue());
579+
Assert.assertEquals("rem", unit.getDimensionUnitText());
580+
Assert.assertNull(unit.getFunctionName());
581+
Assert.assertNull(unit.getParameters());
582+
Assert.assertNull(unit.getStringValue());
583+
584+
Assert.assertNull(unit.getNextLexicalUnit());
585+
Assert.assertNull(unit.getPreviousLexicalUnit());
586+
587+
Assert.assertEquals("1.7rem", unit.toString());
588+
Assert.assertEquals("REM(1.7rem)", ((LexicalUnitImpl) unit).toDebugString());
589+
}
590+
566591
/**
567592
* @throws Exception if any error occurs
568593
*/

0 commit comments

Comments
 (0)