CssSyntaxTree Class
A parsed CSS style sheet.
public sealed class CssSyntaxTree : SyntaxTreeRemarks
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
SyntaxTreeCssSyntaxTree
Properties
| Name | Description |
|---|---|
| FilePath | |
| Options | Gets the options the text was parsed with. |
FilePath
CssSyntaxTree.csOptions
CssSyntaxTree.csMethods
| Name | Description |
|---|---|
| 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.csCreates a tree over root, taking its text from the root itself.
public static CssSyntaxTree Create(CssStyleSheetSyntax root, CssParseOptions? options, string? path = null)Parameters
rootCssStyleSheetSyntaxoptionsCssParseOptions?pathstring?
Returns
Exceptions
ArgumentNullExceptionrootisnull.
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.csCreates a tree over root, taking its text from the root itself.
public static CssSyntaxTree Create(CssStyleSheetSyntax root, string? path = null)Parameters
rootCssStyleSheetSyntaxpathstring?
Returns
Exceptions
ArgumentNullExceptionrootisnull.
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.csDescribes how oldTree would have to change to become this one.
public IReadOnlyList<TextChange> GetChanges(CssSyntaxTree oldTree)GetDiagnostics overloads
GetDiagnostics()
CssSyntaxTree.csGets every diagnostic in the tree, in source order.
public override IReadOnlyList<Diagnostic> GetDiagnostics()Returns
GetDiagnostics(SyntaxNode)
CssSyntaxTree.csGets the diagnostics at or below node.
public override IEnumerable<Diagnostic> GetDiagnostics(SyntaxNode node)GetDiagnostics(SyntaxToken)
CssSyntaxTree.csGets the diagnostics on token and the trivia around it.
public override IEnumerable<Diagnostic> GetDiagnostics(SyntaxToken token)GetDiagnostics(SyntaxTrivia)
CssSyntaxTree.csGets the diagnostics on trivia.
public override IEnumerable<Diagnostic> GetDiagnostics(SyntaxTrivia trivia)GetRoot()
CssSyntaxTree.csGetText()
CssSyntaxTree.csIsEquivalentTo(CssSyntaxTree?)
CssSyntaxTree.csDetermines whether the two trees have the same structure and text.
public bool IsEquivalentTo(CssSyntaxTree? other)ParseText overloads
ParseText(SourceText, CssParseOptions?, string?)
CssSyntaxTree.csParses text.
public static CssSyntaxTree ParseText(SourceText text, CssParseOptions? options, string? path = null)Parameters
textSourceTextoptionsCssParseOptions?pathstring?
Returns
Exceptions
ArgumentNullExceptiontextisnull.
ParseText(SourceText, string?)
CssSyntaxTree.csParses text as a style sheet.
public static CssSyntaxTree ParseText(SourceText text, string? path = null)ParseText(string, CssParseOptions?, string?)
CssSyntaxTree.csParses text.
public static CssSyntaxTree ParseText(string text, CssParseOptions? options, string? path = null)Parameters
textstring- The CSS to read.
optionsCssParseOptions?- How to read it, or
nullforCssParseOptions.Default. pathstring?- Where the text came from, for the diagnostics to refer to.
Returns
Exceptions
ArgumentNullExceptiontextisnull.
ParseText(string, string?)
CssSyntaxTree.csParses text as a style sheet.
public static CssSyntaxTree ParseText(string text, string? path = null)WithChangedText(SourceText)
CssSyntaxTree.csReturns a tree over newText, parsed with the same options.
public CssSyntaxTree WithChangedText(SourceText newText)WithChanges overloads
WithChanges(TextChange[])
CssSyntaxTree.csReturns a tree over this text with changes applied.
public CssSyntaxTree WithChanges(params TextChange[] changes)WithChanges(IEnumerable<TextChange>)
CssSyntaxTree.csReturns a tree over this text with changes applied.
public CssSyntaxTree WithChanges(IEnumerable<TextChange> changes)WithRoot(CssStyleSheetSyntax)
CssSyntaxTree.csReturns a tree whose root is root.
public CssSyntaxTree WithRoot(CssStyleSheetSyntax root)