parser.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. 'use strict'
  2. let AtRule = require('./at-rule')
  3. let Comment = require('./comment')
  4. let Declaration = require('./declaration')
  5. let Root = require('./root')
  6. let Rule = require('./rule')
  7. let tokenizer = require('./tokenize')
  8. const SAFE_COMMENT_NEIGHBOR = {
  9. empty: true,
  10. space: true
  11. }
  12. function findLastWithPosition(tokens) {
  13. for (let i = tokens.length - 1; i >= 0; i--) {
  14. let token = tokens[i]
  15. let pos = token[3] || token[2]
  16. if (pos) return pos
  17. }
  18. }
  19. class Parser {
  20. constructor(input) {
  21. this.input = input
  22. this.root = new Root()
  23. this.current = this.root
  24. this.spaces = ''
  25. this.semicolon = false
  26. this.createTokenizer()
  27. this.root.source = { input, start: { column: 1, line: 1, offset: 0 } }
  28. }
  29. atrule(token) {
  30. let node = new AtRule()
  31. node.name = token[1].slice(1)
  32. if (node.name === '') {
  33. this.unnamedAtrule(node, token)
  34. }
  35. this.init(node, token[2])
  36. let type
  37. let prev
  38. let shift
  39. let last = false
  40. let open = false
  41. let params = []
  42. let brackets = []
  43. while (!this.tokenizer.endOfFile()) {
  44. token = this.tokenizer.nextToken()
  45. type = token[0]
  46. if (type === '(' || type === '[') {
  47. brackets.push(type === '(' ? ')' : ']')
  48. } else if (type === '{' && brackets.length > 0) {
  49. brackets.push('}')
  50. } else if (type === brackets[brackets.length - 1]) {
  51. brackets.pop()
  52. }
  53. if (brackets.length === 0) {
  54. if (type === ';') {
  55. node.source.end = this.getPosition(token[2])
  56. node.source.end.offset++
  57. this.semicolon = true
  58. break
  59. } else if (type === '{') {
  60. open = true
  61. break
  62. } else if (type === '}') {
  63. if (params.length > 0) {
  64. shift = params.length - 1
  65. prev = params[shift]
  66. while (prev && prev[0] === 'space') {
  67. prev = params[--shift]
  68. }
  69. if (prev) {
  70. node.source.end = this.getPosition(prev[3] || prev[2])
  71. node.source.end.offset++
  72. }
  73. }
  74. this.end(token)
  75. break
  76. } else {
  77. params.push(token)
  78. }
  79. } else {
  80. params.push(token)
  81. }
  82. if (this.tokenizer.endOfFile()) {
  83. last = true
  84. break
  85. }
  86. }
  87. node.raws.between = this.spacesAndCommentsFromEnd(params)
  88. if (params.length) {
  89. node.raws.afterName = this.spacesAndCommentsFromStart(params)
  90. this.raw(node, 'params', params)
  91. if (last) {
  92. token = params[params.length - 1]
  93. node.source.end = this.getPosition(token[3] || token[2])
  94. node.source.end.offset++
  95. this.spaces = node.raws.between
  96. node.raws.between = ''
  97. }
  98. } else {
  99. node.raws.afterName = ''
  100. node.params = ''
  101. }
  102. if (open) {
  103. node.nodes = []
  104. this.current = node
  105. }
  106. }
  107. checkMissedSemicolon(tokens) {
  108. let colon = this.colon(tokens)
  109. if (colon === false) return
  110. let founded = 0
  111. let token
  112. for (let j = colon - 1; j >= 0; j--) {
  113. token = tokens[j]
  114. if (token[0] !== 'space') {
  115. founded += 1
  116. if (founded === 2) break
  117. }
  118. }
  119. // If the token is a word, e.g. `!important`, `red` or any other valid property's value.
  120. // Then we need to return the colon after that word token. [3] is the "end" colon of that word.
  121. // And because we need it after that one we do +1 to get the next one.
  122. throw this.input.error(
  123. 'Missed semicolon',
  124. token[0] === 'word' ? token[3] + 1 : token[2]
  125. )
  126. }
  127. colon(tokens) {
  128. let brackets = 0
  129. let prev, token, type
  130. for (let [i, element] of tokens.entries()) {
  131. token = element
  132. type = token[0]
  133. if (type === '(') {
  134. brackets += 1
  135. }
  136. if (type === ')') {
  137. brackets -= 1
  138. }
  139. if (brackets === 0 && type === ':') {
  140. if (!prev) {
  141. this.doubleColon(token)
  142. } else if (prev[0] === 'word' && prev[1] === 'progid') {
  143. continue
  144. } else {
  145. return i
  146. }
  147. }
  148. prev = token
  149. }
  150. return false
  151. }
  152. comment(token) {
  153. let node = new Comment()
  154. this.init(node, token[2])
  155. node.source.end = this.getPosition(token[3] || token[2])
  156. node.source.end.offset++
  157. let text = token[1].slice(2, -2)
  158. if (/^\s*$/.test(text)) {
  159. node.text = ''
  160. node.raws.left = text
  161. node.raws.right = ''
  162. } else {
  163. let match = text.match(/^(\s*)([^]*\S)(\s*)$/)
  164. node.text = match[2]
  165. node.raws.left = match[1]
  166. node.raws.right = match[3]
  167. }
  168. }
  169. createTokenizer() {
  170. this.tokenizer = tokenizer(this.input)
  171. }
  172. decl(tokens, customProperty) {
  173. let node = new Declaration()
  174. this.init(node, tokens[0][2])
  175. let last = tokens[tokens.length - 1]
  176. if (last[0] === ';') {
  177. this.semicolon = true
  178. tokens.pop()
  179. }
  180. node.source.end = this.getPosition(
  181. last[3] || last[2] || findLastWithPosition(tokens)
  182. )
  183. node.source.end.offset++
  184. while (tokens[0][0] !== 'word') {
  185. if (tokens.length === 1) this.unknownWord(tokens)
  186. node.raws.before += tokens.shift()[1]
  187. }
  188. node.source.start = this.getPosition(tokens[0][2])
  189. node.prop = ''
  190. while (tokens.length) {
  191. let type = tokens[0][0]
  192. if (type === ':' || type === 'space' || type === 'comment') {
  193. break
  194. }
  195. node.prop += tokens.shift()[1]
  196. }
  197. node.raws.between = ''
  198. let token
  199. while (tokens.length) {
  200. token = tokens.shift()
  201. if (token[0] === ':') {
  202. node.raws.between += token[1]
  203. break
  204. } else {
  205. if (token[0] === 'word' && /\w/.test(token[1])) {
  206. this.unknownWord([token])
  207. }
  208. node.raws.between += token[1]
  209. }
  210. }
  211. if (node.prop[0] === '_' || node.prop[0] === '*') {
  212. node.raws.before += node.prop[0]
  213. node.prop = node.prop.slice(1)
  214. }
  215. let firstSpaces = []
  216. let next
  217. while (tokens.length) {
  218. next = tokens[0][0]
  219. if (next !== 'space' && next !== 'comment') break
  220. firstSpaces.push(tokens.shift())
  221. }
  222. this.precheckMissedSemicolon(tokens)
  223. for (let i = tokens.length - 1; i >= 0; i--) {
  224. token = tokens[i]
  225. if (token[1].toLowerCase() === '!important') {
  226. node.important = true
  227. let string = this.stringFrom(tokens, i)
  228. string = this.spacesFromEnd(tokens) + string
  229. if (string !== ' !important') node.raws.important = string
  230. break
  231. } else if (token[1].toLowerCase() === 'important') {
  232. let cache = tokens.slice(0)
  233. let str = ''
  234. for (let j = i; j > 0; j--) {
  235. let type = cache[j][0]
  236. if (str.trim().startsWith('!') && type !== 'space') {
  237. break
  238. }
  239. str = cache.pop()[1] + str
  240. }
  241. if (str.trim().startsWith('!')) {
  242. node.important = true
  243. node.raws.important = str
  244. tokens = cache
  245. }
  246. }
  247. if (token[0] !== 'space' && token[0] !== 'comment') {
  248. break
  249. }
  250. }
  251. let hasWord = tokens.some(i => i[0] !== 'space' && i[0] !== 'comment')
  252. if (hasWord) {
  253. node.raws.between += firstSpaces.map(i => i[1]).join('')
  254. firstSpaces = []
  255. }
  256. this.raw(node, 'value', firstSpaces.concat(tokens), customProperty)
  257. if (node.value.includes(':') && !customProperty) {
  258. this.checkMissedSemicolon(tokens)
  259. }
  260. }
  261. doubleColon(token) {
  262. throw this.input.error(
  263. 'Double colon',
  264. { offset: token[2] },
  265. { offset: token[2] + token[1].length }
  266. )
  267. }
  268. emptyRule(token) {
  269. let node = new Rule()
  270. this.init(node, token[2])
  271. node.selector = ''
  272. node.raws.between = ''
  273. this.current = node
  274. }
  275. end(token) {
  276. if (this.current.nodes && this.current.nodes.length) {
  277. this.current.raws.semicolon = this.semicolon
  278. }
  279. this.semicolon = false
  280. this.current.raws.after = (this.current.raws.after || '') + this.spaces
  281. this.spaces = ''
  282. if (this.current.parent) {
  283. this.current.source.end = this.getPosition(token[2])
  284. this.current.source.end.offset++
  285. this.current = this.current.parent
  286. } else {
  287. this.unexpectedClose(token)
  288. }
  289. }
  290. endFile() {
  291. if (this.current.parent) this.unclosedBlock()
  292. if (this.current.nodes && this.current.nodes.length) {
  293. this.current.raws.semicolon = this.semicolon
  294. }
  295. this.current.raws.after = (this.current.raws.after || '') + this.spaces
  296. this.root.source.end = this.getPosition(this.tokenizer.position())
  297. }
  298. freeSemicolon(token) {
  299. this.spaces += token[1]
  300. if (this.current.nodes) {
  301. let prev = this.current.nodes[this.current.nodes.length - 1]
  302. if (prev && prev.type === 'rule' && !prev.raws.ownSemicolon) {
  303. prev.raws.ownSemicolon = this.spaces
  304. this.spaces = ''
  305. }
  306. }
  307. }
  308. // Helpers
  309. getPosition(offset) {
  310. let pos = this.input.fromOffset(offset)
  311. return {
  312. column: pos.col,
  313. line: pos.line,
  314. offset
  315. }
  316. }
  317. init(node, offset) {
  318. this.current.push(node)
  319. node.source = {
  320. input: this.input,
  321. start: this.getPosition(offset)
  322. }
  323. node.raws.before = this.spaces
  324. this.spaces = ''
  325. if (node.type !== 'comment') this.semicolon = false
  326. }
  327. other(start) {
  328. let end = false
  329. let type = null
  330. let colon = false
  331. let bracket = null
  332. let brackets = []
  333. let customProperty = start[1].startsWith('--')
  334. let tokens = []
  335. let token = start
  336. while (token) {
  337. type = token[0]
  338. tokens.push(token)
  339. if (type === '(' || type === '[') {
  340. if (!bracket) bracket = token
  341. brackets.push(type === '(' ? ')' : ']')
  342. } else if (customProperty && colon && type === '{') {
  343. if (!bracket) bracket = token
  344. brackets.push('}')
  345. } else if (brackets.length === 0) {
  346. if (type === ';') {
  347. if (colon) {
  348. this.decl(tokens, customProperty)
  349. return
  350. } else {
  351. break
  352. }
  353. } else if (type === '{') {
  354. this.rule(tokens)
  355. return
  356. } else if (type === '}') {
  357. this.tokenizer.back(tokens.pop())
  358. end = true
  359. break
  360. } else if (type === ':') {
  361. colon = true
  362. }
  363. } else if (type === brackets[brackets.length - 1]) {
  364. brackets.pop()
  365. if (brackets.length === 0) bracket = null
  366. }
  367. token = this.tokenizer.nextToken()
  368. }
  369. if (this.tokenizer.endOfFile()) end = true
  370. if (brackets.length > 0) this.unclosedBracket(bracket)
  371. if (end && colon) {
  372. if (!customProperty) {
  373. while (tokens.length) {
  374. token = tokens[tokens.length - 1][0]
  375. if (token !== 'space' && token !== 'comment') break
  376. this.tokenizer.back(tokens.pop())
  377. }
  378. }
  379. this.decl(tokens, customProperty)
  380. } else {
  381. this.unknownWord(tokens)
  382. }
  383. }
  384. parse() {
  385. let token
  386. while (!this.tokenizer.endOfFile()) {
  387. token = this.tokenizer.nextToken()
  388. switch (token[0]) {
  389. case 'space':
  390. this.spaces += token[1]
  391. break
  392. case ';':
  393. this.freeSemicolon(token)
  394. break
  395. case '}':
  396. this.end(token)
  397. break
  398. case 'comment':
  399. this.comment(token)
  400. break
  401. case 'at-word':
  402. this.atrule(token)
  403. break
  404. case '{':
  405. this.emptyRule(token)
  406. break
  407. default:
  408. this.other(token)
  409. break
  410. }
  411. }
  412. this.endFile()
  413. }
  414. precheckMissedSemicolon(/* tokens */) {
  415. // Hook for Safe Parser
  416. }
  417. raw(node, prop, tokens, customProperty) {
  418. let token, type
  419. let length = tokens.length
  420. let value = ''
  421. let clean = true
  422. let next, prev
  423. for (let i = 0; i < length; i += 1) {
  424. token = tokens[i]
  425. type = token[0]
  426. if (type === 'space' && i === length - 1 && !customProperty) {
  427. clean = false
  428. } else if (type === 'comment') {
  429. prev = tokens[i - 1] ? tokens[i - 1][0] : 'empty'
  430. next = tokens[i + 1] ? tokens[i + 1][0] : 'empty'
  431. if (!SAFE_COMMENT_NEIGHBOR[prev] && !SAFE_COMMENT_NEIGHBOR[next]) {
  432. if (value.slice(-1) === ',') {
  433. clean = false
  434. } else {
  435. value += token[1]
  436. }
  437. } else {
  438. clean = false
  439. }
  440. } else {
  441. value += token[1]
  442. }
  443. }
  444. if (!clean) {
  445. let raw = tokens.reduce((all, i) => all + i[1], '')
  446. node.raws[prop] = { raw, value }
  447. }
  448. node[prop] = value
  449. }
  450. rule(tokens) {
  451. tokens.pop()
  452. let node = new Rule()
  453. this.init(node, tokens[0][2])
  454. node.raws.between = this.spacesAndCommentsFromEnd(tokens)
  455. this.raw(node, 'selector', tokens)
  456. this.current = node
  457. }
  458. spacesAndCommentsFromEnd(tokens) {
  459. let lastTokenType
  460. let spaces = ''
  461. while (tokens.length) {
  462. lastTokenType = tokens[tokens.length - 1][0]
  463. if (lastTokenType !== 'space' && lastTokenType !== 'comment') break
  464. spaces = tokens.pop()[1] + spaces
  465. }
  466. return spaces
  467. }
  468. // Errors
  469. spacesAndCommentsFromStart(tokens) {
  470. let next
  471. let spaces = ''
  472. while (tokens.length) {
  473. next = tokens[0][0]
  474. if (next !== 'space' && next !== 'comment') break
  475. spaces += tokens.shift()[1]
  476. }
  477. return spaces
  478. }
  479. spacesFromEnd(tokens) {
  480. let lastTokenType
  481. let spaces = ''
  482. while (tokens.length) {
  483. lastTokenType = tokens[tokens.length - 1][0]
  484. if (lastTokenType !== 'space') break
  485. spaces = tokens.pop()[1] + spaces
  486. }
  487. return spaces
  488. }
  489. stringFrom(tokens, from) {
  490. let result = ''
  491. for (let i = from; i < tokens.length; i++) {
  492. result += tokens[i][1]
  493. }
  494. tokens.splice(from, tokens.length - from)
  495. return result
  496. }
  497. unclosedBlock() {
  498. let pos = this.current.source.start
  499. throw this.input.error('Unclosed block', pos.line, pos.column)
  500. }
  501. unclosedBracket(bracket) {
  502. throw this.input.error(
  503. 'Unclosed bracket',
  504. { offset: bracket[2] },
  505. { offset: bracket[2] + 1 }
  506. )
  507. }
  508. unexpectedClose(token) {
  509. throw this.input.error(
  510. 'Unexpected }',
  511. { offset: token[2] },
  512. { offset: token[2] + 1 }
  513. )
  514. }
  515. unknownWord(tokens) {
  516. throw this.input.error(
  517. 'Unknown word',
  518. { offset: tokens[0][2] },
  519. { offset: tokens[0][2] + tokens[0][1].length }
  520. )
  521. }
  522. unnamedAtrule(node, token) {
  523. throw this.input.error(
  524. 'At-rule without name',
  525. { offset: token[2] },
  526. { offset: token[2] + token[1].length }
  527. )
  528. }
  529. }
  530. module.exports = Parser