Skip to content
This repository was archived by the owner on Jun 6, 2022. It is now read-only.

Commit 5f524ed

Browse files
committed
Merge pull request #17 from tbck/master
Properly detect circular dependencies
2 parents 4fc5672 + 75a6915 commit 5f524ed

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

Diff for: index.js

+12-10
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ var EXTENSION_RE = /\(\s*(--[\w-]+)\s*\)/g
55
/*
66
* Resolve custom media values.
77
*/
8-
function resolveValue(value, map, result) {
9-
if (!EXTENSION_RE.test(value)) {
10-
return value
8+
function resolveValue(query, depChain, map, result) {
9+
if (!EXTENSION_RE.test(query.value)) {
10+
return query.value
1111
}
12-
return value.replace(EXTENSION_RE, function(orig, name) {
12+
var val = query.value.replace(EXTENSION_RE, function(orig, name) {
1313
if (!map[name]) {
1414
return orig
1515
}
@@ -19,14 +19,18 @@ function resolveValue(value, map, result) {
1919
return mq.value
2020
}
2121

22-
if (mq.deps.indexOf(name) !== -1) {
22+
if (depChain.indexOf(name) !== -1) {
2323
mq.circular = true
2424
return orig
2525
}
26-
mq.deps.push(name)
27-
mq.value = resolveValue(mq.value, map, result)
26+
depChain.push(name)
27+
mq.value = resolveValue(mq, depChain, map, result)
2828
return mq.value
2929
})
30+
if (val === query.value) {
31+
query.circular = true
32+
}
33+
return val
3034
}
3135

3236
/*
@@ -61,7 +65,6 @@ function customMedia(options) {
6165
// map[<extension-name>] = <media-query-list>
6266
map[params.shift()] = {
6367
value: params.join(" "),
64-
deps: [],
6568
circular: false,
6669
resolved: false,
6770
}
@@ -75,14 +78,13 @@ function customMedia(options) {
7578
Object.keys(extensions).forEach(function(name) {
7679
map[name] = {
7780
value: extensions[name],
78-
deps: [],
7981
circular: false,
8082
resolved: false,
8183
}
8284
})
8385

8486
Object.keys(map).forEach(function(name) {
85-
map[name].value = resolveValue(map[name].value, map, result)
87+
map[name].value = resolveValue(map[name], [name], map, result)
8688
map[name].resolved = true
8789
})
8890

Diff for: test/fixtures/transform-circular-reference.css

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ body { color: #000 }
33
@custom-media --a (--b);
44
@custom-media --b (--a);
55

6+
@media (--a) {
7+
selector {
8+
property: value;
9+
}
10+
}
11+
612
@media (--b) {
713
selector {
814
property: value;

0 commit comments

Comments
 (0)