Skip to content

Commit 6095807

Browse files
committed
Upgrade examples to css4j 5.1
1 parent f2e1554 commit 6095807

File tree

2 files changed

+51
-19
lines changed

2 files changed

+51
-19
lines changed

src/examples/java/io/sf/carte/example/Css4jTest.java

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@
9191
import io.sf.carte.doc.style.css.property.LexicalValue;
9292
import io.sf.carte.doc.style.css.property.NumberValue;
9393
import io.sf.carte.doc.style.css.property.StyleValue;
94-
import io.sf.carte.doc.style.css.property.VarValue;
9594
import nu.validator.htmlparser.common.XmlViolationPolicy;
9695
import nu.validator.htmlparser.sax.HtmlParser;
9796

@@ -304,29 +303,48 @@ public void testUsageNativeDOM() throws Exception {
304303

305304
// Check the primitive type
306305
CSSValue.Type primiType1 = color.getPrimitiveType();
307-
// It is a var()
308-
assertEquals(CSSValue.Type.VAR, primiType1);
306+
// It is a LEXICAL value (var() or attr())
307+
assertEquals(CSSValue.Type.LEXICAL, primiType1);
308+
309309
// Obtain the custom property name
310-
VarValue varValue = (VarValue) color;
311-
String customPropertyName = varValue.getName();
310+
LexicalValue varValue = (LexicalValue) color;
311+
LexicalUnit lexUnit = varValue.getLexicalUnit();
312+
// It is a var()
313+
assertEquals(LexicalType.VAR, lexUnit.getLexicalUnitType());
314+
315+
// Obtain function parameters
316+
LexicalUnit param = lexUnit.getParameters();
317+
// Let's see its unit type
318+
LexicalType luType = param.getLexicalUnitType();
319+
// It is IDENT (identifier)
320+
assertEquals(LexicalType.IDENT, param.getLexicalUnitType());
321+
String customPropertyName = param.getStringValue();
312322
// Name is --myColor
313323
assertEquals("--myColor", customPropertyName);
314324

325+
// Continue with the lexical chain
326+
param = param.getNextLexicalUnit();
327+
// Let's see its unit type
328+
luType = param.getLexicalUnitType();
329+
// It is OPERATOR_COMMA
330+
assertEquals(LexicalType.OPERATOR_COMMA, param.getLexicalUnitType());
331+
315332
// In this case we have a fallback value (otherwise next line returns null)
316-
LexicalUnit lexUnit = varValue.getFallback();
333+
param = param.getNextLexicalUnit();
317334
// Let's see its unit type
318-
LexicalType luType = lexUnit.getLexicalUnitType();
335+
luType = param.getLexicalUnitType();
319336
// It is a RGBCOLOR
320337
assertEquals(LexicalType.RGBCOLOR, luType);
321338
// And the color is...
322-
String strColor = lexUnit.getCssText();
339+
String strColor = param.getCssText();
323340
// Is '#46f'
324341
assertEquals("#46f", strColor);
325342

326343
// Access the color components:
327344
// RGB colors are accessed as a rgb() function, so we retrieve
328-
// the function parameters:
329-
LexicalUnit firstComponent = lexUnit.getParameters();
345+
// the function parameters as a doubly-linked list:
346+
LexicalUnit firstComponent = param.getParameters();
347+
330348
// Let's see the lexical type to see how we access the value
331349
LexicalType firstLexUnitType = firstComponent.getLexicalUnitType();
332350
// It is a LexicalType.INTEGER (RGB components could be PERCENTAGE as well)

usage.html

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -689,27 +689,41 @@ <h3 class="subtema" id="use-declared-styles">Using the declared styles</h3>
689689

690690
// Check the primitive type
691691
CSSValue.Type primiType1 = color.getPrimitiveType();
692-
// It is a var() function (CSSValue.Type.VAR)
692+
// It is a LEXICAL value (var() or attr())
693693

694694
// Obtain the custom property name
695-
VarValue varValue = (VarValue) color;
696-
String customPropertyName = varValue.getName();
695+
LexicalValue varValue = (LexicalValue) color;
696+
LexicalUnit lexUnit = varValue.getLexicalUnit();
697+
// It is a VAR (var() function)
698+
699+
// Obtain function parameters
700+
LexicalUnit param = lexUnit.getParameters();
701+
// Let's see its unit type
702+
LexicalType luType = param.getLexicalUnitType();
703+
// It is IDENT (identifier)
704+
String customPropertyName = param.getStringValue();
697705
// Name is --myColor
698706

699-
// In this case we have a fallback value (otherwise next line returns null)
700-
LexicalUnit lexUnit = varValue.getFallback();
707+
// Continue with the lexical chain
708+
param = param.getNextLexicalUnit();
701709
// Let's see its unit type
702-
LexicalType luType = lexUnit.getLexicalUnitType();
703-
// It is a LexicalType.RGBCOLOR
710+
luType = param.getLexicalUnitType();
711+
// It is OPERATOR_COMMA
704712

713+
// In this case we have a fallback value (otherwise next line returns null)
714+
param = param.getNextLexicalUnit();
715+
// Let's see its unit type
716+
luType = param.getLexicalUnitType();
717+
// It is a RGBCOLOR
718+
assertEquals(LexicalType.RGBCOLOR, luType);
705719
// And the color is...
706-
String strColor = lexUnit.getCssText();
720+
String strColor = param.getCssText();
707721
// Is '#46f'
708722

709723
// Access the individual color components.
710724
// RGB colors are accessed as a rgb() function, so we retrieve
711725
// the function parameters as a doubly-linked list:
712-
LexicalUnit firstComponent = lexUnit.getParameters();
726+
LexicalUnit firstComponent = param.getParameters();
713727

714728
// Let's see the lexical type to see how we access the value
715729
LexicalType firstLexUnitType = firstComponent.getLexicalUnitType();

0 commit comments

Comments
 (0)