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.

353 lines
8.0 KiB

2 years ago
  1. 'use strict'
  2. const DEFAULT_RAW = {
  3. colon: ': ',
  4. indent: ' ',
  5. beforeDecl: '\n',
  6. beforeRule: '\n',
  7. beforeOpen: ' ',
  8. beforeClose: '\n',
  9. beforeComment: '\n',
  10. after: '\n',
  11. emptyBody: '',
  12. commentLeft: ' ',
  13. commentRight: ' ',
  14. semicolon: false
  15. }
  16. function capitalize(str) {
  17. return str[0].toUpperCase() + str.slice(1)
  18. }
  19. class Stringifier {
  20. constructor(builder) {
  21. this.builder = builder
  22. }
  23. stringify(node, semicolon) {
  24. /* c8 ignore start */
  25. if (!this[node.type]) {
  26. throw new Error(
  27. 'Unknown AST node type ' +
  28. node.type +
  29. '. ' +
  30. 'Maybe you need to change PostCSS stringifier.'
  31. )
  32. }
  33. /* c8 ignore stop */
  34. this[node.type](node, semicolon)
  35. }
  36. document(node) {
  37. this.body(node)
  38. }
  39. root(node) {
  40. this.body(node)
  41. if (node.raws.after) this.builder(node.raws.after)
  42. }
  43. comment(node) {
  44. let left = this.raw(node, 'left', 'commentLeft')
  45. let right = this.raw(node, 'right', 'commentRight')
  46. this.builder('/*' + left + node.text + right + '*/', node)
  47. }
  48. decl(node, semicolon) {
  49. let between = this.raw(node, 'between', 'colon')
  50. let string = node.prop + between + this.rawValue(node, 'value')
  51. if (node.important) {
  52. string += node.raws.important || ' !important'
  53. }
  54. if (semicolon) string += ';'
  55. this.builder(string, node)
  56. }
  57. rule(node) {
  58. this.block(node, this.rawValue(node, 'selector'))
  59. if (node.raws.ownSemicolon) {
  60. this.builder(node.raws.ownSemicolon, node, 'end')
  61. }
  62. }
  63. atrule(node, semicolon) {
  64. let name = '@' + node.name
  65. let params = node.params ? this.rawValue(node, 'params') : ''
  66. if (typeof node.raws.afterName !== 'undefined') {
  67. name += node.raws.afterName
  68. } else if (params) {
  69. name += ' '
  70. }
  71. if (node.nodes) {
  72. this.block(node, name + params)
  73. } else {
  74. let end = (node.raws.between || '') + (semicolon ? ';' : '')
  75. this.builder(name + params + end, node)
  76. }
  77. }
  78. body(node) {
  79. let last = node.nodes.length - 1
  80. while (last > 0) {
  81. if (node.nodes[last].type !== 'comment') break
  82. last -= 1
  83. }
  84. let semicolon = this.raw(node, 'semicolon')
  85. for (let i = 0; i < node.nodes.length; i++) {
  86. let child = node.nodes[i]
  87. let before = this.raw(child, 'before')
  88. if (before) this.builder(before)
  89. this.stringify(child, last !== i || semicolon)
  90. }
  91. }
  92. block(node, start) {
  93. let between = this.raw(node, 'between', 'beforeOpen')
  94. this.builder(start + between + '{', node, 'start')
  95. let after
  96. if (node.nodes && node.nodes.length) {
  97. this.body(node)
  98. after = this.raw(node, 'after')
  99. } else {
  100. after = this.raw(node, 'after', 'emptyBody')
  101. }
  102. if (after) this.builder(after)
  103. this.builder('}', node, 'end')
  104. }
  105. raw(node, own, detect) {
  106. let value
  107. if (!detect) detect = own
  108. // Already had
  109. if (own) {
  110. value = node.raws[own]
  111. if (typeof value !== 'undefined') return value
  112. }
  113. let parent = node.parent
  114. if (detect === 'before') {
  115. // Hack for first rule in CSS
  116. if (!parent || (parent.type === 'root' && parent.first === node)) {
  117. return ''
  118. }
  119. // `root` nodes in `document` should use only their own raws
  120. if (parent && parent.type === 'document') {
  121. return ''
  122. }
  123. }
  124. // Floating child without parent
  125. if (!parent) return DEFAULT_RAW[detect]
  126. // Detect style by other nodes
  127. let root = node.root()
  128. if (!root.rawCache) root.rawCache = {}
  129. if (typeof root.rawCache[detect] !== 'undefined') {
  130. return root.rawCache[detect]
  131. }
  132. if (detect === 'before' || detect === 'after') {
  133. return this.beforeAfter(node, detect)
  134. } else {
  135. let method = 'raw' + capitalize(detect)
  136. if (this[method]) {
  137. value = this[method](root, node)
  138. } else {
  139. root.walk(i => {
  140. value = i.raws[own]
  141. if (typeof value !== 'undefined') return false
  142. })
  143. }
  144. }
  145. if (typeof value === 'undefined') value = DEFAULT_RAW[detect]
  146. root.rawCache[detect] = value
  147. return value
  148. }
  149. rawSemicolon(root) {
  150. let value
  151. root.walk(i => {
  152. if (i.nodes && i.nodes.length && i.last.type === 'decl') {
  153. value = i.raws.semicolon
  154. if (typeof value !== 'undefined') return false
  155. }
  156. })
  157. return value
  158. }
  159. rawEmptyBody(root) {
  160. let value
  161. root.walk(i => {
  162. if (i.nodes && i.nodes.length === 0) {
  163. value = i.raws.after
  164. if (typeof value !== 'undefined') return false
  165. }
  166. })
  167. return value
  168. }
  169. rawIndent(root) {
  170. if (root.raws.indent) return root.raws.indent
  171. let value
  172. root.walk(i => {
  173. let p = i.parent
  174. if (p && p !== root && p.parent && p.parent === root) {
  175. if (typeof i.raws.before !== 'undefined') {
  176. let parts = i.raws.before.split('\n')
  177. value = parts[parts.length - 1]
  178. value = value.replace(/\S/g, '')
  179. return false
  180. }
  181. }
  182. })
  183. return value
  184. }
  185. rawBeforeComment(root, node) {
  186. let value
  187. root.walkComments(i => {
  188. if (typeof i.raws.before !== 'undefined') {
  189. value = i.raws.before
  190. if (value.includes('\n')) {
  191. value = value.replace(/[^\n]+$/, '')
  192. }
  193. return false
  194. }
  195. })
  196. if (typeof value === 'undefined') {
  197. value = this.raw(node, null, 'beforeDecl')
  198. } else if (value) {
  199. value = value.replace(/\S/g, '')
  200. }
  201. return value
  202. }
  203. rawBeforeDecl(root, node) {
  204. let value
  205. root.walkDecls(i => {
  206. if (typeof i.raws.before !== 'undefined') {
  207. value = i.raws.before
  208. if (value.includes('\n')) {
  209. value = value.replace(/[^\n]+$/, '')
  210. }
  211. return false
  212. }
  213. })
  214. if (typeof value === 'undefined') {
  215. value = this.raw(node, null, 'beforeRule')
  216. } else if (value) {
  217. value = value.replace(/\S/g, '')
  218. }
  219. return value
  220. }
  221. rawBeforeRule(root) {
  222. let value
  223. root.walk(i => {
  224. if (i.nodes && (i.parent !== root || root.first !== i)) {
  225. if (typeof i.raws.before !== 'undefined') {
  226. value = i.raws.before
  227. if (value.includes('\n')) {
  228. value = value.replace(/[^\n]+$/, '')
  229. }
  230. return false
  231. }
  232. }
  233. })
  234. if (value) value = value.replace(/\S/g, '')
  235. return value
  236. }
  237. rawBeforeClose(root) {
  238. let value
  239. root.walk(i => {
  240. if (i.nodes && i.nodes.length > 0) {
  241. if (typeof i.raws.after !== 'undefined') {
  242. value = i.raws.after
  243. if (value.includes('\n')) {
  244. value = value.replace(/[^\n]+$/, '')
  245. }
  246. return false
  247. }
  248. }
  249. })
  250. if (value) value = value.replace(/\S/g, '')
  251. return value
  252. }
  253. rawBeforeOpen(root) {
  254. let value
  255. root.walk(i => {
  256. if (i.type !== 'decl') {
  257. value = i.raws.between
  258. if (typeof value !== 'undefined') return false
  259. }
  260. })
  261. return value
  262. }
  263. rawColon(root) {
  264. let value
  265. root.walkDecls(i => {
  266. if (typeof i.raws.between !== 'undefined') {
  267. value = i.raws.between.replace(/[^\s:]/g, '')
  268. return false
  269. }
  270. })
  271. return value
  272. }
  273. beforeAfter(node, detect) {
  274. let value
  275. if (node.type === 'decl') {
  276. value = this.raw(node, null, 'beforeDecl')
  277. } else if (node.type === 'comment') {
  278. value = this.raw(node, null, 'beforeComment')
  279. } else if (detect === 'before') {
  280. value = this.raw(node, null, 'beforeRule')
  281. } else {
  282. value = this.raw(node, null, 'beforeClose')
  283. }
  284. let buf = node.parent
  285. let depth = 0
  286. while (buf && buf.type !== 'root') {
  287. depth += 1
  288. buf = buf.parent
  289. }
  290. if (value.includes('\n')) {
  291. let indent = this.raw(node, null, 'indent')
  292. if (indent.length) {
  293. for (let step = 0; step < depth; step++) value += indent
  294. }
  295. }
  296. return value
  297. }
  298. rawValue(node, prop) {
  299. let value = node[prop]
  300. let raw = node.raws[prop]
  301. if (raw && raw.value === value) {
  302. return raw.raw
  303. }
  304. return value
  305. }
  306. }
  307. module.exports = Stringifier
  308. Stringifier.default = Stringifier