handleSetStyleTexts method

void handleSetStyleTexts(
  1. int? id,
  2. Map<String, dynamic> params
)

Implementation

void handleSetStyleTexts(int? id, Map<String, dynamic> params) {
  final ctx = dbgContext;
  if (ctx == null) {
    sendToFrontend(id, null);
    return;
  }
  List edits = params['edits'];
  List<CSSStyle?> styles = [];

  // Apply inline style edits by replacing the full inline style text of the target.
  for (Map<String, dynamic> edit in edits) {
    // Use styleSheetId to identify element (handles inline:<nodeId> or numeric).
    final dynamic rawStyleSheetId = edit['styleSheetId'];
    int? nodeId;
    int? frontendNodeId;
    if (rawStyleSheetId is int) {
      frontendNodeId = rawStyleSheetId;
      nodeId = ctx.getTargetIdByNodeId(rawStyleSheetId);
    } else if (rawStyleSheetId is String) {
      String sid = rawStyleSheetId;
      if (sid.startsWith('inline:')) {
        final String rest = sid.substring('inline:'.length);
        final int? nid = int.tryParse(rest);
        if (nid != null) {
          frontendNodeId = nid;
          nodeId = ctx.getTargetIdByNodeId(nid);
        }
      } else {
        final int? nid = int.tryParse(sid);
        if (nid != null) {
          frontendNodeId = nid;
          nodeId = ctx.getTargetIdByNodeId(nid);
        }
      }
    }
    String text = (edit['text'] ?? '').toString();
    if (nodeId == null) {
      styles.add(null);
      continue;
    }
    BindingObject? element = ctx.getBindingObject(Pointer.fromAddress(nodeId));
    if (element is Element) {
      // Replace full inline style with the provided text.
      _applyInlineStyleText(element, text);
      styles.add(buildInlineStyle(element));
      if (frontendNodeId != null) {
        _trackNodeComputedUpdate(frontendNodeId);
      }
    } else {
      styles.add(null);
    }
  }

  if (DebugFlags.enableDevToolsProtocolLogs) {
    try {
      final appliedCount = styles.where((s) => s != null).fold<int>(0, (acc, s) => acc + (s!.cssProperties.length));
      devToolsProtocolLogger.finer('[DevTools] CSS.setStyleTexts edits=${edits.length} appliedProps=$appliedCount');
    } catch (_) {}
  }
  sendToFrontend(
      id,
      JSONEncodableMap({
        'styles': styles,
      }));
}