Class: Nokolexbor::Node

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/nokolexbor/node.rb,
ext/nokolexbor/nl_node.c

Constant Summary collapse

ELEMENT_NODE =
1
ATTRIBUTE_NODE =
2
TEXT_NODE =
3
CDATA_SECTION_NODE =
4
ENTITY_REF_NODE =
5
ENTITY_NODE =
6
PI_NODE =
7
COMMENT_NODE =
8
DOCUMENT_NODE =
9
DOCUMENT_TYPE_NODE =
10
DOCUMENT_FRAG_NODE =
11
NOTATION_NODE =
12
LOOKS_LIKE_XPATH =
%r{^(\./|/|\.\.|\.$)}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#documentDocument (readonly)

Returns The associated Document of this node.

Returns:



21
22
23
# File 'lib/nokolexbor/node.rb', line 21

def document
  @document
end

Class Method Details

.new(name, document) {|Node| ... } ⇒ Node

Create a new node with name that belongs to document.

If you intend to add a node to a document tree, it's likely that you will prefer one of the Nokolexbor::Node methods like #add_child, #add_next_sibling, #replace, etc. which will both create an element (or subtree) and place it in the document tree.

Another alternative, if you are concerned about performance, is Document#create_element which accepts additional arguments for contents or attributes but (like this method) avoids parsing markup.

Yields:

Returns:

Parameters:

  • name (String)
  • document (Document)

    The document to which the the returned node will belong.



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

static VALUE
nl_node_new(int argc, VALUE *argv, VALUE klass)
{
  lxb_dom_document_t *document;
  VALUE rb_name;
  VALUE rb_document;
  VALUE rest;

  rb_scan_args(argc, argv, "2*", &rb_name, &rb_document, &rest);

  if (rb_obj_is_kind_of(rb_document, cNokolexborDocument)) {
    document = nl_rb_document_unwrap(rb_document);
  } else if (rb_obj_is_kind_of(rb_document, cNokolexborNode)) {
    lxb_dom_node_t *node = nl_rb_node_unwrap(rb_document);
    document = node->owner_document;
    rb_document = nl_rb_document_get(rb_document);
  } else {
    rb_raise(rb_eArgError, "Expected a Document or Node, got %s", rb_class2name(CLASS_OF(rb_document)));
  }

  const char *c_name = StringValuePtr(rb_name);
  size_t name_len = RSTRING_LEN(rb_name);
  lxb_dom_element_t *element = lxb_dom_document_create_element(document, (const lxb_char_t *)c_name, name_len, NULL);
  if (element == NULL) {
    rb_raise(rb_eRuntimeError, "Error creating element");
  }

  VALUE rb_node = nl_rb_node_create(&element->node, rb_document);

  if (rb_block_given_p()) {
    rb_yield(rb_node);
  }

  return rb_node;
}

Instance Method Details

#<<(node_or_tags) ⇒ Node

Add node_or_tags as a child of this Node.

Parameters:

Returns:

  • (Node)

    self, to support chaining of calls.

See Also:



208
209
210
211
# File 'lib/nokolexbor/node.rb', line 208

def <<(node_or_tags)
  add_child(node_or_tags)
  self
end

#==(other) ⇒ Boolean Also known as: eql?

Returns true if this Node is equal to other.

Returns:

  • (Boolean)

    true if this Node is equal to other.



934
935
936
937
938
939
940
941
942
943
# File 'ext/nokolexbor/nl_node.c', line 934

static VALUE
nl_node_equals(VALUE self, VALUE other)
{
  if (!rb_obj_is_kind_of(other, cNokolexborNode)) {
    return Qfalse;
  }
  lxb_dom_node_t *node1 = nl_rb_node_unwrap(self);
  lxb_dom_node_t *node2 = nl_rb_node_unwrap(other);
  return node1 == node2 ? Qtrue : Qfalse;
}

#[](name) ⇒ String? Also known as: attr, get_attribute

Fetch an attribute from this node.

Returns:

  • (String, nil)

Parameters:

  • name

    The name of the attribute.

Returns:

  • (String, nil)

    The value of the attribute name, or nil if no matching attribute exists.



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'ext/nokolexbor/nl_node.c', line 270

static VALUE
nl_node_get_attr(VALUE self, VALUE rb_attr)
{
  lxb_dom_node_t *node = nl_rb_node_unwrap(self);

  if (node->type != LXB_DOM_NODE_TYPE_ELEMENT) {
    return Qnil;
  }

  VALUE rb_attr_s = rb_String(rb_attr);
  const char *attr_c = RSTRING_PTR(rb_attr_s);
  size_t attr_len = RSTRING_LEN(rb_attr_s);

  lxb_dom_element_t *element = lxb_dom_interface_element(node);

  if (!lxb_dom_element_has_attribute(element, (const lxb_char_t *)attr_c, attr_len)) {
    return Qnil;
  }

  size_t attr_value_len;
  const lxb_char_t *attr_value = lxb_dom_element_get_attribute(element, (const lxb_char_t *)attr_c, attr_len, &attr_value_len);

  return rb_utf8_str_new((const char *)attr_value, attr_value_len);
}

#[]=(name, value) ⇒ String? Also known as: set_attr, set_attribute

Update the attribute name to value, or create the attribute if it does not exist.

Returns:

  • (String, nil)

Parameters:

  • name

    The name of the attribute.

  • value

    The value of the attribute.

Returns:

  • (String)

    value



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'ext/nokolexbor/nl_node.c', line 305

static VALUE
nl_node_set_attr(VALUE self, VALUE rb_attr, VALUE rb_value)
{
  lxb_dom_node_t *node = nl_rb_node_unwrap(self);

  if (node->type != LXB_DOM_NODE_TYPE_ELEMENT) {
    return Qnil;
  }

  VALUE rb_attr_s = rb_String(rb_attr);
  VALUE rb_value_s = rb_String(rb_value);

  const char *attr_c = RSTRING_PTR(rb_attr_s);
  size_t attr_len = RSTRING_LEN(rb_attr_s);
  const char *value_c = RSTRING_PTR(rb_value_s);
  size_t value_len = RSTRING_LEN(rb_value_s);

  lxb_dom_element_t *element = lxb_dom_interface_element(node);

  lxb_dom_element_set_attribute(element, (const lxb_char_t *)attr_c, attr_len, (const lxb_char_t *)value_c, value_len);

  return rb_value;
}

#add_child(new) ⇒ Node, NodeSet

Add new as a child of this Node.

Parameters:

Returns:



1105
1106
1107
1108
1109
# File 'ext/nokolexbor/nl_node.c', line 1105

static VALUE
nl_node_add_child(VALUE self, VALUE new)
{
  return nl_node_add_nodes(self, new, lxb_dom_node_insert_child, false);
}

#add_class(names) ⇒ Node

Ensure CSS classes are present on self. Any CSS classes in names that already exist in the "class" attribute are not added. Note that any existing duplicates in the "class" attribute are not removed. Compare with #append_class.

This is a convenience function and is equivalent to:

node.kwattr_add("class", names)

Examples:

node.add_class("section") # => <div class="section"></div>
node.add_class("section") # => <div class="section"></div> # duplicate not added
node.add_class("section header") # => <div class="section header"></div>
node.add_class(["section", "header"]) # => <div class="section header"></div>

Parameters:

  • names (String, Array<String>)

    CSS class names to be added to the Node's "class" attribute. May be a string containing whitespace-delimited names, or an Array of String names. Any class names already present will not be added. Any class names not present will be added. If no "class" attribute exists, one is created.

Returns:

  • (Node)

    self, to support chaining of calls.

See Also:



481
482
483
# File 'lib/nokolexbor/node.rb', line 481

def add_class(names)
  kwattr_add("class", names)
end

#add_next_sibling(node_or_tags) ⇒ Node, NodeSet Also known as: next=

Insert node_or_tags after this Node (as a sibling).

Parameters:

Returns:

Raises:

  • (ArgumentError)

See Also:



165
166
167
168
169
170
# File 'lib/nokolexbor/node.rb', line 165

def add_next_sibling(node_or_tags)
  raise ArgumentError,
    "A document may not have multiple root nodes." if parent&.document? && !(node_or_tags.comment? || node_or_tags.processing_instruction?)

  add_sibling(:next, node_or_tags)
end

#add_previous_sibling(node_or_tags) ⇒ Node, NodeSet Also known as: previous=

Insert node_or_tags before this Node (as a sibling).

Parameters:

Returns:

Raises:

  • (ArgumentError)

See Also:



151
152
153
154
155
156
# File 'lib/nokolexbor/node.rb', line 151

def add_previous_sibling(node_or_tags)
  raise ArgumentError,
    "A document may not have multiple root nodes." if parent&.document? && !(node_or_tags.comment? || node_or_tags.processing_instruction?)

  add_sibling(:previous, node_or_tags)
end

#add_sibling(next_or_previous, new) ⇒ Node, NodeSet

Insert node_or_tags before or after this Node (as a sibling).

Returns:

See Also:



1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
# File 'ext/nokolexbor/nl_node.c', line 1082

static VALUE
nl_node_add_sibling(VALUE self, VALUE next_or_previous, VALUE new)
{
  bool insert_after;
  if (rb_str_cmp(rb_String(next_or_previous), rb_str_new_literal("next")) == 0) {
    insert_after = true;
  } else if (rb_str_cmp(rb_String(next_or_previous), rb_str_new_literal("previous")) == 0) {
    insert_after = false;
  } else {
    rb_raise(rb_eArgError, "Unsupported inserting position");
  }

  return insert_after ? nl_node_add_nodes(self, new, lxb_dom_node_insert_after, true)
                      : nl_node_add_nodes(self, new, lxb_dom_node_insert_before, false);
}

#after(node_or_tags) ⇒ Node

Insert node_or_tags after this Node (as a sibling).

Parameters:

Returns:

  • (Node)

    self, to support chaining of calls.

See Also:



191
192
193
194
# File 'lib/nokolexbor/node.rb', line 191

def after(node_or_tags)
  add_next_sibling(node_or_tags)
  self
end

#ancestors(selector = nil) ⇒ NodeSet

Get a list of ancestor Node of this Node

Parameters:

  • selector (String, nil) (defaults to: nil)

    The selector to match ancestors

Returns:

  • (NodeSet)

    A set of matched ancestor nodes



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/nokolexbor/node.rb', line 73

def ancestors(selector = nil)
  return NodeSet.new(@document) unless respond_to?(:parent)
  return NodeSet.new(@document) unless parent

  parents = [parent]

  while parents.last.respond_to?(:parent)
    break unless (ctx_parent = parents.last.parent)

    parents << ctx_parent
  end

  return NodeSet.new(@document, parents) unless selector

  root = parents.last
  search_results = root.search(selector)

  NodeSet.new(@document, parents.find_all do |parent|
    search_results.include?(parent)
  end)
end

#append_class(names) ⇒ Node

Add CSS classes to self, regardless of duplication. Compare with #add_class.

This is a convenience function and is equivalent to:

node.kwattr_append("class", names)

Returns:

  • (Node)

    self, to support chaining of calls.

See Also:



497
498
499
# File 'lib/nokolexbor/node.rb', line 497

def append_class(names)
  kwattr_append("class", names)
end

#at(*args) ⇒ Node? Also known as: %

Like #search, but returns the first match.

Returns:

  • (Node, nil)

    The first matched Node.

See Also:



422
423
424
425
426
427
428
429
430
# File 'lib/nokolexbor/node.rb', line 422

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

  if paths.size == 1 && !LOOKS_LIKE_XPATH.match?(paths.first)
    return at_css(paths.first)
  end

  at_xpath(*(paths + [ns, handler, binds].compact))
end

#at_css(*args) ⇒ Node?

Like #css, but returns the first match.

This method uses Lexbor as the selector engine. Its performance is much higher than #at_xpath or #nokogiri_at_css.

Returns:

  • (Node, nil)

    The first matched Node.

See Also:



346
347
348
# File 'lib/nokolexbor/node.rb', line 346

def at_css(*args)
  at_css_impl(args.join(', '))
end

#at_css_impl(selector) ⇒ Object

Internal implementation of #at_css

See Also:



520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
# File 'ext/nokolexbor/nl_node.c', line 520

static VALUE
nl_node_at_css(VALUE self, VALUE selector)
{
  lxb_dom_node_t *node = nl_rb_node_unwrap(self);
  lexbor_array_t *array = lexbor_array_create();

  lxb_status_t status = nl_node_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, node->owner_document, array);

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

  lexbor_array_destroy(array, true);

  return ret;
}

#at_xpath(*args) ⇒ Node?

Like #xpath, but returns the first match.

It works the same way as Nokogiri::Node#at_xpath.

Returns:

  • (Node, nil)

    The first matched Node.

See Also:



398
399
400
# File 'lib/nokolexbor/node.rb', line 398

def at_xpath(*args)
  xpath(*args).first
end

#attribute(name) ⇒ Attribute

Returns The attribute belonging to this node with name name.

Returns:

  • (Attribute)

    The attribute belonging to this node with name name.



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'ext/nokolexbor/nl_node.c', line 154

static VALUE
nl_node_attribute(VALUE self, VALUE rb_name)
{
  lxb_dom_node_t *node = nl_rb_node_unwrap(self);

  const char *c_name = StringValuePtr(rb_name);
  size_t name_len = RSTRING_LEN(rb_name);

  if (node->type != LXB_DOM_NODE_TYPE_ELEMENT) {
    return Qnil;
  }

  lxb_dom_attr_t *attr = lxb_dom_element_attr_by_name(lxb_dom_interface_element(node), (const lxb_char_t *)c_name, name_len);
  if (attr == NULL) {
    return Qnil;
  }
  if (attr->owner == NULL) {
    attr->owner = lxb_dom_interface_element(node);
  }
  return nl_rb_node_create((lxb_dom_node_t *)attr, nl_rb_document_get(self));
}

#attribute_nodesArray<Attribute>

Returns An array of Attribute belonging to this node.

Returns:



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

static VALUE
nl_node_attribute_nodes(VALUE self)
{
  lxb_dom_node_t *node = nl_rb_node_unwrap(self);
  VALUE ary = rb_ary_new();
  if (node->type != LXB_DOM_NODE_TYPE_ELEMENT) {
    return ary;
  }

  lxb_dom_attr_t *attr = lxb_dom_element_first_attribute(lxb_dom_interface_element(node));

  if (attr == NULL) {
    return ary;
  }

  VALUE rb_doc = nl_rb_document_get(self);
  while (attr != NULL) {
    if (attr->owner == NULL) {
      attr->owner = lxb_dom_interface_element(node);
    }
    rb_ary_push(ary, nl_rb_node_create((lxb_dom_node_t *)attr, rb_doc));
    attr = attr->next;
  }

  return ary;
}

#attributesHash{String => Attribute}

Fetch this node's attributes.

Returns:

  • (Hash{String => Attribute})

    Hash containing attributes belonging to self. The hash keys are String attribute names, and the hash values are Attribute.



248
249
250
251
252
# File 'lib/nokolexbor/node.rb', line 248

def attributes
  attribute_nodes.each_with_object({}) do |node, hash|
    hash[node.name] = node
  end
end

#attrsHash{String => String}

Get a hash of attribute names and values of this Node.

Returns:

  • (Hash{String => String})

    A hash whose keys are attribute names and values are attribute values.



748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
# File 'ext/nokolexbor/nl_node.c', line 748

static VALUE
nl_node_attrs(VALUE self)
{
  lxb_dom_node_t *node = nl_rb_node_unwrap(self);
  VALUE rb_hash = rb_hash_new();

  if (node->type != LXB_DOM_NODE_TYPE_ELEMENT) {
    return rb_hash;
  }

  lxb_dom_attr_t *attr = lxb_dom_element_first_attribute(lxb_dom_interface_element(node));

  while (attr != NULL) {
    size_t tmp_len;
    const lxb_char_t *tmp = lxb_dom_attr_qualified_name(attr, &tmp_len);
    VALUE rb_key = rb_utf8_str_new((const char *)tmp, tmp_len);

    tmp = lxb_dom_attr_value(attr, &tmp_len);
    VALUE rb_value = tmp != NULL ? rb_utf8_str_new((const char *)tmp, tmp_len) : rb_str_new("", 0);

    rb_hash_aset(rb_hash, rb_key, rb_value);

    attr = lxb_dom_element_next_attribute(attr);
  }

  return rb_hash;
}

#before(node_or_tags) ⇒ Node

Insert node_or_tags before this Node (as a sibling).

Parameters:

Returns:

  • (Node)

    self, to support chaining of calls.

See Also:



179
180
181
182
# File 'lib/nokolexbor/node.rb', line 179

def before(node_or_tags)
  add_previous_sibling(node_or_tags)
  self
end

#cdata?Boolean

Returns true if this is a CDATA.

Returns:

  • (Boolean)

    true if this is a CDATA



31
32
33
# File 'lib/nokolexbor/node.rb', line 31

def cdata?
  type == CDATA_SECTION_NODE
end

#childNode?

Get the first child of this node.

Returns:

  • (Node, nil)

    The first child.



895
896
897
898
899
900
901
# File 'ext/nokolexbor/nl_node.c', line 895

static VALUE
nl_node_child(VALUE self)
{
  lxb_dom_node_t *node = nl_rb_node_unwrap(self);
  lxb_dom_node_t *child = node->first_child;
  return child ? nl_rb_node_create(child, nl_rb_document_get(self)) : Qnil;
}

#childrenNodeSet

Get the children of this node.

Returns:

  • (NodeSet)

    The set of this node's children.



853
854
855
856
857
858
859
860
861
862
863
864
865
866
# File 'ext/nokolexbor/nl_node.c', line 853

static VALUE
nl_node_children(VALUE self)
{
  lxb_dom_node_t *node = nl_rb_node_unwrap(self);
  lxb_dom_node_t *child = node->first_child;
  lexbor_array_t *array = lexbor_array_create();

  while (child != NULL) {
    lexbor_array_push(array, child);
    child = child->next;
  }

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

#children=(node) ⇒ Object Also known as: inner_html=

Set the content of this Node.

Parameters:

See Also:



284
285
286
287
# File 'lib/nokolexbor/node.rb', line 284

def children=(node)
  children.remove
  add_child(node)
end

#classesArray

Fetch CSS class names of a Node.

This is a convenience function and is equivalent to:

node.kwattr_values("class")

Examples:

node.classes # => ["section", "title", "header"]

Returns:

  • (Array)

    The CSS classes present in the Node's "class" attribute. If the attribute is empty or non-existent, the return value is an empty array.

See Also:



451
452
453
# File 'lib/nokolexbor/node.rb', line 451

def classes
  kwattr_values("class")
end

#cloneNode Also known as: dup

Copy this node.

Returns:



1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
# File 'ext/nokolexbor/nl_node.c', line 1163

static VALUE
nl_node_clone(VALUE self)
{
  lxb_dom_node_t *node = nl_rb_node_unwrap(self);
  lxb_dom_node_t *clone;

  switch (node->type) {
  case LXB_DOM_NODE_TYPE_ATTRIBUTE:
    clone = (lxb_dom_node_t *)lxb_dom_attr_interface_clone(node->owner_document, lxb_dom_interface_attr(node));
  case LXB_DOM_NODE_TYPE_CDATA_SECTION:
    clone = (lxb_dom_node_t *)lxb_dom_cdata_section_interface_clone(node->owner_document, lxb_dom_interface_cdata_section(node));
  default:
    clone = lxb_dom_node_clone(node, true);
    break;
  }
  return nl_rb_node_create(clone, nl_rb_document_get(self));
}

#comment?Boolean

Returns true if this is a Comment.

Returns:

  • (Boolean)

    true if this is a Comment



26
27
28
# File 'lib/nokolexbor/node.rb', line 26

def comment?
  type == COMMENT_NODE
end

#contentString Also known as: text, inner_text, to_str

Returns Contents of all the text nodes in this node's subtree, concatenated together into a single String.

Returns:

  • (String)

    Contents of all the text nodes in this node's subtree, concatenated together into a single String.



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'ext/nokolexbor/nl_node.c', line 211

static VALUE
nl_node_content(VALUE self)
{
  lxb_dom_node_t *node = nl_rb_node_unwrap(self);

  size_t str_len = 0;
  lxb_char_t *text = lxb_dom_node_text_content(node, &str_len);
  if (text == NULL) {
    return rb_str_new("", 0);
  }
  VALUE rb_str = rb_utf8_str_new((char *)text, str_len);
  lxb_dom_document_destroy_text(node->owner_document, text);

  return rb_str;
}

#content=(content) ⇒ String

Set the Node's content to a Text node containing content. The string gets XML escaped, not interpreted as markup.

Returns:

  • (String)

    content



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

static VALUE
nl_node_content_set(VALUE self, VALUE content)
{
  lxb_dom_node_t *node = nl_rb_node_unwrap(self);

  const char *c_content = StringValuePtr(content);
  size_t content_len = RSTRING_LEN(content);

  if (node->type == LXB_DOM_NODE_TYPE_DOCUMENT_FRAGMENT || node->type == LXB_DOM_NODE_TYPE_ELEMENT) {
    lxb_dom_text_t *text = lxb_dom_document_create_text_node(node->owner_document, (const lxb_char_t *)c_content, content_len);
    if (text == NULL) {
      nl_raise_lexbor_error(LXB_STATUS_ERROR_MEMORY_ALLOCATION);
    }

    // Ruby wrappers may still reference replaced nodes, so detach rather than destroy them.
    while (node->first_child != NULL) {
      lxb_dom_node_remove(node->first_child);
    }
    lxb_dom_node_insert_child(node, lxb_dom_interface_node(text));
  } else {
    lxb_status_t status = lxb_dom_node_text_content_set(node, (const lxb_char_t *)c_content, content_len);
    if (status != LXB_STATUS_OK) {
      nl_raise_lexbor_error(status);
    }
  }
  return content;
}

#css(*args) ⇒ NodeSet

Search this object for CSS rules. rules must be one or more CSS selectors.

This method uses Lexbor as the selector engine. Its performance is much higher than #xpath or #nokogiri_css.

Examples:

node.css('title')
node.css('body h1.bold')
node.css('div + p.green', 'div#one')

Returns:

  • (NodeSet)

    The matched set of Nodes.

See Also:



334
335
336
# File 'lib/nokolexbor/node.rb', line 334

def css(*args)
  css_impl(args.join(', '))
end

#css_impl(selector) ⇒ Object

Internal implementation of #css

See Also:



552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
# File 'ext/nokolexbor/nl_node.c', line 552

static VALUE
nl_node_css(VALUE self, VALUE selector)
{
  lxb_dom_node_t *node = nl_rb_node_unwrap(self);
  lexbor_array_t *array = lexbor_array_create();

  lxb_status_t status = nl_node_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, node->owner_document, array);

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

#css_pathObject

Get the path to this node as a CSS expression



62
63
64
65
66
# File 'lib/nokolexbor/node.rb', line 62

def css_path
  path.split(%r{/}).filter_map do |part|
    part.empty? ? nil : part.gsub(/\[(\d+)\]/, ':nth-of-type(\1)')
  end.join(" > ")
end

#destroynil

Remove this node from its current context and free its allocated memory.

Returns:

  • (nil)

See Also:



923
924
925
926
927
928
929
# File 'ext/nokolexbor/nl_node.c', line 923

static VALUE
nl_node_destroy(VALUE self)
{
  lxb_dom_node_t *node = nl_rb_node_unwrap(self);
  lxb_dom_node_destroy(node);
  return Qnil;
}

#document?Boolean

Returns true if this is a Document.

Returns:

  • (Boolean)

    true if this is a Document



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

def document?
  is_a?(Nokolexbor::Document)
end

#each {|String, String| ... } ⇒ Object

Iterate over each attribute name and value pair of this Node.

Yields:

  • (String, String)

    The name and value of the current attribute.



304
305
306
307
308
# File 'lib/nokolexbor/node.rb', line 304

def each
  attributes.each do |name, node|
    yield [name, node.value]
  end
end

#element?Boolean Also known as: elem?

Returns true if this is an Element.

Returns:

  • (Boolean)

    true if this is an Element



51
52
53
# File 'lib/nokolexbor/node.rb', line 51

def element?
  type == ELEMENT_NODE
end

#element_childrenNodeSet Also known as: elements

Get the element children of this node.

Returns:

  • (NodeSet)

    The set of this node's element children.



873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
# File 'ext/nokolexbor/nl_node.c', line 873

static VALUE
nl_node_element_children(VALUE self)
{
  lxb_dom_node_t *node = nl_rb_node_unwrap(self);
  lxb_dom_node_t *child = node->first_child;
  lexbor_array_t *array = lexbor_array_create();

  while (child != NULL) {
    if (child->type == LXB_DOM_NODE_TYPE_ELEMENT) {
      lexbor_array_push(array, child);
    }
    child = child->next;
  }

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

#first_element_childElement

Returns The first child Node that is an element.

Returns:

  • (Element)

    The first child Node that is an element.



1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
# File 'ext/nokolexbor/nl_node.c', line 1125

static VALUE
nl_node_first_element_child(VALUE self)
{
  lxb_dom_node_t *parent = nl_rb_node_unwrap(self);
  lxb_dom_node_t *cur = parent->first_child;

  while (cur != NULL) {
    if (cur->type == LXB_DOM_NODE_TYPE_ELEMENT) {
      return nl_rb_node_create(cur, nl_rb_document_get(self));
    }
    cur = cur->next;
  }
  return Qnil;
}

#fragment(tags) ⇒ DocumentFragment

Create a DocumentFragment containing tags that is relative to this context node.

Returns:



314
315
316
# File 'lib/nokolexbor/node.rb', line 314

def fragment(tags)
  Nokolexbor::DocumentFragment.new(document, tags, self)
end

#fragment?Boolean

Returns true if this is a DocumentFragment.

Returns:



46
47
48
# File 'lib/nokolexbor/node.rb', line 46

def fragment?
  type == DOCUMENT_FRAG_NODE
end

#inner_html(*args) ⇒ String

Get the inner_html of this Node.

Returns:

  • (String)


574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
# File 'ext/nokolexbor/nl_node.c', line 574

static VALUE
nl_node_inner_html(int argc, VALUE *argv, VALUE self)
{
  lxb_dom_node_t *node = nl_rb_node_unwrap(self);
  lxb_dom_node_t *serialization_root = node;
  lexbor_str_t str = {0};
  VALUE options;
  lxb_status_t status;
  size_t indent = 0;
  rb_scan_args(argc, argv, "01", &options);

  if (node->type == LXB_DOM_NODE_TYPE_ELEMENT
      && node->local_name == LXB_TAG_TEMPLATE
      && node->ns == LXB_NS_HTML) {
    serialization_root = &lxb_html_interface_template(node)->content->node;
  }

  if (TYPE(options) == T_HASH) {
    VALUE rb_indent = rb_hash_aref(options, ID2SYM(rb_intern("indent")));
    if (!NIL_P(rb_indent)) {
      indent = NUM2INT(rb_indent);
    }
  }
  if (indent > 0) {
    status = lxb_html_serialize_pretty_deep_str(serialization_root, 0, 0, &str);
  } else {
    status = lxb_html_serialize_deep_str(serialization_root, &str);
  }
  if (status != LXB_STATUS_OK) {
    if (str.data != NULL) {
      lexbor_str_destroy(&str, node->owner_document->text, false);
    }
    nl_raise_lexbor_error(status);
  }

  if (str.data != NULL) {
    VALUE ret = rb_utf8_str_new((const char *)str.data, str.length);
    lexbor_str_destroy(&str, node->owner_document->text, false);
    return ret;
  }

  return Qnil;
}

#inspect(*args) ⇒ Object



1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
# File 'ext/nokolexbor/nl_node.c', line 1181

static VALUE
nl_node_inspect(int argc, VALUE *argv, VALUE self)
{
  lxb_dom_node_t *node = nl_rb_node_unwrap(self);
  if (node->type == LXB_DOM_NODE_TYPE_DOCUMENT) {
    return rb_call_super(argc, argv);
  }

  VALUE c = rb_class_name(CLASS_OF(self));
  lexbor_str_t str = {0};
  lxb_status_t status = lxb_html_serialize_str(node, &str);
  if (status != LXB_STATUS_OK) {
    if (str.data != NULL) {
      lexbor_str_destroy(&str, node->owner_document->text, false);
    }
    return rb_call_super(argc, argv);
  }

  if (str.data != NULL) {
    VALUE ret = rb_sprintf("#<%" PRIsVALUE " %s>", c, str.data);
    lexbor_str_destroy(&str, node->owner_document->text, false);
    return ret;
  }
  return rb_call_super(argc, argv);
}

#key?(name) ⇒ Boolean Also known as: has_attribute?

Returns true if name is set.

Returns:

  • (Boolean)

Returns:

  • (Boolean)

    true if name is set.



665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
# File 'ext/nokolexbor/nl_node.c', line 665

static VALUE
nl_node_has_key(VALUE self, VALUE rb_attr)
{
  lxb_dom_node_t *node = nl_rb_node_unwrap(self);

  if (node->type != LXB_DOM_NODE_TYPE_ELEMENT) {
    return Qfalse;
  }

  VALUE rb_attr_s = rb_String(rb_attr);
  const char *attr_c = RSTRING_PTR(rb_attr_s);
  size_t attr_len = RSTRING_LEN(rb_attr_s);

  lxb_dom_element_t *element = lxb_dom_interface_element(node);

  return lxb_dom_element_has_attribute(element, (const lxb_char_t *)attr_c, attr_len) ? Qtrue : Qfalse;
}

#keysArray<String>

Get the attribute names of this Node.

Returns:

  • (Array<String>)

    An array of attribute names.



688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
# File 'ext/nokolexbor/nl_node.c', line 688

static VALUE
nl_node_keys(VALUE self)
{
  lxb_dom_node_t *node = nl_rb_node_unwrap(self);
  VALUE ary_keys = rb_ary_new();

  if (node->type != LXB_DOM_NODE_TYPE_ELEMENT) {
    return ary_keys;
  }

  lxb_dom_attr_t *attr = lxb_dom_element_first_attribute(lxb_dom_interface_element(node));

  while (attr != NULL) {
    size_t tmp_len;
    const lxb_char_t *tmp = lxb_dom_attr_qualified_name(attr, &tmp_len);
    rb_ary_push(ary_keys, rb_utf8_str_new((const char *)tmp, tmp_len));

    attr = lxb_dom_element_next_attribute(attr);
  }

  return ary_keys;
}

#kwattr_add(attribute_name, keywords) ⇒ Node

Ensure that values are present in a keyword attribute.

Any values in keywords that already exist in the Node's attribute values are not added. Note that any existing duplicates in the attribute values are not removed. Compare with #kwattr_append.

A "keyword attribute" is a node attribute that contains a set of space-delimited values. Perhaps the most familiar example of this is the HTML "class" attribute used to contain CSS classes. But other keyword attributes exist, for instance the "rel" attribute.

Parameters:

  • attribute_name (String)

    The name of the keyword attribute to be modified.

  • keywords (String, Array<String>)

    Keywords to be added to the attribute named attribute_name. May be a string containing whitespace-delimited values, or an Array of String values. Any values already present will not be added. Any values not present will be added. If the named attribute does not exist, it is created.

Returns:

  • (Node)

    self, to support chaining of calls.

See Also:



576
577
578
579
580
581
582
# File 'lib/nokolexbor/node.rb', line 576

def kwattr_add(attribute_name, keywords)
  keywords = keywordify(keywords)
  current_kws = kwattr_values(attribute_name)
  new_kws = (current_kws + (keywords - current_kws)).join(" ")
  set_attr(attribute_name, new_kws)
  self
end

#kwattr_append(attribute_name, keywords) ⇒ Node

Add keywords to a Node's keyword attribute, regardless of duplication. Compare with #kwattr_add.

A "keyword attribute" is a node attribute that contains a set of space-delimited values. Perhaps the most familiar example of this is the HTML "class" attribute used to contain CSS classes. But other keyword attributes exist, for instance the "rel" attribute.

Parameters:

  • attribute_name (String)

    The name of the keyword attribute to be modified.

  • keywords (String, Array<String>)

    Keywords to be added to the attribute named attribute_name. May be a string containing whitespace-delimited values, or an Array of String values. Any values already present will not be added. Any values not present will be added. If the named attribute does not exist, it is created.

Returns:

  • (Node)

    self, to support chaining of calls.

See Also:



605
606
607
608
609
610
611
# File 'lib/nokolexbor/node.rb', line 605

def kwattr_append(attribute_name, keywords)
  keywords = keywordify(keywords)
  current_kws = kwattr_values(attribute_name)
  new_kws = (current_kws + keywords).join(" ")
  set_attr(attribute_name, new_kws)
  self
end

#kwattr_remove(attribute_name, keywords) ⇒ Node

Remove keywords from a keyword attribute. Any matching keywords that exist in the named attribute are removed, including any multiple entries.

If no keywords remain after this operation, or if keywords is nil, the attribute is deleted from the node.

A "keyword attribute" is a node attribute that contains a set of space-delimited values. Perhaps the most familiar example of this is the HTML "class" attribute used to contain CSS classes. But other keyword attributes exist, for instance the "rel" attribute.

Parameters:

  • attribute_name (String)

    The name of the keyword attribute to be modified.

  • keywords (String, Array<String>)

    Keywords to be added to the attribute named attribute_name. May be a string containing whitespace-delimited values, or an Array of String values. Any values already present will not be added. Any values not present will be added. If the named attribute does not exist, it is created.

Returns:

  • (Node)

    self, to support chaining of calls.

See Also:



637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
# File 'lib/nokolexbor/node.rb', line 637

def kwattr_remove(attribute_name, keywords)
  if keywords.nil?
    remove_attr(attribute_name)
    return self
  end

  keywords = keywordify(keywords)
  current_kws = kwattr_values(attribute_name)
  new_kws = current_kws - keywords
  if new_kws.empty?
    remove_attr(attribute_name)
  else
    set_attr(attribute_name, new_kws.join(" "))
  end
  self
end

#kwattr_values(attribute_name) ⇒ Array<String>

Fetch values from a keyword attribute of a Node.

A "keyword attribute" is a node attribute that contains a set of space-delimited values. Perhaps the most familiar example of this is the HTML "class" attribute used to contain CSS classes. But other keyword attributes exist, for instance the "rel" attribute.

@#kwattr_append @#kwattr_remove

Parameters:

  • attribute_name (String)

    The name of the keyword attribute to be inspected.

Returns:

  • (Array<String>)

    The values present in the Node's attribute_name attribute. If the attribute is empty or non-existent, the return value is an empty array.

See Also:



548
549
550
# File 'lib/nokolexbor/node.rb', line 548

def kwattr_values(attribute_name)
  keywordify(attr(attribute_name) || [])
end

#last_element_childElement

Returns The last child Node that is an element.

Returns:

  • (Element)

    The last child Node that is an element.



1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
# File 'ext/nokolexbor/nl_node.c', line 1143

static VALUE
nl_node_last_element_child(VALUE self)
{
  lxb_dom_node_t *parent = nl_rb_node_unwrap(self);
  lxb_dom_node_t *cur = parent->last_child;

  while (cur != NULL) {
    if (cur->type == LXB_DOM_NODE_TYPE_ELEMENT) {
      return nl_rb_node_create(cur, nl_rb_document_get(self));
    }
    cur = cur->prev;
  }
  return Qnil;
}

#matches?(selector) ⇒ Boolean

Returns true if this Node matches selector.

Parameters:

  • selector (String)

    The selector to match

Returns:

  • (Boolean)

    true if this Node matches selector



241
242
243
# File 'lib/nokolexbor/node.rb', line 241

def matches?(selector)
  ancestors.last.css(selector).any? { |node| node == self }
end

#nameString Also known as: node_name

Get the name of this Node

Returns:

  • (String)

    The name of this Node



967
968
969
970
971
972
973
974
# File 'ext/nokolexbor/nl_node.c', line 967

static VALUE
nl_node_name(VALUE self)
{
  lxb_dom_node_t *node = nl_rb_node_unwrap(self);
  size_t len;
  const lxb_char_t *name = lxb_dom_node_name_qualified(node, &len);
  return rb_utf8_str_new((const char *)name, len);
}

#nextNode? Also known as: next_sibling

Get the next sibling node.

Returns:

  • (Node, nil)

    The previous sibling node



823
824
825
826
827
828
# File 'ext/nokolexbor/nl_node.c', line 823

static VALUE
nl_node_next(VALUE self)
{
  lxb_dom_node_t *node = nl_rb_node_unwrap(self);
  return node->next ? nl_rb_node_create(node->next, nl_rb_document_get(self)) : Qnil;
}

#next_elementElement?

Get the next sibling element.

Returns:

  • (Element, nil)

    The previous sibling element



835
836
837
838
839
840
841
842
843
844
845
846
# File 'ext/nokolexbor/nl_node.c', line 835

static VALUE
nl_node_next_element(VALUE self)
{
  lxb_dom_node_t *node = nl_rb_node_unwrap(self);
  while (node->next != NULL) {
    node = node->next;
    if (node->type == LXB_DOM_NODE_TYPE_ELEMENT) {
      return nl_rb_node_create(node, nl_rb_document_get(self));
    }
  }
  return Qnil;
}

#node_typeInteger Also known as: type

Get the type of this Node

Returns:

  • (Integer)


1116
1117
1118
1119
1120
# File 'ext/nokolexbor/nl_node.c', line 1116

static VALUE
nl_node_get_type(VALUE self)
{
  return INT2NUM(nl_rb_node_unwrap(self)->type);
}

#nokogiri_at_css(*args) ⇒ Node?

Like #nokogiri_css, but returns the first match.

This method uses libxml2 as the selector engine. It works the same way as Nokogiri::Node#at_css.

Returns:

  • (Node, nil)

    The first matched Node.

See Also:



372
373
374
# File 'lib/nokolexbor/node.rb', line 372

def nokogiri_at_css(*args)
  nokogiri_css(*args).first
end

#nokogiri_css(*args) ⇒ NodeSet

Search this object for CSS rules. rules must be one or more CSS selectors. It supports a mixed syntax of CSS selectors and XPath.

This method uses libxml2 as the selector engine. It works the same way as Nokogiri::Node#css.

Returns:

  • (NodeSet)

    The matched set of Nodes.

See Also:



358
359
360
361
362
# File 'lib/nokolexbor/node.rb', line 358

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

  nokogiri_css_internal(self, rules, handler, ns)
end

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

Serialize this Node to HTML, also known as outer_html.

Returns:

  • (String)


623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
# File 'ext/nokolexbor/nl_node.c', line 623

static VALUE
nl_node_outer_html(int argc, VALUE *argv, VALUE self)
{
  lxb_dom_node_t *node = nl_rb_node_unwrap(self);
  lexbor_str_t str = {0};
  VALUE options;
  lxb_status_t status;
  size_t indent = 0;
  rb_scan_args(argc, argv, "01", &options);

  if (TYPE(options) == T_HASH) {
    VALUE rb_indent = rb_hash_aref(options, ID2SYM(rb_intern("indent")));
    if (!NIL_P(rb_indent)) {
      indent = NUM2INT(rb_indent);
    }
  }
  if (indent > 0) {
    status = lxb_html_serialize_pretty_tree_str(node, 0, 0, &str);
  } else {
    status = lxb_html_serialize_tree_str(node, &str);
  }
  if (status != LXB_STATUS_OK) {
    if (str.data != NULL) {
      lexbor_str_destroy(&str, node->owner_document->text, false);
    }
    nl_raise_lexbor_error(status);
  }

  if (str.data != NULL) {
    VALUE ret = rb_utf8_str_new((const char *)str.data, str.length);
    lexbor_str_destroy(&str, node->owner_document->text, false);
    return ret;
  }

  return Qnil;
}

#parentNode?

Get the parent node.

Returns:

  • (Node, nil)

    The parent node



781
782
783
784
785
786
# File 'ext/nokolexbor/nl_node.c', line 781

static VALUE
nl_node_parent(VALUE self)
{
  lxb_dom_node_t *node = nl_rb_node_unwrap(self);
  return node->parent ? nl_rb_node_create(node->parent, nl_rb_document_get(self)) : Qnil;
}

#parent=(parent_node) ⇒ Object

Set the parent Node of this Node.

Parameters:

  • parent_node (Node)

    The parent node.



292
293
294
# File 'lib/nokolexbor/node.rb', line 292

def parent=(parent_node)
  parent_node.add_child(self)
end

#parse(html) ⇒ NodeSet

Parse html as a document fragment within the context of this node.

Parameters:

  • html (String)

    The fragment to be parsed.

Returns:



1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
# File 'ext/nokolexbor/nl_node.c', line 1005

static VALUE
nl_node_parse(VALUE self, VALUE html)
{
  Check_Type(html, T_STRING);
  lxb_dom_node_t *node = nl_rb_node_unwrap(self);
  lxb_dom_document_t *doc = node->owner_document;

  lxb_dom_node_t *frag_root = nl_node_parse_fragment(doc, lxb_dom_interface_element(node), (lxb_char_t *)RSTRING_PTR(html), RSTRING_LEN(html));
  lexbor_array_t *array = lexbor_array_create();

  while (frag_root->first_child != NULL) {
    lxb_dom_node_t *child = frag_root->first_child;
    lxb_dom_node_remove(child);
    lexbor_array_push(array, child);
  }
  lxb_dom_node_destroy(frag_root);
  return nl_rb_node_set_create_with_data(array, nl_rb_document_get(self));
}

#pathString

Returns The path associated with this Node.

Returns:

  • (String)

    The path associated with this Node.



1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
# File 'ext/nokolexbor/nl_node.c', line 1220

static VALUE
nl_node_path(VALUE self)
{
  lxb_dom_node_t *node = nl_rb_node_unwrap(self);
  char* path = nl_xmlGetNodePath(node);
  if (path == NULL) {
    return Qnil;
  }
  VALUE ret = rb_utf8_str_new_cstr(path);
  nl_xmlFree(path);
  return ret;
}

#pointer_idObject Also known as: hash



945
946
947
948
949
950
# File 'ext/nokolexbor/nl_node.c', line 945

static VALUE
nl_node_pointer_id(VALUE self)
{
  lxb_dom_node_t *node = nl_rb_node_unwrap(self);
  return ULL2NUM((unsigned long long)(uintptr_t)node);
}

#prepend_child(node) ⇒ Node, NodeSet

Add node as the first child of this Node.

Parameters:

Returns:

See Also:



220
221
222
223
224
225
226
227
228
229
# File 'lib/nokolexbor/node.rb', line 220

def prepend_child(node)
  if (first = children.first)
    # Mimic the error add_child would raise.
    raise "Document already has a root node" if document? && !(node.comment? || node.processing_instruction?)

    first.add_sibling(:previous, node)
  else
    add_child(node)
  end
end

#previousNode? Also known as: previous_sibling

Get the previous sibling node.

Returns:

  • (Node, nil)

    The previous sibling node



793
794
795
796
797
798
# File 'ext/nokolexbor/nl_node.c', line 793

static VALUE
nl_node_previous(VALUE self)
{
  lxb_dom_node_t *node = nl_rb_node_unwrap(self);
  return node->prev ? nl_rb_node_create(node->prev, nl_rb_document_get(self)) : Qnil;
}

#previous_elementElement?

Get the previous sibling element.

Returns:

  • (Element, nil)

    The previous sibling element



805
806
807
808
809
810
811
812
813
814
815
816
# File 'ext/nokolexbor/nl_node.c', line 805

static VALUE
nl_node_previous_element(VALUE self)
{
  lxb_dom_node_t *node = nl_rb_node_unwrap(self);
  while (node->prev != NULL) {
    node = node->prev;
    if (node->type == LXB_DOM_NODE_TYPE_ELEMENT) {
      return nl_rb_node_create(node, nl_rb_document_get(self));
    }
  }
  return Qnil;
}

#processing_instruction?Boolean

Returns true if this is a ProcessingInstruction.

Returns:



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

def processing_instruction?
  type == PI_NODE
end

#removeNode Also known as: unlink

Remove this node from its current context.

Returns:

  • (Node)

    self, to support chaining of calls.



908
909
910
911
912
913
914
# File 'ext/nokolexbor/nl_node.c', line 908

static VALUE
nl_node_remove(VALUE self)
{
  lxb_dom_node_t *node = nl_rb_node_unwrap(self);
  lxb_dom_node_remove(node);
  return self;
}

#remove_attr(name) ⇒ Boolean Also known as: delete, remove_attribute

Remove the attribute named name.

Returns:

  • (Boolean)

Parameters:

  • name (String)

Returns:

  • (Boolean)

    true if removal success, false if node is not an Element.



338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'ext/nokolexbor/nl_node.c', line 338

static VALUE
nl_node_remove_attr(VALUE self, VALUE rb_attr)
{
  lxb_dom_node_t *node = nl_rb_node_unwrap(self);

  if (node->type != LXB_DOM_NODE_TYPE_ELEMENT) {
    return Qfalse;
  }

  VALUE rb_attr_s = rb_String(rb_attr);

  const char *attr_c = RSTRING_PTR(rb_attr_s);
  size_t attr_len = RSTRING_LEN(rb_attr_s);

  lxb_dom_element_t *element = lxb_dom_interface_element(node);

  lxb_status_t status = lxb_dom_element_remove_attribute(element, (const lxb_char_t *)attr_c, attr_len);
  if (status != LXB_STATUS_OK) {
    nl_raise_lexbor_error(status);
  }

  return Qtrue;
}

#remove_class(names = nil) ⇒ Node

Remove CSS classes from this node. Any CSS class names in css_classes that exist in this node's "class" attribute are removed, including any multiple entries.

If no CSS classes remain after this operation, or if css_classes is nil, the "class" attribute is deleted from the node.

This is a convenience function and is equivalent to:

node.kwattr_remove("class", css_classes)

Examples:

node.remove_class("section")
node.remove_class(["section", "float"])

Parameters:

  • names (String, Array<String>) (defaults to: nil)

    CSS class names to be removed from the Node's "class" attribute. May be a string containing whitespace-delimited names, or an Array of String names. Any class names already present will be removed. If no CSS classes remain, the "class" attribute is deleted.

Returns:

  • (Node)

    self, to support chaining of calls.

See Also:



527
528
529
# File 'lib/nokolexbor/node.rb', line 527

def remove_class(names = nil)
  kwattr_remove("class", names)
end

#replace(node) ⇒ Node, NodeSet

Replace this Node with node.

Parameters:

Returns:

See Also:



261
262
263
264
265
# File 'lib/nokolexbor/node.rb', line 261

def replace(node)
  ret = add_sibling(:previous, node)
  remove
  ret
end

#search(*args) ⇒ NodeSet Also known as: /

Search this object for paths. paths must be one or more XPath or CSS selectors.

Returns:

  • (NodeSet)

    The matched set of Nodes.



405
406
407
408
409
410
411
412
413
# File 'lib/nokolexbor/node.rb', line 405

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

  if paths.size == 1 && !LOOKS_LIKE_XPATH.match?(paths.first)
    return css(paths.first)
  end

  xpath(*(paths + [ns, handler, binds].compact))
end

#source_locationInteger

Returns The node's location at the source HTML. Returns 0 if the node is not parsed from a HTML string.

Returns:

  • (Integer)

    The node's location at the source HTML. Returns 0 if the node is not parsed from a HTML string.



1210
1211
1212
1213
1214
1215
# File 'ext/nokolexbor/nl_node.c', line 1210

static VALUE
nl_node_source_location(VALUE self)
{
  lxb_dom_node_t *node = nl_rb_node_unwrap(self);
  return ULONG2NUM(node->source_location);
}

#swap(node) ⇒ Node

Swap this Node for node.

Parameters:

Returns:

  • (Node)

    self, to support chaining of calls.

See Also:



274
275
276
277
# File 'lib/nokolexbor/node.rb', line 274

def swap(node)
  replace(node)
  self
end

#text?Boolean

Returns true if this is a Text.

Returns:

  • (Boolean)

    true if this is a Text



41
42
43
# File 'lib/nokolexbor/node.rb', line 41

def text?
  type == TEXT_NODE
end

#traverse { ... } ⇒ Object

Traverse self and all children.

Yields:

  • self and all children to block recursively.



233
234
235
236
# File 'lib/nokolexbor/node.rb', line 233

def traverse(&block)
  children.each { |j| j.traverse(&block) }
  yield(self)
end

#value?(value) ⇒ Boolean

Returns true if this Node's attributes include .

Returns:

  • (Boolean)

    true if this Node's attributes include



297
298
299
# File 'lib/nokolexbor/node.rb', line 297

def value?(value)
  values.include?(value)
end

#valuesArray<String>

Get the attribute values of this Node.

Returns:

  • (Array<String>)

    An array of attribute values.



716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
# File 'ext/nokolexbor/nl_node.c', line 716

static VALUE
nl_node_values(VALUE self)
{
  lxb_dom_node_t *node = nl_rb_node_unwrap(self);
  VALUE ary_values = rb_ary_new();

  if (node->type != LXB_DOM_NODE_TYPE_ELEMENT) {
    return ary_values;
  }

  lxb_dom_attr_t *attr = lxb_dom_element_first_attribute(lxb_dom_interface_element(node));

  while (attr != NULL) {
    size_t tmp_len;
    const lxb_char_t *tmp = lxb_dom_attr_value(attr, &tmp_len);
    if (tmp != NULL) {
      rb_ary_push(ary_values, rb_utf8_str_new((const char *)tmp, tmp_len));
    } else {
      rb_ary_push(ary_values, rb_str_new("", 0));
    }

    attr = lxb_dom_element_next_attribute(attr);
  }

  return ary_values;
}

#wrap(node) ⇒ Node

Wrap this Node with another node.

Examples:

with a String argument:


doc = Nokolexbor::HTML('<body><a>123</a></body>')
doc.at_css('a').wrap('<div></div>')
doc.at_css('body').inner_html
# => "<div><a>123</a></div>"

with a Nokolexbor::Node argument:


doc = Nokolexbor::HTML('<body><a>123</a></body>')
doc.at_css('a').wrap(doc.create_element('div'))
doc.at_css('body').inner_html
# => "<div><a>123</a></div>"

Parameters:

  • node (String, Node)

    A string or a node

    • when String: The markup that is parsed and used as the wrapper. If the parsed fragment has multiple roots, the first root node is used as the wrapper.
    • when Nokolexbor::Node: An element that is cloned and used as the wrapper.

Returns:

  • (Node)

    self, to support chaining of calls.

See Also:



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/nokolexbor/node.rb', line 122

def wrap(node)
  case node
  when String
    new_parent = fragment(node).child
  when DocumentFragment
    new_parent = node.child
  when Node
    new_parent = node.dup
  else
    raise ArgumentError, "Requires a String or Node argument, and cannot accept a #{node.class}"
  end

  if parent
    add_sibling(:next, new_parent)
  else
    new_parent.remove
  end
  new_parent.add_child(self)

  self
end

#write_to(io, *options) ⇒ Object Also known as: write_html_to

Serialize Node and write to io.



655
656
657
# File 'lib/nokolexbor/node.rb', line 655

def write_to(io, *options)
  io.write(to_html(*options))
end

#xpath(*args) ⇒ NodeSet

Search this node for XPath paths. paths must be one or more XPath queries.

It works the same way as Nokogiri::Node#xpath.

Examples:

node.xpath('.//title')

Returns:

  • (NodeSet)

    The matched set of Nodes.



385
386
387
388
389
# File 'lib/nokolexbor/node.rb', line 385

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

  xpath_internal(self, paths, handler, ns, binds)
end