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.

124 lines
3.0 KiB

2 years ago
  1. import Container from './container.js'
  2. import Node from './node.js'
  3. interface DeclarationRaws extends Record<string, unknown> {
  4. /**
  5. * The space symbols before the node. It also stores `*`
  6. * and `_` symbols before the declaration (IE hack).
  7. */
  8. before?: string
  9. /**
  10. * The symbols between the property and value for declarations.
  11. */
  12. between?: string
  13. /**
  14. * The content of the important statement, if it is not just `!important`.
  15. */
  16. important?: string
  17. /**
  18. * Declaration value with comments.
  19. */
  20. value?: {
  21. value: string
  22. raw: string
  23. }
  24. }
  25. export interface DeclarationProps {
  26. /** Name of the declaration. */
  27. prop: string
  28. /** Value of the declaration. */
  29. value: string
  30. /** Whether the declaration has an `!important` annotation. */
  31. important?: boolean
  32. /** Information used to generate byte-to-byte equal node string as it was in the origin input. */
  33. raws?: DeclarationRaws
  34. }
  35. /**
  36. * Represents a CSS declaration.
  37. *
  38. * ```js
  39. * Once (root, { Declaration }) {
  40. * let color = new Declaration({ prop: 'color', value: 'black' })
  41. * root.append(color)
  42. * }
  43. * ```
  44. *
  45. * ```js
  46. * const root = postcss.parse('a { color: black }')
  47. * const decl = root.first.first
  48. * decl.type //=> 'decl'
  49. * decl.toString() //=> ' color: black'
  50. * ```
  51. */
  52. export default class Declaration extends Node {
  53. type: 'decl'
  54. parent: Container | undefined
  55. raws: DeclarationRaws
  56. /**
  57. * The declaration's property name.
  58. *
  59. * ```js
  60. * const root = postcss.parse('a { color: black }')
  61. * const decl = root.first.first
  62. * decl.prop //=> 'color'
  63. * ```
  64. */
  65. prop: string
  66. /**
  67. * The declarations value.
  68. *
  69. * This value will be cleaned of comments. If the source value contained
  70. * comments, those comments will be available in the `raws` property.
  71. * If you have not changed the value, the result of `decl.toString()`
  72. * will include the original raws value (comments and all).
  73. *
  74. * ```js
  75. * const root = postcss.parse('a { color: black }')
  76. * const decl = root.first.first
  77. * decl.value //=> 'black'
  78. * ```
  79. */
  80. value: string
  81. /**
  82. * `true` if the declaration has an `!important` annotation.
  83. *
  84. * ```js
  85. * const root = postcss.parse('a { color: black !important; color: red }')
  86. * root.first.first.important //=> true
  87. * root.first.last.important //=> undefined
  88. * ```
  89. */
  90. important: boolean
  91. /**
  92. * `true` if declaration is declaration of CSS Custom Property
  93. * or Sass variable.
  94. *
  95. * ```js
  96. * const root = postcss.parse(':root { --one: 1 }')
  97. * let one = root.first.first
  98. * one.variable //=> true
  99. * ```
  100. *
  101. * ```js
  102. * const root = postcss.parse('$one: 1')
  103. * let one = root.first
  104. * one.variable //=> true
  105. * ```
  106. */
  107. variable: boolean
  108. constructor(defaults?: DeclarationProps)
  109. assign(overrides: object | DeclarationProps): this
  110. clone(overrides?: Partial<DeclarationProps>): this
  111. cloneBefore(overrides?: Partial<DeclarationProps>): this
  112. cloneAfter(overrides?: Partial<DeclarationProps>): this
  113. }