Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
media-query-parser
Advanced tools
media-query-parser
(100px < width < 200px)
Other CSS parsers (e.g. css-tree and postcss) do not support all media query syntax out of the box.
Further, the only other media query parser that I'm aware of is postcss-media-query-parser
- which
is specific to postcss and doesn't parse newer syntax like range expressions (i.e.
(width >= 768px)
).
This package is a spec-compliant media query parser that can be used in Node/Deno/etc, or on the client that precisely matches the spec right down to the quirks.
These are valid media queries that this library supports:
@media (768px <= width < 1200px);
@media only print and (color);
@media not (not (not (((hover) or ((not (color)))))));
@media (🐈: 😸 /* if cat happy */) {
/* this query has valid syntax, but is clearly not a real feature 😿 */
/* For extensions to this project, check out "Libraries that use this" at the bottom of this README */
}
These are invalid media queries that this library will detect:
@media (color) or (hover); /* or cannot be at top level */
@media (min-width: calc(50vw + 10px)); /* functions aren't valid values */
@media not((color)); /* whitespace must follow `not` */
@media (768px < = width < 1200px); /* cannot have a space between `<` and `=` */
This package is available from the npm
registry.
npm install media-query-parser
Supports JavaScript + TypeScript:
import { parseMediaQuery } from "media-query-parser";
const mediaQuery = parseMediaQuery("screen and (min-width: 768px)");
if (!isParserError(mediaQuery)) {
console.log(mediaQuery);
// {
// _t: "query",
// type: "screen",
// condition: {
// _t: "condition",
// op: "and",
// nodes: [
// {
// _t: "in-parens",
// node: {
// _t: "feature",
// context: "value",
// feature: "min-width",
// value: {
// _t: "dimension",
// value: 768,
// unit: "px",
// },
// },
// },
// ],
// },
// }
// // start/end omitted for brevity
console.log(stringify(mediaQuery.condition));
// "(min-width: 768px)"
}
Can also be imported via require("media-query-parser")
.
This source code of this library supports node >=6.5.0
via require()
and
node >=16.0.0 || ^14.13.1
via import. (Below v6.5.0 is possible but you'd need to transpile the
CommonJS code from ES6 yourself)
Dev environment (for contributing) requires:
MIT
FAQs
Parse CSS media queries (spec-compliant)
The npm package media-query-parser receives a total of 598,648 weekly downloads. As such, media-query-parser popularity was classified as popular.
We found that media-query-parser demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.