Interface LexicalUnit
LexicalUnit
interface by Philippe Le Hegaret.-
Nested Class Summary
-
Method Summary
Modifier and TypeMethodDescriptionclone()
Creates a deep copy of this lexical unit and the next ones, unlinked to any previous lexical unit.int
countReplaceBy
(LexicalUnit replacementUnit) Replace this unit in the chain of lexical units (by the unit or sequence of units in the argument) and return the count of replacement units.Get a parsable representation of this unit.short
An integer indicating the type of CSS unit that this lexical value represents.If this unit is aLexicalUnit.LexicalType.DIMENSION
, returns the string representation of the CSS unit returned bygetCssUnit()
.float
Returns the float value represented by this unit.Returns the name of the function.int
Returns the integer value represented by this unit.Gives the type ofLexicalUnit
.The next lexical unit.The function parameters including operators (like the comma).The previous lexical unit.Returns the string value.Returns a sequence of units inside the sub-expression or unicode range.void
insertNextLexicalUnit
(LexicalUnit nextUnit) Insert the given unit as the next lexical unit.boolean
Check if this lexical unit is a parameter that was made available through its parent'sgetParameters()
method.matches
(CSSValueSyntax syntax) Verify whether this value matches the given grammar.remove()
Remove this unit from the chain of lexical units and replace it by the next one, if any.replaceBy
(LexicalUnit replacementUnit) Replace this unit in the chain of lexical units.Creates a shallow copy of this lexical unit, ignoring the next ones, and unlinked to any previous lexical unit.
-
Method Details
-
getLexicalUnitType
LexicalUnit.LexicalType getLexicalUnitType()Gives the type ofLexicalUnit
.- Returns:
- the type of
LexicalUnit
.
-
getCssUnit
short getCssUnit()An integer indicating the type of CSS unit that this lexical value represents.- Returns:
- an integer indicating the type of CSS unit. If this value does not represent a dimension, must return an invalid unit identifier.
-
getNextLexicalUnit
LexicalUnit getNextLexicalUnit()The next lexical unit.Lexical units can form chains of units which can be traversed with
getNextLexicalUnit()
andgetPreviousLexicalUnit()
.- Returns:
- the next lexical unit, or
null
if none.
-
getPreviousLexicalUnit
LexicalUnit getPreviousLexicalUnit()The previous lexical unit.See also
getNextLexicalUnit()
.- Returns:
- the previous lexical unit, or
null
if none.
-
insertNextLexicalUnit
Insert the given unit as the next lexical unit.After the insertion,
nextUnit
shall be the next lexical unit, and the former next lexical unit will be the next one after the last unit in thenextUnit
unit chain.- Parameters:
nextUnit
- the lexical unit to be set as the next one.- Throws:
IllegalArgumentException
- if the argument is a parameter or has a previous unit.
-
remove
LexicalUnit remove()Remove this unit from the chain of lexical units and replace it by the next one, if any.- Returns:
- the next lexical unit, or
null
if there is no next lexical unit.
-
countReplaceBy
Replace this unit in the chain of lexical units (by the unit or sequence of units in the argument) and return the count of replacement units.The replaced unit always returns
null
forgetNextLexicalUnit()
andgetPreviousLexicalUnit()
,false
forisParameter()
.- Parameters:
replacementUnit
- the lexical unit that replaces this one. Ifnull
, this unit is replaced by the next one.- Returns:
- the number of units that replace this one (i.e. the
argument, or
0
if the argument isnull
). - Throws:
IllegalArgumentException
- if the replacement unit is a parameter or has a previous unit.CSSBudgetException
- if the replacement unit has too many chained units (to avoid potential DoS).
-
replaceBy
Replace this unit in the chain of lexical units.The replaced unit always returns
null
forgetNextLexicalUnit()
andgetPreviousLexicalUnit()
,false
forisParameter()
.This method is very similar to
countReplaceBy(LexicalUnit)
which gives important information when you need to replace this unit by an arbitrary (and potentially large) chunk of values.- Parameters:
replacementUnit
- the lexical unit that replaces this one. Ifnull
, this unit is replaced by the next one.- Returns:
- the unit that replaces this one (i.e. the argument, or the
next lexical unit if the argument is
null
). - Throws:
IllegalArgumentException
- if the replacement unit is a parameter or has a previous unit.CSSBudgetException
- if the replacement unit has too many chained units (to avoid potential DoS).- See Also:
-
getIntegerValue
int getIntegerValue()Returns the integer value represented by this unit.- Returns:
- the integer value, or zero if this is not an integer unit.
- See Also:
-
getFloatValue
float getFloatValue()Returns the float value represented by this unit.- Returns:
- the float value, or
NaN
if this value is not a float.
-
getDimensionUnitText
String getDimensionUnitText()If this unit is aLexicalUnit.LexicalType.DIMENSION
, returns the string representation of the CSS unit returned bygetCssUnit()
.- Returns:
- the string representation of the CSS unit, or the empty string if
this lexical unit does not represent a
LexicalUnit.LexicalType.DIMENSION
.
-
getStringValue
String getStringValue()Returns the string value.If the type is
URI
, the return value doesn't containuri(....)
or quotes.If the type is
ATTR
, the return value doesn't containattr(....)
.If the type is
UNICODE_WILDCARD
, the return value is the wildcard without the preceding "U+".- Returns:
- the string value, or
null
if this unit does not have a string to return. - See Also:
-
getFunctionName
String getFunctionName()Returns the name of the function.- Returns:
- the function name, or
null
if this unit is not a function. - See Also:
-
getParameters
LexicalUnit getParameters()The function parameters including operators (like the comma).A RGB color like
#000
is converted torgb(0, 0, 0)
.May return
null
if type isFUNCTION
.- Returns:
- the parameters of this function, or
null
if this unit is not a function, or an emptyFUNCTION
. - See Also:
-
isParameter
boolean isParameter()Check if this lexical unit is a parameter that was made available through its parent'sgetParameters()
method.- Returns:
true
if this lexical unit is a parameter.
-
getSubValues
LexicalUnit getSubValues()Returns a sequence of units inside the sub-expression or unicode range.- Returns:
- the values in the sub-expression, or
null
if this unit is not a sub-expression nor a unicode range. - See Also:
-
getCssText
String getCssText()Get a parsable representation of this unit.The serialization must only include this lexical unit, ignoring the next units if they exist.
The text should be close to how the value was specified (for example, preserving hex or functional notation in rgb colors) but must parse without errors (except for compatibility values like
COMPAT_IDENT
).- Returns:
- the parsable serialization of this unit.
-
matches
Verify whether this value matches the given grammar.This method is intended to be used for generic sanity checks and, consequently, some corner cases may not be taken into account. For example, when matching
var()
, the implementations are allowed to assume that the property substitution shall not be empty, otherwise values likevar(--data) 1px
would give a non-FALSE
match simultaneously on syntaxes with and without a multiplier (+
or#
), which would probably not be what that use case wants.In the case of
calc()
, if implementations do not perform a full dimensional analysis it is more acceptable to return a bogusTRUE
than aFALSE
.Here are some examples of matching:
Value Syntax Match attr(data-width length, 8%)
<percentage> | <length>
TRUE
<length>
PENDING
<percentage>
PENDING
<length>#
PENDING
<length-percentage>
TRUE
<resolution>
FALSE
*
TRUE
6pt var(--custom) 12.3px
<length>+
PENDING
<length>#
PENDING
1<length>
FALSE
<number>+
FALSE
*
TRUE
- Despite the value containing no commas, the syntax with a
#
multiplier matches asPENDING
because the custom property could have commas and start and end with one.
See also:
SyntaxParser
.- Parameters:
syntax
- the syntax.- Returns:
- the matching for the syntax.
- Despite the value containing no commas, the syntax with a
-
clone
LexicalUnit clone()Creates a deep copy of this lexical unit and the next ones, unlinked to any previous lexical unit.The clone's
getPreviousLexicalUnit()
returnsnull
(andisParameter()
false
) regardless of what the the original object returned.- Returns:
- a copy of this unit.
-
shallowClone
LexicalUnit shallowClone()Creates a shallow copy of this lexical unit, ignoring the next ones, and unlinked to any previous lexical unit.The shallow clone's
getPreviousLexicalUnit()
andgetNextLexicalUnit()
both returnnull
(andisParameter()
false
) regardless of what the the original object returned.- Returns:
- a shallow copy of this unit.
-