You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

177 lines
4.2 KiB

2 years ago
  1. declare namespace postcssValueParser {
  2. interface BaseNode {
  3. /**
  4. * The offset, inclusive, inside the CSS value at which the node starts.
  5. */
  6. sourceIndex: number;
  7. /**
  8. * The offset, exclusive, inside the CSS value at which the node ends.
  9. */
  10. sourceEndIndex: number;
  11. /**
  12. * The node's characteristic value
  13. */
  14. value: string;
  15. }
  16. interface ClosableNode {
  17. /**
  18. * Whether the parsed CSS value ended before the node was properly closed
  19. */
  20. unclosed?: true;
  21. }
  22. interface AdjacentAwareNode {
  23. /**
  24. * The token at the start of the node
  25. */
  26. before: string;
  27. /**
  28. * The token at the end of the node
  29. */
  30. after: string;
  31. }
  32. interface CommentNode extends BaseNode, ClosableNode {
  33. type: "comment";
  34. }
  35. interface DivNode extends BaseNode, AdjacentAwareNode {
  36. type: "div";
  37. }
  38. interface FunctionNode extends BaseNode, ClosableNode, AdjacentAwareNode {
  39. type: "function";
  40. /**
  41. * Nodes inside the function
  42. */
  43. nodes: Node[];
  44. }
  45. interface SpaceNode extends BaseNode {
  46. type: "space";
  47. }
  48. interface StringNode extends BaseNode, ClosableNode {
  49. type: "string";
  50. /**
  51. * The quote type delimiting the string
  52. */
  53. quote: '"' | "'";
  54. }
  55. interface UnicodeRangeNode extends BaseNode {
  56. type: "unicode-range";
  57. }
  58. interface WordNode extends BaseNode {
  59. type: "word";
  60. }
  61. /**
  62. * Any node parsed from a CSS value
  63. */
  64. type Node =
  65. | CommentNode
  66. | DivNode
  67. | FunctionNode
  68. | SpaceNode
  69. | StringNode
  70. | UnicodeRangeNode
  71. | WordNode;
  72. interface CustomStringifierCallback {
  73. /**
  74. * @param node The node to stringify
  75. * @returns The serialized CSS representation of the node
  76. */
  77. (nodes: Node): string | undefined;
  78. }
  79. interface WalkCallback {
  80. /**
  81. * @param node The currently visited node
  82. * @param index The index of the node in the series of parsed nodes
  83. * @param nodes The series of parsed nodes
  84. * @returns Returning `false` will prevent traversal of descendant nodes (only applies if `bubble` was set to `true` in the `walk()` call)
  85. */
  86. (node: Node, index: number, nodes: Node[]): void | boolean;
  87. }
  88. /**
  89. * A CSS dimension, decomposed into its numeric and unit parts
  90. */
  91. interface Dimension {
  92. number: string;
  93. unit: string;
  94. }
  95. /**
  96. * A wrapper around a parsed CSS value that allows for inspecting and walking nodes
  97. */
  98. interface ParsedValue {
  99. /**
  100. * The series of parsed nodes
  101. */
  102. nodes: Node[];
  103. /**
  104. * Walk all parsed nodes, applying a callback
  105. *
  106. * @param callback A visitor callback that will be executed for each node
  107. * @param bubble When set to `true`, walking will be done inside-out instead of outside-in
  108. */
  109. walk(callback: WalkCallback, bubble?: boolean): this;
  110. }
  111. interface ValueParser {
  112. /**
  113. * Decompose a CSS dimension into its numeric and unit part
  114. *
  115. * @param value The dimension to decompose
  116. * @returns An object representing `number` and `unit` part of the dimension or `false` if the decomposing fails
  117. */
  118. unit(value: string): Dimension | false;
  119. /**
  120. * Serialize a series of nodes into a CSS value
  121. *
  122. * @param nodes The nodes to stringify
  123. * @param custom A custom stringifier callback
  124. * @returns The generated CSS value
  125. */
  126. stringify(nodes: Node | Node[], custom?: CustomStringifierCallback): string;
  127. /**
  128. * Walk a series of nodes, applying a callback
  129. *
  130. * @param nodes The nodes to walk
  131. * @param callback A visitor callback that will be executed for each node
  132. * @param bubble When set to `true`, walking will be done inside-out instead of outside-in
  133. */
  134. walk(nodes: Node[], callback: WalkCallback, bubble?: boolean): void;
  135. /**
  136. * Parse a CSS value into a series of nodes to operate on
  137. *
  138. * @param value The value to parse
  139. */
  140. new (value: string): ParsedValue;
  141. /**
  142. * Parse a CSS value into a series of nodes to operate on
  143. *
  144. * @param value The value to parse
  145. */
  146. (value: string): ParsedValue;
  147. }
  148. }
  149. declare const postcssValueParser: postcssValueParser.ValueParser;
  150. export = postcssValueParser;