Security News
38% of CISOs Fear They’re Not Moving Fast Enough on AI
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
media-query-parser
Advanced tools
media-query-parser
This package's v3 (currently in beta) involves a complete overhaul from v2 to a more sustainable shape. See v2 API docs instead
(100px < width < 200px)
This repo/package contains only the parser, stringify and isParserError.
media-query-fns
uses this library internally to achieve common use-cases.
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.
(see `media-query-fns` for feature checking) */
}
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)); /* operators need whitespace */
@media (768px < = width < 1200px); /* cannot have a space between `<` and `=` */
This package is available from the npm
registry.
npm install media-query-parser
The shape of Errors are
Supports JavaScript + TypeScript:
import { parseMediaQuery } from "media-query-parser";
const mediaQuery = parseMediaQuery("screen and (min-width: 768px)");
if (!isParserError(mediaQuery)) {
console.log(mediaQuery);
// {
// type: "query",
// mediaType: "screen",
// mediaCondition: {
// type: "condition",
// children: [{
// type: "feature",
// context: "value",
// prefix: "min",
// feature: "width",
// value: {
// type: "dimension", value: 768, unit: "px", flag: "number"
// },
// }],
// },
// }
console.log(stringify(mediaQuery.mediaCondition.children[0]));
// "(min-width: 768px)"
}
Can also be imported via require("media-query-parser")
.
Dev environment (for contributing) requires:
MIT
FAQs
Parse CSS media queries (spec-compliant)
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
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.
Security News
Company News
Socket is joining TC54 to help develop standards for software supply chain security, contributing to the evolution of SBOMs, CycloneDX, and Package URL specifications.