convertDocument method

Delta convertDocument(
  1. Document $document
)

Converts a full DOM document into Delta operations.

Processes the entire DOM document $document and converts its nodes into Delta operations. Custom blocks can be applied based on registered customBlocks.

Parameters:

  • $document: The DOM document to convert into Delta operations.

Returns: A Delta object representing the formatted content from the DOM document.

Example:

final document = dparser.parse('<p>Hello <strong>world</strong></p>');
final delta = converter.convertDocument(document);
print(delta.toJson()); // Output: [{"insert":"Hello "},{"insert":"world","attributes":{"bold":true}},{"insert":"\n"}]

Implementation

Delta convertDocument(dom.Document $document) {
  final Delta delta = Delta();
  final dom.Element? $body = $document.body;
  final dom.Element? $html = $document.documentElement;

  // Determine nodes to process: <body>, <html>, or document nodes if neither is present
  final List<dom.Node> nodesToProcess =
      $body?.nodes ?? $html?.nodes ?? $document.nodes;

  for (var node in nodesToProcess) {
    if (customBlocks != null &&
        customBlocks!.isNotEmpty &&
        node is dom.Element) {
      for (var customBlock in customBlocks!) {
        if (customBlock.matches(node)) {
          final operations = customBlock.convert(node);
          operations.forEach((Operation op) {
            delta.insert(op.data, op.attributes);
          });
          continue;
        }
      }
    }
    final List<Operation> operations = nodeToOperation(node, htmlToOp);
    if (operations.isNotEmpty) {
      for (final op in operations) {
        delta.insert(op.data, op.attributes);
      }
    }
  }
  //ensure insert a new line at the final to avoid any conflict with assertions
  final lastOpdata = delta.last;
  final bool lastDataIsNotNewLine = lastOpdata.data.toString() != '\n';
  final bool hasAttributes = lastOpdata.attributes != null;
  if (lastDataIsNotNewLine && hasAttributes ||
      lastDataIsNotNewLine ||
      !lastDataIsNotNewLine && hasAttributes) {
    delta.insert('\n');
  }
  return delta;
}