wishthis/node_modules/autoprefixer/lib/resolution.js

98 lines
2.3 KiB
JavaScript
Raw Normal View History

2023-08-17 09:47:40 +00:00
let FractionJs = require('fraction.js')
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
let Prefixer = require('./prefixer')
let utils = require('./utils')
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
const REGEXP = /(min|max)-resolution\s*:\s*\d*\.?\d+(dppx|dpcm|dpi|x)/gi
const SPLIT = /(min|max)-resolution(\s*:\s*)(\d*\.?\d+)(dppx|dpcm|dpi|x)/i
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
class Resolution extends Prefixer {
/**
* Remove prefixed queries
*/
clean(rule) {
if (!this.bad) {
this.bad = []
for (let prefix of this.prefixes) {
this.bad.push(this.prefixName(prefix, 'min'))
this.bad.push(this.prefixName(prefix, 'max'))
}
}
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
rule.params = utils.editList(rule.params, queries => {
return queries.filter(query => this.bad.every(i => !query.includes(i)))
})
2022-01-21 08:28:41 +00:00
}
/**
2023-08-17 09:47:40 +00:00
* Return prefixed query name
*/
prefixName(prefix, name) {
2022-01-21 08:28:41 +00:00
if (prefix === '-moz-') {
2023-08-17 09:47:40 +00:00
return name + '--moz-device-pixel-ratio'
2022-01-21 08:28:41 +00:00
} else {
2023-08-17 09:47:40 +00:00
return prefix + name + '-device-pixel-ratio'
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 prefixed query
*/
prefixQuery(prefix, name, colon, value, units) {
value = new FractionJs(value)
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
// 1dpcm = 2.54dpi
// 1dppx = 96dpi
2022-01-21 08:28:41 +00:00
if (units === 'dpi') {
2023-08-17 09:47:40 +00:00
value = value.div(96)
} else if (units === 'dpcm') {
value = value.mul(2.54).div(96)
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
value = value.simplify()
2022-01-21 08:28:41 +00:00
if (prefix === '-o-') {
2023-08-17 09:47:40 +00:00
value = value.n + '/' + value.d
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
return this.prefixName(prefix, name) + colon + value
2022-01-21 08:28:41 +00:00
}
/**
2023-08-17 09:47:40 +00:00
* Add prefixed queries
*/
process(rule) {
let parent = this.parentPrefix(rule)
let prefixes = parent ? [parent] : this.prefixes
rule.params = utils.editList(rule.params, (origin, prefixed) => {
for (let query of origin) {
if (
!query.includes('min-resolution') &&
!query.includes('max-resolution')
) {
prefixed.push(query)
continue
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
for (let prefix of prefixes) {
let processed = query.replace(REGEXP, str => {
let parts = str.match(SPLIT)
return this.prefixQuery(
prefix,
parts[1],
parts[2],
parts[3],
parts[4]
)
})
prefixed.push(processed)
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
prefixed.push(query)
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
return utils.uniq(prefixed)
})
}
}
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
module.exports = Resolution