extractInlineStylePropertyValue function

String? extractInlineStylePropertyValue(
  1. String styleAttr,
  2. String propertyName
)

Implementation

String? extractInlineStylePropertyValue(String styleAttr, String propertyName) {
  if (styleAttr.isEmpty || propertyName.isEmpty) return null;

  final String lowerStyle = styleAttr.toLowerCase();
  final String lowerProp = propertyName.toLowerCase();

  int searchFrom = 0;
  while (true) {
    final int idx = lowerStyle.indexOf(lowerProp, searchFrom);
    if (idx == -1) return null;

    // Ensure property boundary: start or preceded by whitespace/semicolon.
    if (idx > 0) {
      final int prev = lowerStyle.codeUnitAt(idx - 1);
      if (prev != 0x3B /* ; */ && !isAsciiWhitespaceCodeUnit(prev)) {
        searchFrom = idx + 1;
        continue;
      }
    }

    int i = idx + lowerProp.length;
    while (i < styleAttr.length && isAsciiWhitespaceCodeUnit(styleAttr.codeUnitAt(i))) {
      i++;
    }
    if (i >= styleAttr.length || styleAttr.codeUnitAt(i) != 0x3A /* : */) {
      searchFrom = idx + 1;
      continue;
    }

    i++; // skip ':'
    while (i < styleAttr.length && isAsciiWhitespaceCodeUnit(styleAttr.codeUnitAt(i))) {
      i++;
    }
    if (i >= styleAttr.length) return '';

    final int valueStart = i;
    int parenDepth = 0;
    String? quote;
    bool escape = false;

    while (i < styleAttr.length) {
      final String ch = styleAttr[i];
      final int cu = styleAttr.codeUnitAt(i);

      if (quote != null) {
        if (escape) {
          escape = false;
        } else if (ch == '\\') {
          escape = true;
        } else if (ch == quote) {
          quote = null;
        }
        i++;
        continue;
      }

      if (ch == '"' || ch == '\'') {
        quote = ch;
        i++;
        continue;
      }

      if (cu == 0x28 /* ( */) {
        parenDepth++;
        i++;
        continue;
      }
      if (cu == 0x29 /* ) */) {
        parenDepth = parenDepth > 0 ? parenDepth - 1 : 0;
        i++;
        continue;
      }

      if (cu == 0x3B /* ; */ && parenDepth == 0) {
        break;
      }
      i++;
    }

    return styleAttr.substring(valueStart, i).trim();
  }
}