github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

tenderlove / nokogiri

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 778
    • 54
  • Source
  • Commits
  • Network (54)
  • Issues (17)
  • Downloads (21)
  • Wiki (7)
  • Graphs
  • Tree: 92b5d8f

click here to add a description

click here to add a homepage

  • Branches (8)
    • dtd
    • fix-mkmf-mf
    • java
    • large-node-set-optimize
    • macruby
    • master
    • rip
    • sax
  • Tags (21)
    • REL_1.4.1
    • REL_1.4.0
    • REL_1.3.3
    • REL_1.3.2
    • REL_1.3.1
    • REL_1.3.0rc1
    • REL_1.3.0
    • REL_1.2.3
    • REL_1.2.2
    • REL_1.2.1
    • REL_1.2.0
    • REL_1.1.1
    • REL_1.1.0
    • REL_1.0.7
    • REL_1.0.6
    • REL_1.0.5
    • REL_1.0.4
    • REL_1.0.3
    • REL_1.0.2
    • REL_1.0.1
    • REL_1.0.0
Sending Request…
Click here to lend your support to: nokogiri and make a donation at www.pledgie.com ! Edit Pledgie Setup

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

Nokogiri (鋸) is an HTML, XML, SAX, and Reader parser with XPath and CSS selector support. — Read more

  cancel

http://nokogiri.org/

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

Natural Japanese 
ujihisa (author)
Sun Jun 07 16:47:19 -0700 2009
commit  92b5d8f86bd4e2aaf3349988459d29ac39fe2808
tree    a3bb66b7d6f799304c55daf4ec34a140c10aa022
parent  db22dd21bafc0b5230a09142d9926c424701e014
nokogiri / ext / nokogiri / xml_node_set.c ext/nokogiri/xml_node_set.c
100644 387 lines (323 sloc) 10.255 kb
edit raw blame history
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#include <xml_node_set.h>
#include <libxml/xpathInternals.h>
 
/*
* call-seq:
* dup
*
* Duplicate this node set
*/
static VALUE duplicate(VALUE self)
{
  xmlNodeSetPtr node_set;
  Data_Get_Struct(self, xmlNodeSet, node_set);
 
  xmlNodeSetPtr dupl = xmlXPathNodeSetMerge(NULL, node_set);
 
  return Nokogiri_wrap_xml_node_set(dupl);
}
 
/*
* call-seq:
* length
*
* Get the length of the node set
*/
static VALUE length(VALUE self)
{
  xmlNodeSetPtr node_set;
  Data_Get_Struct(self, xmlNodeSet, node_set);
 
  if(node_set)
    return INT2NUM(node_set->nodeNr);
 
  return INT2NUM(0);
}
 
/*
* call-seq:
* push(node)
*
* Append +node+ to the NodeSet.
*/
static VALUE push(VALUE self, VALUE rb_node)
{
  xmlNodeSetPtr node_set;
  xmlNodePtr node;
 
  if(! rb_funcall(rb_node, rb_intern("is_a?"), 1, cNokogiriXmlNode))
    rb_raise(rb_eArgError, "node must be a Nokogiri::XML::Node");
 
  Data_Get_Struct(self, xmlNodeSet, node_set);
  Data_Get_Struct(rb_node, xmlNode, node);
  xmlXPathNodeSetAdd(node_set, node);
  return self;
}
 
/*
* call-seq:
* delete(node)
*
* Delete +node+ from the Nodeset, if it is a member. Returns the deleted node
* if found, otherwise returns nil.
*/
static VALUE delete(VALUE self, VALUE rb_node)
{
  xmlNodeSetPtr node_set ;
  xmlNodePtr node ;
 
  if(! rb_funcall(rb_node, rb_intern("is_a?"), 1, cNokogiriXmlNode))
    rb_raise(rb_eArgError, "node must be a Nokogiri::XML::Node");
  
  Data_Get_Struct(self, xmlNodeSet, node_set);
  Data_Get_Struct(rb_node, xmlNode, node);
 
  if (xmlXPathNodeSetContains(node_set, node)) {
    xmlXPathNodeSetDel(node_set, node);
    return rb_node ;
  }
 
  return Qnil ;
}
 
 
/*
* call-seq:
* &(node_set)
*
* Set Intersection — Returns a new NodeSet containing nodes common to the two NodeSets.
*/
static VALUE intersection(VALUE self, VALUE rb_other)
{
  xmlNodeSetPtr node_set;
  xmlNodeSetPtr other;
 
  if(! rb_funcall(rb_other, rb_intern("is_a?"), 1, cNokogiriXmlNodeSet))
    rb_raise(rb_eArgError, "node_set must be a Nokogiri::XML::NodeSet");
 
  Data_Get_Struct(self, xmlNodeSet, node_set);
  Data_Get_Struct(rb_other, xmlNodeSet, other);
 
  return Nokogiri_wrap_xml_node_set(xmlXPathIntersection(node_set, other));
}
 
 
/*
* call-seq:
* include?(node)
*
* Returns true if any member of node set equals +node+.
*/
static VALUE include_eh(VALUE self, VALUE rb_node)
{
  xmlNodeSetPtr node_set;
  xmlNodePtr node;
 
  if(! rb_funcall(rb_node, rb_intern("is_a?"), 1, cNokogiriXmlNode))
    rb_raise(rb_eArgError, "node must be a Nokogiri::XML::Node");
 
  Data_Get_Struct(self, xmlNodeSet, node_set);
  Data_Get_Struct(rb_node, xmlNode, node);
 
  return (xmlXPathNodeSetContains(node_set, node) ? Qtrue : Qfalse);
}
 
 
/*
* call-seq:
* +(node_set)
*
* Concatenation - returns a new NodeSet built by concatenating the node set
* with +node_set+ to produce a third NodeSet
*/
static VALUE plus(VALUE self, VALUE rb_other)
{
  xmlNodeSetPtr node_set;
  xmlNodeSetPtr other;
  xmlNodeSetPtr new;
 
  if(! rb_funcall(rb_other, rb_intern("is_a?"), 1, cNokogiriXmlNodeSet))
    rb_raise(rb_eArgError, "node_set must be a Nokogiri::XML::NodeSet");
 
  Data_Get_Struct(self, xmlNodeSet, node_set);
  Data_Get_Struct(rb_other, xmlNodeSet, other);
 
  new = xmlXPathNodeSetMerge(NULL, node_set);
  new = xmlXPathNodeSetMerge(new, other);
 
  return Nokogiri_wrap_xml_node_set(new);
}
 
/*
* call-seq:
* -(node_set)
*
* Difference - returns a new NodeSet that is a copy of this NodeSet, removing
* each item that also appears in +node_set+
*/
static VALUE minus(VALUE self, VALUE rb_other)
{
  xmlNodeSetPtr node_set;
  xmlNodeSetPtr other;
  xmlNodeSetPtr new;
  int j ;
 
  if(! rb_funcall(rb_other, rb_intern("is_a?"), 1, cNokogiriXmlNodeSet))
    rb_raise(rb_eArgError, "node_set must be a Nokogiri::XML::NodeSet");
 
  Data_Get_Struct(self, xmlNodeSet, node_set);
  Data_Get_Struct(rb_other, xmlNodeSet, other);
 
  new = xmlXPathNodeSetMerge(NULL, node_set);
  for (j = 0 ; j < other->nodeNr ; ++j) {
    xmlXPathNodeSetDel(new, other->nodeTab[j]);
  }
 
  return Nokogiri_wrap_xml_node_set(new);
}
 
 
static VALUE index_at(VALUE self, long offset)
{
  xmlNodeSetPtr node_set;
  Data_Get_Struct(self, xmlNodeSet, node_set);
 
  if(offset >= node_set->nodeNr || abs(offset) > node_set->nodeNr) return Qnil;
  if(offset < 0) offset = offset + node_set->nodeNr;
 
  return Nokogiri_wrap_xml_node(Qnil, node_set->nodeTab[offset]);
}
 
static VALUE subseq(VALUE self, long beg, long len)
{
  int j;
  xmlNodeSetPtr node_set;
  xmlNodeSetPtr new_set ;
 
  Data_Get_Struct(self, xmlNodeSet, node_set);
 
  if (beg > node_set->nodeNr) return Qnil ;
  if (beg < 0 || len < 0) return Qnil ;
 
  new_set = xmlXPathNodeSetCreate(NULL);
  for (j = beg ; j < beg+len ; ++j) {
    xmlXPathNodeSetAdd(new_set, node_set->nodeTab[j]);
  }
  return Nokogiri_wrap_xml_node_set(new_set);
}
 
/*
* call-seq:
* [index] -> Node or nil
* [start, length] -> NodeSet or nil
* [range] -> NodeSet or nil
* slice(index) -> Node or nil
* slice(start, length) -> NodeSet or nil
* slice(range) -> NodeSet or nil
*
* Element reference - returns the node at +index+, or returns a NodeSet
* containing nodes starting at +start+ and continuing for +length+ elements, or
* returns a NodeSet containing nodes specified by +range+. Negative +indices+
* count backward from the end of the +node_set+ (-1 is the last node). Returns
* nil if the +index+ (or +start+) are out of range.
*/
static VALUE slice(int argc, VALUE *argv, VALUE self)
{
  VALUE arg ;
  long beg, len ;
  xmlNodeSetPtr node_set;
  Data_Get_Struct(self, xmlNodeSet, node_set);
 
  if (argc == 2) {
    beg = NUM2LONG(argv[0]);
    len = NUM2LONG(argv[1]);
    if (beg < 0) {
      beg += node_set->nodeNr ;
    }
    return subseq(self, beg, len);
  }
 
  if (argc != 1) {
    rb_scan_args(argc, argv, "11", NULL, NULL);
  }
  arg = argv[0];
 
  if (FIXNUM_P(arg)) {
    return index_at(self, FIX2LONG(arg));
  }
  
  /* if arg is Range */
  switch (rb_range_beg_len(arg, &beg, &len, node_set->nodeNr, 0)) {
  case Qfalse:
    break;
  case Qnil:
    return Qnil;
  default:
    return subseq(self, beg, len);
  }
 
  return index_at(self, NUM2LONG(arg));
}
 
 
/*
* call-seq:
* to_a
*
* Return this list as an Array
*/
static VALUE to_array(VALUE self, VALUE rb_node)
{
  xmlNodeSetPtr set;
  Data_Get_Struct(self, xmlNodeSet, set);
 
  VALUE *elts = calloc((size_t)set->nodeNr, sizeof(VALUE *));
  int i;
  for(i = 0; i < set->nodeNr; i++) {
    if(set->nodeTab[i]->_private) {
      elts[i] = (VALUE)set->nodeTab[i]->_private;
    } else {
      elts[i] = Nokogiri_wrap_xml_node(Qnil, set->nodeTab[i]);
    }
  }
 
  VALUE list = rb_ary_new4(set->nodeNr, elts);
 
  free(elts);
 
  return list;
}
 
/*
* call-seq:
* unlink
*
* Unlink this NodeSet and all Node objects it contains from their current context.
*/
static VALUE unlink_nodeset(VALUE self)
{
  xmlNodeSetPtr node_set;
  int j, nodeNr ;
 
  Data_Get_Struct(self, xmlNodeSet, node_set);
  nodeNr = node_set->nodeNr ;
  for (j = 0 ; j < nodeNr ; j++) {
    VALUE node ;
    xmlNodePtr node_ptr;
    node = Nokogiri_wrap_xml_node(Qnil, node_set->nodeTab[j]);
    rb_funcall(node, rb_intern("unlink"), 0); /* modifies the C struct out from under the object */
    Data_Get_Struct(node, xmlNode, node_ptr);
    node_set->nodeTab[j] = node_ptr ;
  }
  return self ;
}
 
 
static void deallocate(xmlNodeSetPtr node_set)
{
  /*
* xmlXPathFreeNodeSet() contains an implicit assumption that it is being
* called before any of its pointed-to nodes have been free()d. this
* assumption lies in the operation where it dereferences nodeTab pointers
* while searching for namespace nodes to free.
*
* however, since Ruby's GC mechanism cannot guarantee the strict order in
* which ruby objects will be GC'd, nodes may be garbage collected before a
* nodeset containing pointers to those nodes. (this is true regardless of
* how we declare dependencies between objects with rb_gc_mark().)
*
* as a result, xmlXPathFreeNodeSet() will perform unsafe memory operations,
* and calling it would be evil.
*
* on the bright side, though, Nokogiri's API currently does not cause
* namespace nodes to be included in node sets, ever.
*
* armed with that fact, we examined xmlXPathFreeNodeSet() and related libxml
* code and determined that, within the Nokogiri abstraction, we will not
* leak memory if we simply free the node set's memory directly. that's only
* quasi-evil!
*
* there's probably a lesson in here somewhere about intermingling, within a
* single array, structs with different memory-ownership semantics. or more
* generally, a lesson about building an API in C/C++ that does not contain
* assumptions about the strict order in which memory will be released. hey,
* that sounds like a great idea for a blog post! get to it!
*
* "In Valgrind We Trust." seriously.
*/
  NOKOGIRI_DEBUG_START(node_set) ;
  if (node_set->nodeTab != NULL)
    xmlFree(node_set->nodeTab);
  xmlFree(node_set);
  NOKOGIRI_DEBUG_END(node_set) ;
}
 
static VALUE allocate(VALUE klass)
{
  return Nokogiri_wrap_xml_node_set(xmlXPathNodeSetCreate(NULL));
}
 
VALUE Nokogiri_wrap_xml_node_set(xmlNodeSetPtr node_set)
{
  return Data_Wrap_Struct(cNokogiriXmlNodeSet, 0, deallocate, node_set);
}
 
VALUE cNokogiriXmlNodeSet ;
void init_xml_node_set(void)
{
  VALUE nokogiri = rb_define_module("Nokogiri");
  VALUE xml = rb_define_module_under(nokogiri, "XML");
  VALUE klass = rb_define_class_under(xml, "NodeSet", rb_cObject);
  cNokogiriXmlNodeSet = klass;
 
  rb_define_alloc_func(klass, allocate);
  rb_define_method(klass, "length", length, 0);
  rb_define_method(klass, "[]", slice, -1);
  rb_define_method(klass, "slice", slice, -1);
  rb_define_method(klass, "push", push, 1);
  rb_define_method(klass, "+", plus, 1);
  rb_define_method(klass, "-", minus, 1);
  rb_define_method(klass, "unlink", unlink_nodeset, 0);
  rb_define_method(klass, "to_a", to_array, 0);
  rb_define_method(klass, "dup", duplicate, 0);
  rb_define_method(klass, "delete", delete, 1);
  rb_define_method(klass, "&", intersection, 1);
  rb_define_method(klass, "include?", include_eh, 1);
}
 
Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server