MCPcopy Create free account
hub / github.com/Khan/perseus / getConditionalExpressionValue

Function getConditionalExpressionValue

utils/extract-css.js:461–526  ·  view source on GitHub ↗
(propertyName, expressionNode)

Source from the content-addressed store, hash-verified

459};
460
461const getConditionalExpressionValue = (propertyName, expressionNode) => {
462 // Convert conditional values into their own class that can be applied as needed.
463
464 let nodeValue = expressionNode.alternate.value;
465 const alternateValue =
466 propertyRejectsPx(propertyName) ||
467 nodeValue === 0 ||
468 `${nodeValue}`.includes("px") ||
469 isNaN(nodeValue)
470 ? `${nodeValue}`
471 : `${nodeValue}px`;
472
473 // Conditional value used in a separate class
474 nodeValue = expressionNode.consequent.value;
475 let consequentValue =
476 propertyRejectsPx(propertyName) ||
477 nodeValue === 0 ||
478 `${nodeValue}`.includes("px") ||
479 isNaN(nodeValue)
480 ? `${nodeValue}`
481 : `${nodeValue}px`;
482 consequentValue = replacePxWithRem(propertyName, `${consequentValue}`);
483 consequentValue = convertToWbColor(propertyName, consequentValue);
484 let testName = "";
485 if (expressionNode.test.type === "Identifier") {
486 testName = camelToKabob(expressionNode.test.name);
487 } else if (expressionNode.test.type === "MemberExpression") {
488 testName = camelToKabob(expressionNode.test.property.name);
489 } else if (expressionNode.test.type === "BinaryExpression") {
490 const identifierName = expressionNode.test.left.type.includes(
491 "Expression",
492 )
493 ? `${expressionNode.test.left.property.name}-${expressionNode.test.right.value}`
494 : `${expressionNode.test.right.property.name}-${expressionNode.test.left.value}`;
495 testName = camelToKabob(identifierName);
496 }
497 if (testName !== "") {
498 const nestedRuleSet = [
499 {
500 property: `.${testName}`,
501 value: "",
502 line: null,
503 leadingComments: [],
504 trailingComments: [],
505 nestedRuleSet: [
506 {
507 property: propertyName,
508 value: consequentValue,
509 line: null,
510 leadingComments: [],
511 trailingComments: [],
512 nestedRuleSet: null,
513 },
514 ],
515 },
516 ];
517 return {
518 conditionalValue: alternateValue,

Callers 1

getCssPropertyInfoFunction · 0.85

Calls 5

propertyRejectsPxFunction · 0.85
isNaNFunction · 0.85
replacePxWithRemFunction · 0.85
convertToWbColorFunction · 0.85
camelToKabobFunction · 0.85

Tested by

no test coverage detected