Skip to content

Commit

Permalink
feat(lxp): Add parser option to disable attribute positions
Browse files Browse the repository at this point in the history
  • Loading branch information
Omikhleia authored and Didier Willis committed Dec 6, 2024
1 parent 947d2e9 commit 612d99e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
14 changes: 9 additions & 5 deletions docs/manual.html
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,16 @@ <h4>Finishing parsing</h4>
<h4>Constructor</h4>

<dl class="reference">
<dt><strong>lxp.new(<em>callbacks [, separator[, merge_character_data]]</em>)</strong></dt>
<dt><strong>lxp.new(<em>callbacks [, separator[, [merge_character_data, attribute_position]]]</em>)</strong></dt>
<dd>The parser is created by a call to the function <strong>lxp.new</strong>,
which returns the created parser or raises a Lua error. It
receives the callbacks table and optionally the parser <a href="#separator">
separator character</a> used in the namespace expanded element names.
If <em>merge_character_data</em> is false then LuaExpat will not combine multiple
CharacterData calls into one. For more info on this behaviour see CharacterData below.</dd>
CharacterData calls into one. For more info on this behaviour see CharacterData below.
If <em>attribute_position</em> is false then LuaExpat will not include the
attribute position in the attributes table. For more info on this behaviour see
StartElement below.</dd>
</dl>

<h4>Methods</h4>
Expand Down Expand Up @@ -424,7 +427,8 @@ <h4>Callbacks</h4>
every attribute in the element start tag and entries for the
default attributes for that element.<br/>
The attributes are listed by name (including the inherited ones)
and by position (inherited attributes are not considered in the
and, unless <em>attribute_position</em> is set to false when calling
<em>lxp.new()</em>, by position (inherited attributes are not considered in the
position list).<br/>
As an example if the <em>book</em> element has attributes
<em>author</em>, <em>title</em> and an optional <em>format</em>
Expand All @@ -434,8 +438,8 @@ <h4>Callbacks</h4>
</pre>
would be represented as<br/>
<pre class="example">
{[1] = "Ierusalimschy, Roberto",
[2] = "Programming in Lua",
{[1] = "author",
[2] = "title",
author = "Ierusalimschy, Roberto",
format = "printed",
title = "Programming in Lua"}
Expand Down
6 changes: 5 additions & 1 deletion src/lxplib.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ struct lxp_userdata {
enum XPState state;
luaL_Buffer *b; /* to concatenate sequences of cdata pieces */
int bufferCharData; /* whether to buffer cdata pieces */
int attributePosition; /* whether to include attribute position */
};

typedef struct lxp_userdata lxp_userdata;
Expand Down Expand Up @@ -201,7 +202,7 @@ static void f_DefaultExpand (void *ud, const char *data, int len) {
static void f_StartElement (void *ud, const char *name, const char **attrs) {
lxp_userdata *xpu = (lxp_userdata *)ud;
lua_State *L = xpu->L;
int lastspec = XML_GetSpecifiedAttributeCount(xpu->parser) / 2;
int lastspec = xpu->attributePosition != 0 ? XML_GetSpecifiedAttributeCount(xpu->parser) / 2 : 0;
int i = 1;
if (getHandle(xpu, StartElementKey) == 0) return; /* no handle */
lua_pushstring(L, name);
Expand Down Expand Up @@ -546,9 +547,12 @@ static void checkcallbacks (lua_State *L) {

static int lxp_make_parser (lua_State *L) {
XML_Parser p;
int attributePosition = (lua_type(L, 4) != LUA_TBOOLEAN) || (lua_toboolean(L, 4) != 0);
int bufferCharData = (lua_type(L, 3) != LUA_TBOOLEAN) || (lua_toboolean(L, 3) != 0);
char sep = *luaL_optstring(L, 2, "");
lxp_userdata *xpu = createlxp(L);
printf("lxp_make_parser: '%c' %d %d\n", sep, bufferCharData, attributePosition);
xpu->attributePosition = attributePosition;
xpu->bufferCharData = bufferCharData;
p = xpu->parser = (sep == '\0') ? XML_ParserCreate(NULL) :
XML_ParserCreateNS(NULL, sep);
Expand Down

0 comments on commit 612d99e

Please sign in to comment.