Skip to content

Commit

Permalink
Merge pull request #300 from NitroPack/fix/inverse-media-queries
Browse files Browse the repository at this point in the history
Change how inversed media queries are handled
  • Loading branch information
pocketjoso authored May 1, 2020
2 parents bcfc292 + ea17f93 commit 67f66e1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
33 changes: 21 additions & 12 deletions src/non-matching-media-query-remover.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,17 @@ function _isMatchingMediaQuery (mediaQuery, matchConfig) {
// rather than the inverse field, but for our purposes we want to treat
// them the same.
const isInverse = mq.inverse || mq.type === 'not'
if (
(!isInverse && mq.type === 'print') ||
(isInverse && mq.type === 'screen')
) {
return false
}
// f.e. @media all {}
// go for false positives over false negatives,
// i.e. accept @media randomThing {}
if (mq.expressions.length === 0) {
// the checks below are only valid if the media query has no other properties other than the media type
if (
(!isInverse && mq.type === 'print') ||
(isInverse && mq.type === 'screen')
) {
return false
}
return true
}

Expand All @@ -46,19 +47,27 @@ function _isMatchingMediaQuery (mediaQuery, matchConfig) {
*/
return mq.expressions.some(function ({ modifier, feature, value }) {
if (modifier === 'min') {
const constructedQuery = `${
isInverse ? 'not ' : ''
}(min-${feature}: ${value})`
return cssMediaQuery.match(constructedQuery, matchConfig)
const constructedQuery = `(min-${feature}: ${value})`
// css-mediaquery does not match inversed queries correctly, hence the if..else below
if (!isInverse) {
return cssMediaQuery.match(constructedQuery, matchConfig)
} else {
return !cssMediaQuery.match(constructedQuery, matchConfig)
}
} else {
if (mq.expressions.length > 1) {
const constructedQuery = mq.expressions
.map(
({ modifier, feature, value }) =>
`${isInverse ? 'not ' : ''}(${modifier}-${feature}: ${value})`
`(${modifier}-${feature}: ${value})`
)
.join(' and ')
return cssMediaQuery.match(constructedQuery, matchConfig)
// css-mediaquery does not match inversed queries correctly, hence the if..else below
if (!isInverse) {
return cssMediaQuery.match(constructedQuery, matchConfig)
} else {
return !cssMediaQuery.match(constructedQuery, matchConfig)
}
} else {
return true
}
Expand Down
8 changes: 6 additions & 2 deletions test/pre-formatting.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,17 @@ describe('penthouse pre formatting tests', () => {
`@media oiasjdoiasd {}`,
// covering combined queries
`@media (min-width: 320px) and (max-width: 400px) {}`,
`@media (min-width: 150px) and (max-width: 1700px) {}`
`@media (min-width: 150px) and (max-width: 1700px) {}`,
`@media not screen and (min-width: 1800px) {}`,
`@media not all and (min-width: 101em) {}`
]
// 1300, 1600
const mediaToRemoveAlways = [
`@media print {}`,
`@media not screen {}`,
`@media not (min-width: 1px) {}`
`@media not (min-width: 1px) {}`,
`@media not screen and (min-width: 800px) {}`,
`@media not all and (min-width: 50em) {}`
]
// 1600
const mediaToRemoveUnlessLarge = [
Expand Down

0 comments on commit 67f66e1

Please sign in to comment.