processor.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. let parser = require('postcss-value-parser')
  2. let Value = require('./value')
  3. let insertAreas = require('./hacks/grid-utils').insertAreas
  4. const OLD_LINEAR = /(^|[^-])linear-gradient\(\s*(top|left|right|bottom)/i
  5. const OLD_RADIAL = /(^|[^-])radial-gradient\(\s*\d+(\w*|%)\s+\d+(\w*|%)\s*,/i
  6. const IGNORE_NEXT = /(!\s*)?autoprefixer:\s*ignore\s+next/i
  7. const GRID_REGEX = /(!\s*)?autoprefixer\s*grid:\s*(on|off|(no-)?autoplace)/i
  8. const SIZES = [
  9. 'width',
  10. 'height',
  11. 'min-width',
  12. 'max-width',
  13. 'min-height',
  14. 'max-height',
  15. 'inline-size',
  16. 'min-inline-size',
  17. 'max-inline-size',
  18. 'block-size',
  19. 'min-block-size',
  20. 'max-block-size'
  21. ]
  22. function hasGridTemplate(decl) {
  23. return decl.parent.some(
  24. i => i.prop === 'grid-template' || i.prop === 'grid-template-areas'
  25. )
  26. }
  27. function hasRowsAndColumns(decl) {
  28. let hasRows = decl.parent.some(i => i.prop === 'grid-template-rows')
  29. let hasColumns = decl.parent.some(i => i.prop === 'grid-template-columns')
  30. return hasRows && hasColumns
  31. }
  32. class Processor {
  33. constructor(prefixes) {
  34. this.prefixes = prefixes
  35. }
  36. /**
  37. * Add necessary prefixes
  38. */
  39. add(css, result) {
  40. // At-rules
  41. let resolution = this.prefixes.add['@resolution']
  42. let keyframes = this.prefixes.add['@keyframes']
  43. let viewport = this.prefixes.add['@viewport']
  44. let supports = this.prefixes.add['@supports']
  45. css.walkAtRules(rule => {
  46. if (rule.name === 'keyframes') {
  47. if (!this.disabled(rule, result)) {
  48. return keyframes && keyframes.process(rule)
  49. }
  50. } else if (rule.name === 'viewport') {
  51. if (!this.disabled(rule, result)) {
  52. return viewport && viewport.process(rule)
  53. }
  54. } else if (rule.name === 'supports') {
  55. if (
  56. this.prefixes.options.supports !== false &&
  57. !this.disabled(rule, result)
  58. ) {
  59. return supports.process(rule)
  60. }
  61. } else if (rule.name === 'media' && rule.params.includes('-resolution')) {
  62. if (!this.disabled(rule, result)) {
  63. return resolution && resolution.process(rule)
  64. }
  65. }
  66. return undefined
  67. })
  68. // Selectors
  69. css.walkRules(rule => {
  70. if (this.disabled(rule, result)) return undefined
  71. return this.prefixes.add.selectors.map(selector => {
  72. return selector.process(rule, result)
  73. })
  74. })
  75. function insideGrid(decl) {
  76. return decl.parent.nodes.some(node => {
  77. if (node.type !== 'decl') return false
  78. let displayGrid =
  79. node.prop === 'display' && /(inline-)?grid/.test(node.value)
  80. let gridTemplate = node.prop.startsWith('grid-template')
  81. let gridGap = /^grid-([A-z]+-)?gap/.test(node.prop)
  82. return displayGrid || gridTemplate || gridGap
  83. })
  84. }
  85. let gridPrefixes =
  86. this.gridStatus(css, result) &&
  87. this.prefixes.add['grid-area'] &&
  88. this.prefixes.add['grid-area'].prefixes
  89. css.walkDecls(decl => {
  90. if (this.disabledDecl(decl, result)) return undefined
  91. let parent = decl.parent
  92. let prop = decl.prop
  93. let value = decl.value
  94. if (prop === 'color-adjust') {
  95. if (parent.every(i => i.prop !== 'print-color-adjust')) {
  96. result.warn(
  97. 'Replace color-adjust to print-color-adjust. ' +
  98. 'The color-adjust shorthand is currently deprecated.',
  99. { node: decl }
  100. )
  101. }
  102. } else if (prop === 'grid-row-span') {
  103. result.warn(
  104. 'grid-row-span is not part of final Grid Layout. Use grid-row.',
  105. { node: decl }
  106. )
  107. return undefined
  108. } else if (prop === 'grid-column-span') {
  109. result.warn(
  110. 'grid-column-span is not part of final Grid Layout. Use grid-column.',
  111. { node: decl }
  112. )
  113. return undefined
  114. } else if (prop === 'display' && value === 'box') {
  115. result.warn(
  116. 'You should write display: flex by final spec ' +
  117. 'instead of display: box',
  118. { node: decl }
  119. )
  120. return undefined
  121. } else if (prop === 'text-emphasis-position') {
  122. if (value === 'under' || value === 'over') {
  123. result.warn(
  124. 'You should use 2 values for text-emphasis-position ' +
  125. 'For example, `under left` instead of just `under`.',
  126. { node: decl }
  127. )
  128. }
  129. } else if (prop === 'text-decoration-skip' && value === 'ink') {
  130. result.warn(
  131. 'Replace text-decoration-skip: ink to ' +
  132. 'text-decoration-skip-ink: auto, because spec had been changed',
  133. { node: decl }
  134. )
  135. } else {
  136. if (gridPrefixes && this.gridStatus(decl, result)) {
  137. if (decl.value === 'subgrid') {
  138. result.warn('IE does not support subgrid', { node: decl })
  139. }
  140. if (/^(align|justify|place)-items$/.test(prop) && insideGrid(decl)) {
  141. let fixed = prop.replace('-items', '-self')
  142. result.warn(
  143. `IE does not support ${prop} on grid containers. ` +
  144. `Try using ${fixed} on child elements instead: ` +
  145. `${decl.parent.selector} > * { ${fixed}: ${decl.value} }`,
  146. { node: decl }
  147. )
  148. } else if (
  149. /^(align|justify|place)-content$/.test(prop) &&
  150. insideGrid(decl)
  151. ) {
  152. result.warn(`IE does not support ${decl.prop} on grid containers`, {
  153. node: decl
  154. })
  155. } else if (prop === 'display' && decl.value === 'contents') {
  156. result.warn(
  157. 'Please do not use display: contents; ' +
  158. 'if you have grid setting enabled',
  159. { node: decl }
  160. )
  161. return undefined
  162. } else if (decl.prop === 'grid-gap') {
  163. let status = this.gridStatus(decl, result)
  164. if (
  165. status === 'autoplace' &&
  166. !hasRowsAndColumns(decl) &&
  167. !hasGridTemplate(decl)
  168. ) {
  169. result.warn(
  170. 'grid-gap only works if grid-template(-areas) is being ' +
  171. 'used or both rows and columns have been declared ' +
  172. 'and cells have not been manually ' +
  173. 'placed inside the explicit grid',
  174. { node: decl }
  175. )
  176. } else if (
  177. (status === true || status === 'no-autoplace') &&
  178. !hasGridTemplate(decl)
  179. ) {
  180. result.warn(
  181. 'grid-gap only works if grid-template(-areas) is being used',
  182. { node: decl }
  183. )
  184. }
  185. } else if (prop === 'grid-auto-columns') {
  186. result.warn('grid-auto-columns is not supported by IE', {
  187. node: decl
  188. })
  189. return undefined
  190. } else if (prop === 'grid-auto-rows') {
  191. result.warn('grid-auto-rows is not supported by IE', { node: decl })
  192. return undefined
  193. } else if (prop === 'grid-auto-flow') {
  194. let hasRows = parent.some(i => i.prop === 'grid-template-rows')
  195. let hasCols = parent.some(i => i.prop === 'grid-template-columns')
  196. if (hasGridTemplate(decl)) {
  197. result.warn('grid-auto-flow is not supported by IE', {
  198. node: decl
  199. })
  200. } else if (value.includes('dense')) {
  201. result.warn('grid-auto-flow: dense is not supported by IE', {
  202. node: decl
  203. })
  204. } else if (!hasRows && !hasCols) {
  205. result.warn(
  206. 'grid-auto-flow works only if grid-template-rows and ' +
  207. 'grid-template-columns are present in the same rule',
  208. { node: decl }
  209. )
  210. }
  211. return undefined
  212. } else if (value.includes('auto-fit')) {
  213. result.warn('auto-fit value is not supported by IE', {
  214. node: decl,
  215. word: 'auto-fit'
  216. })
  217. return undefined
  218. } else if (value.includes('auto-fill')) {
  219. result.warn('auto-fill value is not supported by IE', {
  220. node: decl,
  221. word: 'auto-fill'
  222. })
  223. return undefined
  224. } else if (prop.startsWith('grid-template') && value.includes('[')) {
  225. result.warn(
  226. 'Autoprefixer currently does not support line names. ' +
  227. 'Try using grid-template-areas instead.',
  228. { node: decl, word: '[' }
  229. )
  230. }
  231. }
  232. if (value.includes('radial-gradient')) {
  233. if (OLD_RADIAL.test(decl.value)) {
  234. result.warn(
  235. 'Gradient has outdated direction syntax. ' +
  236. 'New syntax is like `closest-side at 0 0` ' +
  237. 'instead of `0 0, closest-side`.',
  238. { node: decl }
  239. )
  240. } else {
  241. let ast = parser(value)
  242. for (let i of ast.nodes) {
  243. if (i.type === 'function' && i.value === 'radial-gradient') {
  244. for (let word of i.nodes) {
  245. if (word.type === 'word') {
  246. if (word.value === 'cover') {
  247. result.warn(
  248. 'Gradient has outdated direction syntax. ' +
  249. 'Replace `cover` to `farthest-corner`.',
  250. { node: decl }
  251. )
  252. } else if (word.value === 'contain') {
  253. result.warn(
  254. 'Gradient has outdated direction syntax. ' +
  255. 'Replace `contain` to `closest-side`.',
  256. { node: decl }
  257. )
  258. }
  259. }
  260. }
  261. }
  262. }
  263. }
  264. }
  265. if (value.includes('linear-gradient')) {
  266. if (OLD_LINEAR.test(value)) {
  267. result.warn(
  268. 'Gradient has outdated direction syntax. ' +
  269. 'New syntax is like `to left` instead of `right`.',
  270. { node: decl }
  271. )
  272. }
  273. }
  274. }
  275. if (SIZES.includes(decl.prop)) {
  276. if (!decl.value.includes('-fill-available')) {
  277. if (decl.value.includes('fill-available')) {
  278. result.warn(
  279. 'Replace fill-available to stretch, ' +
  280. 'because spec had been changed',
  281. { node: decl }
  282. )
  283. } else if (decl.value.includes('fill')) {
  284. let ast = parser(value)
  285. if (ast.nodes.some(i => i.type === 'word' && i.value === 'fill')) {
  286. result.warn(
  287. 'Replace fill to stretch, because spec had been changed',
  288. { node: decl }
  289. )
  290. }
  291. }
  292. }
  293. }
  294. let prefixer
  295. if (decl.prop === 'transition' || decl.prop === 'transition-property') {
  296. // Transition
  297. return this.prefixes.transition.add(decl, result)
  298. } else if (decl.prop === 'align-self') {
  299. // align-self flexbox or grid
  300. let display = this.displayType(decl)
  301. if (display !== 'grid' && this.prefixes.options.flexbox !== false) {
  302. prefixer = this.prefixes.add['align-self']
  303. if (prefixer && prefixer.prefixes) {
  304. prefixer.process(decl)
  305. }
  306. }
  307. if (this.gridStatus(decl, result) !== false) {
  308. prefixer = this.prefixes.add['grid-row-align']
  309. if (prefixer && prefixer.prefixes) {
  310. return prefixer.process(decl, result)
  311. }
  312. }
  313. } else if (decl.prop === 'justify-self') {
  314. // justify-self flexbox or grid
  315. if (this.gridStatus(decl, result) !== false) {
  316. prefixer = this.prefixes.add['grid-column-align']
  317. if (prefixer && prefixer.prefixes) {
  318. return prefixer.process(decl, result)
  319. }
  320. }
  321. } else if (decl.prop === 'place-self') {
  322. prefixer = this.prefixes.add['place-self']
  323. if (
  324. prefixer &&
  325. prefixer.prefixes &&
  326. this.gridStatus(decl, result) !== false
  327. ) {
  328. return prefixer.process(decl, result)
  329. }
  330. } else {
  331. // Properties
  332. prefixer = this.prefixes.add[decl.prop]
  333. if (prefixer && prefixer.prefixes) {
  334. return prefixer.process(decl, result)
  335. }
  336. }
  337. return undefined
  338. })
  339. // Insert grid-area prefixes. We need to be able to store the different
  340. // rules as a data and hack API is not enough for this
  341. if (this.gridStatus(css, result)) {
  342. insertAreas(css, this.disabled)
  343. }
  344. // Values
  345. return css.walkDecls(decl => {
  346. if (this.disabledValue(decl, result)) return
  347. let unprefixed = this.prefixes.unprefixed(decl.prop)
  348. let list = this.prefixes.values('add', unprefixed)
  349. if (Array.isArray(list)) {
  350. for (let value of list) {
  351. if (value.process) value.process(decl, result)
  352. }
  353. }
  354. Value.save(this.prefixes, decl)
  355. })
  356. }
  357. /**
  358. * Check for control comment and global options
  359. */
  360. disabled(node, result) {
  361. if (!node) return false
  362. if (node._autoprefixerDisabled !== undefined) {
  363. return node._autoprefixerDisabled
  364. }
  365. if (node.parent) {
  366. let p = node.prev()
  367. if (p && p.type === 'comment' && IGNORE_NEXT.test(p.text)) {
  368. node._autoprefixerDisabled = true
  369. node._autoprefixerSelfDisabled = true
  370. return true
  371. }
  372. }
  373. let value = null
  374. if (node.nodes) {
  375. let status
  376. node.each(i => {
  377. if (i.type !== 'comment') return
  378. if (/(!\s*)?autoprefixer:\s*(off|on)/i.test(i.text)) {
  379. if (typeof status !== 'undefined') {
  380. result.warn(
  381. 'Second Autoprefixer control comment ' +
  382. 'was ignored. Autoprefixer applies control ' +
  383. 'comment to whole block, not to next rules.',
  384. { node: i }
  385. )
  386. } else {
  387. status = /on/i.test(i.text)
  388. }
  389. }
  390. })
  391. if (status !== undefined) {
  392. value = !status
  393. }
  394. }
  395. if (!node.nodes || value === null) {
  396. if (node.parent) {
  397. let isParentDisabled = this.disabled(node.parent, result)
  398. if (node.parent._autoprefixerSelfDisabled === true) {
  399. value = false
  400. } else {
  401. value = isParentDisabled
  402. }
  403. } else {
  404. value = false
  405. }
  406. }
  407. node._autoprefixerDisabled = value
  408. return value
  409. }
  410. /**
  411. * Check for grid/flexbox options.
  412. */
  413. disabledDecl(node, result) {
  414. if (node.type === 'decl' && this.gridStatus(node, result) === false) {
  415. if (node.prop.includes('grid') || node.prop === 'justify-items') {
  416. return true
  417. }
  418. }
  419. if (node.type === 'decl' && this.prefixes.options.flexbox === false) {
  420. let other = ['order', 'justify-content', 'align-items', 'align-content']
  421. if (node.prop.includes('flex') || other.includes(node.prop)) {
  422. return true
  423. }
  424. }
  425. return this.disabled(node, result)
  426. }
  427. /**
  428. * Check for grid/flexbox options.
  429. */
  430. disabledValue(node, result) {
  431. if (this.gridStatus(node, result) === false && node.type === 'decl') {
  432. if (node.prop === 'display' && node.value.includes('grid')) {
  433. return true
  434. }
  435. }
  436. if (this.prefixes.options.flexbox === false && node.type === 'decl') {
  437. if (node.prop === 'display' && node.value.includes('flex')) {
  438. return true
  439. }
  440. }
  441. if (node.type === 'decl' && node.prop === 'content') {
  442. return true
  443. }
  444. return this.disabled(node, result)
  445. }
  446. /**
  447. * Is it flebox or grid rule
  448. */
  449. displayType(decl) {
  450. for (let i of decl.parent.nodes) {
  451. if (i.prop !== 'display') {
  452. continue
  453. }
  454. if (i.value.includes('flex')) {
  455. return 'flex'
  456. }
  457. if (i.value.includes('grid')) {
  458. return 'grid'
  459. }
  460. }
  461. return false
  462. }
  463. /**
  464. * Set grid option via control comment
  465. */
  466. gridStatus(node, result) {
  467. if (!node) return false
  468. if (node._autoprefixerGridStatus !== undefined) {
  469. return node._autoprefixerGridStatus
  470. }
  471. let value = null
  472. if (node.nodes) {
  473. let status
  474. node.each(i => {
  475. if (i.type !== 'comment') return
  476. if (GRID_REGEX.test(i.text)) {
  477. let hasAutoplace = /:\s*autoplace/i.test(i.text)
  478. let noAutoplace = /no-autoplace/i.test(i.text)
  479. if (typeof status !== 'undefined') {
  480. result.warn(
  481. 'Second Autoprefixer grid control comment was ' +
  482. 'ignored. Autoprefixer applies control comments to the whole ' +
  483. 'block, not to the next rules.',
  484. { node: i }
  485. )
  486. } else if (hasAutoplace) {
  487. status = 'autoplace'
  488. } else if (noAutoplace) {
  489. status = true
  490. } else {
  491. status = /on/i.test(i.text)
  492. }
  493. }
  494. })
  495. if (status !== undefined) {
  496. value = status
  497. }
  498. }
  499. if (node.type === 'atrule' && node.name === 'supports') {
  500. let params = node.params
  501. if (params.includes('grid') && params.includes('auto')) {
  502. value = false
  503. }
  504. }
  505. if (!node.nodes || value === null) {
  506. if (node.parent) {
  507. let isParentGrid = this.gridStatus(node.parent, result)
  508. if (node.parent._autoprefixerSelfDisabled === true) {
  509. value = false
  510. } else {
  511. value = isParentGrid
  512. }
  513. } else if (typeof this.prefixes.options.grid !== 'undefined') {
  514. value = this.prefixes.options.grid
  515. } else if (typeof process.env.AUTOPREFIXER_GRID !== 'undefined') {
  516. if (process.env.AUTOPREFIXER_GRID === 'autoplace') {
  517. value = 'autoplace'
  518. } else {
  519. value = true
  520. }
  521. } else {
  522. value = false
  523. }
  524. }
  525. node._autoprefixerGridStatus = value
  526. return value
  527. }
  528. /**
  529. * Normalize spaces in cascade declaration group
  530. */
  531. reduceSpaces(decl) {
  532. let stop = false
  533. this.prefixes.group(decl).up(() => {
  534. stop = true
  535. return true
  536. })
  537. if (stop) {
  538. return
  539. }
  540. let parts = decl.raw('before').split('\n')
  541. let prevMin = parts[parts.length - 1].length
  542. let diff = false
  543. this.prefixes.group(decl).down(other => {
  544. parts = other.raw('before').split('\n')
  545. let last = parts.length - 1
  546. if (parts[last].length > prevMin) {
  547. if (diff === false) {
  548. diff = parts[last].length - prevMin
  549. }
  550. parts[last] = parts[last].slice(0, -diff)
  551. other.raws.before = parts.join('\n')
  552. }
  553. })
  554. }
  555. /**
  556. * Remove unnecessary pefixes
  557. */
  558. remove(css, result) {
  559. // At-rules
  560. let resolution = this.prefixes.remove['@resolution']
  561. css.walkAtRules((rule, i) => {
  562. if (this.prefixes.remove[`@${rule.name}`]) {
  563. if (!this.disabled(rule, result)) {
  564. rule.parent.removeChild(i)
  565. }
  566. } else if (
  567. rule.name === 'media' &&
  568. rule.params.includes('-resolution') &&
  569. resolution
  570. ) {
  571. resolution.clean(rule)
  572. }
  573. })
  574. // Selectors
  575. css.walkRules((rule, i) => {
  576. if (this.disabled(rule, result)) return
  577. for (let checker of this.prefixes.remove.selectors) {
  578. if (checker.check(rule)) {
  579. rule.parent.removeChild(i)
  580. return
  581. }
  582. }
  583. })
  584. return css.walkDecls((decl, i) => {
  585. if (this.disabled(decl, result)) return
  586. let rule = decl.parent
  587. let unprefixed = this.prefixes.unprefixed(decl.prop)
  588. // Transition
  589. if (decl.prop === 'transition' || decl.prop === 'transition-property') {
  590. this.prefixes.transition.remove(decl)
  591. }
  592. // Properties
  593. if (
  594. this.prefixes.remove[decl.prop] &&
  595. this.prefixes.remove[decl.prop].remove
  596. ) {
  597. let notHack = this.prefixes.group(decl).down(other => {
  598. return this.prefixes.normalize(other.prop) === unprefixed
  599. })
  600. if (unprefixed === 'flex-flow') {
  601. notHack = true
  602. }
  603. if (decl.prop === '-webkit-box-orient') {
  604. let hacks = { 'flex-direction': true, 'flex-flow': true }
  605. if (!decl.parent.some(j => hacks[j.prop])) return
  606. }
  607. if (notHack && !this.withHackValue(decl)) {
  608. if (decl.raw('before').includes('\n')) {
  609. this.reduceSpaces(decl)
  610. }
  611. rule.removeChild(i)
  612. return
  613. }
  614. }
  615. // Values
  616. for (let checker of this.prefixes.values('remove', unprefixed)) {
  617. if (!checker.check) continue
  618. if (!checker.check(decl.value)) continue
  619. unprefixed = checker.unprefixed
  620. let notHack = this.prefixes.group(decl).down(other => {
  621. return other.value.includes(unprefixed)
  622. })
  623. if (notHack) {
  624. rule.removeChild(i)
  625. return
  626. }
  627. }
  628. })
  629. }
  630. /**
  631. * Some rare old values, which is not in standard
  632. */
  633. withHackValue(decl) {
  634. return (
  635. (decl.prop === '-webkit-background-clip' && decl.value === 'text') ||
  636. // Do not remove -webkit-box-orient when -webkit-line-clamp is present.
  637. // https://github.com/postcss/autoprefixer/issues/1510
  638. (decl.prop === '-webkit-box-orient' &&
  639. decl.parent.some(d => d.prop === '-webkit-line-clamp'))
  640. )
  641. }
  642. }
  643. module.exports = Processor