setSheetStyle method

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

Implementation

void setSheetStyle(String property, String value,
    {String? baseHref, bool fromNative = false, bool important = false}) {
  property = CSSStyleDeclaration.normalizePropertyName(property);
  final bool enableBlink = ownerDocument.ownerView.enableBlink;
  final bool validate = !(fromNative && enableBlink);
  final bool previousImportant = _sheetStyle?.isImportant(property) ?? false;

  bool derivedImportant = important;
  String derivedValue = value;

  // Compatibility: allow `!important` suffix in the value string.
  if (fromNative && !derivedImportant) {
    int end = derivedValue.length;
    while (end > 0 && derivedValue.codeUnitAt(end - 1) <= 0x20) {
      end--;
    }

    const String keyword = 'important';
    if (end >= keyword.length) {
      final int keywordStart = end - keyword.length;
      if (derivedValue.substring(keywordStart, end).toLowerCase() == keyword) {
        int i = keywordStart;
        while (i > 0 && derivedValue.codeUnitAt(i - 1) <= 0x20) {
          i--;
        }
        if (i > 0 && derivedValue.codeUnitAt(i - 1) == 0x21) {
          derivedImportant = true;
          derivedValue = derivedValue.substring(0, i - 1).trimRight();
        }
      }
    }
  }

  InlineStyleEntry entry = InlineStyleEntry(derivedValue, important: derivedImportant);
  if (fromNative && !derivedImportant) {
    entry = _normalizeInlineStyleEntryFromNative(entry);
  }

  // Removing a sheet declaration.
  if (entry.value.isEmpty) {
    _sheetStyle?.removeProperty(property);
    if (_sheetStyle?.isEmpty == true) {
      _sheetStyle = null;
    }

    // If there is an inline declaration, re-enqueue it so we don't lose it
    // after clearing a previously-winning sheet `!important`.
    final InlineStyleEntry? inlineEntry = inlineStyle[property];
    if (inlineEntry != null && inlineEntry.value.isNotEmpty) {
      style.enqueueInlineProperty(
        property,
        inlineEntry.value,
        isImportant: inlineEntry.important ? true : null,
        validate: validate,
      );
    } else {
      style.removeProperty(property, previousImportant ? true : null);
    }
    return;
  }

  (_sheetStyle ??= CSSStyleDeclaration.sheet()).setProperty(
    property,
    entry.value,
    isImportant: entry.important ? true : null,
    propertyType: PropertyType.sheet,
    baseHref: baseHref,
    validate: validate,
  );

  // If importance was downgraded, clear the previous important value so
  // subsequent sheet updates are not blocked by stale `!important`.
  if (previousImportant && !entry.important) {
    style.removeProperty(property, true);
  }
  style.enqueueSheetProperty(
    property,
    entry.value,
    isImportant: entry.important ? true : null,
    baseHref: baseHref,
    validate: validate,
  );
}