Skip to content

Commit 3d43f4f

Browse files
committed
add method CSSValueImpl.getLexicalUnitType()
1 parent 5669267 commit 3d43f4f

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,13 @@ else if (value_ instanceof CounterImpl) {
265265
return CSS_UNKNOWN;
266266
}
267267

268+
public LexicalUnit.LexicalUnitType getLexicalUnitType() {
269+
if (value_ instanceof LexicalUnit) {
270+
return ((LexicalUnit) value_).getLexicalUnitType();
271+
}
272+
return null;
273+
}
274+
268275
@Override
269276
public void setFloatValue(final short unitType, final float floatValue) throws DOMException {
270277
value_ = LexicalUnitImpl.createNumber(null, floatValue);

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public void attr() throws Exception {
3939

4040
Assert.assertEquals("attr(attrValue)", value.getCssText());
4141
Assert.assertEquals(CSSPrimitiveValue.CSS_ATTR, value.getPrimitiveType());
42+
Assert.assertEquals(LexicalUnit.LexicalUnitType.ATTR, value.getLexicalUnitType());
4243
Assert.assertEquals(0.0, value.getFloatValue(CSSPrimitiveValue.CSS_NUMBER), 0.00001);
4344
Assert.assertEquals("attrValue", value.getStringValue());
4445
}
@@ -53,6 +54,7 @@ public void cm() throws Exception {
5354

5455
Assert.assertEquals("1.2cm", value.getCssText());
5556
Assert.assertEquals(CSSPrimitiveValue.CSS_CM, value.getPrimitiveType());
57+
Assert.assertEquals(LexicalUnit.LexicalUnitType.CENTIMETER, value.getLexicalUnitType());
5658
Assert.assertEquals(1.2, value.getFloatValue(CSSPrimitiveValue.CSS_NUMBER), 0.00001);
5759
try {
5860
value.getStringValue();
@@ -73,6 +75,7 @@ public void counter() throws Exception {
7375

7476
Assert.assertEquals("counter()", value.getCssText());
7577
Assert.assertEquals(CSSPrimitiveValue.CSS_COUNTER, value.getPrimitiveType());
78+
Assert.assertEquals(LexicalUnit.LexicalUnitType.COUNTER_FUNCTION, value.getLexicalUnitType());
7679
Assert.assertEquals(0.0, value.getFloatValue(CSSPrimitiveValue.CSS_NUMBER), 0.00001);
7780
try {
7881
value.getStringValue();
@@ -93,6 +96,7 @@ public void deg() throws Exception {
9396

9497
Assert.assertEquals("1.2deg", value.getCssText());
9598
Assert.assertEquals(CSSPrimitiveValue.CSS_DEG, value.getPrimitiveType());
99+
Assert.assertEquals(LexicalUnit.LexicalUnitType.DEGREE, value.getLexicalUnitType());
96100
Assert.assertEquals(1.2, value.getFloatValue(CSSPrimitiveValue.CSS_NUMBER), 0.00001);
97101
try {
98102
value.getStringValue();
@@ -113,6 +117,7 @@ public void dimension() throws Exception {
113117

114118
Assert.assertEquals("1.2lumen", value.getCssText());
115119
Assert.assertEquals(CSSPrimitiveValue.CSS_DIMENSION, value.getPrimitiveType());
120+
Assert.assertEquals(LexicalUnit.LexicalUnitType.DIMENSION, value.getLexicalUnitType());
116121
Assert.assertEquals(1.2, value.getFloatValue(CSSPrimitiveValue.CSS_NUMBER), 0.00001);
117122
try {
118123
value.getStringValue();
@@ -133,6 +138,7 @@ public void em() throws Exception {
133138

134139
Assert.assertEquals("1.2em", value.getCssText());
135140
Assert.assertEquals(CSSPrimitiveValue.CSS_EMS, value.getPrimitiveType());
141+
Assert.assertEquals(LexicalUnit.LexicalUnitType.EM, value.getLexicalUnitType());
136142
Assert.assertEquals(1.2, value.getFloatValue(CSSPrimitiveValue.CSS_NUMBER), 0.00001);
137143
try {
138144
value.getStringValue();
@@ -153,6 +159,7 @@ public void emx() throws Exception {
153159

154160
Assert.assertEquals("1.2ex", value.getCssText());
155161
Assert.assertEquals(CSSPrimitiveValue.CSS_EXS, value.getPrimitiveType());
162+
Assert.assertEquals(LexicalUnit.LexicalUnitType.EX, value.getLexicalUnitType());
156163
Assert.assertEquals(1.2, value.getFloatValue(CSSPrimitiveValue.CSS_NUMBER), 0.00001);
157164
try {
158165
value.getStringValue();
@@ -173,6 +180,7 @@ public void function() throws Exception {
173180

174181
Assert.assertEquals("foo(\"param\")", value.getCssText());
175182
Assert.assertEquals(CSSPrimitiveValue.CSS_STRING, value.getPrimitiveType());
183+
Assert.assertEquals(LexicalUnit.LexicalUnitType.FUNCTION, value.getLexicalUnitType());
176184
Assert.assertEquals("foo(\"param\")", value.getStringValue());
177185
}
178186

@@ -190,6 +198,7 @@ public void functionTwoParams() throws Exception {
190198

191199
Assert.assertEquals("foo(10, 11)", value.getCssText());
192200
Assert.assertEquals(CSSPrimitiveValue.CSS_STRING, value.getPrimitiveType());
201+
Assert.assertEquals(LexicalUnit.LexicalUnitType.FUNCTION, value.getLexicalUnitType());
193202
Assert.assertEquals("foo(10, 11)", value.getStringValue());
194203
}
195204

@@ -203,6 +212,7 @@ public void gradian() throws Exception {
203212

204213
Assert.assertEquals("1.2grad", value.getCssText());
205214
Assert.assertEquals(CSSPrimitiveValue.CSS_GRAD, value.getPrimitiveType());
215+
Assert.assertEquals(LexicalUnit.LexicalUnitType.GRADIAN, value.getLexicalUnitType());
206216
Assert.assertEquals(1.2, value.getFloatValue(CSSPrimitiveValue.CSS_NUMBER), 0.00001);
207217
try {
208218
value.getStringValue();
@@ -223,6 +233,7 @@ public void hertz() throws Exception {
223233

224234
Assert.assertEquals("1.2Hz", value.getCssText());
225235
Assert.assertEquals(CSSPrimitiveValue.CSS_HZ, value.getPrimitiveType());
236+
Assert.assertEquals(LexicalUnit.LexicalUnitType.HERTZ, value.getLexicalUnitType());
226237
Assert.assertEquals(1.2, value.getFloatValue(CSSPrimitiveValue.CSS_NUMBER), 0.00001);
227238
try {
228239
value.getStringValue();
@@ -243,6 +254,7 @@ public void ident() throws Exception {
243254

244255
Assert.assertEquals("id", value.getCssText());
245256
Assert.assertEquals(CSSPrimitiveValue.CSS_IDENT, value.getPrimitiveType());
257+
Assert.assertEquals(LexicalUnit.LexicalUnitType.IDENT, value.getLexicalUnitType());
246258
Assert.assertEquals(0, value.getFloatValue(CSSPrimitiveValue.CSS_NUMBER), 0.00001);
247259
Assert.assertEquals("id", value.getStringValue());
248260
}
@@ -257,6 +269,7 @@ public void inch() throws Exception {
257269

258270
Assert.assertEquals("1.2in", value.getCssText());
259271
Assert.assertEquals(CSSPrimitiveValue.CSS_IN, value.getPrimitiveType());
272+
Assert.assertEquals(LexicalUnit.LexicalUnitType.INCH, value.getLexicalUnitType());
260273
Assert.assertEquals(1.2, value.getFloatValue(CSSPrimitiveValue.CSS_NUMBER), 0.00001);
261274
try {
262275
value.getStringValue();
@@ -277,6 +290,7 @@ public void kiloHertz() throws Exception {
277290

278291
Assert.assertEquals("1.2kHz", value.getCssText());
279292
Assert.assertEquals(CSSPrimitiveValue.CSS_KHZ, value.getPrimitiveType());
293+
Assert.assertEquals(LexicalUnit.LexicalUnitType.KILOHERTZ, value.getLexicalUnitType());
280294
Assert.assertEquals(1.2, value.getFloatValue(CSSPrimitiveValue.CSS_NUMBER), 0.00001);
281295
try {
282296
value.getStringValue();
@@ -297,6 +311,7 @@ public void millimeter() throws Exception {
297311

298312
Assert.assertEquals("1.2mm", value.getCssText());
299313
Assert.assertEquals(CSSPrimitiveValue.CSS_MM, value.getPrimitiveType());
314+
Assert.assertEquals(LexicalUnit.LexicalUnitType.MILLIMETER, value.getLexicalUnitType());
300315
Assert.assertEquals(1.2, value.getFloatValue(CSSPrimitiveValue.CSS_NUMBER), 0.00001);
301316
try {
302317
value.getStringValue();
@@ -317,6 +332,7 @@ public void millisecond() throws Exception {
317332

318333
Assert.assertEquals("1.2ms", value.getCssText());
319334
Assert.assertEquals(CSSPrimitiveValue.CSS_MS, value.getPrimitiveType());
335+
Assert.assertEquals(LexicalUnit.LexicalUnitType.MILLISECOND, value.getLexicalUnitType());
320336
Assert.assertEquals(1.2, value.getFloatValue(CSSPrimitiveValue.CSS_NUMBER), 0.00001);
321337
try {
322338
value.getStringValue();
@@ -337,6 +353,7 @@ public void numberFloat() throws Exception {
337353

338354
Assert.assertEquals("1.2", value.getCssText());
339355
Assert.assertEquals(CSSPrimitiveValue.CSS_NUMBER, value.getPrimitiveType());
356+
Assert.assertEquals(LexicalUnit.LexicalUnitType.REAL, value.getLexicalUnitType());
340357
Assert.assertEquals(1.2, value.getFloatValue(CSSPrimitiveValue.CSS_NUMBER), 0.00001);
341358
try {
342359
value.getStringValue();
@@ -357,6 +374,7 @@ public void numberInt() throws Exception {
357374

358375
Assert.assertEquals("12", value.getCssText());
359376
Assert.assertEquals(CSSPrimitiveValue.CSS_NUMBER, value.getPrimitiveType());
377+
Assert.assertEquals(LexicalUnit.LexicalUnitType.INTEGER, value.getLexicalUnitType());
360378
Assert.assertEquals(12, value.getFloatValue(CSSPrimitiveValue.CSS_NUMBER), 0.00001);
361379
try {
362380
value.getStringValue();
@@ -377,6 +395,7 @@ public void pica() throws Exception {
377395

378396
Assert.assertEquals("1.2pc", value.getCssText());
379397
Assert.assertEquals(CSSPrimitiveValue.CSS_PC, value.getPrimitiveType());
398+
Assert.assertEquals(LexicalUnit.LexicalUnitType.PICA, value.getLexicalUnitType());
380399
Assert.assertEquals(1.2, value.getFloatValue(CSSPrimitiveValue.CSS_NUMBER), 0.00001);
381400
try {
382401
value.getStringValue();
@@ -397,6 +416,7 @@ public void percentage() throws Exception {
397416

398417
Assert.assertEquals("1.2%", value.getCssText());
399418
Assert.assertEquals(CSSPrimitiveValue.CSS_PERCENTAGE, value.getPrimitiveType());
419+
Assert.assertEquals(LexicalUnit.LexicalUnitType.PERCENTAGE, value.getLexicalUnitType());
400420
Assert.assertEquals(1.2, value.getFloatValue(CSSPrimitiveValue.CSS_NUMBER), 0.00001);
401421
try {
402422
value.getStringValue();
@@ -417,6 +437,7 @@ public void point() throws Exception {
417437

418438
Assert.assertEquals("1.2pt", value.getCssText());
419439
Assert.assertEquals(CSSPrimitiveValue.CSS_PT, value.getPrimitiveType());
440+
Assert.assertEquals(LexicalUnit.LexicalUnitType.POINT, value.getLexicalUnitType());
420441
Assert.assertEquals(1.2, value.getFloatValue(CSSPrimitiveValue.CSS_NUMBER), 0.00001);
421442
try {
422443
value.getStringValue();
@@ -437,6 +458,7 @@ public void pixel() throws Exception {
437458

438459
Assert.assertEquals("1.2px", value.getCssText());
439460
Assert.assertEquals(CSSPrimitiveValue.CSS_PX, value.getPrimitiveType());
461+
Assert.assertEquals(LexicalUnit.LexicalUnitType.PIXEL, value.getLexicalUnitType());
440462
Assert.assertEquals(1.2, value.getFloatValue(CSSPrimitiveValue.CSS_NUMBER), 0.00001);
441463
try {
442464
value.getStringValue();
@@ -457,6 +479,7 @@ public void radian() throws Exception {
457479

458480
Assert.assertEquals("1.2rad", value.getCssText());
459481
Assert.assertEquals(CSSPrimitiveValue.CSS_RAD, value.getPrimitiveType());
482+
Assert.assertEquals(LexicalUnit.LexicalUnitType.RADIAN, value.getLexicalUnitType());
460483
Assert.assertEquals(1.2, value.getFloatValue(CSSPrimitiveValue.CSS_NUMBER), 0.00001);
461484
try {
462485
value.getStringValue();
@@ -485,6 +508,7 @@ public void rect() throws Exception {
485508

486509
Assert.assertEquals("rect(1, 2, 3, 4)", value.getCssText());
487510
Assert.assertEquals(CSSPrimitiveValue.CSS_RECT, value.getPrimitiveType());
511+
Assert.assertEquals(null, value.getLexicalUnitType());
488512
try {
489513
value.getFloatValue(CSSPrimitiveValue.CSS_NUMBER);
490514
Assert.fail("DomException expected");
@@ -501,6 +525,27 @@ public void rect() throws Exception {
501525
}
502526
}
503527

528+
/**
529+
* @throws Exception if any error occurs
530+
*/
531+
@Test
532+
public void rem() throws Exception {
533+
final LexicalUnit lu = LexicalUnitImpl.createRem(null, 1.2f);
534+
final CSSValueImpl value = new CSSValueImpl(lu, false);
535+
536+
Assert.assertEquals("1.2rem", value.getCssText());
537+
Assert.assertEquals(CSSPrimitiveValue.CSS_UNKNOWN, value.getPrimitiveType());
538+
Assert.assertEquals(LexicalUnit.LexicalUnitType.REM, value.getLexicalUnitType());
539+
Assert.assertEquals(1.2, value.getFloatValue(CSSPrimitiveValue.CSS_NUMBER), 0.00001);
540+
try {
541+
value.getStringValue();
542+
Assert.fail("DomException expected");
543+
}
544+
catch (final DOMException e) {
545+
// expected
546+
}
547+
}
548+
504549
/**
505550
* @throws Exception if any error occurs
506551
*/
@@ -517,6 +562,7 @@ public void rgbColor() throws Exception {
517562

518563
Assert.assertEquals("rgb(255, 128, 0)", value.getCssText());
519564
Assert.assertEquals(CSSPrimitiveValue.CSS_RGBCOLOR, value.getPrimitiveType());
565+
Assert.assertEquals(null, value.getLexicalUnitType());
520566
try {
521567
value.getFloatValue(CSSPrimitiveValue.CSS_NUMBER);
522568
Assert.fail("DomException expected");
@@ -543,6 +589,7 @@ public void second() throws Exception {
543589

544590
Assert.assertEquals("1.2s", value.getCssText());
545591
Assert.assertEquals(CSSPrimitiveValue.CSS_S, value.getPrimitiveType());
592+
Assert.assertEquals(LexicalUnit.LexicalUnitType.SECOND, value.getLexicalUnitType());
546593
Assert.assertEquals(1.2, value.getFloatValue(CSSPrimitiveValue.CSS_NUMBER), 0.00001);
547594
try {
548595
value.getStringValue();
@@ -563,6 +610,7 @@ public void string() throws Exception {
563610

564611
Assert.assertEquals("\"value\"", value.getCssText());
565612
Assert.assertEquals(CSSPrimitiveValue.CSS_STRING, value.getPrimitiveType());
613+
Assert.assertEquals(LexicalUnit.LexicalUnitType.STRING_VALUE, value.getLexicalUnitType());
566614
Assert.assertEquals(0.0, value.getFloatValue(CSSPrimitiveValue.CSS_NUMBER), 0.00001); // TODO is this correct?
567615
Assert.assertEquals("value", value.getStringValue());
568616
}
@@ -577,6 +625,7 @@ public void uri() throws Exception {
577625

578626
Assert.assertEquals("url(cssparser)", value.getCssText());
579627
Assert.assertEquals(CSSPrimitiveValue.CSS_URI, value.getPrimitiveType());
628+
Assert.assertEquals(LexicalUnit.LexicalUnitType.URI, value.getLexicalUnitType());
580629
Assert.assertEquals(0.0, value.getFloatValue(CSSPrimitiveValue.CSS_NUMBER), 0.00001);
581630
Assert.assertEquals("cssparser", value.getStringValue());
582631
}

0 commit comments

Comments
 (0)