MCPcopy Create free account
hub / github.com/webstudio-is/webstudio / parseMediaQuery

Function parseMediaQuery

packages/css-data/src/parse-css.ts:1135–1194  ·  view source on GitHub ↗
(
  mediaQuery: string
)

Source from the content-addressed store, hash-verified

1133};
1134
1135export const parseMediaQuery = (
1136 mediaQuery: string
1137): undefined | ParsedBreakpoint => {
1138 const ast = csstree.parse(mediaQuery, { context: "mediaQuery" });
1139 let minWidth: undefined | number;
1140 let maxWidth: undefined | number;
1141 let currentWidthProperty: undefined | "minWidth" | "maxWidth";
1142 const otherFeatures: string[] = [];
1143
1144 csstree.walk(ast, (node) => {
1145 if (node.type === "Feature") {
1146 if (node.name === "min-width") {
1147 currentWidthProperty = "minWidth";
1148 } else if (node.name === "max-width") {
1149 currentWidthProperty = "maxWidth";
1150 } else {
1151 currentWidthProperty = undefined;
1152 // Capture any other media feature as custom condition
1153 const generated = csstree.generate(node);
1154 // Remove outer parentheses if present
1155 let cleaned =
1156 generated.startsWith("(") && generated.endsWith(")")
1157 ? generated.slice(1, -1)
1158 : generated;
1159 // Normalize whitespace: remove spaces around colons for consistency
1160 cleaned = cleaned.replace(/\s*:\s*/g, ":");
1161 otherFeatures.push(cleaned);
1162 }
1163 }
1164 if (node.type === "Dimension" && node.unit === "px") {
1165 const value = Number(node.value);
1166 if (currentWidthProperty === "minWidth") {
1167 minWidth = value;
1168 } else if (currentWidthProperty === "maxWidth") {
1169 maxWidth = value;
1170 }
1171 currentWidthProperty = undefined;
1172 }
1173 });
1174
1175 const condition =
1176 otherFeatures.length > 0 ? otherFeatures.join(" and ") : undefined;
1177
1178 const hasWidth = minWidth !== undefined || maxWidth !== undefined;
1179
1180 // If there's a custom condition and no width, return only condition
1181 if (condition !== undefined && !hasWidth) {
1182 return { condition };
1183 }
1184
1185 if (!hasWidth && condition === undefined) {
1186 return;
1187 }
1188
1189 return {
1190 ...(minWidth !== undefined ? { minWidth } : {}),
1191 ...(maxWidth !== undefined ? { maxWidth } : {}),
1192 ...(condition !== undefined ? { condition } : {}),

Callers 5

parse-css.test.tsFile · 0.90
getBreakpointIdFunction · 0.90
generateFragmentFromHtmlFunction · 0.90
adaptBreakpointsFunction · 0.90
getBreakpointIdFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected