-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
31 lines (27 loc) · 762 Bytes
/
index.js
File metadata and controls
31 lines (27 loc) · 762 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
module.exports = customMedia
function customMedia(string, map) {
return customMedia.replace(string, customMedia.parse(string, map))
}
customMedia.parse = function (string, map) {
map = map || {}
var RE_CM = /(?:^|\}|\s)@custom-media\s+--([\w-]+)\s+\((.*)\)\s*;/g
var m
while (m = RE_CM.exec(string)) map[m[1]] = m[2]
return map
}
customMedia.replace = function (string, map) {
var RE_MQ = /(?:^|\}|\s)@media[^\{]*\((\s*--([\w-]+\s*))\)[^\{]*\{/g
var i = 0
var _prev
while (string !== _prev) {
string = (_prev = string).replace(RE_MQ, replacer)
}
return string
function replacer(str, p1, p2, idx) {
if (idx < i) return str
var query = map[p2]
if (query) return str.replace(p1, query)
i = idx
return str
}
}