processor.js 21 KB

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