CssWriter Class

Writes the text of a style sheet, a rule or a declaration block. The default implementation is the CSSOM serialization (https://drafts.csswg.org/cssom/#serialize-a-css-rule); a derived class customizes the output by overriding the method of each part of a rule.

public class CssWriter

Remarks

A rule is written as its prelude (CssWriter.WritePrelude) followed by a semicolon (CssWriter.WriteStatementEnd) for the statement at-rules (@import, @namespace and the @layer statements), or by a block: CssWriter.WriteBlockStart, the declarations or descriptors (CssWriter.WriteDeclaration), the child rules and CssWriter.WriteBlockEnd. The nested declarations (CssNestedDeclarations) have no prelude and no block: only their declarations are written.

The default implementation of CssWriter.WritePrelude and CssWriter.WriteDeclaration writes the whitespace that precedes the rule or the declaration in the CSSOM serialization. An instance writes one style sheet, rule or declaration block: create a new writer for each call (see CssFormatter.CreateWriter).

Constructors

NameDescription
CssWriter(TextWriter)Creates a writer that emits CSS to the given output.

CssWriter(TextWriter)

CssWriter.cs

Creates a writer that emits CSS to the given output.

public CssWriter(TextWriter output)

Parameters

output TextWriter

Properties

NameDescription
DepthThe number of blocks, between the root and the rule or the declaration being written, whose start is written: 0 for the rules of a style sheet, the rule passed to CssWriter.Write and the declarations of a declaration block; the items of a block are at the depth of its rule plus 1.
OutputThe writer the text is written to.

The number of blocks, between the root and the rule or the declaration being written, whose start is written: 0 for the rules of a style sheet, the rule passed to CssWriter.Write and the declarations of a declaration block; the items of a block are at the depth of its rule plus 1.

protected int Depth { get; }

Property value

int

The writer the text is written to.

protected TextWriter Output { get; }

Property value

TextWriter

Methods

NameDescription
ShouldWrite(CssRule)Returns whether rule and its child rules are written. Returns true by default.
Write(CssRule)Writes rule, with its prelude, its declarations and its child rules (as the CssRule.CssText getter).
Write(CssStyleDeclaration)Writes the declarations of declarations (as the CssStyleDeclaration.CssText getter): nothing for a computed style.
Write(CssStyleSheet)Writes the rules of styleSheet.
WriteBlockEnd(CssRule)Writes the end of the block of a rule: }, on its own line for the rules that contain rules, preceded by a space for the rules that only contain declarations.
WriteBlockStart(CssRule)Writes the start of the block of a rule: a space and {.
WriteDeclaration(CssRule?, string, string, bool)Writes a declaration or a descriptor: name: value;, with !important before the semicolon when it is important.
WritePrelude(CssRule, string)Writes the prelude of a rule: the selectors of a style rule, the key text of a keyframe, the at-keyword and the prelude of an at-rule (e.g. @media screen). It is not called for the nested declarations.
WriteStatementEnd(CssRule)Writes the semicolon that ends a statement at-rule (@import, @namespace and the @layer statements).

ShouldWrite(CssRule)

CssWriter.cs

Returns whether rule and its child rules are written. Returns true by default.

protected virtual bool ShouldWrite(CssRule rule)

Parameters

rule CssRule

Returns

bool

Write overloads

Write(CssRule)

CssWriter.cs

Writes rule, with its prelude, its declarations and its child rules (as the CssRule.CssText getter).

public virtual void Write(CssRule rule)
Parameters
rule CssRule

Write(CssStyleDeclaration)

CssWriter.cs

Writes the declarations of declarations (as the CssStyleDeclaration.CssText getter): nothing for a computed style.

public virtual void Write(CssStyleDeclaration declarations)
Parameters
declarations CssStyleDeclaration

Write(CssStyleSheet)

CssWriter.cs

Writes the rules of styleSheet.

public virtual void Write(CssStyleSheet styleSheet)
Parameters
styleSheet CssStyleSheet
Exceptions
DomException
The style sheet is imported from another origin (SecurityError, see CssStyleSheet.CssRules).

WriteBlockEnd(CssRule)

CssWriter.cs

Writes the end of the block of a rule: }, on its own line for the rules that contain rules, preceded by a space for the rules that only contain declarations.

protected virtual void WriteBlockEnd(CssRule rule)

Parameters

rule CssRule

WriteBlockStart(CssRule)

CssWriter.cs

Writes the start of the block of a rule: a space and {.

protected virtual void WriteBlockStart(CssRule rule)

Parameters

rule CssRule

WriteDeclaration(CssRule?, string, string, bool)

CssWriter.cs

Writes a declaration or a descriptor: name: value;, with !important before the semicolon when it is important.

protected virtual void WriteDeclaration(CssRule? rule, string name, string value, bool important)

Parameters

rule CssRule?
The rule that contains the declaration, or null for the declarations of a style attribute.
name string
The serialized property name: a shorthand when the longhands are written together.
value string
The serialized value.
important bool
Whether the declaration is important.

WritePrelude(CssRule, string)

CssWriter.cs

Writes the prelude of a rule: the selectors of a style rule, the key text of a keyframe, the at-keyword and the prelude of an at-rule (e.g. @media screen). It is not called for the nested declarations.

protected virtual void WritePrelude(CssRule rule, string prelude)

Parameters

rule CssRule
The rule.
prelude string
The serialized prelude of the rule.

WriteStatementEnd(CssRule)

CssWriter.cs

Writes the semicolon that ends a statement at-rule (@import, @namespace and the @layer statements).

protected virtual void WriteStatementEnd(CssRule rule)

Parameters

rule CssRule