Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'postcss-media-query-parser' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
function parseMediaQuery(params) {
const mediaParser = require("postcss-media-query-parser").default;
let result = null;
try {
result = mediaParser(params);
} catch (e) {
// Ignore bad media queries
return {
type: "selector-unknown",
value: params
};
}
return addTypePrefix(addMissingType(result), "media-");
}
function checkMedia(node, value, getIndex) {
mediaParser(node.params).walk(/^media-feature$/i, (mediaFeatureNode) => {
const mediaName = getMediaFeatureName(mediaFeatureNode);
const parentValue = mediaFeatureNode.parent.value;
valueParser(value).walk(function(valueNode) {
// Ignore all non-word valueNode and
// the values not included in the parentValue string
if (valueNode.type !== 'word' || !parentValue.includes(valueNode.value)) {
return;
}
check(
node,
getIndex(node),
valueNode,
mediaName,
options ? options.ignoreMediaFeatureNames : {},
root.walkAtRules(/^media$/i, (atRule) => {
mediaParser(atRule.params).walk(/^media-feature-expression$/i, (node) => {
// Ignore boolean and range context
if (!node.value.includes(':')) {
return;
}
const mediaFeatureNode = _.find(node.nodes, { type: 'media-feature' });
const valueNode = _.find(node.nodes, { type: 'value' });
const mediaFeatureName = mediaFeatureNode.value;
const value = valueNode.value;
const unprefixedMediaFeatureName = postcss.vendor.unprefixed(mediaFeatureName);
const featureWhitelist = _.find(whitelist, (v, whitelistFeatureName) =>
matchesStringOrRegExp(unprefixedMediaFeatureName, whitelistFeatureName),
);
if (featureWhitelist === undefined) {
root.walkAtRules(/^import$/i, (atRule) => {
const params = valueParser(atRule.params).nodes;
if (!params.length) {
return;
}
// extract uri from url() if exists
const uri =
params[0].type === 'function' && params[0].value === 'url'
? params[0].nodes[0].value
: params[0].value;
// extract media queries if any
const media = mediaParser(valueParser.stringify(params.slice(1)))
.nodes.map((n) => n.value.replace(/\s/g, ''))
.filter((n) => n.length);
const isDuplicate = media.length
? imports[uri] && media.some((q) => imports[uri].includes(q))
: imports[uri];
if (isDuplicate) {
report({
message: messages.rejected(uri),
node: atRule,
result,
ruleName,
});
return;
result[size] = images.url[i] + suffix;
} else {
result.default = images.url[i] + suffix;
}
}
}
return result;
});
// add the default image to the decl
decl.value = parsedValues.map(val => val.default).join(',');
// check for the media queries
const media = decl.parent.parent.params;
const parsedMedia = media && mediaParser(media);
Object.keys(mediaQueryList)
.sort()
.forEach(size => {
const minResQuery = `(min-resolution: ${size}dpi)`;
const minDPRQuery = `(-webkit-min-device-pixel-ratio: ${mediaQueryList[size]})`;
const paramStr = parsedMedia ?
parsedMedia.nodes
.map(queryNode => `${queryNode.value} and ${minDPRQuery}, ${queryNode.value} and ${minResQuery}`)
.join(',') :
`${minDPRQuery}, ${minResQuery}`;
const atrule = postcss.atRule({
name: 'media',
params: paramStr
}
// Selector
if (item.selector !== undefined) {
results = results.concat(findInterpolation(item.selector));
}
if (item.type === "atrule") {
// @forward and @use
if (item.name === "forward" || item.name === "use") {
return;
}
// Media queries
if (item.name === "media" || item.name === "import") {
mediaQueryParser(item.params).walk(node => {
const type = node.type;
if (["keyword", "media-type", "media-feature"].includes(type)) {
results = results.concat(
findInterpolation(
node.value,
atRuleParamIndex(item) + node.sourceIndex
)
);
} else if (["value", "url"].includes(type)) {
results.push({
source: node.value,
operators: findOperators({
string: node.value,
globalIndex: atRuleParamIndex(item) + node.sourceIndex,
isAfterColon: true
root.walkAtRules(/^media$/i, (atRule) => {
mediaParser(atRule.params).walk(/^media-feature$/i, (mediaFeatureNode) => {
const parent = mediaFeatureNode.parent;
const sourceIndex = mediaFeatureNode.sourceIndex;
const value = mediaFeatureNode.value;
if (
isRangeContextMediaFeature(parent.value) ||
!isStandardSyntaxMediaFeatureName(value) ||
isCustomMediaQuery(value)
) {
return;
}
if (matchesStringOrRegExp(value, whitelist)) {
return;
}
root.walkAtRules(/^media$/i, (atRule) => {
mediaParser(atRule.params).walk(/^media-feature$/i, (mediaFeatureNode) => {
const parent = mediaFeatureNode.parent;
const sourceIndex = mediaFeatureNode.sourceIndex;
const value = mediaFeatureNode.value;
if (
isRangeContextMediaFeature(parent.value) ||
!isStandardSyntaxMediaFeatureName(value) ||
isCustomMediaQuery(value)
) {
return;
}
if (!matchesStringOrRegExp(value, blacklist)) {
return;
}
root.walkAtRules(/^media$/i, (atRule) => {
let hasComments = _.get(atRule, 'raws.params.raw');
const mediaRule = hasComments ? hasComments : atRule.params;
mediaParser(mediaRule).walk(/^media-feature$/i, (mediaFeatureNode) => {
const parent = mediaFeatureNode.parent;
const sourceIndex = mediaFeatureNode.sourceIndex;
const value = mediaFeatureNode.value;
if (
isRangeContextMediaFeature(parent.value) ||
!isStandardSyntaxMediaFeatureName(value) ||
isCustomMediaQuery(value)
) {
return;
}
const expectedFeatureName =
expectation === 'lower' ? value.toLowerCase() : value.toUpperCase();
if (value === expectedFeatureName) {
root.walkAtRules(/^media$/i, (atRule) => {
mediaParser(atRule.params).walk(/^media-feature$/i, (mediaFeatureNode) => {
const parent = mediaFeatureNode.parent;
const sourceIndex = mediaFeatureNode.sourceIndex;
const value = mediaFeatureNode.value;
if (
isRangeContextMediaFeature(parent.value) ||
!isStandardSyntaxMediaFeatureName(value) ||
isCustomMediaQuery(value)
) {
return;
}
if (optionsMatches(options, 'ignoreMediaFeatureNames', value)) {
return;
}