Skip to content

Instantly share code, notes, and snippets.

@tenderlove
Created Apr 29, 2009
Embed
What would you like to do?
From 761718bfe69bd0ab61741487931409971c485f19 Mon Sep 17 00:00:00 2001
From: Aaron Patterson <aaron.patterson@gmail.com>
Date: Wed, 29 Apr 2009 00:09:20 -0700
Subject: [PATCH] html parser should keep track of line numbers
---
HTMLparser.c | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
diff --git a/HTMLparser.c b/HTMLparser.c
index 24b0fc0..de497eb 100644
--- a/HTMLparser.c
+++ b/HTMLparser.c
@@ -5652,6 +5652,7 @@ htmlSAXParseFile(const char *filename, const char *encoding, htmlSAXHandlerPtr s
xmlInitParser();
ctxt = htmlCreateFileParserCtxt(filename, encoding);
+ ctxt->linenumbers = 1;
if (ctxt == NULL) return(NULL);
if (sax != NULL) {
oldsax = ctxt->sax;
--
1.6.0.4
#include <stdio.h>
#include <stdlib.h>
#include <libxml/HTMLparser.h>
#include <libxml/HTMLtree.h>
void walk_tree(xmlNodePtr node) {
printf("name: '%s', line: %d\n", node->name, node->line);
xmlNodePtr child = node->children;
while(NULL != child) {
walk_tree(child);
child = child->next;
}
}
int main(int argc, char *argv[]) {
printf("############ HTML\n");
htmlDocPtr html_doc = htmlParseFile(argv[1], NULL);
walk_tree(xmlDocGetRootElement(html_doc));
printf("############ XML\n");
xmlDocPtr xml_doc = xmlParseFile(argv[1]);
walk_tree(xmlDocGetRootElement(xml_doc));
return 0;
}
<html>
<body>
<p>
Hello world
</p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment