Struct CssToken
One CSS token, with its position in the source.
public readonly struct CssToken : IEquatable<CssToken>
- Implements
- Inherited Members
Examples
// CSS Syntax 3 tokens, and component values: tokens, functions and simple blocks.
IReadOnlyList<CssToken> tokens = CssTokenizer.Tokenize("rgb(255 0 0 / 50%) 12px");
CssToken first = tokens[0];
bool isFunction = first.Kind == CssTokenKind.Function; // "rgb(" at line 1, column 1
IReadOnlyList<CssComponentValue> values = CssComponentValue.ParseList("calc(100% - 2px) [a] x");
var calc = (CssFunction)values[0];
var block = (CssSimpleBlock)values[2];
var word = (CssTokenValue)values[4];
string text = CssComponentValue.Serialize(values); // "calc(100% - 2px) [a] x"
Remarks
Two tokens are equal when they have the same kind, value, number, unit and flags; their positions are not compared.
Constructors
CssToken(CssTokenKind, string?, double, string?, bool, bool, int, int)
Creates a token.
public CssToken(CssTokenKind kind, string? value = null, double number = 0, string? unit = null, bool isInteger = false, bool isIdHash = false, int line = 0, int column = 0)
Parameters
kindCssTokenKindThe kind of token.
valuestringThe value: see Value.
numberdoubleThe numeric value of a number, percentage or dimension.
unitstringThe unit of a dimension.
isIntegerboolWhether a number, percentage or dimension was written as an integer.
isIdHashboolWhether a hash would be a valid identifier.
lineintThe 1-based source line, or 0 when unknown.
columnintThe 1-based source column, or 0 when unknown.
Properties
Column
Gets the 1-based column the token starts at, in UTF-16 code units, or 0 when unknown.
public int Column { get; }
Property Value
IsIdHash
Gets whether a hash has the type "id", that is, its name would be a valid identifier.
public bool IsIdHash { get; }
Property Value
IsInteger
Gets whether a number, percentage or dimension was written without a fraction or exponent.
public bool IsInteger { get; }
Property Value
Kind
Gets the kind of token.
public CssTokenKind Kind { get; }
Property Value
Line
Gets the 1-based line the token starts on, or 0 when unknown.
public int Line { get; }
Property Value
Number
Gets the numeric value of a number, percentage or dimension.
public double Number { get; }
Property Value
Unit
Gets the unit of a dimension, with escapes resolved, or an empty string.
public string Unit { get; }
Property Value
Value
Gets the token's value, with escapes resolved: the name of an identifier, function, at-keyword or hash; the text of a
string or url; the character of a delim; and the number as written (such as +1.50) for a number, percentage or
dimension. Empty for other kinds.
public string Value { get; }
Property Value
Methods
Equals(CssToken)
Indicates whether the current object is equal to another object of the same type.
public bool Equals(CssToken other)
Parameters
otherCssTokenAn object to compare with this object.
Returns
Equals(object?)
Indicates whether this instance and a specified object are equal.
public override bool Equals(object? obj)
Parameters
objobjectThe object to compare with the current instance.
Returns
- bool
true if
objand this instance are the same type and represent the same value; otherwise, false.
GetHashCode()
Returns the hash code for this instance.
public override int GetHashCode()
Returns
- int
A 32-bit signed integer that is the hash code for this instance.
IsDelim(char)
Gets whether this is a delim token for c.
public bool IsDelim(char c)
Parameters
ccharThe character.
Returns
- bool
True when the token is that delim.
IsIdent(string)
Gets whether this is an identifier equal to name, compared ASCII case-insensitively.
public bool IsIdent(string name)
Parameters
namestringThe name, in lowercase.
Returns
- bool
True when the token is that identifier.
ToString()
Serializes the token as CSS text.
public override string ToString()
Returns
- string
The CSS text.
Operators
operator ==(CssToken, CssToken)
Compares two tokens.
public static bool operator ==(CssToken left, CssToken right)
Parameters
Returns
- bool
True when they are equal.
operator !=(CssToken, CssToken)
Compares two tokens.
public static bool operator !=(CssToken left, CssToken right)
Parameters
Returns
- bool
True when they differ.