Table of Contents

Struct CssToken

Namespace
Egami.Css
Assembly
Egami.Css.dll

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

kind CssTokenKind

The kind of token.

value string

The value: see Value.

number double

The numeric value of a number, percentage or dimension.

unit string

The unit of a dimension.

isInteger bool

Whether a number, percentage or dimension was written as an integer.

isIdHash bool

Whether a hash would be a valid identifier.

line int

The 1-based source line, or 0 when unknown.

column int

The 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

int

IsIdHash

Gets whether a hash has the type "id", that is, its name would be a valid identifier.

public bool IsIdHash { get; }

Property Value

bool

IsInteger

Gets whether a number, percentage or dimension was written without a fraction or exponent.

public bool IsInteger { get; }

Property Value

bool

Kind

Gets the kind of token.

public CssTokenKind Kind { get; }

Property Value

CssTokenKind

Line

Gets the 1-based line the token starts on, or 0 when unknown.

public int Line { get; }

Property Value

int

Number

Gets the numeric value of a number, percentage or dimension.

public double Number { get; }

Property Value

double

Unit

Gets the unit of a dimension, with escapes resolved, or an empty string.

public string Unit { get; }

Property Value

string

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

string

Methods

Equals(CssToken)

Indicates whether the current object is equal to another object of the same type.

public bool Equals(CssToken other)

Parameters

other CssToken

An object to compare with this object.

Returns

bool

true if the current object is equal to the other parameter; otherwise, false.

Equals(object?)

Indicates whether this instance and a specified object are equal.

public override bool Equals(object? obj)

Parameters

obj object

The object to compare with the current instance.

Returns

bool

true if obj and 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

c char

The 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

name string

The 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

left CssToken

The first token.

right CssToken

The second token.

Returns

bool

True when they are equal.

operator !=(CssToken, CssToken)

Compares two tokens.

public static bool operator !=(CssToken left, CssToken right)

Parameters

left CssToken

The first token.

right CssToken

The second token.

Returns

bool

True when they differ.