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

Function isValidDeclaration

packages/css-data/src/parse-css-value.ts:250–354  ·  view source on GitHub ↗
(
  property: CssProperty,
  value: string
)

Source from the content-addressed store, hash-verified

248// Because csstree parser has bugs we use CSSStyleValue to validate css properties if available
249// and fall back to csstree.
250export const isValidDeclaration = (
251 property: CssProperty,
252 value: string
253): boolean => {
254 // Custom properties accept any valid declaration value token stream, but
255 // malformed strings, URLs, comments, or blocks can invalidate the whole rule.
256 if (property.startsWith("--")) {
257 return isValidCustomPropertyValue(value);
258 }
259
260 // Parse once upfront for structural inspection. cssTryParseValue may return
261 // null for values that the browser can still handle (csstree has known gaps),
262 // so null here does NOT mean the value is invalid — we fall through to other paths.
263 const ast = cssTryParseValue(value);
264
265 // Two CSS constructs cannot be validated by any lexer path and must be accepted
266 // unconditionally regardless of property:
267 // var() — the variable's value is unknown at validation time
268 // relative color (rgb(from ...), oklch(from ...), etc.) — csstree lexer
269 // returns the same "Mismatch" error as genuinely invalid values
270 // Detecting these here also ensures var() stays valid for the keyword-only
271 // properties below, which don't go through CSSStyleValue.parse.
272 if (ast != null) {
273 let hasUncheckedSyntax = false;
274 walk(ast, (node) => {
275 if (node.type === "Function") {
276 if (
277 node.name === "var" ||
278 (node.children.first?.type === "Identifier" &&
279 node.children.first.name === "from")
280 ) {
281 hasUncheckedSyntax = true;
282 }
283 }
284 });
285 if (hasUncheckedSyntax) {
286 return true;
287 }
288 }
289
290 // these properties have poor support in browser
291 // though rendered styles are merged as shorthand
292 // so validate artifically
293 if (
294 property === "white-space-collapse" ||
295 property === "text-wrap-mode" ||
296 property === "text-wrap-style"
297 ) {
298 return keywordValues[property].includes(value);
299 }
300
301 // @todo remove after csstree fixes
302 // - https://github.com/csstree/csstree/issues/246
303 // - https://github.com/csstree/csstree/issues/164
304 if (typeof CSS !== "undefined" && CSS.supports(property, value)) {
305 return true;
306 }
307

Callers 5

handleChangeFunction · 0.90
buildOptionsFunction · 0.90
validateValueFunction · 0.90
parseCssValueFunction · 0.85

Calls 5

cssTryParseValueFunction · 0.85
canFallbackToCssMathFunction · 0.85
walkFunction · 0.50

Tested by

no test coverage detected