summaryrefslogtreecommitdiff
blob: c0bc8c1a7b41c5cb3e4ec01547135f513f93b346 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
From 4ef48128bd9a725daca1d5a4aabe0c1665d78742 Mon Sep 17 00:00:00 2001
From: Matthew Wild <mwild1@gmail.com>
Date: Wed, 2 Apr 2014 21:02:29 +0100
Subject: [PATCH] Make merging of CharacterData events optional, controlled by
 the 3rd parameter of lxp.new()

---
 src/lxplib.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/src/lxplib.c b/src/lxplib.c
index a7d6c42..794f6e3 100644
--- a/src/lxplib.c
+++ b/src/lxplib.c
@@ -57,6 +57,7 @@ struct lxp_userdata {
   int tableref;  /* table with callbacks for this parser */
   enum XPState state;
   luaL_Buffer *b;  /* to concatenate sequences of cdata pieces */
+  int bufferCharData; /* whether to buffer cdata pieces */
 };
 
 typedef struct lxp_userdata lxp_userdata;
@@ -171,8 +172,13 @@ static void f_CharData (void *ud, const char *s, int len) {
   lxp_userdata *xpu = (lxp_userdata *)ud;
   if (xpu->state == XPSok) {
     if (getHandle(xpu, CharDataKey) == 0) return;  /* no handle */
-    xpu->state = XPSstring;
-    luaL_buffinit(xpu->L, xpu->b);
+    if(xpu->bufferCharData != 0) {
+      xpu->state = XPSstring;
+      luaL_buffinit(xpu->L, xpu->b);
+    } else {
+      lua_pushlstring(xpu->L, s, len);
+      docall(xpu, 1, 0);
+    }
   }
   if (xpu->state == XPSstring)
     luaL_addlstring(xpu->b, s, len);
@@ -393,8 +399,10 @@ static void checkcallbacks (lua_State *L) {
 
 static int lxp_make_parser (lua_State *L) {
   XML_Parser p;
+  int bufferCharData = (lua_type(L, 3) != LUA_TBOOLEAN) || (lua_toboolean(L, 3) != 0);
   char sep = *luaL_optstring(L, 2, "");
   lxp_userdata *xpu = createlxp(L);
+  xpu->bufferCharData = bufferCharData;
   p = xpu->parser = (sep == '\0') ? XML_ParserCreate(NULL) :
                                     XML_ParserCreateNS(NULL, sep);
   if (!p)
From cbaf85740f6032cfc025c7bde3f9861d15825928 Mon Sep 17 00:00:00 2001
From: Matthew Wild <mwild1@gmail.com>
Date: Wed, 2 Apr 2014 20:56:58 +0100
Subject: [PATCH] Add support for XmlDecl handlers

---
 src/lxplib.c | 14 +++++++++++++-
 src/lxplib.h |  1 +
 2 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/src/lxplib.c b/src/lxplib.c
index 35bec3c..a7d6c42 100644
--- a/src/lxplib.c
+++ b/src/lxplib.c
@@ -345,6 +345,16 @@ static void f_StartDoctypeDecl (void *ud, const XML_Char *doctypeName,
   docall(xpu, 4, 0);
 }
 
+static void f_XmlDecl (void *ud, const XML_Char *version,
+                                 const XML_Char *encoding,
+                                 int standalone) {
+  lxp_userdata *xpu = (lxp_userdata *)ud;
+  if (getHandle(xpu, XmlDeclKey) == 0) return;  /* no handle */
+  lua_pushstring(xpu->L, version);
+  lua_pushstring(xpu->L, encoding);
+  lua_pushboolean(xpu->L, standalone);
+  docall(xpu, 3, 0);
+}
 /* }====================================================== */
 
 
@@ -365,7 +375,7 @@ static void checkcallbacks (lua_State *L) {
     "Default", "DefaultExpand", "StartElement", "EndElement",
     "ExternalEntityRef", "StartNamespaceDecl", "EndNamespaceDecl",
     "NotationDecl", "NotStandalone", "ProcessingInstruction",
-    "UnparsedEntityDecl", "StartDoctypeDecl", NULL};
+    "UnparsedEntityDecl", "StartDoctypeDecl", "XmlDecl", NULL};
   if (hasfield(L, "_nonstrict")) return;
   lua_pushnil(L);
   while (lua_next(L, 1)) {
@@ -420,6 +430,8 @@ static int lxp_make_parser (lua_State *L) {
     XML_SetUnparsedEntityDeclHandler(p, f_UnparsedEntityDecl);
   if (hasfield(L, StartDoctypeDeclKey))
     XML_SetStartDoctypeDeclHandler(p, f_StartDoctypeDecl);
+  if (hasfield(L, XmlDeclKey))
+    XML_SetXmlDeclHandler(p, f_XmlDecl);
   return 1;
 }
 
diff --git a/src/lxplib.h b/src/lxplib.h
index 9c0be4f..4c7084c 100644
--- a/src/lxplib.h
+++ b/src/lxplib.h
@@ -20,5 +20,6 @@
 #define ProcessingInstructionKey	"ProcessingInstruction"
 #define UnparsedEntityDeclKey		"UnparsedEntityDecl"
 #define StartDoctypeDeclKey		"StartDoctypeDecl"
+#define XmlDeclKey			"XmlDecl"
 
 int luaopen_lxp (lua_State *L);