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 CssWriterRemarks
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
| Name | Description |
|---|---|
| CssWriter(TextWriter) | Creates a writer that emits CSS to the given output. |
CssWriter(TextWriter)
CssWriter.csCreates a writer that emits CSS to the given output.
public CssWriter(TextWriter output)Parameters
outputTextWriter
Properties
| Name | Description |
|---|---|
| Depth | 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. |
| Output | The writer the text is written to. |
Depth
CssWriter.csThe 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
Output
CssWriter.csMethods
| Name | Description |
|---|---|
| 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.csWrite overloads
Write(CssRule)
CssWriter.csWrites rule, with its prelude, its declarations and its child rules (as the CssRule.CssText getter).
Parameters
ruleCssRule
Write(CssStyleDeclaration)
CssWriter.csWrites the declarations of declarations (as the CssStyleDeclaration.CssText getter): nothing for a computed style.
public virtual void Write(CssStyleDeclaration declarations)Parameters
declarationsCssStyleDeclaration
Write(CssStyleSheet)
CssWriter.csWrites the rules of styleSheet.
public virtual void Write(CssStyleSheet styleSheet)Parameters
styleSheetCssStyleSheet
Exceptions
DomException- The style sheet is imported from another origin (SecurityError, see
CssStyleSheet.CssRules).
WriteBlockEnd(CssRule)
CssWriter.csWriteBlockStart(CssRule)
CssWriter.csWriteDeclaration(CssRule?, string, string, bool)
CssWriter.csWrites a declaration or a descriptor: name: value;, with !important before the semicolon when it is important.
WritePrelude(CssRule, string)
CssWriter.csWrites 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.