debugPrintGumboNodeTree function

void debugPrintGumboNodeTree(
  1. Pointer<NativeGumboNode> nodePtr, [
  2. int indent = 0
])

Implementation

void debugPrintGumboNodeTree(Pointer<NativeGumboNode> nodePtr, [int indent = 0]) {
  final p = ' ' * (indent * 2);
  final type = nodePtr.ref.type;
  print('$p node type: $type');
  switch(type) {
    case GumboNodeType.GUMBO_NODE_ELEMENT: {
      // element
      final element = nodePtr.ref.v.element;
      final children = element.children;
      final tag = element.tag;
      final tagName = element.original_tag.length != 0 ? element.original_tag.data.toDartString(length: element.original_tag.length) : '(none)';

      print('$p tag: $tag($tagName) in namespace ${element.tag_namespace} with ${children.length} child');

      final attributes = element.attributes;
      for (int i = 0; i < attributes.length; i++) {
        final attr = attributes.data[i] as Pointer<NativeGumboAttribute>;
        final name = attr.ref.name.toDartString();
        final value = attr.ref.value.toDartString();
        print('$p $name=$value');
      }

      for (int i = 0; i < children.length; i++) {
        final childRef = children.data[i] as Pointer<NativeGumboNode>;
        debugPrintGumboNodeTree(childRef, indent + 1);
      }
      break;
    }
    case GumboNodeType.GUMBO_NODE_TEXT:
    case GumboNodeType.GUMBO_NODE_COMMENT: {
      final node = nodePtr.ref.v.text;
      final content = node.text.toDartString();
      print('$p "$content"');
      break;
    }
    case GumboNodeType.GUMBO_NODE_WHITESPACE: {
      // others whitespace, ignored
      break;
    }
  }
}