File: html_element_description.c

package info (click to toggle)
ruby-nokogiri 1.10.0%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 5,088 kB
  • sloc: xml: 28,081; ruby: 16,687; java: 13,293; ansic: 4,954; yacc: 265; sh: 76; makefile: 19
file content (279 lines) | stat: -rw-r--r-- 5,990 bytes parent folder | download | duplicates (2)
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
#include <html_element_description.h>

/*
 * call-seq:
 *  required_attributes
 *
 * A list of required attributes for this element
 */
static VALUE required_attributes(VALUE self)
{
  const htmlElemDesc * description;
  VALUE list;
  int i;

  Data_Get_Struct(self, htmlElemDesc, description);

  list = rb_ary_new();

  if(NULL == description->attrs_req) return list;

  for(i = 0; description->attrs_depr[i]; i++) {
    rb_ary_push(list, NOKOGIRI_STR_NEW2(description->attrs_req[i]));
  }

  return list;
}

/*
 * call-seq:
 *  deprecated_attributes
 *
 * A list of deprecated attributes for this element
 */
static VALUE deprecated_attributes(VALUE self)
{
  const htmlElemDesc * description;
  VALUE list;
  int i;

  Data_Get_Struct(self, htmlElemDesc, description);

  list = rb_ary_new();

  if(NULL == description->attrs_depr) return list;

  for(i = 0; description->attrs_depr[i]; i++) {
    rb_ary_push(list, NOKOGIRI_STR_NEW2(description->attrs_depr[i]));
  }

  return list;
}

/*
 * call-seq:
 *  optional_attributes
 *
 * A list of optional attributes for this element
 */
static VALUE optional_attributes(VALUE self)
{
  const htmlElemDesc * description;
  VALUE list;
  int i;

  Data_Get_Struct(self, htmlElemDesc, description);

  list = rb_ary_new();

  if(NULL == description->attrs_opt) return list;

  for(i = 0; description->attrs_opt[i]; i++) {
    rb_ary_push(list, NOKOGIRI_STR_NEW2(description->attrs_opt[i]));
  }

  return list;
}

/*
 * call-seq:
 *  default_sub_element
 *
 * The default sub element for this element
 */
static VALUE default_sub_element(VALUE self)
{
  const htmlElemDesc * description;
  Data_Get_Struct(self, htmlElemDesc, description);

  if (description->defaultsubelt)
    return NOKOGIRI_STR_NEW2(description->defaultsubelt);

  return Qnil;
}

/*
 * call-seq:
 *  sub_elements
 *
 * A list of allowed sub elements for this element.
 */
static VALUE sub_elements(VALUE self)
{
  const htmlElemDesc * description;
  VALUE list;
  int i;

  Data_Get_Struct(self, htmlElemDesc, description);

  list = rb_ary_new();

  if(NULL == description->subelts) return list;

  for(i = 0; description->subelts[i]; i++) {
    rb_ary_push(list, NOKOGIRI_STR_NEW2(description->subelts[i]));
  }

  return list;
}

/*
 * call-seq:
 *  description
 *
 * The description for this element
 */
static VALUE description(VALUE self)
{
  const htmlElemDesc * description;
  Data_Get_Struct(self, htmlElemDesc, description);

  return NOKOGIRI_STR_NEW2(description->desc);
}

/*
 * call-seq:
 *  inline?
 *
 * Is this element an inline element?
 */
static VALUE inline_eh(VALUE self)
{
  const htmlElemDesc * description;
  Data_Get_Struct(self, htmlElemDesc, description);

  if(description->isinline) return Qtrue;
  return Qfalse;
}

/*
 * call-seq:
 *  deprecated?
 *
 * Is this element deprecated?
 */
static VALUE deprecated_eh(VALUE self)
{
  const htmlElemDesc * description;
  Data_Get_Struct(self, htmlElemDesc, description);

  if(description->depr) return Qtrue;
  return Qfalse;
}

/*
 * call-seq:
 *  empty?
 *
 * Is this an empty element?
 */
static VALUE empty_eh(VALUE self)
{
  const htmlElemDesc * description;
  Data_Get_Struct(self, htmlElemDesc, description);

  if(description->empty) return Qtrue;
  return Qfalse;
}

/*
 * call-seq:
 *  save_end_tag?
 *
 * Should the end tag be saved?
 */
static VALUE save_end_tag_eh(VALUE self)
{
  const htmlElemDesc * description;
  Data_Get_Struct(self, htmlElemDesc, description);

  if(description->saveEndTag) return Qtrue;
  return Qfalse;
}

/*
 * call-seq:
 *  implied_end_tag?
 *
 * Can the end tag be implied for this tag?
 */
static VALUE implied_end_tag_eh(VALUE self)
{
  const htmlElemDesc * description;
  Data_Get_Struct(self, htmlElemDesc, description);

  if(description->endTag) return Qtrue;
  return Qfalse;
}

/*
 * call-seq:
 *  implied_start_tag?
 *
 * Can the start tag be implied for this tag?
 */
static VALUE implied_start_tag_eh(VALUE self)
{
  const htmlElemDesc * description;
  Data_Get_Struct(self, htmlElemDesc, description);

  if(description->startTag) return Qtrue;
  return Qfalse;
}

/*
 * call-seq:
 *  name
 *
 * Get the tag name for this ElemementDescription
 */
static VALUE name(VALUE self)
{
  const htmlElemDesc * description;
  Data_Get_Struct(self, htmlElemDesc, description);

  if(NULL == description->name) return Qnil;
  return NOKOGIRI_STR_NEW2(description->name);
}

/*
 * call-seq:
 *  [](tag_name)
 *
 * Get ElemementDescription for +tag_name+
 */
static VALUE get_description(VALUE klass, VALUE tag_name)
{
  const htmlElemDesc * description = htmlTagLookup(
      (const xmlChar *)StringValueCStr(tag_name)
  );

  if(NULL == description) return Qnil;
  return Data_Wrap_Struct(klass, 0, 0, (void *)(uintptr_t)description);
}

VALUE cNokogiriHtmlElementDescription ;
void init_html_element_description()
{
  VALUE nokogiri = rb_define_module("Nokogiri");
  VALUE html     = rb_define_module_under(nokogiri, "HTML");
  VALUE klass    = rb_define_class_under(html, "ElementDescription",rb_cObject);

  cNokogiriHtmlElementDescription = klass;

  rb_define_singleton_method(klass, "[]", get_description, 1);

  rb_define_method(klass, "name", name, 0);
  rb_define_method(klass, "implied_start_tag?", implied_start_tag_eh, 0);
  rb_define_method(klass, "implied_end_tag?", implied_end_tag_eh, 0);
  rb_define_method(klass, "save_end_tag?", save_end_tag_eh, 0);
  rb_define_method(klass, "empty?", empty_eh, 0);
  rb_define_method(klass, "deprecated?", deprecated_eh, 0);
  rb_define_method(klass, "inline?", inline_eh, 0);
  rb_define_method(klass, "description", description, 0);
  rb_define_method(klass, "sub_elements", sub_elements, 0);
  rb_define_method(klass, "default_sub_element", default_sub_element, 0);
  rb_define_method(klass, "optional_attributes", optional_attributes, 0);
  rb_define_method(klass, "deprecated_attributes", deprecated_attributes, 0);
  rb_define_method(klass, "required_attributes", required_attributes, 0);
}