nodeToOperation method

List<Operation> nodeToOperation(
  1. Node node,
  2. HtmlOperations htmlToOp, [
  3. bool nextIsBlock = false,
  4. bool transformTableAsEmbed = false,
])

Converts a single DOM node into Delta operations using htmlToOp.

Processes a single DOM node and converts it into Delta operations using the provided htmlToOp instance.

Parameters:

  • node: The DOM node to convert into Delta operations.

Returns: A list of Operation objects representing the formatted content of the node.

Example:

final node = dparser.parseFragment('<strong>Hello</strong>');
final operations = converter.nodeToOperation(node.firstChild!, converter.htmlToOp);
print(operations); // Output: [Operation{insert: "Hello", attributes: {bold: true}}]

Implementation

List<Operation> nodeToOperation(
  dom.Node node,
  HtmlOperations htmlToOp, [
  bool nextIsBlock = false,
  bool transformTableAsEmbed = false,
]) {
  List<Operation> operations = [];
  if (node is dom.Text) {
    operations.add(Operation.insert(node.text));
  }
  if (node is dom.Element) {
    if (blackNodesList.contains(node.localName)) {
      if (nextIsBlock) operations.add(Operation.insert('\n'));
      operations.add(Operation.insert(node.text));
      return operations;
    }
    List<Operation> ops = htmlToOp.resolveCurrentElement(
      node,
      0,
      transformTableAsEmbed,
    );
    operations.addAll(ops);
    // Check if the nextElement is a block AND if the last
    // operation already has a new line, this would otherwise
    // create a double new line
    if (nextIsBlock && operations.last.data != '\n') {
      operations.add(Operation.insert('\n'));
    }
  }

  return operations;
}