forked from craigbarnes/lua-gumbo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gumbo.c
287 lines (265 loc) · 9.46 KB
/
gumbo.c
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
/*
Lua bindings for the Gumbo HTML5 parsing library.
Copyright (c) 2013-2014, Craig Barnes.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stddef.h>
#include <lua.h>
#include <lauxlib.h>
#include <gumbo.h>
#include "compat.h"
static const char attrnsmap[][6] = {"none", "xlink", "xml", "xmlns"};
static const char tagnsmap[][5] = {"html", "svg", "math"};
static const char quirksmap[][15] = {"no-quirks", "quirks", "limited-quirks"};
static const char flagmap[][33] = { // Fixed size allows storage in rodata
"insertion_by_parser",
"implicit_end_tag",
"", // Unused index
"insertion_implied",
"converted_from_end_tag",
"insertion_from_isindex",
"insertion_from_image",
"reconstructed_formatting_element",
"adoption_agency_cloned",
"adoption_agency_moved",
"foster_parented"
};
#define add_literal(L, k, v) ( \
lua_pushliteral(L, v), \
lua_setfield(L, -2, k) \
)
static inline void add_string(lua_State *L, const char *k, const char *v) {
lua_pushstring(L, v);
lua_setfield(L, -2, k);
}
static inline void add_integer(lua_State *L, const char *k, const int v) {
lua_pushinteger(L, v);
lua_setfield(L, -2, k);
}
static void add_attributes(lua_State *L, const GumboVector *attrs) {
const unsigned int length = attrs->length;
if (length > 0) {
lua_createtable(L, length, length);
for (unsigned int i = 0; i < length; i++) {
const GumboAttribute *attr = (const GumboAttribute *)attrs->data[i];
add_string(L, attr->name, attr->value);
lua_createtable(L, 0, 6);
add_string(L, "name", attr->name);
add_string(L, "value", attr->value);
add_integer(L, "line", attr->name_start.line);
add_integer(L, "column", attr->name_start.column);
add_integer(L, "offset", attr->name_start.offset);
if (attr->attr_namespace != GUMBO_ATTR_NAMESPACE_NONE)
add_string(L, "namespace", attrnsmap[attr->attr_namespace]);
lua_rawseti(L, -2, i+1);
}
lua_setfield(L, -2, "attr");
}
}
static void add_tag(lua_State *L, const GumboElement *element) {
if (element->tag_namespace == GUMBO_NAMESPACE_SVG) {
GumboStringPiece original_tag = element->original_tag;
gumbo_tag_from_original_text(&original_tag);
const char *normalized = gumbo_normalize_svg_tagname(&original_tag);
if (normalized) {
add_string(L, "tag", normalized);
return;
}
}
if (element->tag == GUMBO_TAG_UNKNOWN) {
GumboStringPiece original_tag = element->original_tag;
gumbo_tag_from_original_text(&original_tag);
luaL_Buffer b;
luaL_buffinit(L, &b);
for (size_t i = 0, n = original_tag.length; i < n; i++) {
const char c = original_tag.data[i];
luaL_addchar(&b, (c <= 'Z' && c >= 'A') ? c + 32 : c);
}
luaL_pushresult(&b);
} else {
lua_pushstring(L, gumbo_normalized_tagname(element->tag));
}
lua_setfield(L, -2, "tag");
}
static void add_parseflags(lua_State *L, const GumboParseFlags flags) {
static const unsigned int nflags = sizeof(flagmap) / sizeof(flagmap[0]);
if (flags != GUMBO_INSERTION_NORMAL) {
lua_createtable(L, 0, 1);
for (unsigned int i = 0; i < nflags; i++) {
if ((flags & (1 << i)) != 0) {
lua_pushboolean(L, 1);
lua_setfield(L, -2, flagmap[i]);
}
}
lua_setfield(L, -2, "parse_flags");
}
}
static void create_text_node(lua_State *L, const GumboText *text) {
lua_createtable(L, 0, 5);
add_string(L, "text", text->text);
add_integer(L, "line", text->start_pos.line);
add_integer(L, "column", text->start_pos.column);
add_integer(L, "offset", text->start_pos.offset);
}
// Forward declaration, to allow mutual recursion with add_children()
static void push_node(lua_State *L, const GumboNode *node);
static void add_children(lua_State *L, const GumboVector *children) {
const unsigned int length = children->length;
for (unsigned int i = 0; i < length; i++) {
push_node(L, (const GumboNode *)children->data[i]);
lua_rawseti(L, -2, i + 1);
}
}
static void push_node(lua_State *L, const GumboNode *node) {
luaL_checkstack(L, 10, "element nesting too deep");
switch (node->type) {
case GUMBO_NODE_DOCUMENT: {
const GumboDocument *document = &node->v.document;
lua_createtable(L, document->children.length, 4);
add_literal(L, "type", "document");
add_string(L, "quirks_mode", quirksmap[document->doc_type_quirks_mode]);
if (document->has_doctype) {
lua_createtable(L, 0, 3);
add_string(L, "name", document->name);
add_string(L, "publicId", document->public_identifier);
add_string(L, "systemId", document->system_identifier);
lua_setfield(L, -2, "doctype");
}
add_children(L, &document->children);
return;
}
case GUMBO_NODE_ELEMENT: {
const GumboElement *element = &node->v.element;
lua_createtable(L, element->children.length, 7);
lua_getfield(L, LUA_REGISTRYINDEX, "gumbo.element");
lua_setmetatable(L, -2);
add_tag(L, element);
add_string(L, "tag_namespace", tagnsmap[element->tag_namespace]);
add_integer(L, "line", element->start_pos.line);
add_integer(L, "column", element->start_pos.column);
add_integer(L, "offset", element->start_pos.offset);
add_parseflags(L, node->parse_flags);
add_attributes(L, &element->attributes);
add_children(L, &element->children);
return;
}
case GUMBO_NODE_TEXT:
create_text_node(L, &node->v.text);
add_literal(L, "type", "text");
return;
case GUMBO_NODE_COMMENT:
create_text_node(L, &node->v.text);
add_literal(L, "type", "comment");
return;
case GUMBO_NODE_CDATA:
create_text_node(L, &node->v.text);
add_literal(L, "type", "cdata");
return;
case GUMBO_NODE_WHITESPACE:
create_text_node(L, &node->v.text);
add_literal(L, "type", "whitespace");
return;
}
}
static int attr_next(lua_State *L) {
const lua_Integer i = luaL_checkinteger(L, 2) + 1;
lua_rawgeti(L, 1, i);
if (lua_istable(L, 3)) {
lua_pushinteger(L, i);
lua_getfield(L, 3, "name");
lua_getfield(L, 3, "value");
lua_getfield(L, 3, "namespace");
lua_getfield(L, 3, "line");
lua_getfield(L, 3, "column");
lua_getfield(L, 3, "offset");
return 7;
}
return 0;
}
static int Element_attr_iter(lua_State *L) {
luaL_checktype(L, 1, LUA_TTABLE);
lua_pushcfunction(L, attr_next);
lua_getfield(L, 1, "attr");
lua_pushinteger(L, 0);
return 3;
}
static int parse(lua_State *L) {
size_t length;
const char *input = luaL_checklstring(L, 1, &length);
GumboOptions options = kGumboDefaultOptions;
options.tab_stop = luaL_optint(L, 2, 8);
GumboOutput *output = gumbo_parse_with_options(&options, input, length);
if (output) {
push_node(L, output->document);
lua_rawgeti(L, -1, output->root->index_within_parent + 1);
lua_setfield(L, -2, "root");
gumbo_destroy_output(&options, output);
return 1;
} else {
lua_pushnil(L);
lua_pushliteral(L, "Failed to parse");
return 2;
}
}
static int parse_file(lua_State *L) {
const int tabstop = luaL_optint(L, 2, 8);
lua_settop(L, 1);
if (lua_isstring(L, 1)) {
lua_getglobal(L, "io");
lua_getfield(L, -1, "open");
lua_pushvalue(L, 1);
lua_call(L, 1, 2);
if (lua_isnil(L, -2))
return 2;
lua_pop(L, 1);
}
if (!lua_isuserdata(L, -1))
return luaL_argerror(L, 1, "not a file handle or filename string");
if (!luaL_getmetafield(L, -1, "read"))
return luaL_argerror(L, 1, "not a file handle or filename string");
lua_pushvalue(L, -2);
lua_pushliteral(L, "*a");
lua_call(L, 2, 2);
if (lua_isnil(L, -2))
return 2;
lua_pushcfunction(L, parse);
lua_pushvalue(L, -3);
lua_pushinteger(L, tabstop);
lua_call(L, 2, 2);
if (lua_isnil(L, -2)) {
return 2;
} else {
lua_pop(L, 1);
return 1;
}
}
static const luaL_Reg Element[] = {
{"attr_iter", Element_attr_iter},
{NULL, NULL}
};
static const luaL_Reg lib[] = {
{"parse", parse},
{"parse_file", parse_file},
{NULL, NULL}
};
int luaopen_gumbo(lua_State *L) {
if (luaL_newmetatable(L, "gumbo.element")) {
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
lua_newtable(L);
lua_setfield(L, -2, "attr");
add_literal(L, "type", "element");
luaL_setfuncs(L, Element, 0);
}
luaL_newlib(L, lib);
return 1;
}