Skip to content

Commit 1658376

Browse files
committed
Made "or" queries work as they do for @media queries
1 parent 85cba47 commit 1658376

7 files changed

+244
-146
lines changed

CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# CHANGELOG
22

3+
## 1.2.0 (2017-02-28)
4+
5+
- Made "or" queries work as they do for @media queries
6+
7+
## 1.1.1 (2017-02-27)
8+
9+
- Attempt to make the readme appear on npm
10+
311
## 1.1.0 (2017-02-27)
412

513
- cmin / cmax unit support

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@zeecoder/container-query",
3-
"version": "1.1.1",
3+
"version": "1.2.0",
44
"description": "A PostCSS plugin and Javascript runtime combination, which allows you to write @container queries in your CSS the same way you would write @media queries.",
55
"main": "index.js",
66
"author": "Viktor Hubert <[email protected]>",

src/postcss/containerQuery.spec.js

+32-11
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ test('proper json and css output', () => {
8888
}
8989
/* Ignore this */
9090
}
91+
92+
@container (height >= 100px) and (width >= 100px), (aspect-ratio > 3.5) {
93+
.container {
94+
background: #000;
95+
}
96+
}
9197
9298
/* Ignore this */
9399
@@ -150,10 +156,10 @@ test('proper json and css output', () => {
150156
]
151157
},
152158
{
153-
"conditions": [
159+
"conditions": [[
154160
[ "height", ">=", 100 ],
155161
[ "width", ">=", 100 ],
156-
],
162+
]],
157163
"elements": [
158164
{
159165
"selector": ".container",
@@ -164,9 +170,9 @@ test('proper json and css output', () => {
164170
]
165171
},
166172
{
167-
"conditions": [
173+
"conditions": [[
168174
[ "height", ">=", 100 ],
169-
],
175+
]],
170176
"elements": [
171177
{
172178
"selector": ".container",
@@ -175,6 +181,25 @@ test('proper json and css output', () => {
175181
}
176182
}
177183
]
184+
},
185+
{
186+
"conditions": [
187+
[
188+
[ "height", ">=", 100 ],
189+
[ "width", ">=", 100 ],
190+
],
191+
[
192+
[ "aspect-ratio", ">", 3.5 ]
193+
]
194+
],
195+
"elements": [
196+
{
197+
"selector": ".container",
198+
"styles": {
199+
"background": "#000"
200+
}
201+
}
202+
]
178203
}
179204
]
180205
});
@@ -202,13 +227,9 @@ test('proper json and css output', () => {
202227
]
203228
},
204229
{
205-
"conditions": [
206-
[
207-
"orientation",
208-
":",
209-
"portrait"
210-
]
211-
],
230+
"conditions": [[
231+
[ "orientation", ":", "portrait" ]
232+
]],
212233
"elements": [
213234
{
214235
"selector": ".container2",

src/postcss/getConditionsFromQueryParams.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@ import trim from 'lodash.trim';
99
* @returns {string[]}
1010
*/
1111
export default function getConditionsFromQueryParams (params) {
12-
let conditionArr = params.match(/\(([^\)]*)\)/g);
12+
return params.split(',').map((andParams) => {
13+
return andParams.match(/\(([^\)]*)\)/g).map((condition) => {
14+
let conditionArr = trim(condition, '()');
1315

14-
return conditionArr.map((condition) => {
15-
let conditionArr = trim(condition, '()');
16+
conditionArr = conditionArr.match(/([a-z-]*)([ :><=]*)([a-z0-9\.]*)/i);
17+
conditionArr.shift();
1618

17-
conditionArr = conditionArr.match(/([a-z-]*)([ :><=]*)([a-z0-9\.]*)/i);
18-
conditionArr.shift();
19+
conditionArr = conditionArr.map(trim);
1920

20-
conditionArr = conditionArr.map(trim);
21+
if ([ 'landscape', 'portrait' ].indexOf(conditionArr[2]) === -1) {
22+
conditionArr[2] = parseFloat(conditionArr[2]);
23+
}
2124

22-
if ([ 'landscape', 'portrait' ].indexOf(conditionArr[2]) === -1) {
23-
conditionArr[2] = parseFloat(conditionArr[2]);
24-
}
25-
26-
return conditionArr;
25+
return conditionArr;
26+
});
2727
});
2828
}
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,51 @@
11
import getConditionsFromQueryParams from './getConditionsFromQueryParams';
22

33
test('width condition should work with the "<", ">", "<=", ">=" and ":" operators', () => {
4-
expect(getConditionsFromQueryParams('(width >= 42px)')).toEqual([
4+
expect(getConditionsFromQueryParams('(width >= 42px)')).toEqual([[
55
[ 'width', '>=', 42 ],
6-
]);
7-
expect(getConditionsFromQueryParams('(width > 42px)')).toEqual([
6+
]]);
7+
expect(getConditionsFromQueryParams('(width > 42px)')).toEqual([[
88
[ 'width', '>', 42 ],
9-
]);
10-
expect(getConditionsFromQueryParams('(width <= 42px)')).toEqual([
9+
]]);
10+
expect(getConditionsFromQueryParams('(width <= 42px)')).toEqual([[
1111
[ 'width', '<=', 42 ],
12-
]);
13-
expect(getConditionsFromQueryParams('(width < 42px)')).toEqual([
12+
]]);
13+
expect(getConditionsFromQueryParams('(width < 42px)')).toEqual([[
1414
[ 'width', '<', 42 ],
15-
]);
15+
]]);
1616
});
1717

1818
test('aspect-ratio should work with the "<", ">", "<=", ">=" and ":" operators', () => {
19-
expect(getConditionsFromQueryParams('(aspect-ratio >= 0.5)')).toEqual([
19+
expect(getConditionsFromQueryParams('(aspect-ratio >= 0.5)')).toEqual([[
2020
[ 'aspect-ratio', '>=', 0.5 ],
21-
]);
22-
expect(getConditionsFromQueryParams('(aspect-ratio > 0.5)')).toEqual([
21+
]]);
22+
expect(getConditionsFromQueryParams('(aspect-ratio > 0.5)')).toEqual([[
2323
[ 'aspect-ratio', '>', 0.5 ],
24-
]);
25-
expect(getConditionsFromQueryParams('(aspect-ratio <= 0.5)')).toEqual([
24+
]]);
25+
expect(getConditionsFromQueryParams('(aspect-ratio <= 0.5)')).toEqual([[
2626
[ 'aspect-ratio', '<=', 0.5 ],
27-
]);
28-
expect(getConditionsFromQueryParams('(aspect-ratio < 0.5)')).toEqual([
27+
]]);
28+
expect(getConditionsFromQueryParams('(aspect-ratio < 0.5)')).toEqual([[
2929
[ 'aspect-ratio', '<', 0.5 ],
30-
]);
30+
]]);
3131
});
3232

3333
test('orientations with the : operator', () => {
34-
expect(getConditionsFromQueryParams('(orientation: portrait)')).toEqual([
34+
expect(getConditionsFromQueryParams('(orientation: portrait)')).toEqual([[
3535
[ 'orientation', ':', 'portrait' ],
36-
]);
37-
expect(getConditionsFromQueryParams('(orientation: landscape)')).toEqual([
36+
]]);
37+
expect(getConditionsFromQueryParams('(orientation: landscape)')).toEqual([[
3838
[ 'orientation', ':', 'landscape' ],
39-
]);
39+
]]);
4040
});
4141

4242
test('should handle multiple "and" conditions', () => {
43-
expect(getConditionsFromQueryParams('(orientation: landscape) and (width > 42px)')).toEqual([
43+
expect(getConditionsFromQueryParams('(orientation: landscape) and (width > 42px)')).toEqual([[
4444
[ 'orientation', ':', 'landscape' ],
4545
[ 'width', '>', 42 ],
46-
]);
47-
expect(getConditionsFromQueryParams('(width < 42px) and (height >= 42px)')).toEqual([
46+
]]);
47+
expect(getConditionsFromQueryParams('(width < 42px) and (height >= 42px)')).toEqual([[
4848
[ 'width', '<', 42 ],
4949
[ 'height', '>=', 42 ],
50-
]);
50+
]]);
5151
});

0 commit comments

Comments
 (0)