* xml.c (make_dom): Return comments, too.
[bpt/emacs.git] / src / xml.c
CommitLineData
381408e2 1/* Interface to libxml2.
73b0cd50 2 Copyright (C) 2010-2011 Free Software Foundation, Inc.
381408e2
LMI
3
4This file is part of GNU Emacs.
5
6GNU Emacs is free software: you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation, either version 3 of the License, or
9(at your option) any later version.
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
18
19#include <config.h>
20
21#ifdef HAVE_LIBXML2
22
23#include <setjmp.h>
24#include <libxml/tree.h>
25#include <libxml/parser.h>
26#include <libxml/HTMLparser.h>
27
28#include "lisp.h"
29#include "buffer.h"
30
c9735e30
PE
31static Lisp_Object
32make_dom (xmlNode *node)
381408e2 33{
4b9832a6
CY
34 if (node->type == XML_ELEMENT_NODE)
35 {
c45e5276 36 Lisp_Object result = Fcons (intern ((char *) node->name), Qnil);
4b9832a6
CY
37 xmlNode *child;
38 xmlAttr *property;
39 Lisp_Object plist = Qnil;
40
41 /* First add the attributes. */
42 property = node->properties;
43 while (property != NULL)
44 {
45 if (property->children &&
46 property->children->content)
47 {
c45e5276
PE
48 char *content = (char *) property->children->content;
49 plist = Fcons (Fcons (intern ((char *) property->name),
50 build_string (content)),
4b9832a6
CY
51 plist);
52 }
53 property = property->next;
54 }
55 result = Fcons (Fnreverse (plist), result);
56
57 /* Then add the children of the node. */
58 child = node->children;
59 while (child != NULL)
60 {
61 result = Fcons (make_dom (child), result);
62 child = child->next;
63 }
64
65 return Fnreverse (result);
381408e2 66 }
3c2317e8 67 else if (node->type == XML_TEXT_NODE || node->type == XML_CDATA_SECTION_NODE)
4b9832a6
CY
68 {
69 if (node->content)
c45e5276 70 return build_string ((char *) node->content);
4b9832a6
CY
71 else
72 return Qnil;
381408e2 73 }
1e98674d
LMI
74 else if (node->type == XML_COMMENT_NODE)
75 {
76 if (node->content)
77 return list3 (intern ("comment"), Qnil,
78 build_string ((char *) node->content));
79 else
80 return Qnil;
81 }
4b9832a6 82 else
381408e2
LMI
83 return Qnil;
84}
85
86static Lisp_Object
1b217849 87parse_region (Lisp_Object start, Lisp_Object end, Lisp_Object base_url, int htmlp)
381408e2
LMI
88{
89 xmlDoc *doc;
90 xmlNode *node;
c97c655f 91 Lisp_Object result = Qnil;
55e572ef 92 const char *burl = "";
1b217849
LMI
93 EMACS_INT bytes;
94 EMACS_INT istart, iend;
8b620f11 95
381408e2 96 LIBXML_TEST_VERSION;
8b620f11 97
1b217849 98 validate_region (&start, &end);
20a5e996 99
1b217849
LMI
100 istart = XINT (start);
101 iend = XINT (end);
102
103 if (istart < GPT && GPT < iend)
104 move_gap (iend);
381408e2 105
4b9832a6
CY
106 if (! NILP (base_url))
107 {
108 CHECK_STRING (base_url);
42a5b22f 109 burl = SSDATA (base_url);
4b9832a6 110 }
8b620f11 111
1b217849 112 bytes = CHAR_TO_BYTE (iend) - CHAR_TO_BYTE (istart);
20a5e996 113
1b217849 114 if (htmlp)
c45e5276 115 doc = htmlReadMemory ((char *) BYTE_POS_ADDR (CHAR_TO_BYTE (istart)),
1b217849
LMI
116 bytes, burl, "utf-8",
117 HTML_PARSE_RECOVER|HTML_PARSE_NONET|
3c2317e8
LMI
118 HTML_PARSE_NOWARNING|HTML_PARSE_NOERROR|
119 HTML_PARSE_NOBLANKS);
1b217849 120 else
c45e5276 121 doc = xmlReadMemory ((char *) BYTE_POS_ADDR (CHAR_TO_BYTE (istart)),
1b217849
LMI
122 bytes, burl, "utf-8",
123 XML_PARSE_NONET|XML_PARSE_NOWARNING|
87302331 124 XML_PARSE_NOBLANKS |XML_PARSE_NOERROR);
4b9832a6
CY
125
126 if (doc != NULL)
127 {
128 node = xmlDocGetRootElement (doc);
129 if (node != NULL)
130 result = make_dom (node);
131 xmlFreeDoc (doc);
132 xmlCleanupParser ();
133 }
8b620f11 134
381408e2
LMI
135 return result;
136}
137
1b217849
LMI
138DEFUN ("libxml-parse-html-region", Flibxml_parse_html_region,
139 Slibxml_parse_html_region,
140 2, 3, 0,
141 doc: /* Parse the region as an HTML document and return the parse tree.
4b9832a6 142If BASE-URL is non-nil, it is used to expand relative URLs. */)
1b217849 143 (Lisp_Object start, Lisp_Object end, Lisp_Object base_url)
381408e2 144{
1b217849 145 return parse_region (start, end, base_url, 1);
381408e2
LMI
146}
147
1b217849
LMI
148DEFUN ("libxml-parse-xml-region", Flibxml_parse_xml_region,
149 Slibxml_parse_xml_region,
150 2, 3, 0,
151 doc: /* Parse the region as an XML document and return the parse tree.
4b9832a6 152If BASE-URL is non-nil, it is used to expand relative URLs. */)
1b217849 153 (Lisp_Object start, Lisp_Object end, Lisp_Object base_url)
381408e2 154{
1b217849 155 return parse_region (start, end, base_url, 0);
381408e2
LMI
156}
157
158\f
159/***********************************************************************
160 Initialization
161 ***********************************************************************/
162void
163syms_of_xml (void)
164{
1b217849
LMI
165 defsubr (&Slibxml_parse_html_region);
166 defsubr (&Slibxml_parse_xml_region);
381408e2
LMI
167}
168
169#endif /* HAVE_LIBXML2 */