2023-08-17 09:47:40 +00:00
|
|
|
let parser = require('postcss-value-parser')
|
|
|
|
|
|
|
|
let Value = require('./value')
|
|
|
|
let insertAreas = require('./hacks/grid-utils').insertAreas
|
|
|
|
|
|
|
|
const OLD_LINEAR = /(^|[^-])linear-gradient\(\s*(top|left|right|bottom)/i
|
|
|
|
const OLD_RADIAL = /(^|[^-])radial-gradient\(\s*\d+(\w*|%)\s+\d+(\w*|%)\s*,/i
|
|
|
|
const IGNORE_NEXT = /(!\s*)?autoprefixer:\s*ignore\s+next/i
|
|
|
|
const GRID_REGEX = /(!\s*)?autoprefixer\s*grid:\s*(on|off|(no-)?autoplace)/i
|
|
|
|
|
|
|
|
const SIZES = [
|
|
|
|
'width',
|
|
|
|
'height',
|
|
|
|
'min-width',
|
|
|
|
'max-width',
|
|
|
|
'min-height',
|
|
|
|
'max-height',
|
|
|
|
'inline-size',
|
|
|
|
'min-inline-size',
|
|
|
|
'max-inline-size',
|
|
|
|
'block-size',
|
|
|
|
'min-block-size',
|
|
|
|
'max-block-size'
|
|
|
|
]
|
2022-01-21 08:28:41 +00:00
|
|
|
|
|
|
|
function hasGridTemplate(decl) {
|
2023-08-17 09:47:40 +00:00
|
|
|
return decl.parent.some(
|
|
|
|
i => i.prop === 'grid-template' || i.prop === 'grid-template-areas'
|
|
|
|
)
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function hasRowsAndColumns(decl) {
|
2023-08-17 09:47:40 +00:00
|
|
|
let hasRows = decl.parent.some(i => i.prop === 'grid-template-rows')
|
|
|
|
let hasColumns = decl.parent.some(i => i.prop === 'grid-template-columns')
|
|
|
|
return hasRows && hasColumns
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
class Processor {
|
|
|
|
constructor(prefixes) {
|
|
|
|
this.prefixes = prefixes
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
2023-08-17 09:47:40 +00:00
|
|
|
|
2022-01-21 08:28:41 +00:00
|
|
|
/**
|
|
|
|
* Add necessary prefixes
|
|
|
|
*/
|
2023-08-17 09:47:40 +00:00
|
|
|
add(css, result) {
|
2022-01-21 08:28:41 +00:00
|
|
|
// At-rules
|
2023-08-17 09:47:40 +00:00
|
|
|
let resolution = this.prefixes.add['@resolution']
|
|
|
|
let keyframes = this.prefixes.add['@keyframes']
|
|
|
|
let viewport = this.prefixes.add['@viewport']
|
|
|
|
let supports = this.prefixes.add['@supports']
|
|
|
|
|
|
|
|
css.walkAtRules(rule => {
|
2022-01-21 08:28:41 +00:00
|
|
|
if (rule.name === 'keyframes') {
|
2023-08-17 09:47:40 +00:00
|
|
|
if (!this.disabled(rule, result)) {
|
|
|
|
return keyframes && keyframes.process(rule)
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
} else if (rule.name === 'viewport') {
|
2023-08-17 09:47:40 +00:00
|
|
|
if (!this.disabled(rule, result)) {
|
|
|
|
return viewport && viewport.process(rule)
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
} else if (rule.name === 'supports') {
|
2023-08-17 09:47:40 +00:00
|
|
|
if (
|
|
|
|
this.prefixes.options.supports !== false &&
|
|
|
|
!this.disabled(rule, result)
|
|
|
|
) {
|
|
|
|
return supports.process(rule)
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
} else if (rule.name === 'media' && rule.params.includes('-resolution')) {
|
2023-08-17 09:47:40 +00:00
|
|
|
if (!this.disabled(rule, result)) {
|
|
|
|
return resolution && resolution.process(rule)
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
return undefined
|
|
|
|
})
|
|
|
|
|
|
|
|
// Selectors
|
|
|
|
css.walkRules(rule => {
|
|
|
|
if (this.disabled(rule, result)) return undefined
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
return this.prefixes.add.selectors.map(selector => {
|
|
|
|
return selector.process(rule, result)
|
|
|
|
})
|
|
|
|
})
|
2022-01-21 08:28:41 +00:00
|
|
|
|
|
|
|
function insideGrid(decl) {
|
2023-08-17 09:47:40 +00:00
|
|
|
return decl.parent.nodes.some(node => {
|
|
|
|
if (node.type !== 'decl') return false
|
|
|
|
let displayGrid =
|
|
|
|
node.prop === 'display' && /(inline-)?grid/.test(node.value)
|
|
|
|
let gridTemplate = node.prop.startsWith('grid-template')
|
|
|
|
let gridGap = /^grid-([A-z]+-)?gap/.test(node.prop)
|
|
|
|
return displayGrid || gridTemplate || gridGap
|
|
|
|
})
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
function insideFlex(decl) {
|
2023-08-17 09:47:40 +00:00
|
|
|
return decl.parent.some(node => {
|
|
|
|
return node.prop === 'display' && /(inline-)?flex/.test(node.value)
|
|
|
|
})
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
let gridPrefixes =
|
|
|
|
this.gridStatus(css, result) &&
|
|
|
|
this.prefixes.add['grid-area'] &&
|
|
|
|
this.prefixes.add['grid-area'].prefixes
|
|
|
|
|
|
|
|
css.walkDecls(decl => {
|
|
|
|
if (this.disabledDecl(decl, result)) return undefined
|
|
|
|
|
|
|
|
let parent = decl.parent
|
|
|
|
let prop = decl.prop
|
|
|
|
let value = decl.value
|
|
|
|
|
|
|
|
if (prop === 'color-adjust') {
|
|
|
|
if (parent.every(i => i.prop !== 'print-color-adjust')) {
|
|
|
|
result.warn(
|
|
|
|
'Replace color-adjust to print-color-adjust. ' +
|
|
|
|
'The color-adjust shorthand is currently deprecated.',
|
|
|
|
{ node: decl }
|
|
|
|
)
|
|
|
|
}
|
|
|
|
} else if (prop === 'grid-row-span') {
|
|
|
|
result.warn(
|
|
|
|
'grid-row-span is not part of final Grid Layout. Use grid-row.',
|
|
|
|
{ node: decl }
|
|
|
|
)
|
|
|
|
return undefined
|
2022-01-21 08:28:41 +00:00
|
|
|
} else if (prop === 'grid-column-span') {
|
2023-08-17 09:47:40 +00:00
|
|
|
result.warn(
|
|
|
|
'grid-column-span is not part of final Grid Layout. Use grid-column.',
|
|
|
|
{ node: decl }
|
|
|
|
)
|
|
|
|
return undefined
|
2022-01-21 08:28:41 +00:00
|
|
|
} else if (prop === 'display' && value === 'box') {
|
2023-08-17 09:47:40 +00:00
|
|
|
result.warn(
|
|
|
|
'You should write display: flex by final spec ' +
|
|
|
|
'instead of display: box',
|
|
|
|
{ node: decl }
|
|
|
|
)
|
|
|
|
return undefined
|
2022-01-21 08:28:41 +00:00
|
|
|
} else if (prop === 'text-emphasis-position') {
|
|
|
|
if (value === 'under' || value === 'over') {
|
2023-08-17 09:47:40 +00:00
|
|
|
result.warn(
|
|
|
|
'You should use 2 values for text-emphasis-position ' +
|
|
|
|
'For example, `under left` instead of just `under`.',
|
|
|
|
{ node: decl }
|
|
|
|
)
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
2023-08-17 09:47:40 +00:00
|
|
|
} else if (
|
|
|
|
/^(align|justify|place)-(items|content)$/.test(prop) &&
|
|
|
|
insideFlex(decl)
|
|
|
|
) {
|
2022-01-21 08:28:41 +00:00
|
|
|
if (value === 'start' || value === 'end') {
|
2023-08-17 09:47:40 +00:00
|
|
|
result.warn(
|
|
|
|
`${value} value has mixed support, consider using ` +
|
|
|
|
`flex-${value} instead`,
|
|
|
|
{ node: decl }
|
|
|
|
)
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
} else if (prop === 'text-decoration-skip' && value === 'ink') {
|
2023-08-17 09:47:40 +00:00
|
|
|
result.warn(
|
|
|
|
'Replace text-decoration-skip: ink to ' +
|
|
|
|
'text-decoration-skip-ink: auto, because spec had been changed',
|
|
|
|
{ node: decl }
|
|
|
|
)
|
2022-01-21 08:28:41 +00:00
|
|
|
} else {
|
2023-08-17 09:47:40 +00:00
|
|
|
if (gridPrefixes && this.gridStatus(decl, result)) {
|
2022-01-21 08:28:41 +00:00
|
|
|
if (decl.value === 'subgrid') {
|
2023-08-17 09:47:40 +00:00
|
|
|
result.warn('IE does not support subgrid', { node: decl })
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
if (/^(align|justify|place)-items$/.test(prop) && insideGrid(decl)) {
|
2023-08-17 09:47:40 +00:00
|
|
|
let fixed = prop.replace('-items', '-self')
|
|
|
|
result.warn(
|
|
|
|
`IE does not support ${prop} on grid containers. ` +
|
|
|
|
`Try using ${fixed} on child elements instead: ` +
|
|
|
|
`${decl.parent.selector} > * { ${fixed}: ${decl.value} }`,
|
|
|
|
{ node: decl }
|
|
|
|
)
|
|
|
|
} else if (
|
|
|
|
/^(align|justify|place)-content$/.test(prop) &&
|
|
|
|
insideGrid(decl)
|
|
|
|
) {
|
|
|
|
result.warn(`IE does not support ${decl.prop} on grid containers`, {
|
2022-01-21 08:28:41 +00:00
|
|
|
node: decl
|
2023-08-17 09:47:40 +00:00
|
|
|
})
|
2022-01-21 08:28:41 +00:00
|
|
|
} else if (prop === 'display' && decl.value === 'contents') {
|
2023-08-17 09:47:40 +00:00
|
|
|
result.warn(
|
|
|
|
'Please do not use display: contents; ' +
|
|
|
|
'if you have grid setting enabled',
|
|
|
|
{ node: decl }
|
|
|
|
)
|
|
|
|
return undefined
|
2022-01-21 08:28:41 +00:00
|
|
|
} else if (decl.prop === 'grid-gap') {
|
2023-08-17 09:47:40 +00:00
|
|
|
let status = this.gridStatus(decl, result)
|
|
|
|
if (
|
|
|
|
status === 'autoplace' &&
|
|
|
|
!hasRowsAndColumns(decl) &&
|
|
|
|
!hasGridTemplate(decl)
|
|
|
|
) {
|
|
|
|
result.warn(
|
|
|
|
'grid-gap only works if grid-template(-areas) is being ' +
|
|
|
|
'used or both rows and columns have been declared ' +
|
|
|
|
'and cells have not been manually ' +
|
|
|
|
'placed inside the explicit grid',
|
|
|
|
{ node: decl }
|
|
|
|
)
|
|
|
|
} else if (
|
|
|
|
(status === true || status === 'no-autoplace') &&
|
|
|
|
!hasGridTemplate(decl)
|
|
|
|
) {
|
|
|
|
result.warn(
|
|
|
|
'grid-gap only works if grid-template(-areas) is being used',
|
|
|
|
{ node: decl }
|
|
|
|
)
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
} else if (prop === 'grid-auto-columns') {
|
|
|
|
result.warn('grid-auto-columns is not supported by IE', {
|
|
|
|
node: decl
|
2023-08-17 09:47:40 +00:00
|
|
|
})
|
|
|
|
return undefined
|
2022-01-21 08:28:41 +00:00
|
|
|
} else if (prop === 'grid-auto-rows') {
|
2023-08-17 09:47:40 +00:00
|
|
|
result.warn('grid-auto-rows is not supported by IE', { node: decl })
|
|
|
|
return undefined
|
2022-01-21 08:28:41 +00:00
|
|
|
} else if (prop === 'grid-auto-flow') {
|
2023-08-17 09:47:40 +00:00
|
|
|
let hasRows = parent.some(i => i.prop === 'grid-template-rows')
|
|
|
|
let hasCols = parent.some(i => i.prop === 'grid-template-columns')
|
2022-01-21 08:28:41 +00:00
|
|
|
|
|
|
|
if (hasGridTemplate(decl)) {
|
|
|
|
result.warn('grid-auto-flow is not supported by IE', {
|
|
|
|
node: decl
|
2023-08-17 09:47:40 +00:00
|
|
|
})
|
2022-01-21 08:28:41 +00:00
|
|
|
} else if (value.includes('dense')) {
|
|
|
|
result.warn('grid-auto-flow: dense is not supported by IE', {
|
|
|
|
node: decl
|
2023-08-17 09:47:40 +00:00
|
|
|
})
|
2022-01-21 08:28:41 +00:00
|
|
|
} else if (!hasRows && !hasCols) {
|
2023-08-17 09:47:40 +00:00
|
|
|
result.warn(
|
|
|
|
'grid-auto-flow works only if grid-template-rows and ' +
|
|
|
|
'grid-template-columns are present in the same rule',
|
|
|
|
{ node: decl }
|
|
|
|
)
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
2023-08-17 09:47:40 +00:00
|
|
|
return undefined
|
2022-01-21 08:28:41 +00:00
|
|
|
} else if (value.includes('auto-fit')) {
|
|
|
|
result.warn('auto-fit value is not supported by IE', {
|
|
|
|
node: decl,
|
|
|
|
word: 'auto-fit'
|
2023-08-17 09:47:40 +00:00
|
|
|
})
|
|
|
|
return undefined
|
2022-01-21 08:28:41 +00:00
|
|
|
} else if (value.includes('auto-fill')) {
|
|
|
|
result.warn('auto-fill value is not supported by IE', {
|
|
|
|
node: decl,
|
|
|
|
word: 'auto-fill'
|
2023-08-17 09:47:40 +00:00
|
|
|
})
|
|
|
|
return undefined
|
2022-01-21 08:28:41 +00:00
|
|
|
} else if (prop.startsWith('grid-template') && value.includes('[')) {
|
2023-08-17 09:47:40 +00:00
|
|
|
result.warn(
|
|
|
|
'Autoprefixer currently does not support line names. ' +
|
|
|
|
'Try using grid-template-areas instead.',
|
|
|
|
{ node: decl, word: '[' }
|
|
|
|
)
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (value.includes('radial-gradient')) {
|
|
|
|
if (OLD_RADIAL.test(decl.value)) {
|
2023-08-17 09:47:40 +00:00
|
|
|
result.warn(
|
|
|
|
'Gradient has outdated direction syntax. ' +
|
|
|
|
'New syntax is like `closest-side at 0 0` ' +
|
|
|
|
'instead of `0 0, closest-side`.',
|
|
|
|
{ node: decl }
|
|
|
|
)
|
2022-01-21 08:28:41 +00:00
|
|
|
} else {
|
2023-08-17 09:47:40 +00:00
|
|
|
let ast = parser(value)
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
for (let i of ast.nodes) {
|
2022-01-21 08:28:41 +00:00
|
|
|
if (i.type === 'function' && i.value === 'radial-gradient') {
|
2023-08-17 09:47:40 +00:00
|
|
|
for (let word of i.nodes) {
|
2022-01-21 08:28:41 +00:00
|
|
|
if (word.type === 'word') {
|
|
|
|
if (word.value === 'cover') {
|
2023-08-17 09:47:40 +00:00
|
|
|
result.warn(
|
|
|
|
'Gradient has outdated direction syntax. ' +
|
|
|
|
'Replace `cover` to `farthest-corner`.',
|
|
|
|
{ node: decl }
|
|
|
|
)
|
2022-01-21 08:28:41 +00:00
|
|
|
} else if (word.value === 'contain') {
|
2023-08-17 09:47:40 +00:00
|
|
|
result.warn(
|
|
|
|
'Gradient has outdated direction syntax. ' +
|
|
|
|
'Replace `contain` to `closest-side`.',
|
|
|
|
{ node: decl }
|
|
|
|
)
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (value.includes('linear-gradient')) {
|
|
|
|
if (OLD_LINEAR.test(value)) {
|
2023-08-17 09:47:40 +00:00
|
|
|
result.warn(
|
|
|
|
'Gradient has outdated direction syntax. ' +
|
|
|
|
'New syntax is like `to left` instead of `right`.',
|
|
|
|
{ node: decl }
|
|
|
|
)
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SIZES.includes(decl.prop)) {
|
|
|
|
if (!decl.value.includes('-fill-available')) {
|
|
|
|
if (decl.value.includes('fill-available')) {
|
2023-08-17 09:47:40 +00:00
|
|
|
result.warn(
|
|
|
|
'Replace fill-available to stretch, ' +
|
|
|
|
'because spec had been changed',
|
|
|
|
{ node: decl }
|
|
|
|
)
|
2022-01-21 08:28:41 +00:00
|
|
|
} else if (decl.value.includes('fill')) {
|
2023-08-17 09:47:40 +00:00
|
|
|
let ast = parser(value)
|
|
|
|
if (ast.nodes.some(i => i.type === 'word' && i.value === 'fill')) {
|
|
|
|
result.warn(
|
|
|
|
'Replace fill to stretch, because spec had been changed',
|
|
|
|
{ node: decl }
|
|
|
|
)
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
let prefixer
|
2022-01-21 08:28:41 +00:00
|
|
|
|
|
|
|
if (decl.prop === 'transition' || decl.prop === 'transition-property') {
|
|
|
|
// Transition
|
2023-08-17 09:47:40 +00:00
|
|
|
return this.prefixes.transition.add(decl, result)
|
2022-01-21 08:28:41 +00:00
|
|
|
} else if (decl.prop === 'align-self') {
|
|
|
|
// align-self flexbox or grid
|
2023-08-17 09:47:40 +00:00
|
|
|
let display = this.displayType(decl)
|
|
|
|
if (display !== 'grid' && this.prefixes.options.flexbox !== false) {
|
|
|
|
prefixer = this.prefixes.add['align-self']
|
2022-01-21 08:28:41 +00:00
|
|
|
if (prefixer && prefixer.prefixes) {
|
2023-08-17 09:47:40 +00:00
|
|
|
prefixer.process(decl)
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-17 09:47:40 +00:00
|
|
|
if (this.gridStatus(decl, result) !== false) {
|
|
|
|
prefixer = this.prefixes.add['grid-row-align']
|
2022-01-21 08:28:41 +00:00
|
|
|
if (prefixer && prefixer.prefixes) {
|
2023-08-17 09:47:40 +00:00
|
|
|
return prefixer.process(decl, result)
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (decl.prop === 'justify-self') {
|
|
|
|
// justify-self flexbox or grid
|
2023-08-17 09:47:40 +00:00
|
|
|
if (this.gridStatus(decl, result) !== false) {
|
|
|
|
prefixer = this.prefixes.add['grid-column-align']
|
2022-01-21 08:28:41 +00:00
|
|
|
if (prefixer && prefixer.prefixes) {
|
2023-08-17 09:47:40 +00:00
|
|
|
return prefixer.process(decl, result)
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (decl.prop === 'place-self') {
|
2023-08-17 09:47:40 +00:00
|
|
|
prefixer = this.prefixes.add['place-self']
|
|
|
|
if (
|
|
|
|
prefixer &&
|
|
|
|
prefixer.prefixes &&
|
|
|
|
this.gridStatus(decl, result) !== false
|
|
|
|
) {
|
|
|
|
return prefixer.process(decl, result)
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Properties
|
2023-08-17 09:47:40 +00:00
|
|
|
prefixer = this.prefixes.add[decl.prop]
|
2022-01-21 08:28:41 +00:00
|
|
|
if (prefixer && prefixer.prefixes) {
|
2023-08-17 09:47:40 +00:00
|
|
|
return prefixer.process(decl, result)
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
return undefined
|
|
|
|
})
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
// Insert grid-area prefixes. We need to be able to store the different
|
|
|
|
// rules as a data and hack API is not enough for this
|
2022-01-21 08:28:41 +00:00
|
|
|
if (this.gridStatus(css, result)) {
|
2023-08-17 09:47:40 +00:00
|
|
|
insertAreas(css, this.disabled)
|
|
|
|
}
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
// Values
|
|
|
|
return css.walkDecls(decl => {
|
|
|
|
if (this.disabledValue(decl, result)) return
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
let unprefixed = this.prefixes.unprefixed(decl.prop)
|
|
|
|
let list = this.prefixes.values('add', unprefixed)
|
2022-01-21 08:28:41 +00:00
|
|
|
if (Array.isArray(list)) {
|
2023-08-17 09:47:40 +00:00
|
|
|
for (let value of list) {
|
|
|
|
if (value.process) value.process(decl, result)
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-17 09:47:40 +00:00
|
|
|
Value.save(this.prefixes, decl)
|
|
|
|
})
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
2023-08-17 09:47:40 +00:00
|
|
|
|
2022-01-21 08:28:41 +00:00
|
|
|
/**
|
2023-08-17 09:47:40 +00:00
|
|
|
* Check for control comment and global options
|
2022-01-21 08:28:41 +00:00
|
|
|
*/
|
2023-08-17 09:47:40 +00:00
|
|
|
disabled(node, result) {
|
|
|
|
if (!node) return false
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
if (node._autoprefixerDisabled !== undefined) {
|
|
|
|
return node._autoprefixerDisabled
|
|
|
|
}
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
if (node.parent) {
|
|
|
|
let p = node.prev()
|
|
|
|
if (p && p.type === 'comment' && IGNORE_NEXT.test(p.text)) {
|
|
|
|
node._autoprefixerDisabled = true
|
|
|
|
node._autoprefixerSelfDisabled = true
|
|
|
|
return true
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
let value = null
|
|
|
|
if (node.nodes) {
|
|
|
|
let status
|
|
|
|
node.each(i => {
|
|
|
|
if (i.type !== 'comment') return
|
|
|
|
if (/(!\s*)?autoprefixer:\s*(off|on)/i.test(i.text)) {
|
|
|
|
if (typeof status !== 'undefined') {
|
|
|
|
result.warn(
|
|
|
|
'Second Autoprefixer control comment ' +
|
|
|
|
'was ignored. Autoprefixer applies control ' +
|
|
|
|
'comment to whole block, not to next rules.',
|
|
|
|
{ node: i }
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
status = /on/i.test(i.text)
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-17 09:47:40 +00:00
|
|
|
})
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
if (status !== undefined) {
|
|
|
|
value = !status
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!node.nodes || value === null) {
|
|
|
|
if (node.parent) {
|
|
|
|
let isParentDisabled = this.disabled(node.parent, result)
|
|
|
|
if (node.parent._autoprefixerSelfDisabled === true) {
|
|
|
|
value = false
|
|
|
|
} else {
|
|
|
|
value = isParentDisabled
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
2023-08-17 09:47:40 +00:00
|
|
|
} else {
|
|
|
|
value = false
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
2023-08-17 09:47:40 +00:00
|
|
|
}
|
|
|
|
node._autoprefixerDisabled = value
|
|
|
|
return value
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check for grid/flexbox options.
|
|
|
|
*/
|
2023-08-17 09:47:40 +00:00
|
|
|
disabledDecl(node, result) {
|
2022-01-21 08:28:41 +00:00
|
|
|
if (this.gridStatus(node, result) === false && node.type === 'decl') {
|
2023-08-17 09:47:40 +00:00
|
|
|
if (node.prop.includes('grid') || node.prop === 'justify-items') {
|
|
|
|
return true
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (this.prefixes.options.flexbox === false && node.type === 'decl') {
|
2023-08-17 09:47:40 +00:00
|
|
|
let other = ['order', 'justify-content', 'align-items', 'align-content']
|
|
|
|
if (node.prop.includes('flex') || other.includes(node.prop)) {
|
|
|
|
return true
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
return this.disabled(node, result)
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
2023-08-17 09:47:40 +00:00
|
|
|
|
2022-01-21 08:28:41 +00:00
|
|
|
/**
|
|
|
|
* Check for grid/flexbox options.
|
|
|
|
*/
|
2023-08-17 09:47:40 +00:00
|
|
|
disabledValue(node, result) {
|
2022-01-21 08:28:41 +00:00
|
|
|
if (this.gridStatus(node, result) === false && node.type === 'decl') {
|
2023-08-17 09:47:40 +00:00
|
|
|
if (node.prop === 'display' && node.value.includes('grid')) {
|
|
|
|
return true
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (this.prefixes.options.flexbox === false && node.type === 'decl') {
|
2023-08-17 09:47:40 +00:00
|
|
|
if (node.prop === 'display' && node.value.includes('flex')) {
|
|
|
|
return true
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-17 09:47:40 +00:00
|
|
|
if (node.type === 'decl' && node.prop === 'content') {
|
|
|
|
return true
|
|
|
|
}
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
return this.disabled(node, result)
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
2023-08-17 09:47:40 +00:00
|
|
|
|
2022-01-21 08:28:41 +00:00
|
|
|
/**
|
2023-08-17 09:47:40 +00:00
|
|
|
* Is it flebox or grid rule
|
2022-01-21 08:28:41 +00:00
|
|
|
*/
|
2023-08-17 09:47:40 +00:00
|
|
|
displayType(decl) {
|
|
|
|
for (let i of decl.parent.nodes) {
|
|
|
|
if (i.prop !== 'display') {
|
|
|
|
continue
|
|
|
|
}
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
if (i.value.includes('flex')) {
|
|
|
|
return 'flex'
|
|
|
|
}
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
if (i.value.includes('grid')) {
|
|
|
|
return 'grid'
|
|
|
|
}
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
return false
|
|
|
|
}
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
/**
|
|
|
|
* Set grid option via control comment
|
|
|
|
*/
|
|
|
|
gridStatus(node, result) {
|
|
|
|
if (!node) return false
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
if (node._autoprefixerGridStatus !== undefined) {
|
|
|
|
return node._autoprefixerGridStatus
|
|
|
|
}
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
let value = null
|
2022-01-21 08:28:41 +00:00
|
|
|
if (node.nodes) {
|
2023-08-17 09:47:40 +00:00
|
|
|
let status
|
|
|
|
node.each(i => {
|
|
|
|
if (i.type !== 'comment') return
|
|
|
|
if (GRID_REGEX.test(i.text)) {
|
|
|
|
let hasAutoplace = /:\s*autoplace/i.test(i.text)
|
|
|
|
let noAutoplace = /no-autoplace/i.test(i.text)
|
2022-01-21 08:28:41 +00:00
|
|
|
if (typeof status !== 'undefined') {
|
2023-08-17 09:47:40 +00:00
|
|
|
result.warn(
|
|
|
|
'Second Autoprefixer grid control comment was ' +
|
|
|
|
'ignored. Autoprefixer applies control comments to the whole ' +
|
|
|
|
'block, not to the next rules.',
|
|
|
|
{ node: i }
|
|
|
|
)
|
|
|
|
} else if (hasAutoplace) {
|
|
|
|
status = 'autoplace'
|
|
|
|
} else if (noAutoplace) {
|
|
|
|
status = true
|
2022-01-21 08:28:41 +00:00
|
|
|
} else {
|
2023-08-17 09:47:40 +00:00
|
|
|
status = /on/i.test(i.text)
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-17 09:47:40 +00:00
|
|
|
})
|
2022-01-21 08:28:41 +00:00
|
|
|
|
|
|
|
if (status !== undefined) {
|
2023-08-17 09:47:40 +00:00
|
|
|
value = status
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (node.type === 'atrule' && node.name === 'supports') {
|
|
|
|
let params = node.params
|
|
|
|
if (params.includes('grid') && params.includes('auto')) {
|
|
|
|
value = false
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!node.nodes || value === null) {
|
|
|
|
if (node.parent) {
|
2023-08-17 09:47:40 +00:00
|
|
|
let isParentGrid = this.gridStatus(node.parent, result)
|
2022-01-21 08:28:41 +00:00
|
|
|
if (node.parent._autoprefixerSelfDisabled === true) {
|
2023-08-17 09:47:40 +00:00
|
|
|
value = false
|
2022-01-21 08:28:41 +00:00
|
|
|
} else {
|
2023-08-17 09:47:40 +00:00
|
|
|
value = isParentGrid
|
|
|
|
}
|
|
|
|
} else if (typeof this.prefixes.options.grid !== 'undefined') {
|
|
|
|
value = this.prefixes.options.grid
|
|
|
|
} else if (typeof process.env.AUTOPREFIXER_GRID !== 'undefined') {
|
|
|
|
if (process.env.AUTOPREFIXER_GRID === 'autoplace') {
|
|
|
|
value = 'autoplace'
|
|
|
|
} else {
|
|
|
|
value = true
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
} else {
|
2023-08-17 09:47:40 +00:00
|
|
|
value = false
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
node._autoprefixerGridStatus = value
|
|
|
|
return value
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
2023-08-17 09:47:40 +00:00
|
|
|
|
2022-01-21 08:28:41 +00:00
|
|
|
/**
|
|
|
|
* Normalize spaces in cascade declaration group
|
|
|
|
*/
|
2023-08-17 09:47:40 +00:00
|
|
|
reduceSpaces(decl) {
|
|
|
|
let stop = false
|
|
|
|
this.prefixes.group(decl).up(() => {
|
|
|
|
stop = true
|
|
|
|
return true
|
|
|
|
})
|
2022-01-21 08:28:41 +00:00
|
|
|
if (stop) {
|
2023-08-17 09:47:40 +00:00
|
|
|
return
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
let parts = decl.raw('before').split('\n')
|
|
|
|
let prevMin = parts[parts.length - 1].length
|
|
|
|
let diff = false
|
|
|
|
|
|
|
|
this.prefixes.group(decl).down(other => {
|
|
|
|
parts = other.raw('before').split('\n')
|
|
|
|
let last = parts.length - 1
|
2022-01-21 08:28:41 +00:00
|
|
|
|
|
|
|
if (parts[last].length > prevMin) {
|
|
|
|
if (diff === false) {
|
2023-08-17 09:47:40 +00:00
|
|
|
diff = parts[last].length - prevMin
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
parts[last] = parts[last].slice(0, -diff)
|
|
|
|
other.raws.before = parts.join('\n')
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
2023-08-17 09:47:40 +00:00
|
|
|
})
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
2023-08-17 09:47:40 +00:00
|
|
|
|
2022-01-21 08:28:41 +00:00
|
|
|
/**
|
2023-08-17 09:47:40 +00:00
|
|
|
* Remove unnecessary pefixes
|
2022-01-21 08:28:41 +00:00
|
|
|
*/
|
2023-08-17 09:47:40 +00:00
|
|
|
remove(css, result) {
|
|
|
|
// At-rules
|
|
|
|
let resolution = this.prefixes.remove['@resolution']
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
css.walkAtRules((rule, i) => {
|
|
|
|
if (this.prefixes.remove[`@${rule.name}`]) {
|
|
|
|
if (!this.disabled(rule, result)) {
|
|
|
|
rule.parent.removeChild(i)
|
|
|
|
}
|
|
|
|
} else if (
|
|
|
|
rule.name === 'media' &&
|
|
|
|
rule.params.includes('-resolution') &&
|
|
|
|
resolution
|
|
|
|
) {
|
|
|
|
resolution.clean(rule)
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
2023-08-17 09:47:40 +00:00
|
|
|
})
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
// Selectors
|
|
|
|
for (let checker of this.prefixes.remove.selectors) {
|
|
|
|
css.walkRules((rule, i) => {
|
|
|
|
if (checker.check(rule)) {
|
|
|
|
if (!this.disabled(rule, result)) {
|
|
|
|
rule.parent.removeChild(i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
return css.walkDecls((decl, i) => {
|
|
|
|
if (this.disabled(decl, result)) return
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
let rule = decl.parent
|
|
|
|
let unprefixed = this.prefixes.unprefixed(decl.prop)
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
// Transition
|
|
|
|
if (decl.prop === 'transition' || decl.prop === 'transition-property') {
|
|
|
|
this.prefixes.transition.remove(decl)
|
|
|
|
}
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
// Properties
|
|
|
|
if (
|
|
|
|
this.prefixes.remove[decl.prop] &&
|
|
|
|
this.prefixes.remove[decl.prop].remove
|
|
|
|
) {
|
|
|
|
let notHack = this.prefixes.group(decl).down(other => {
|
|
|
|
return this.prefixes.normalize(other.prop) === unprefixed
|
|
|
|
})
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
if (unprefixed === 'flex-flow') {
|
|
|
|
notHack = true
|
|
|
|
}
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
if (decl.prop === '-webkit-box-orient') {
|
|
|
|
let hacks = { 'flex-direction': true, 'flex-flow': true }
|
|
|
|
if (!decl.parent.some(j => hacks[j.prop])) return
|
|
|
|
}
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
if (notHack && !this.withHackValue(decl)) {
|
|
|
|
if (decl.raw('before').includes('\n')) {
|
|
|
|
this.reduceSpaces(decl)
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
2023-08-17 09:47:40 +00:00
|
|
|
rule.removeChild(i)
|
|
|
|
return
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
// Values
|
|
|
|
for (let checker of this.prefixes.values('remove', unprefixed)) {
|
|
|
|
if (!checker.check) continue
|
|
|
|
if (!checker.check(decl.value)) continue
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
unprefixed = checker.unprefixed
|
|
|
|
let notHack = this.prefixes.group(decl).down(other => {
|
|
|
|
return other.value.includes(unprefixed)
|
|
|
|
})
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
if (notHack) {
|
|
|
|
rule.removeChild(i)
|
|
|
|
return
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-17 09:47:40 +00:00
|
|
|
})
|
|
|
|
}
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
/**
|
|
|
|
* Some rare old values, which is not in standard
|
|
|
|
*/
|
|
|
|
withHackValue(decl) {
|
|
|
|
return decl.prop === '-webkit-background-clip' && decl.value === 'text'
|
|
|
|
}
|
|
|
|
}
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
module.exports = Processor
|