CssSyntaxTree Class

A parsed CSS style sheet.

public sealed class CssSyntaxTree : SyntaxTree

Remarks

Parsing never throws and never gives up: whatever the text says, the tree reproduces it exactly, and anything wrong with it is reported through CssSyntaxTree.GetDiagnostics -- from an unclosed block to a selector a browser would reject. An error is something a browser drops; a warning is something it accepts, or that may only be newer than this parser.

A tree made from a root with CssSyntaxTree.Create or CssSyntaxTree.WithRoot reports the diagnostics of its text instead, which it parses again the first time it is asked: an edit can build nodes that do not read back as themselves, such as two type selectors put next to each other without the whitespace that would make them a descendant combinator.

Example

var tree = CssSyntaxTree.ParseText(".card { color: red; &:hover { color: blue } }");
var rule = (CssQualifiedRuleSyntax)tree.GetRoot().Statements[0];
var selectors = (CssSelectorListSyntax)rule.Prelude!;
Assert.Empty(tree.GetDiagnostics());
Assert.Equal(".card", selectors.Selectors.Single().ToString());
Inheritance
SyntaxTree CssSyntaxTree

Properties

NameDescription
FilePath
OptionsGets the options the text was parsed with.
public override string? FilePath { get; }

Property value

string?

No documentation is available.

Gets the options the text was parsed with.

public CssParseOptions Options { get; }

Property value

CssParseOptions

Methods

NameDescription
Create(CssStyleSheetSyntax, CssParseOptions?, string?)Creates a tree over root, taking its text from the root itself.
Create(CssStyleSheetSyntax, string?)Creates a tree over root, taking its text from the root itself.
GetChanges(CssSyntaxTree)Describes how oldTree would have to change to become this one.
GetDiagnostics()Gets every diagnostic in the tree, in source order.
GetDiagnostics(SyntaxNode)Gets the diagnostics at or below node.
GetDiagnostics(SyntaxToken)Gets the diagnostics on token and the trivia around it.
GetDiagnostics(SyntaxTrivia)Gets the diagnostics on trivia.
GetRoot()Gets the root of the tree.
GetText()
IsEquivalentTo(CssSyntaxTree?)Determines whether the two trees have the same structure and text.
ParseText(SourceText, CssParseOptions?, string?)Parses text.
ParseText(SourceText, string?)Parses text as a style sheet.
ParseText(string, CssParseOptions?, string?)Parses text.
ParseText(string, string?)Parses text as a style sheet.
WithChangedText(SourceText)Returns a tree over newText, parsed with the same options.
WithChanges(TextChange[])Returns a tree over this text with changes applied.
WithChanges(IEnumerable<TextChange>)Returns a tree over this text with changes applied.
WithRoot(CssStyleSheetSyntax)Returns a tree whose root is root.

Create overloads

Create(CssStyleSheetSyntax, CssParseOptions?, string?)

CssSyntaxTree.cs

Creates a tree over root, taking its text from the root itself.

public static CssSyntaxTree Create(CssStyleSheetSyntax root, CssParseOptions? options, string? path = null)
Parameters
root CssStyleSheetSyntax
options CssParseOptions?
path string?
Returns

CssSyntaxTree

Exceptions
ArgumentNullException
root is null.
Remarks

root becomes the root of the tree when it is not part of another one yet. The nodes are kept, but the diagnostics are those of the text: it is parsed again with options the first time they are asked for, so that they are right even when an edit built nodes that do not read back as themselves.

Create(CssStyleSheetSyntax, string?)

CssSyntaxTree.cs

Creates a tree over root, taking its text from the root itself.

public static CssSyntaxTree Create(CssStyleSheetSyntax root, string? path = null)
Parameters
root CssStyleSheetSyntax
path string?
Returns

CssSyntaxTree

Exceptions
ArgumentNullException
root is null.
Remarks

root becomes the root of the tree when it is not part of another one yet. The nodes are kept, but the diagnostics are those of the text: it is parsed again the first time they are asked for, so that they are right even when an edit built nodes that do not read back as themselves.

GetChanges(CssSyntaxTree)

CssSyntaxTree.cs

Describes how oldTree would have to change to become this one.

public IReadOnlyList<TextChange> GetChanges(CssSyntaxTree oldTree)

Parameters

oldTree CssSyntaxTree

Returns

IReadOnlyList<TextChange>

Exceptions

ArgumentNullException
oldTree is null.

GetDiagnostics overloads

GetDiagnostics()

CssSyntaxTree.cs

Gets every diagnostic in the tree, in source order.

public override IReadOnlyList<Diagnostic> GetDiagnostics()

GetDiagnostics(SyntaxNode)

CssSyntaxTree.cs

Gets the diagnostics at or below node.

public override IEnumerable<Diagnostic> GetDiagnostics(SyntaxNode node)
Parameters
node SyntaxNode
Returns

IEnumerable<Diagnostic>

Exceptions
ArgumentNullException
node is null.

GetDiagnostics(SyntaxToken)

CssSyntaxTree.cs

Gets the diagnostics on token and the trivia around it.

public override IEnumerable<Diagnostic> GetDiagnostics(SyntaxToken token)
Parameters
token SyntaxToken
Returns

IEnumerable<Diagnostic>

GetDiagnostics(SyntaxTrivia)

CssSyntaxTree.cs

Gets the diagnostics on trivia.

public override IEnumerable<Diagnostic> GetDiagnostics(SyntaxTrivia trivia)
Parameters
trivia SyntaxTrivia
Returns

IEnumerable<Diagnostic>

Gets the root of the tree.

public CssStyleSheetSyntax GetRoot()
public override SourceText GetText()

Returns

SourceText

No documentation is available.

IsEquivalentTo(CssSyntaxTree?)

CssSyntaxTree.cs

Determines whether the two trees have the same structure and text.

public bool IsEquivalentTo(CssSyntaxTree? other)

Parameters

other CssSyntaxTree?

Returns

bool

ParseText overloads

ParseText(SourceText, CssParseOptions?, string?)

CssSyntaxTree.cs

Parses text.

public static CssSyntaxTree ParseText(SourceText text, CssParseOptions? options, string? path = null)
Parameters
text SourceText
options CssParseOptions?
path string?
Returns

CssSyntaxTree

Exceptions
ArgumentNullException
text is null.

ParseText(SourceText, string?)

CssSyntaxTree.cs

Parses text as a style sheet.

public static CssSyntaxTree ParseText(SourceText text, string? path = null)
Parameters
text SourceText
path string?
Returns

CssSyntaxTree

Exceptions
ArgumentNullException
text is null.

ParseText(string, CssParseOptions?, string?)

CssSyntaxTree.cs

Parses text.

public static CssSyntaxTree ParseText(string text, CssParseOptions? options, string? path = null)
Parameters
text string
The CSS to read.
options CssParseOptions?
How to read it, or null for CssParseOptions.Default.
path string?
Where the text came from, for the diagnostics to refer to.
Returns

CssSyntaxTree

Exceptions
ArgumentNullException
text is null.

ParseText(string, string?)

CssSyntaxTree.cs

Parses text as a style sheet.

public static CssSyntaxTree ParseText(string text, string? path = null)
Parameters
text string
The CSS to read.
path string?
Where the text came from, for the diagnostics to refer to.
Returns

CssSyntaxTree

Exceptions
ArgumentNullException
text is null.

WithChangedText(SourceText)

CssSyntaxTree.cs

Returns a tree over newText, parsed with the same options.

public CssSyntaxTree WithChangedText(SourceText newText)

Parameters

newText SourceText

Returns

CssSyntaxTree

Exceptions

ArgumentNullException
newText is null.

WithChanges overloads

WithChanges(TextChange[])

CssSyntaxTree.cs

Returns a tree over this text with changes applied.

public CssSyntaxTree WithChanges(params TextChange[] changes)
Parameters
changes TextChange[]
Returns

CssSyntaxTree

Exceptions
ArgumentNullException
changes is null.

WithChanges(IEnumerable<TextChange>)

CssSyntaxTree.cs

Returns a tree over this text with changes applied.

public CssSyntaxTree WithChanges(IEnumerable<TextChange> changes)
Parameters
changes IEnumerable<TextChange>
Returns

CssSyntaxTree

Exceptions
ArgumentNullException
changes is null.

WithRoot(CssStyleSheetSyntax)

CssSyntaxTree.cs

Returns a tree whose root is root.

public CssSyntaxTree WithRoot(CssStyleSheetSyntax root)

Parameters

root CssStyleSheetSyntax

Returns

CssSyntaxTree

Exceptions

ArgumentNullException
root is null.