Skip to content

Commit 2db3239

Browse files
authoredDec 1, 2022
Correctly sort media queries before joining them (#513)
1 parent 2780457 commit 2db3239

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed
 

‎lib/join-media.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"use strict"
22

3+
const startsWithKeywordRegexp = /^(all|not|only|print|screen)/i
4+
35
module.exports = function (parentMedia, childMedia) {
46
if (!parentMedia.length && childMedia.length) return childMedia
57
if (parentMedia.length && !childMedia.length) return parentMedia
@@ -8,8 +10,17 @@ module.exports = function (parentMedia, childMedia) {
810
const media = []
911

1012
parentMedia.forEach(parentItem => {
13+
const parentItemStartsWithKeyword = startsWithKeywordRegexp.test(parentItem)
14+
1115
childMedia.forEach(childItem => {
12-
if (parentItem !== childItem) media.push(`${parentItem} and ${childItem}`)
16+
const childItemStartsWithKeyword = startsWithKeywordRegexp.test(childItem)
17+
if (parentItem !== childItem) {
18+
if (childItemStartsWithKeyword && !parentItemStartsWithKeyword) {
19+
media.push(`${childItem} and ${parentItem}`)
20+
} else {
21+
media.push(`${parentItem} and ${childItem}`)
22+
}
23+
}
1324
})
1425
})
1526

‎test/fixtures/filter-ignore.expected.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
@import "http://css" (min-width: 25em);
2-
@import "http://css-screen" (min-width: 25em) and screen;
2+
@import "http://css-screen" screen and (min-width: 25em);
33
@import "http://css";
44
@import "https://css";
55
@import 'http://css';

‎test/fixtures/ignore.expected.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
@import "http://css" (min-width: 25em);
2-
@import "http://css-screen" (min-width: 25em) and screen;
2+
@import "http://css-screen" screen and (min-width: 25em);
33
@import "http://css";
44
@import "https://css";
55
@import 'http://css';

0 commit comments

Comments
 (0)
Please sign in to comment.