wishthis/node_modules/autoprefixer/lib/prefixes.js

429 lines
13 KiB
JavaScript
Raw Normal View History

2023-08-17 09:47:40 +00:00
let vendor = require('./vendor')
let Declaration = require('./declaration')
let Resolution = require('./resolution')
let Transition = require('./transition')
let Processor = require('./processor')
let Supports = require('./supports')
let Browsers = require('./browsers')
let Selector = require('./selector')
let AtRule = require('./at-rule')
let Value = require('./value')
let utils = require('./utils')
let hackFullscreen = require('./hacks/fullscreen')
let hackPlaceholder = require('./hacks/placeholder')
let hackPlaceholderShown = require('./hacks/placeholder-shown')
let hackFileSelectorButton = require('./hacks/file-selector-button')
let hackFlex = require('./hacks/flex')
let hackOrder = require('./hacks/order')
let hackFilter = require('./hacks/filter')
let hackGridEnd = require('./hacks/grid-end')
let hackAnimation = require('./hacks/animation')
let hackFlexFlow = require('./hacks/flex-flow')
let hackFlexGrow = require('./hacks/flex-grow')
let hackFlexWrap = require('./hacks/flex-wrap')
let hackGridArea = require('./hacks/grid-area')
let hackPlaceSelf = require('./hacks/place-self')
let hackGridStart = require('./hacks/grid-start')
let hackAlignSelf = require('./hacks/align-self')
let hackAppearance = require('./hacks/appearance')
let hackFlexBasis = require('./hacks/flex-basis')
let hackMaskBorder = require('./hacks/mask-border')
let hackMaskComposite = require('./hacks/mask-composite')
let hackAlignItems = require('./hacks/align-items')
let hackUserSelect = require('./hacks/user-select')
let hackFlexShrink = require('./hacks/flex-shrink')
let hackBreakProps = require('./hacks/break-props')
let hackWritingMode = require('./hacks/writing-mode')
let hackBorderImage = require('./hacks/border-image')
let hackAlignContent = require('./hacks/align-content')
let hackBorderRadius = require('./hacks/border-radius')
let hackBlockLogical = require('./hacks/block-logical')
let hackGridTemplate = require('./hacks/grid-template')
let hackInlineLogical = require('./hacks/inline-logical')
let hackGridRowAlign = require('./hacks/grid-row-align')
let hackTransformDecl = require('./hacks/transform-decl')
let hackFlexDirection = require('./hacks/flex-direction')
let hackImageRendering = require('./hacks/image-rendering')
let hackBackdropFilter = require('./hacks/backdrop-filter')
let hackBackgroundClip = require('./hacks/background-clip')
let hackTextDecoration = require('./hacks/text-decoration')
let hackJustifyContent = require('./hacks/justify-content')
let hackBackgroundSize = require('./hacks/background-size')
let hackGridRowColumn = require('./hacks/grid-row-column')
let hackGridRowsColumns = require('./hacks/grid-rows-columns')
let hackGridColumnAlign = require('./hacks/grid-column-align')
let hackPrintColorAdjust = require('./hacks/print-color-adjust')
let hackOverscrollBehavior = require('./hacks/overscroll-behavior')
let hackGridTemplateAreas = require('./hacks/grid-template-areas')
let hackTextEmphasisPosition = require('./hacks/text-emphasis-position')
let hackTextDecorationSkipInk = require('./hacks/text-decoration-skip-ink')
let hackGradient = require('./hacks/gradient')
let hackIntrinsic = require('./hacks/intrinsic')
let hackPixelated = require('./hacks/pixelated')
let hackImageSet = require('./hacks/image-set')
let hackCrossFade = require('./hacks/cross-fade')
let hackDisplayFlex = require('./hacks/display-flex')
let hackDisplayGrid = require('./hacks/display-grid')
let hackFilterValue = require('./hacks/filter-value')
let hackAutofill = require('./hacks/autofill')
Selector.hack(hackAutofill)
Selector.hack(hackFullscreen)
Selector.hack(hackPlaceholder)
Selector.hack(hackPlaceholderShown)
Selector.hack(hackFileSelectorButton)
Declaration.hack(hackFlex)
Declaration.hack(hackOrder)
Declaration.hack(hackFilter)
Declaration.hack(hackGridEnd)
Declaration.hack(hackAnimation)
Declaration.hack(hackFlexFlow)
Declaration.hack(hackFlexGrow)
Declaration.hack(hackFlexWrap)
Declaration.hack(hackGridArea)
Declaration.hack(hackPlaceSelf)
Declaration.hack(hackGridStart)
Declaration.hack(hackAlignSelf)
Declaration.hack(hackAppearance)
Declaration.hack(hackFlexBasis)
Declaration.hack(hackMaskBorder)
Declaration.hack(hackMaskComposite)
Declaration.hack(hackAlignItems)
Declaration.hack(hackUserSelect)
Declaration.hack(hackFlexShrink)
Declaration.hack(hackBreakProps)
Declaration.hack(hackWritingMode)
Declaration.hack(hackBorderImage)
Declaration.hack(hackAlignContent)
Declaration.hack(hackBorderRadius)
Declaration.hack(hackBlockLogical)
Declaration.hack(hackGridTemplate)
Declaration.hack(hackInlineLogical)
Declaration.hack(hackGridRowAlign)
Declaration.hack(hackTransformDecl)
Declaration.hack(hackFlexDirection)
Declaration.hack(hackImageRendering)
Declaration.hack(hackBackdropFilter)
Declaration.hack(hackBackgroundClip)
Declaration.hack(hackTextDecoration)
Declaration.hack(hackJustifyContent)
Declaration.hack(hackBackgroundSize)
Declaration.hack(hackGridRowColumn)
Declaration.hack(hackGridRowsColumns)
Declaration.hack(hackGridColumnAlign)
Declaration.hack(hackOverscrollBehavior)
Declaration.hack(hackGridTemplateAreas)
Declaration.hack(hackPrintColorAdjust)
Declaration.hack(hackTextEmphasisPosition)
Declaration.hack(hackTextDecorationSkipInk)
Value.hack(hackGradient)
Value.hack(hackIntrinsic)
Value.hack(hackPixelated)
Value.hack(hackImageSet)
Value.hack(hackCrossFade)
Value.hack(hackDisplayFlex)
Value.hack(hackDisplayGrid)
Value.hack(hackFilterValue)
let declsCache = new Map()
class Prefixes {
constructor(data, browsers, options = {}) {
this.data = data
this.browsers = browsers
this.options = options
;[this.add, this.remove] = this.preprocess(this.select(this.data))
this.transition = new Transition(this)
this.processor = new Processor(this)
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
2022-01-21 08:28:41 +00:00
/**
* Return clone instance to remove all prefixes
*/
2023-08-17 09:47:40 +00:00
cleaner() {
2022-01-21 08:28:41 +00:00
if (this.cleanerCache) {
2023-08-17 09:47:40 +00:00
return this.cleanerCache
2022-01-21 08:28:41 +00:00
}
if (this.browsers.selected.length) {
2023-08-17 09:47:40 +00:00
let empty = new Browsers(this.browsers.data, [])
this.cleanerCache = new Prefixes(this.data, empty, this.options)
2022-01-21 08:28:41 +00:00
} else {
2023-08-17 09:47:40 +00:00
return this
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
return this.cleanerCache
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
* Declaration loader with caching
2022-01-21 08:28:41 +00:00
*/
2023-08-17 09:47:40 +00:00
decl(prop) {
if (!declsCache.has(prop)) {
declsCache.set(prop, Declaration.load(prop))
}
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
return declsCache.get(prop)
}
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
/**
* Group declaration by unprefixed property to check them
*/
group(decl) {
let rule = decl.parent
let index = rule.index(decl)
let { length } = rule.nodes
let unprefixed = this.unprefixed(decl.prop)
let checker = (step, callback) => {
index += step
while (index >= 0 && index < length) {
let other = rule.nodes[index]
if (other.type === 'decl') {
if (step === -1 && other.prop === unprefixed) {
if (!Browsers.withPrefix(other.value)) {
break
}
}
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
if (this.unprefixed(other.prop) !== unprefixed) {
break
} else if (callback(other) === true) {
return true
}
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
if (step === +1 && other.prop === unprefixed) {
if (!Browsers.withPrefix(other.value)) {
break
}
}
}
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
index += step
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
return {
down(callback) {
return checker(+1, callback)
},
up(callback) {
return checker(-1, callback)
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
* Normalize prefix for remover
2022-01-21 08:28:41 +00:00
*/
2023-08-17 09:47:40 +00:00
normalize(prop) {
return this.decl(prop).normalize(prop)
}
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
/**
* Return prefixed version of property
*/
prefixed(prop, prefix) {
prop = vendor.unprefixed(prop)
return this.decl(prop).prefixed(prop, prefix)
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
2022-01-21 08:28:41 +00:00
/**
* Cache prefixes data to fast CSS processing
*/
2023-08-17 09:47:40 +00:00
preprocess(selected) {
let add = {
'@supports': new Supports(Prefixes, this),
'selectors': []
}
for (let name in selected.add) {
let prefixes = selected.add[name]
2022-01-21 08:28:41 +00:00
if (name === '@keyframes' || name === '@viewport') {
2023-08-17 09:47:40 +00:00
add[name] = new AtRule(name, prefixes, this)
2022-01-21 08:28:41 +00:00
} else if (name === '@resolution') {
2023-08-17 09:47:40 +00:00
add[name] = new Resolution(name, prefixes, this)
2022-01-21 08:28:41 +00:00
} else if (this.data[name].selector) {
2023-08-17 09:47:40 +00:00
add.selectors.push(Selector.load(name, prefixes, this))
2022-01-21 08:28:41 +00:00
} else {
2023-08-17 09:47:40 +00:00
let props = this.data[name].props
2022-01-21 08:28:41 +00:00
if (props) {
2023-08-17 09:47:40 +00:00
let value = Value.load(name, prefixes, this)
for (let prop of props) {
2022-01-21 08:28:41 +00:00
if (!add[prop]) {
2023-08-17 09:47:40 +00:00
add[prop] = { values: [] }
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
add[prop].values.push(value)
2022-01-21 08:28:41 +00:00
}
} else {
2023-08-17 09:47:40 +00:00
let values = (add[name] && add[name].values) || []
add[name] = Declaration.load(name, prefixes, this)
add[name].values = values
2022-01-21 08:28:41 +00:00
}
}
}
2023-08-17 09:47:40 +00:00
let remove = { selectors: [] }
for (let name in selected.remove) {
let prefixes = selected.remove[name]
if (this.data[name].selector) {
let selector = Selector.load(name, prefixes)
for (let prefix of prefixes) {
remove.selectors.push(selector.old(prefix))
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
} else if (name === '@keyframes' || name === '@viewport') {
for (let prefix of prefixes) {
let prefixed = `@${prefix}${name.slice(1)}`
remove[prefixed] = { remove: true }
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
} else if (name === '@resolution') {
remove[name] = new Resolution(name, prefixes, this)
2022-01-21 08:28:41 +00:00
} else {
2023-08-17 09:47:40 +00:00
let props = this.data[name].props
if (props) {
let value = Value.load(name, [], this)
for (let prefix of prefixes) {
let old = value.old(prefix)
2022-01-21 08:28:41 +00:00
if (old) {
2023-08-17 09:47:40 +00:00
for (let prop of props) {
if (!remove[prop]) {
remove[prop] = {}
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
if (!remove[prop].values) {
remove[prop].values = []
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
remove[prop].values.push(old)
2022-01-21 08:28:41 +00:00
}
}
}
} else {
2023-08-17 09:47:40 +00:00
for (let p of prefixes) {
let olds = this.decl(name).old(name, p)
if (name === 'align-self') {
let a = add[name] && add[name].prefixes
2022-01-21 08:28:41 +00:00
if (a) {
if (p === '-webkit- 2009' && a.includes('-webkit-')) {
2023-08-17 09:47:40 +00:00
continue
2022-01-21 08:28:41 +00:00
} else if (p === '-webkit-' && a.includes('-webkit- 2009')) {
2023-08-17 09:47:40 +00:00
continue
2022-01-21 08:28:41 +00:00
}
}
}
2023-08-17 09:47:40 +00:00
for (let prefixed of olds) {
if (!remove[prefixed]) {
remove[prefixed] = {}
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
remove[prefixed].remove = true
2022-01-21 08:28:41 +00:00
}
}
}
}
}
2023-08-17 09:47:40 +00:00
return [add, remove]
2022-01-21 08:28:41 +00:00
}
/**
2023-08-17 09:47:40 +00:00
* Select prefixes from data, which is necessary for selected browsers
2022-01-21 08:28:41 +00:00
*/
2023-08-17 09:47:40 +00:00
select(list) {
let selected = { add: {}, remove: {} }
for (let name in list) {
let data = list[name]
let add = data.browsers.map(i => {
let params = i.split(' ')
return {
browser: `${params[0]} ${params[1]}`,
note: params[2]
}
})
let notes = add
.filter(i => i.note)
.map(i => `${this.browsers.prefix(i.browser)} ${i.note}`)
notes = utils.uniq(notes)
add = add
.filter(i => this.browsers.isSelected(i.browser))
.map(i => {
let prefix = this.browsers.prefix(i.browser)
if (i.note) {
return `${prefix} ${i.note}`
} else {
return prefix
}
})
add = this.sort(utils.uniq(add))
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
if (this.options.flexbox === 'no-2009') {
add = add.filter(i => !i.includes('2009'))
}
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
let all = data.browsers.map(i => this.browsers.prefix(i))
if (data.mistakes) {
all = all.concat(data.mistakes)
}
all = all.concat(notes)
all = utils.uniq(all)
if (add.length) {
selected.add[name] = add
if (add.length < all.length) {
selected.remove[name] = all.filter(i => !add.includes(i))
}
} else {
selected.remove[name] = all
}
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
return selected
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
* Sort vendor prefixes
2022-01-21 08:28:41 +00:00
*/
2023-08-17 09:47:40 +00:00
sort(prefixes) {
return prefixes.sort((a, b) => {
let aLength = utils.removeNote(a).length
let bLength = utils.removeNote(b).length
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
if (aLength === bLength) {
return b.length - a.length
} else {
return bLength - aLength
}
})
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
* Return unprefixed version of property
2022-01-21 08:28:41 +00:00
*/
2023-08-17 09:47:40 +00:00
unprefixed(prop) {
let value = this.normalize(vendor.unprefixed(prop))
if (value === 'flex-direction') {
value = 'flex-flow'
}
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
/**
* Return values, which must be prefixed in selected property
*/
2023-08-17 09:47:40 +00:00
values(type, prop) {
let data = this[type]
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
let global = data['*'] && data['*'].values
let values = data[prop] && data[prop].values
2022-01-21 08:28:41 +00:00
if (global && values) {
2023-08-17 09:47:40 +00:00
return utils.uniq(global.concat(values))
2022-01-21 08:28:41 +00:00
} else {
2023-08-17 09:47:40 +00:00
return global || values || []
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
module.exports = Prefixes