From 0ea98d68a3a51ae5dc10a69eaf5a0a04a4d27c8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Afflerbach?= Date: Tue, 25 Oct 2016 10:34:20 +0200 Subject: [PATCH] Save comments adjacent to the document element. Fixes issue #3 (https://github.com/nostrademons/gumbo-libxml/issues/3) --- gumbo_libxml.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/gumbo_libxml.c b/gumbo_libxml.c index 76a0038..c124f39 100644 --- a/gumbo_libxml.c +++ b/gumbo_libxml.c @@ -98,8 +98,23 @@ xmlDocPtr gumbo_libxml_parse_with_options( BAD_CAST doctype->name, BAD_CAST doctype->public_identifier, BAD_CAST doctype->system_identifier); - - xmlDocSetRootElement(doc, convert_node(doc, output->root, false)); + + GumboVector* children = &output->document->v.element.children; + for (unsigned int i = 0; i < children->length; i++) { + GumboNode* child = (GumboNode*) children->data[i]; + + switch (child->type) { + case GUMBO_NODE_COMMENT: + xmlAddChild((xmlNodePtr) doc, xmlNewDocComment(doc, BAD_CAST child->v.text.text)); + break; + case GUMBO_NODE_ELEMENT: + xmlDocSetRootElement(doc, convert_node(doc, output->root, false)); + break; + default: + break; + } + } + gumbo_destroy_output(options, output); return doc;