source: node_modules/autoprefixer/lib/resolution.js@ 2058e5c

Last change on this file since 2058e5c was 2058e5c, checked in by istevanoska <ilinastevanoska@…>, 6 months ago

Working / before login

  • Property mode set to 100644
File size: 2.3 KB
RevLine 
[2058e5c]1let FractionJs = require('fraction.js')
2
3let Prefixer = require('./prefixer')
4let utils = require('./utils')
5
6const REGEXP = /(min|max)-resolution\s*:\s*\d*\.?\d+(dppx|dpcm|dpi|x)/gi
7const SPLIT = /(min|max)-resolution(\s*:\s*)(\d*\.?\d+)(dppx|dpcm|dpi|x)/i
8
9class Resolution extends Prefixer {
10 /**
11 * Remove prefixed queries
12 */
13 clean(rule) {
14 if (!this.bad) {
15 this.bad = []
16 for (let prefix of this.prefixes) {
17 this.bad.push(this.prefixName(prefix, 'min'))
18 this.bad.push(this.prefixName(prefix, 'max'))
19 }
20 }
21
22 rule.params = utils.editList(rule.params, queries => {
23 return queries.filter(query => this.bad.every(i => !query.includes(i)))
24 })
25 }
26
27 /**
28 * Return prefixed query name
29 */
30 prefixName(prefix, name) {
31 if (prefix === '-moz-') {
32 return name + '--moz-device-pixel-ratio'
33 } else {
34 return prefix + name + '-device-pixel-ratio'
35 }
36 }
37
38 /**
39 * Return prefixed query
40 */
41 prefixQuery(prefix, name, colon, value, units) {
42 value = new FractionJs(value)
43
44 // 1dpcm = 2.54dpi
45 // 1dppx = 96dpi
46 if (units === 'dpi') {
47 value = value.div(96)
48 } else if (units === 'dpcm') {
49 value = value.mul(2.54).div(96)
50 }
51 value = value.simplify()
52
53 if (prefix === '-o-') {
54 value = value.n + '/' + value.d
55 }
56 return this.prefixName(prefix, name) + colon + value
57 }
58
59 /**
60 * Add prefixed queries
61 */
62 process(rule) {
63 let parent = this.parentPrefix(rule)
64 let prefixes = parent ? [parent] : this.prefixes
65
66 rule.params = utils.editList(rule.params, (origin, prefixed) => {
67 for (let query of origin) {
68 if (
69 !query.includes('min-resolution') &&
70 !query.includes('max-resolution')
71 ) {
72 prefixed.push(query)
73 continue
74 }
75
76 for (let prefix of prefixes) {
77 let processed = query.replace(REGEXP, str => {
78 let parts = str.match(SPLIT)
79 return this.prefixQuery(
80 prefix,
81 parts[1],
82 parts[2],
83 parts[3],
84 parts[4]
85 )
86 })
87 prefixed.push(processed)
88 }
89 prefixed.push(query)
90 }
91
92 return utils.uniq(prefixed)
93 })
94 }
95}
96
97module.exports = Resolution
Note: See TracBrowser for help on using the repository browser.