resolveBackgroundImage static method

dynamic resolveBackgroundImage(
  1. String present,
  2. RenderStyle renderStyle,
  3. String property,
  4. WebFController controller,
  5. String? baseHref,
)

Implementation

static resolveBackgroundImage(
    String present, RenderStyle renderStyle, String property, WebFController controller, String? baseHref) {
  // Expand CSS variables inside the background-image string so that
  // values like linear-gradient(..., var(--tw-gradient-stops)) work.
  // Tailwind sets --tw-gradient-stops to a comma-separated list
  // (e.g., "var(--tw-gradient-from), var(--tw-gradient-to)") which
  // must be expanded before parsing function args, otherwise only the
  // first token would be seen and gradients would be dropped.
  String expanded = _expandBackgroundVars(present, renderStyle);
  List<CSSFunctionalNotation> functions = CSSFunction.parseFunction(expanded);
  // Validate gradient syntaxes early. In particular, we must reject invalid
  // color-stop tokens like "green 75% green 100%" (missing comma) which
  // browsers treat as an invalid gradient and thus ignore.
  if (functions.isNotEmpty) {
    final List<CSSFunctionalNotation> filtered = <CSSFunctionalNotation>[];
    for (final f in functions) {
      if (f.name == 'linear-gradient' || f.name == 'repeating-linear-gradient') {
        final bool ok = _isValidLinearGradientArgs(f.args, renderStyle, property);
        if (!ok) {
          if (DebugFlags.enableBackgroundLogs) {
            renderingLogger.warning('[Background] drop invalid ${f.name} args=${f.args} present="$present"');
          }
          continue;
        }
      }
      filtered.add(f);
    }
    functions = filtered;
  }
  if (DebugFlags.enableBackgroundLogs) {
    renderingLogger.finer('[Background] resolveBackgroundImage present="$present" expanded="$expanded" '
        'fnCount=${functions.length}');
    for (final f in functions) {
      if (f.name == 'url') {
        final raw = f.args.isNotEmpty ? f.args[0] : '';
        renderingLogger.finer('[Background] resolve image url raw=$raw baseHref=${baseHref ?? controller.url}');
      } else if (f.name.contains('gradient')) {
        renderingLogger.finer('[Background] resolve gradient ${f.name} args=${f.args.length} rawArgs=${f.args}');
      }
    }
  }
  return CSSBackgroundImage(functions, renderStyle, controller, baseHref: baseHref);
}