scss-parser.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. let { Comment } = require('postcss')
  2. let Parser = require('postcss/lib/parser')
  3. let NestedDeclaration = require('./nested-declaration')
  4. let scssTokenizer = require('./scss-tokenize')
  5. class ScssParser extends Parser {
  6. createTokenizer () {
  7. this.tokenizer = scssTokenizer(this.input)
  8. }
  9. rule (tokens) {
  10. let withColon = false
  11. let brackets = 0
  12. let value = ''
  13. for (let i of tokens) {
  14. if (withColon) {
  15. if (i[0] !== 'comment' && i[0] !== '{') {
  16. value += i[1]
  17. }
  18. } else if (i[0] === 'space' && i[1].includes('\n')) {
  19. break
  20. } else if (i[0] === '(') {
  21. brackets += 1
  22. } else if (i[0] === ')') {
  23. brackets -= 1
  24. } else if (brackets === 0 && i[0] === ':') {
  25. withColon = true
  26. }
  27. }
  28. if (!withColon || value.trim() === '' || /^[#:A-Za-z-]/.test(value)) {
  29. super.rule(tokens)
  30. } else {
  31. tokens.pop()
  32. let node = new NestedDeclaration()
  33. this.init(node, tokens[0][2])
  34. let last
  35. for (let i = tokens.length - 1; i >= 0; i--) {
  36. if (tokens[i][0] !== 'space') {
  37. last = tokens[i]
  38. break
  39. }
  40. }
  41. if (last[3]) {
  42. let pos = this.input.fromOffset(last[3])
  43. node.source.end = { offset: last[3], line: pos.line, column: pos.col }
  44. } else {
  45. let pos = this.input.fromOffset(last[2])
  46. node.source.end = { offset: last[2], line: pos.line, column: pos.col }
  47. }
  48. while (tokens[0][0] !== 'word') {
  49. node.raws.before += tokens.shift()[1]
  50. }
  51. node.source.start = { line: tokens[0][2], column: tokens[0][3] }
  52. node.prop = ''
  53. while (tokens.length) {
  54. let type = tokens[0][0]
  55. if (type === ':' || type === 'space' || type === 'comment') {
  56. break
  57. }
  58. node.prop += tokens.shift()[1]
  59. }
  60. node.raws.between = ''
  61. let token
  62. while (tokens.length) {
  63. token = tokens.shift()
  64. if (token[0] === ':') {
  65. node.raws.between += token[1]
  66. break
  67. } else {
  68. node.raws.between += token[1]
  69. }
  70. }
  71. if (node.prop[0] === '_' || node.prop[0] === '*') {
  72. node.raws.before += node.prop[0]
  73. node.prop = node.prop.slice(1)
  74. }
  75. node.raws.between += this.spacesAndCommentsFromStart(tokens)
  76. this.precheckMissedSemicolon(tokens)
  77. for (let i = tokens.length - 1; i > 0; i--) {
  78. token = tokens[i]
  79. if (token[1] === '!important') {
  80. node.important = true
  81. let string = this.stringFrom(tokens, i)
  82. string = this.spacesFromEnd(tokens) + string
  83. if (string !== ' !important') {
  84. node.raws.important = string
  85. }
  86. break
  87. } else if (token[1] === 'important') {
  88. let cache = tokens.slice(0)
  89. let str = ''
  90. for (let j = i; j > 0; j--) {
  91. let type = cache[j][0]
  92. if (str.trim().indexOf('!') === 0 && type !== 'space') {
  93. break
  94. }
  95. str = cache.pop()[1] + str
  96. }
  97. if (str.trim().indexOf('!') === 0) {
  98. node.important = true
  99. node.raws.important = str
  100. tokens = cache
  101. }
  102. }
  103. if (token[0] !== 'space' && token[0] !== 'comment') {
  104. break
  105. }
  106. }
  107. this.raw(node, 'value', tokens)
  108. if (node.value.includes(':')) {
  109. this.checkMissedSemicolon(tokens)
  110. }
  111. this.current = node
  112. }
  113. }
  114. comment (token) {
  115. if (token[4] === 'inline') {
  116. let node = new Comment()
  117. this.init(node, token[2])
  118. node.raws.inline = true
  119. let pos = this.input.fromOffset(token[3])
  120. node.source.end = { offset: token[3], line: pos.line, column: pos.col }
  121. let text = token[1].slice(2)
  122. if (/^\s*$/.test(text)) {
  123. node.text = ''
  124. node.raws.left = text
  125. node.raws.right = ''
  126. } else {
  127. let match = text.match(/^(\s*)([^]*\S)(\s*)$/)
  128. let fixed = match[2].replace(/(\*\/|\/\*)/g, '*//*')
  129. node.text = fixed
  130. node.raws.left = match[1]
  131. node.raws.right = match[3]
  132. node.raws.text = match[2]
  133. }
  134. } else {
  135. super.comment(token)
  136. }
  137. }
  138. raw (node, prop, tokens) {
  139. super.raw(node, prop, tokens)
  140. if (node.raws[prop]) {
  141. let scss = node.raws[prop].raw
  142. node.raws[prop].raw = tokens.reduce((all, i) => {
  143. if (i[0] === 'comment' && i[4] === 'inline') {
  144. let text = i[1].slice(2).replace(/(\*\/|\/\*)/g, '*//*')
  145. return all + '/*' + text + '*/'
  146. } else {
  147. return all + i[1]
  148. }
  149. }, '')
  150. if (scss !== node.raws[prop].raw) {
  151. node.raws[prop].scss = scss
  152. }
  153. }
  154. }
  155. }
  156. module.exports = ScssParser