setInlineStyle method

void setInlineStyle(
  1. String property,
  2. String value, {
  3. String? baseHref,
  4. bool fromNative = false,
})

Implementation

void setInlineStyle(String property, String value,
    {String? baseHref, bool fromNative = false}) {
  final bool enableBlink = ownerDocument.ownerView.enableBlink;
  final bool validate = !(fromNative && enableBlink);
  final String raw = value;
  String cleaned = raw.trim();
  var important = false;
  final String lower = cleaned.toLowerCase();
  const String importantSuffix = '!important';
  if (lower.endsWith(importantSuffix)) {
    important = true;
    cleaned = cleaned
        .substring(0, cleaned.length - importantSuffix.length)
        .trimRight();
  }

  // CSSOM setter semantics require that syntactically-invalid assignments
  // do nothing (keep the previous specified value). For gap properties,
  // validate early so we don't mutate Element.inlineStyle before the
  // CSSStyleDeclaration validation/expansion runs.
  final bool isGapProp = property == GAP || property == ROW_GAP || property == COLUMN_GAP;
  if (isGapProp && cleaned.isNotEmpty) {
    bool isValidGapDeclaration(String v) {
      // CSS-wide keywords are allowed on the shorthand as a single token.
      if (v == INHERIT || CSSLength.isInitial(v)) return true;
      final List<String> tokens = splitByAsciiWhitespacePreservingGroups(v);
      if (tokens.isEmpty || tokens.length > 2) return false;
      for (final token in tokens) {
        // CSS-wide keywords cannot appear as components in a multi-token shorthand.
        if (token == INHERIT || CSSLength.isInitial(token)) return tokens.length == 1;
        if (!CSSGap.isValidGapValue(token)) return false;
      }
      return true;
    }

    if (property == GAP) {
      if (!isValidGapDeclaration(cleaned)) {
        return;
      }
    } else if (property == ROW_GAP || property == COLUMN_GAP) {
      if (!(cleaned == INHERIT || CSSLength.isInitial(cleaned) || CSSGap.isValidGapValue(cleaned))) {
        return;
      }
    }
  }

  // Current only for mark property is setting by inline style.
  inlineStyle[property] = cleaned;
  if (important) {
    _inlineStyleImportants[property] = true;
  } else {
    _inlineStyleImportants.remove(property);
  }

  // recalculate matching styles for element when inline styles are removed.
  if (cleaned.isEmpty) {
    inlineStyle.remove(property);
    final bool prevImportant =
        _inlineStyleImportants.remove(property) == true;
    style.removeProperty(property, prevImportant ? true : null);
    // When Blink CSS is enabled, style cascading and validation happen on
    // the native side. Avoid expensive Dart-side recalculation here.
    if (!(fromNative && enableBlink)) {
      recalculateStyle();
    }
  } else {
    style.setProperty(property, cleaned,
        isImportant: important ? true : null,
        baseHref: baseHref,
        validate: validate);
  }
}