Class: Nokolexbor::NodeSet

Inherits:
Node
  • Object
show all
Includes:
Enumerable
Defined in:
lib/nokolexbor/node_set.rb,
ext/nokolexbor/nl_node_set.c

Constant Summary

Constants inherited from Node

Nokolexbor::Node::ATTRIBUTE_NODE, Nokolexbor::Node::CDATA_SECTION_NODE, Nokolexbor::Node::COMMENT_NODE, Nokolexbor::Node::DOCUMENT_FRAG_NODE, Nokolexbor::Node::DOCUMENT_NODE, Nokolexbor::Node::DOCUMENT_TYPE_NODE, Nokolexbor::Node::ELEMENT_NODE, Nokolexbor::Node::ENTITY_NODE, Nokolexbor::Node::ENTITY_REF_NODE, Nokolexbor::Node::LOOKS_LIKE_XPATH, Nokolexbor::Node::NOTATION_NODE, Nokolexbor::Node::PI_NODE, Nokolexbor::Node::TEXT_NODE

Instance Attribute Summary

Attributes inherited from Node

#document

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Node

#[]=, #add_child, #add_class, #add_next_sibling, #add_previous_sibling, #add_sibling, #after, #ancestors, #append_class, #at, #at_css_impl, #at_xpath, #attribute, #attributes, #attrs, #before, #cdata?, #child, #children=, #classes, #clone, #comment?, #content=, #css_impl, #document?, #element?, #first_element_child, #fragment, #fragment?, #key?, #keys, #keywordify, #kwattr_add, #kwattr_append, #kwattr_remove, #kwattr_values, #last_element_child, #matches?, #name, #next, #next_element, #node_type, #nokogiri_at_css, #parent, #parent=, #parse, #prepend_child, #previous, #previous_element, #processing_instruction?, #remove_attr, #remove_class, #replace, #search, #text?, #traverse, #values, #wrap, #write_to

Class Method Details

.new(document, list = []) {|obj| ... } ⇒ Object

Yields:

  • (obj)


7
8
9
10
11
12
13
# File 'lib/nokolexbor/node_set.rb', line 7

def self.new(document, list = [])
  obj = allocate
  obj.instance_variable_set(:@document, document)
  list.each { |x| obj << x }
  yield obj if block_given?
  obj
end

Instance Method Details

#==(other) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/nokolexbor/node_set.rb', line 92

def ==(other)
  return false unless other.is_a?(NodeSet)
  return false unless length == other.length

  each_with_index do |node, i|
    return false unless node == other[i]
  end
  true
end

#[](*args) ⇒ Object



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
# File 'ext/nokolexbor/nl_node_set.c', line 169

static VALUE
nl_node_set_slice(int argc, VALUE *argv, VALUE self)
{
  VALUE arg;
  long beg, len;

  lexbor_array_t *array = nl_rb_node_set_unwrap(self);

  if (argc == 2) {
    beg = NUM2LONG(argv[0]);
    len = NUM2LONG(argv[1]);
    if (beg < 0) {
      beg += array->length;
    }
    return nl_node_set_subseq(self, beg, len);
  }

  if (argc != 1) {
    rb_scan_args(argc, argv, "11", NULL, NULL);
  }
  arg = argv[0];

  if (FIXNUM_P(arg)) {
    return nl_node_set_index_at(self, FIX2LONG(arg));
  }

  /* if arg is Range */
  switch (rb_range_beg_len(arg, &beg, &len, array->length, 0)) {
  case Qfalse:
    break;
  case Qnil:
    return Qnil;
  default:
    return nl_node_set_subseq(self, beg, len);
  }

  return nl_node_set_index_at(self, NUM2LONG(arg));
}

#at_css(selector) ⇒ Object



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
# File 'ext/nokolexbor/nl_node_set.c', line 304

static VALUE
nl_node_set_at_css(VALUE self, VALUE selector)
{
  lexbor_array_t *array = lexbor_array_create();
  lxb_dom_document_t *doc = nl_rb_document_unwrap(nl_rb_document_get(self));

  lxb_status_t status = nl_node_set_find(self, selector, nl_node_at_css_callback, array);

  if (status != LXB_STATUS_OK) {
    lexbor_array_destroy(array, true);
    nl_raise_lexbor_error(status);
  }

  if (array->length == 0) {
    lexbor_array_destroy(array, true);
    return Qnil;
  }

  nl_sort_nodes_if_necessary(selector, doc, array);

  VALUE ret = nl_rb_node_create(array->list[0], nl_rb_document_get(self));

  lexbor_array_destroy(array, true);

  return ret;
}

#childrenObject



102
103
104
105
106
107
108
# File 'lib/nokolexbor/node_set.rb', line 102

def children
  node_set = NodeSet.new(@document)
  each do |node|
    node.children.each { |n| node_set.push(n) }
  end
  node_set
end

#contentObject Also known as: text, inner_text, to_str



49
50
51
# File 'lib/nokolexbor/node_set.rb', line 49

def content
  self.map(&:content).join
end

#css(selector) ⇒ Object



331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'ext/nokolexbor/nl_node_set.c', line 331

static VALUE
nl_node_set_css(VALUE self, VALUE selector)
{
  lexbor_array_t *array = lexbor_array_create();
  lxb_dom_document_t *doc = nl_rb_document_unwrap(nl_rb_document_get(self));

  lxb_status_t status = nl_node_set_find(self, selector, nl_node_css_callback, array);
  if (status != LXB_STATUS_OK) {
    lexbor_array_destroy(array, true);
    nl_raise_lexbor_error(status);
  }

  nl_sort_nodes_if_necessary(selector, doc, array);

  return nl_rb_node_set_create_with_data(array, nl_rb_document_get(self));
}

#delete(rb_node) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'ext/nokolexbor/nl_node_set.c', line 86

static VALUE
nl_node_set_delete(VALUE self, VALUE rb_node)
{
  lexbor_array_t *array = nl_rb_node_set_unwrap(self);
  lxb_dom_node_t *node = nl_rb_node_unwrap(rb_node);

  size_t i;
  for (i = 0; i < array->length; i++)
    if (array->list[i] == node) {
      break;
    }

  if (i >= array->length) {
    // not found
    return Qnil;
  }
  lexbor_array_delete(array, i, 1);
  return rb_node;
}

#destroyObject



76
77
78
# File 'lib/nokolexbor/node_set.rb', line 76

def destroy
  self.each(&:destroy)
end

#eachObject



15
16
17
18
19
20
21
22
# File 'lib/nokolexbor/node_set.rb', line 15

def each
  return to_enum unless block_given?

  0.upto(length - 1) do |x|
    yield self[x]
  end
  self
end

#empty?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/nokolexbor/node_set.rb', line 36

def empty?
  length == 0
end

#first(n = nil) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/nokolexbor/node_set.rb', line 24

def first(n = nil)
  return self[0] unless n

  list = []
  [n, length].min.times { |i| list << self[i] }
  list
end

#include?(rb_node) ⇒ Boolean

Returns:

  • (Boolean)


106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'ext/nokolexbor/nl_node_set.c', line 106

static VALUE
nl_node_set_is_include(VALUE self, VALUE rb_node)
{
  lexbor_array_t *array = nl_rb_node_set_unwrap(self);
  lxb_dom_node_t *node = nl_rb_node_unwrap(rb_node);

  for (size_t i = 0; i < array->length; i++)
    if (array->list[i] == node) {
      return Qtrue;
    }

  return Qfalse;
}

#index(node = nil) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/nokolexbor/node_set.rb', line 40

def index(node = nil)
  if node
    each_with_index { |member, j| return j if member == node }
  elsif block_given?
    each_with_index { |member, j| return j if yield(member) }
  end
  nil
end

#inner_html(*args) ⇒ Object



57
58
59
# File 'lib/nokolexbor/node_set.rb', line 57

def inner_html(*args)
  self.map { |n| n.inner_html(*args) }.join
end

#lastObject



32
33
34
# File 'lib/nokolexbor/node_set.rb', line 32

def last
  self[-1]
end

#lengthObject Also known as: size



66
67
68
69
70
# File 'ext/nokolexbor/nl_node_set.c', line 66

static VALUE
nl_node_set_length(VALUE self)
{
  return INT2NUM(nl_rb_node_set_unwrap(self)->length);
}

#nokogiri_css(*args) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/nokolexbor/node_set.rb', line 130

def nokogiri_css(*args)
  rules, handler, ns, _ = extract_params(args)
  paths = css_rules_to_xpath(rules, ns)

  NodeSet.new(@document) do |set|
    each do |node|
      node.send(:xpath_internal, node, paths, handler, ns, nil).each do |inner_node|
        set << inner_node
      end
    end
  end
end

#outer_html(*args) ⇒ Object Also known as: to_s, to_html, serialize



61
62
63
# File 'lib/nokolexbor/node_set.rb', line 61

def outer_html(*args)
  self.map { |n| n.outer_html(*args) }.join
end

#popObject



80
81
82
83
84
# File 'lib/nokolexbor/node_set.rb', line 80

def pop
  return nil if length == 0

  delete(last)
end

#push(rb_node) ⇒ Object Also known as: <<



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'ext/nokolexbor/nl_node_set.c', line 72

static VALUE
nl_node_set_push(VALUE self, VALUE rb_node)
{
  lexbor_array_t *array = nl_rb_node_set_unwrap(self);
  lxb_dom_node_t *node = nl_rb_node_unwrap(rb_node);

  lxb_status_t status = lexbor_array_push_unique(array, node);
  if (status != LXB_STATUS_OK && status != LXB_STATUS_STOPPED) {
    nl_raise_lexbor_error(status);
  }

  return self;
}

#removeObject Also known as: unlink



69
70
71
# File 'lib/nokolexbor/node_set.rb', line 69

def remove
  self.each(&:remove)
end

#reverseObject



110
111
112
113
114
115
116
# File 'lib/nokolexbor/node_set.rb', line 110

def reverse
  node_set = NodeSet.new(@document)
  (length - 1).downto(0) do |x|
    node_set.push(self[x])
  end
  node_set
end

#shiftObject



86
87
88
89
90
# File 'lib/nokolexbor/node_set.rb', line 86

def shift
  return nil if length == 0

  delete(first)
end

#slice(*args) ⇒ Object



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
# File 'ext/nokolexbor/nl_node_set.c', line 169

static VALUE
nl_node_set_slice(int argc, VALUE *argv, VALUE self)
{
  VALUE arg;
  long beg, len;

  lexbor_array_t *array = nl_rb_node_set_unwrap(self);

  if (argc == 2) {
    beg = NUM2LONG(argv[0]);
    len = NUM2LONG(argv[1]);
    if (beg < 0) {
      beg += array->length;
    }
    return nl_node_set_subseq(self, beg, len);
  }

  if (argc != 1) {
    rb_scan_args(argc, argv, "11", NULL, NULL);
  }
  arg = argv[0];

  if (FIXNUM_P(arg)) {
    return nl_node_set_index_at(self, FIX2LONG(arg));
  }

  /* if arg is Range */
  switch (rb_range_beg_len(arg, &beg, &len, array->length, 0)) {
  case Qfalse:
    break;
  case Qnil:
    return Qnil;
  default:
    return nl_node_set_subseq(self, beg, len);
  }

  return nl_node_set_index_at(self, NUM2LONG(arg));
}

#to_aObject Also known as: to_ary



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'ext/nokolexbor/nl_node_set.c', line 208

static VALUE
nl_node_set_to_array(VALUE self)
{
  lexbor_array_t *array = nl_rb_node_set_unwrap(self);

  VALUE list = rb_ary_new2(array->length);
  VALUE doc = nl_rb_document_get(self);
  for (size_t i = 0; i < array->length; i++) {
    lxb_dom_node_t *node = (lxb_dom_node_t *)array->list[i];
    VALUE rb_node = nl_rb_node_create(node, doc);
    rb_ary_push(list, rb_node);
  }

  return list;
}

#xpath(*args) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/nokolexbor/node_set.rb', line 118

def xpath(*args)
  paths, handler, ns, binds = extract_params(args)

  NodeSet.new(@document) do |set|
    each do |node|
      node.send(:xpath_internal, node, paths, handler, ns, binds).each do |inner_node|
        set << inner_node
      end
    end
  end
end

#|(other) ⇒ Object Also known as: +



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
# File 'ext/nokolexbor/nl_node_set.c', line 224

static VALUE
nl_node_set_union(VALUE self, VALUE other)
{
  if (!rb_obj_is_kind_of(other, cNokolexborNodeSet)) {
    rb_raise(rb_eArgError, "Parameter must be a Nokolexbor::NodeSet");
  }

  lexbor_array_t *self_array = nl_rb_node_set_unwrap(self);
  lexbor_array_t *other_array = nl_rb_node_set_unwrap(other);

  if (self_array->length + other_array->length == 0) {
    return nl_rb_node_set_create_with_data(NULL, nl_rb_document_get(self));
  }

  lexbor_array_t *new_array = lexbor_array_create();
  lxb_status_t status = lexbor_array_init(new_array, self_array->length + other_array->length);
  if (status != LXB_STATUS_OK) {
    nl_raise_lexbor_error(status);
  }

  memcpy(new_array->list, self_array->list, sizeof(lxb_dom_node_t *) * self_array->length);
  new_array->length = self_array->length;

  for (size_t i = 0; i < other_array->length; i++) {
    lexbor_array_push_unique(new_array, other_array->list[i]);
  }

  return nl_rb_node_set_create_with_data(new_array, nl_rb_document_get(self));
}