Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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
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;
}