stringifier.js 8.0 KB

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