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_sibling, #ancestors, #append_class, #at, #at_css_impl, #at_xpath, #attribute, #attributes, #attrs, #cdata?, #child, #children=, #classes, #clone, #comment?, #css_impl, #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, #parent, #previous, #previous_element, #processing_instruction?, #remove_attr, #remove_class, #replace, #search, #text?, #values, #wrap

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



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

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



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

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



334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'ext/nokolexbor/nl_node_set.c', line 334

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));

  nl_node_set_find(self, selector, nl_node_at_css_callback, array);

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

  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



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

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



356
357
358
359
360
361
362
363
364
365
366
367
# File 'ext/nokolexbor/nl_node_set.c', line 356

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));

  nl_node_set_find(self, selector, nl_node_css_callback, array);

  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



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

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



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

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)


110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'ext/nokolexbor/nl_node_set.c', line 110

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_htmlObject



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

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

#lastObject



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

def last
  self[-1]
end

#lengthObject Also known as: size



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

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

#outer_htmlObject Also known as: to_s, to_html



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

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

#popObject



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

def pop
  return nil if length == 0

  delete(last)
end

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



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

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



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

def remove
  self.each(&:remove)
end

#reverseObject



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

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

#shiftObject



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

def shift
  return nil if length == 0

  delete(first)
end

#slice(*args) ⇒ Object



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

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



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'ext/nokolexbor/nl_node_set.c', line 227

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



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

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: +



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

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));
}