aboutsummaryrefslogtreecommitdiff
path: root/src/Dom/Dom.c
blob: ede7ddd47ba4bfc84fcd24260ac85bd8c91f3124 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#include "Dom/Dom.h"

#include <lauxlib.h>
#include <limits.h>
#include <stddef.h>
#include <string.h>
#include <strings.h>

static int absolute_index(lua_State *lua, int index) {
  return index < 0 ? lua_gettop(lua) + index + 1 : index;
}

static int is_lua_element(const xmlNode *node) {
  return node->type == XML_ELEMENT_NODE &&
         strcasecmp((const char *)node->name, "lua") == 0;
}

static void set_string_field(lua_State *lua, int table_index, const char *name,
                             const char *value) {
  lua_pushstring(lua, value);
  lua_setfield(lua, table_index, name);
}

static int has_structured_children(const xmlNode *node) {
  const xmlNode *child;

  for (child = node->children; child != NULL; child = child->next) {
    if (is_lua_element(child)) {
      continue;
    }
    if (child->type != XML_TEXT_NODE && child->type != XML_CDATA_SECTION_NODE) {
      return 1;
    }
  }
  return 0;
}

static void push_element(lua_State *lua, const xmlNode *node);

static void push_special_node(lua_State *lua, const xmlNode *node) {
  lua_newtable(lua);
  if (node->type == XML_COMMENT_NODE) {
    set_string_field(lua, -2, "kind", "comment");
  } else {
    set_string_field(lua, -2, "kind", "processing_instruction");
    set_string_field(lua, -2, "target", (const char *)node->name);
  }
  set_string_field(lua, -2, "text",
                   node->content == NULL ? "" : (const char *)node->content);
}

static void add_child_alias(lua_State *lua, int element_index,
                            const xmlNode *child, int child_index) {
  const char *name = (const char *)child->name;

  lua_getfield(lua, element_index, name);
  if (lua_isnil(lua, -1)) {
    lua_pop(lua, 1);
    lua_pushvalue(lua, child_index);
    lua_setfield(lua, element_index, name);
    return;
  }
  lua_pop(lua, 1);
}

static void push_children(lua_State *lua, const xmlNode *node,
                          int element_index, int include_text) {
  const xmlNode *child;
  int children_index;
  int array_index = 1;

  lua_newtable(lua);
  children_index = lua_gettop(lua);

  for (child = node->children; child != NULL; child = child->next) {
    if (is_lua_element(child)) {
      continue;
    }

    if (child->type == XML_ELEMENT_NODE) {
      int child_index;

      push_element(lua, child);
      child_index = lua_gettop(lua);
      lua_pushvalue(lua, child_index);
      lua_rawseti(lua, children_index, array_index++);
      add_child_alias(lua, element_index, child, child_index);
      lua_pop(lua, 1);
    } else if (include_text && (child->type == XML_TEXT_NODE ||
                                child->type == XML_CDATA_SECTION_NODE)) {
      lua_pushstring(
          lua, child->content == NULL ? "" : (const char *)child->content);
      lua_rawseti(lua, children_index, array_index++);
    } else if (child->type == XML_COMMENT_NODE || child->type == XML_PI_NODE) {
      push_special_node(lua, child);
      lua_rawseti(lua, children_index, array_index++);
    }
  }

  lua_setfield(lua, element_index, "children");
}

static void push_attributes(lua_State *lua, const xmlNode *node,
                            int element_index) {
  const xmlAttr *attribute;

  lua_newtable(lua);
  for (attribute = node->properties; attribute != NULL;
       attribute = attribute->next) {
    xmlChar *value = xmlNodeListGetString(node->doc, attribute->children, 1);

    lua_pushstring(lua, value == NULL ? "" : (const char *)value);
    lua_setfield(lua, -2, (const char *)attribute->name);
    xmlFree(value);
  }
  lua_setfield(lua, element_index, "attributes");
}

static void push_text(lua_State *lua, const xmlNode *node, int element_index) {
  const xmlNode *child;
  luaL_Buffer buffer;

  luaL_buffinit(lua, &buffer);
  for (child = node->children; child != NULL; child = child->next) {
    if (child->type == XML_TEXT_NODE || child->type == XML_CDATA_SECTION_NODE) {
      luaL_addstring(
          &buffer, child->content == NULL ? "" : (const char *)child->content);
    }
  }
  luaL_pushresult(&buffer);
  lua_setfield(lua, element_index, "text");
}

static void push_element(lua_State *lua, const xmlNode *node) {
  int element_index;
  int structured_children = has_structured_children(node);

  lua_newtable(lua);
  element_index = lua_gettop(lua);
  set_string_field(lua, element_index, "tag", (const char *)node->name);
  push_attributes(lua, node, element_index);
  push_children(lua, node, element_index, structured_children);
  if (!structured_children) {
    push_text(lua, node, element_index);
  }
}

void dom_push_page(lua_State *lua, const xmlNode *root) {
  push_element(lua, root);
}

static xmlNodePtr node_from_table(lua_State *lua, int table_index, int *valid);

static int add_attributes(lua_State *lua, int table_index, xmlNodePtr node) {
  int attributes_index;
  int valid = 1;

  lua_getfield(lua, table_index, "attributes");
  attributes_index = lua_gettop(lua);
  if (lua_istable(lua, -1)) {
    lua_pushnil(lua);
    while (valid && lua_next(lua, -2) != 0) {
      if (lua_type(lua, -2) != LUA_TSTRING ||
          lua_type(lua, -1) != LUA_TSTRING ||
          xmlNewProp(node, BAD_CAST lua_tostring(lua, -2),
                     BAD_CAST lua_tostring(lua, -1)) == NULL) {
        valid = 0;
      }
      lua_pop(lua, 1);
    }
  }
  lua_settop(lua, attributes_index - 1);
  return valid ? 0 : -1;
}

static int append_lua_child(lua_State *lua, xmlNodePtr parent,
                            int child_index) {
  xmlNodePtr child = NULL;
  int valid = 1;

  if (lua_type(lua, child_index) == LUA_TSTRING) {
    child = xmlNewText(BAD_CAST lua_tostring(lua, child_index));
  } else if (lua_istable(lua, child_index)) {
    child = node_from_table(lua, child_index, &valid);
  } else {
    valid = 0;
  }

  if (!valid || child == NULL) {
    xmlFreeNode(child);
    return -1;
  }
  if (xmlAddChild(parent, child) == NULL) {
    xmlFreeNode(child);
    return -1;
  }
  return 0;
}

static int add_children(lua_State *lua, int table_index, xmlNodePtr node) {
  size_t index;
  size_t length;
  int result = 0;

  lua_getfield(lua, table_index, "children");
  length = lua_istable(lua, -1) ? lua_objlen(lua, -1) : 0;

  if (length > INT_MAX) {
    lua_pop(lua, 1);
    return -1;
  }

  for (index = 1; result == 0 && index <= length; ++index) {
    lua_rawgeti(lua, -1, (int)index);
    result = append_lua_child(lua, node, lua_gettop(lua));
    lua_pop(lua, 1);
  }
  lua_pop(lua, 1);

  if (result == 0 && length == 0) {
    lua_getfield(lua, table_index, "text");
    if (lua_type(lua, -1) == LUA_TSTRING) {
      xmlNodePtr text = xmlNewText(BAD_CAST lua_tostring(lua, -1));

      if (text == NULL || xmlAddChild(node, text) == NULL) {
        xmlFreeNode(text);
        result = -1;
      }
    }
    lua_pop(lua, 1);
  }
  return result;
}

static xmlNodePtr special_node_from_table(lua_State *lua, int table_index,
                                          const char *kind, int *valid) {
  xmlNodePtr node;
  const char *text;

  lua_getfield(lua, table_index, "text");
  text = lua_tostring(lua, -1);

  if (strcmp(kind, "comment") == 0) {
    node = xmlNewComment(BAD_CAST(text == NULL ? "" : text));
  } else if (strcmp(kind, "processing_instruction") == 0) {
    const char *target;

    lua_getfield(lua, table_index, "target");
    target = lua_tostring(lua, -1);
    node = target == NULL || target[0] == '\0'
               ? NULL
               : xmlNewPI(BAD_CAST target, BAD_CAST(text == NULL ? "" : text));
    lua_pop(lua, 1);
  } else {
    node = NULL;
  }

  lua_pop(lua, 1);
  if (node == NULL) {
    *valid = 0;
  }
  return node;
}

static xmlNodePtr node_from_table(lua_State *lua, int table_index, int *valid) {
  const char *kind;
  const char *tag;
  xmlNodePtr node;

  table_index = absolute_index(lua, table_index);
  lua_getfield(lua, table_index, "kind");
  kind = lua_tostring(lua, -1);
  if (kind != NULL) {
    node = special_node_from_table(lua, table_index, kind, valid);
    lua_pop(lua, 1);
    return node;
  }
  lua_pop(lua, 1);

  lua_getfield(lua, table_index, "tag");
  tag = lua_tostring(lua, -1);
  node = tag == NULL || tag[0] == '\0' ? NULL : xmlNewNode(NULL, BAD_CAST tag);
  lua_pop(lua, 1);

  if (node == NULL || add_attributes(lua, table_index, node) != 0 ||
      add_children(lua, table_index, node) != 0) {
    xmlFreeNode(node);
    *valid = 0;
    return NULL;
  }
  return node;
}

xmlNodePtr dom_page_from_lua(lua_State *lua, int table_index) {
  int valid = 1;
  xmlNodePtr root = node_from_table(lua, table_index, &valid);

  if (!valid) {
    xmlFreeNode(root);
    return NULL;
  }
  return root;
}