2023-08-17 09:47:40 +00:00
|
|
|
let featureQueries = require('caniuse-lite/data/features/css-featurequeries.js')
|
|
|
|
let feature = require('caniuse-lite/dist/unpacker/feature')
|
|
|
|
let { parse } = require('postcss')
|
|
|
|
|
|
|
|
let Browsers = require('./browsers')
|
|
|
|
let brackets = require('./brackets')
|
|
|
|
let Value = require('./value')
|
|
|
|
let utils = require('./utils')
|
|
|
|
|
|
|
|
let data = feature(featureQueries)
|
|
|
|
|
|
|
|
let supported = []
|
|
|
|
for (let browser in data.stats) {
|
|
|
|
let versions = data.stats[browser]
|
|
|
|
for (let version in versions) {
|
|
|
|
let support = versions[version]
|
2022-01-21 08:28:41 +00:00
|
|
|
if (/y/.test(support)) {
|
2023-08-17 09:47:40 +00:00
|
|
|
supported.push(browser + ' ' + version)
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
class Supports {
|
|
|
|
constructor(Prefixes, all) {
|
|
|
|
this.Prefixes = Prefixes
|
|
|
|
this.all = all
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
/**
|
|
|
|
* Add prefixes
|
|
|
|
*/
|
|
|
|
add(nodes, all) {
|
|
|
|
return nodes.map(i => {
|
|
|
|
if (this.isProp(i)) {
|
|
|
|
let prefixed = this.prefixed(i[0])
|
|
|
|
if (prefixed.length > 1) {
|
|
|
|
return this.convert(prefixed)
|
|
|
|
}
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
return i
|
|
|
|
}
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
if (typeof i === 'object') {
|
|
|
|
return this.add(i, all)
|
|
|
|
}
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
return i
|
|
|
|
})
|
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
|
|
|
* Clean brackets with one child
|
|
|
|
*/
|
|
|
|
cleanBrackets(nodes) {
|
|
|
|
return nodes.map(i => {
|
|
|
|
if (typeof i !== 'object') {
|
|
|
|
return i
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
if (i.length === 1 && typeof i[0] === 'object') {
|
|
|
|
return this.cleanBrackets(i[0])
|
|
|
|
}
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
return this.cleanBrackets(i)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add " or " between properties and convert it to brackets format
|
|
|
|
*/
|
|
|
|
convert(progress) {
|
|
|
|
let result = ['']
|
|
|
|
for (let i of progress) {
|
|
|
|
result.push([`${i.prop}: ${i.value}`])
|
|
|
|
result.push(' or ')
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
2023-08-17 09:47:40 +00:00
|
|
|
result[result.length - 1] = ''
|
|
|
|
return result
|
|
|
|
}
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
/**
|
|
|
|
* Check global options
|
|
|
|
*/
|
|
|
|
disabled(node) {
|
|
|
|
if (!this.all.options.grid) {
|
|
|
|
if (node.prop === 'display' && node.value.includes('grid')) {
|
|
|
|
return true
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
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
|
|
|
}
|
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 (this.all.options.flexbox === false) {
|
|
|
|
if (node.prop === 'display' && node.value.includes('flex')) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
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 false
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-08-17 09:47:40 +00:00
|
|
|
* Return true if prefixed property has no unprefixed
|
|
|
|
*/
|
|
|
|
isHack(all, unprefixed) {
|
|
|
|
let check = new RegExp(`(\\(|\\s)${utils.escapeRegexp(unprefixed)}:`)
|
|
|
|
return !check.test(all)
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-08-17 09:47:40 +00:00
|
|
|
* Return true if brackets node is "not" word
|
|
|
|
*/
|
|
|
|
isNot(node) {
|
|
|
|
return typeof node === 'string' && /not\s*/i.test(node)
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
/**
|
|
|
|
* Return true if brackets node is "or" word
|
|
|
|
*/
|
|
|
|
isOr(node) {
|
|
|
|
return typeof node === 'string' && /\s*or\s*/i.test(node)
|
|
|
|
}
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
/**
|
|
|
|
* Return true if brackets node is (prop: value)
|
|
|
|
*/
|
|
|
|
isProp(node) {
|
|
|
|
return (
|
|
|
|
typeof node === 'object' &&
|
|
|
|
node.length === 1 &&
|
|
|
|
typeof node[0] === 'string'
|
|
|
|
)
|
|
|
|
}
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
/**
|
|
|
|
* Compress value functions into a string nodes
|
|
|
|
*/
|
|
|
|
normalize(nodes) {
|
|
|
|
if (typeof nodes !== 'object') {
|
|
|
|
return nodes
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
nodes = nodes.filter(i => i !== '')
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
if (typeof nodes[0] === 'string') {
|
|
|
|
let firstNode = nodes[0].trim()
|
|
|
|
|
|
|
|
if (
|
|
|
|
firstNode.includes(':') ||
|
|
|
|
firstNode === 'selector' ||
|
|
|
|
firstNode === 'not selector'
|
|
|
|
) {
|
|
|
|
return [brackets.stringify(nodes)]
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-17 09:47:40 +00:00
|
|
|
return nodes.map(i => this.normalize(i))
|
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
|
|
|
* Parse string into declaration property and value
|
|
|
|
*/
|
|
|
|
parse(str) {
|
|
|
|
let parts = str.split(':')
|
|
|
|
let prop = parts[0]
|
|
|
|
let value = parts[1]
|
|
|
|
if (!value) value = ''
|
|
|
|
return [prop.trim(), value.trim()]
|
|
|
|
}
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
/**
|
|
|
|
* Return array of Declaration with all necessary prefixes
|
|
|
|
*/
|
|
|
|
prefixed(str) {
|
|
|
|
let rule = this.virtual(str)
|
|
|
|
if (this.disabled(rule.first)) {
|
|
|
|
return rule.nodes
|
|
|
|
}
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
let result = { warn: () => null }
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
let prefixer = this.prefixer().add[rule.first.prop]
|
|
|
|
prefixer && prefixer.process && prefixer.process(rule.first, result)
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
for (let decl of rule.nodes) {
|
|
|
|
for (let value of this.prefixer().values('add', rule.first.prop)) {
|
|
|
|
value.process(decl)
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
2023-08-17 09:47:40 +00:00
|
|
|
Value.save(this.all, decl)
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
return rule.nodes
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-08-17 09:47:40 +00:00
|
|
|
* Return prefixer only with @supports supported browsers
|
|
|
|
*/
|
|
|
|
prefixer() {
|
|
|
|
if (this.prefixerCache) {
|
|
|
|
return this.prefixerCache
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
let filtered = this.all.browsers.selected.filter(i => {
|
|
|
|
return supported.includes(i)
|
|
|
|
})
|
|
|
|
|
|
|
|
let browsers = new Browsers(
|
|
|
|
this.all.browsers.data,
|
|
|
|
filtered,
|
|
|
|
this.all.options
|
|
|
|
)
|
|
|
|
this.prefixerCache = new this.Prefixes(
|
|
|
|
this.all.data,
|
|
|
|
browsers,
|
|
|
|
this.all.options
|
|
|
|
)
|
|
|
|
return this.prefixerCache
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-08-17 09:47:40 +00:00
|
|
|
* Add prefixed declaration
|
|
|
|
*/
|
|
|
|
process(rule) {
|
|
|
|
let ast = brackets.parse(rule.params)
|
|
|
|
ast = this.normalize(ast)
|
|
|
|
ast = this.remove(ast, rule.params)
|
|
|
|
ast = this.add(ast, rule.params)
|
|
|
|
ast = this.cleanBrackets(ast)
|
|
|
|
rule.params = brackets.stringify(ast)
|
|
|
|
}
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
/**
|
|
|
|
* Remove all unnecessary prefixes
|
|
|
|
*/
|
|
|
|
remove(nodes, all) {
|
|
|
|
let i = 0
|
|
|
|
while (i < nodes.length) {
|
|
|
|
if (
|
|
|
|
!this.isNot(nodes[i - 1]) &&
|
|
|
|
this.isProp(nodes[i]) &&
|
|
|
|
this.isOr(nodes[i + 1])
|
|
|
|
) {
|
|
|
|
if (this.toRemove(nodes[i][0], all)) {
|
|
|
|
nodes.splice(i, 2)
|
|
|
|
continue
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
i += 2
|
|
|
|
continue
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
if (typeof nodes[i] === 'object') {
|
|
|
|
nodes[i] = this.remove(nodes[i], all)
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
i += 1
|
|
|
|
}
|
|
|
|
return nodes
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
/**
|
|
|
|
* Return true if we need to remove node
|
|
|
|
*/
|
|
|
|
toRemove(str, all) {
|
|
|
|
let [prop, value] = this.parse(str)
|
|
|
|
let unprefixed = this.all.unprefixed(prop)
|
|
|
|
|
|
|
|
let cleaner = this.all.cleaner()
|
|
|
|
|
|
|
|
if (
|
|
|
|
cleaner.remove[prop] &&
|
|
|
|
cleaner.remove[prop].remove &&
|
|
|
|
!this.isHack(all, unprefixed)
|
|
|
|
) {
|
|
|
|
return true
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
for (let checker of cleaner.values('remove', unprefixed)) {
|
|
|
|
if (checker.check(value)) {
|
|
|
|
return true
|
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
|
|
|
/**
|
|
|
|
* Create virtual rule to process it by prefixer
|
|
|
|
*/
|
|
|
|
virtual(str) {
|
|
|
|
let [prop, value] = this.parse(str)
|
|
|
|
let rule = parse('a{}').first
|
|
|
|
rule.append({ prop, raws: { before: '' }, value })
|
|
|
|
return rule
|
|
|
|
}
|
|
|
|
}
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
module.exports = Supports
|