GithubHelp home page GithubHelp logo

ericf / css-mediaquery Goto Github PK

View Code? Open in Web Editor NEW
110.0 110.0 19.0 444 KB

Parses and determines if a given CSS Media Query matches a set of values via JavaScript.

License: Other

JavaScript 100.00%

css-mediaquery's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

css-mediaquery's Issues

Generates incorrect AST when media query has no expressions

I get an incorrect AST when I enter a media query without any expressions (basically anything of this format):

media_query: [[only | not]? <media_type> 

For example:

console.log(mediaquery.parse('not screen'));

/*
logs out
[ { inverse: false, type: 'all', expressions: [] } ]
*/

This should really be:

[ { inverse: true, type: 'screen', expressions: [] } ]

'only' keyword is ignored

var mq = require('css-mediaquery');
mq.parse('only screen and (min-width: 950px)')
[{  
    "inverse": false,
    "type": "screen",
    "expressions": [{  
        "modifier": "min",
        "feature": "width",
        "value": "950px"
    }]
}]

which means the 'only' clause is just ignored, so data is lost and mediaquery cannot be rebuilt (related to #13)

Bug in min-width and max-width matching with em values

We noticed a strange behavior when trying to do matches. Issue demostrated below:

> const css = require('css-mediaquery')
undefined
> css.match('screen and (min-width: 48em)', { width: '48em' })
false
> css.match('screen and (max-width: 48em)', { width: '48em' })
false

I think these both should return true. That's how Chrome matchMedia()works when tested.

Add support for prefers-reduced-motion and other modern media features

I'd love to see this library get updated, as it's in fairly regular use and hasn't been touched in 9 years. In particular, I'm trying to parse a media query using prefers-reduced-motion and hitting an error. I notice that there is an (archived) fork of the repo which handles this so it should be possible.

If you don't have the capacity to continue working on this, is it possible to consider transferring package ownership/publishing rights to somebody else who would be willing to take this on?

Add color-gamut and any-pointer support

In contrast to other media features, color-gamut and any-pointer can match multiple values at the same time. For example, if the user display supports DCI-P3, the following expressions will match:

  • (color-gamut: p3)
  • (color-gamut: srgb) (because DCI-P3 includes sRGB, so if a monitor supports DCI-P3, it does support sRGB)

And the following won't:

  • (color-gamut: rec2020)

In order to support this feature, the match function shall either take multiple values for a mocked property or have a built-in knowledge about these features (not applicable to any-pointer).

It will be handy if the library supports arbitrary media features. It will make it future proof (allow to use with forthcoming media features without updating).

Wrong feature detection for grid, color, color-index and monochrome

The features at https://github.com/ericf/css-mediaquery/blob/master/index.js#L67-74
does not handle the following case (one or both expressions without an value) on the correct way:

mediaQuery.match('screen and (color)', {
  type : 'screen',
  color: 8
});

See http://www.w3.org/TR/css3-mediaqueries/#color

One possible approach might be this (@ericf, -1 and -1 returns true โ€“ but a specified cannot be negative, in general!?):

expValue = !isNaN(expValue) ? parseInt(expValue, 10) : value > 0 ? value : 1;
value = !isNaN(value) ? parseInt(value, 10) : expValue > 0 ? expValue : 0;

Feature request: serialize parsed query

I am using css-mediaquey to manipulate a media query rule, so I need to parse and write back the query.

There is no serialize method included so far but I believe it could be in the scope of this library. If you are willing to include such functionality I'll happily provide a pull request. Let me know if there are any constraints on naming or scope.

npm outdated

did you published the last version on npm?

When I run npm install css-mediaquery I get an old code. The package.json shows 0.1.2 version but the code is from 0.1.1.

For example, the commit d1413c5 is not present.

css-mediaquery does not work with comments in media queries and with Opera's device pixel ratio

Two samples which fail to parse:

@media (-o-min-device-pixel-ratio: 3 / 2) {
    body { background: url('hd-background.png'); }
}
@media (-webkit-min-device-pixel-ratio: 1.5),
    (min--moz-device-pixel-ratio: 1.5),
    /* Opera Device Pixel ratio not supported by css-mediaquery 0.1.2 */
    /* (-o-min-device-pixel-ratio: 3 / 2), */
    (min-device-pixel-ratio: 1.5) {
    body { background: url('hd-background.png'); }
}

This works:

@media (min-device-pixel-ratio: 1.5) {
    body { background: url('hd-background.png'); }
}

new npm release?

i like your latest changes for strict parsing. do you plan a new npm release with this changes soon?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.