Class: Nokolexbor::Node
- Inherits:
-
Object
- Object
- Nokolexbor::Node
- 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
-
#document ⇒ Object
readonly
Returns the value of attribute document.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #[](rb_attr) ⇒ Object (also: #attr)
- #[]=(rb_attr, rb_value) ⇒ Object (also: #set_attr)
- #add_child(new) ⇒ Object
- #add_class(names) ⇒ Object
- #add_sibling(next_or_previous, new) ⇒ Object
- #ancestors(selector = nil) ⇒ Object
- #append_class(names) ⇒ Object
- #at(*args) ⇒ Object (also: #%)
- #at_css(*args) ⇒ Object
- #at_css_impl(selector) ⇒ Object
- #at_xpath(*args) ⇒ Object
- #attribute(name) ⇒ Object
- #attributes ⇒ Object
- #attrs ⇒ Object
- #cdata? ⇒ Boolean
- #child ⇒ Object
- #children ⇒ Object
- #children=(node) ⇒ Object (also: #inner_html=)
- #classes ⇒ Object
- #clone ⇒ Object (also: #dup)
- #comment? ⇒ Boolean
- #content ⇒ Object (also: #text, #inner_text, #to_str)
- #css(*args) ⇒ Object
- #css_impl(selector) ⇒ Object
- #destroy ⇒ Object
- #each ⇒ Object
- #element? ⇒ Boolean
- #first_element_child ⇒ Object
- #fragment(html) ⇒ Object
- #fragment? ⇒ Boolean
- #inner_html ⇒ Object
- #key?(rb_attr) ⇒ Boolean
- #keys ⇒ Object
- #keywordify(keywords) ⇒ Object
- #kwattr_add(attribute_name, keywords) ⇒ Object
- #kwattr_append(attribute_name, keywords) ⇒ Object
- #kwattr_remove(attribute_name, keywords) ⇒ Object
- #kwattr_values(attribute_name) ⇒ Object
- #last_element_child ⇒ Object
- #matches?(selector) ⇒ Boolean
- #name ⇒ Object
- #next ⇒ Object
- #next_element ⇒ Object
- #node_type ⇒ Object (also: #type)
- #outer_html ⇒ Object (also: #to_html, #to_s)
- #parent ⇒ Object
- #previous ⇒ Object
- #previous_element ⇒ Object
- #processing_instruction? ⇒ Boolean
- #remove ⇒ Object (also: #unlink)
- #remove_attr(rb_attr) ⇒ Object (also: #delete)
- #remove_class(names = nil) ⇒ Object
- #replace(node) ⇒ Object
- #search(*args) ⇒ Object (also: #/)
- #text? ⇒ Boolean
- #values ⇒ Object
- #wrap(node) ⇒ Object
- #xpath(*args) ⇒ Object
Instance Attribute Details
#document ⇒ Object (readonly)
Returns the value of attribute document.
20 21 22 |
# File 'lib/nokolexbor/node.rb', line 20 def document @document end |
Class Method Details
.new(*args) ⇒ Object
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 |
# File 'ext/nokolexbor/nl_node.c', line 55
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))
{
rb_raise(rb_eArgError, "Document must be a Nokolexbor::Document");
}
document = nl_rb_document_unwrap(rb_document);
lxb_dom_element_t *element = lxb_dom_document_create_element(document, (const lxb_char_t *)StringValueCStr(rb_name), RSTRING_LEN(rb_name), 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
#==(other) ⇒ Object
590 591 592 593 594 595 596 |
# File 'ext/nokolexbor/nl_node.c', line 590
static VALUE
nl_node_equals(VALUE self, VALUE other)
{
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;
}
|
#[](rb_attr) ⇒ Object Also known as: attr
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 |
# File 'ext/nokolexbor/nl_node.c', line 105
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);
}
|
#[]=(rb_attr, rb_value) ⇒ Object Also known as: set_attr
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'ext/nokolexbor/nl_node.c', line 132
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) ⇒ Object
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 |
# File 'ext/nokolexbor/nl_node.c', line 697
static VALUE
nl_node_add_child(VALUE self, VALUE new)
{
lxb_dom_node_t *node = nl_rb_node_unwrap(self);
lxb_dom_document_t *doc = node->owner_document;
if (TYPE(new) == T_STRING)
{
lxb_dom_node_t *frag_root = nl_node_parse_fragment(doc, (lxb_char_t *)RSTRING_PTR(new), RSTRING_LEN(new));
while (frag_root->first_child != NULL)
{
lxb_dom_node_t *child = frag_root->first_child;
lxb_dom_node_remove(child);
lxb_dom_node_insert_child(node, child);
}
lxb_dom_node_destroy(frag_root);
}
else if (rb_obj_class(new) == cNokolexborNode)
{
lxb_dom_node_t *node_new = nl_rb_node_unwrap(new);
lxb_dom_node_remove(node_new);
lxb_dom_node_insert_child(node, node_new);
}
else
{
rb_raise(rb_eArgError, "Unsupported node type");
}
return Qnil;
}
|
#add_class(names) ⇒ Object
175 176 177 |
# File 'lib/nokolexbor/node.rb', line 175 def add_class(names) kwattr_add("class", names) end |
#add_sibling(next_or_previous, new) ⇒ Object
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 |
# File 'ext/nokolexbor/nl_node.c', line 652
static VALUE
nl_node_add_sibling(VALUE self, VALUE next_or_previous, VALUE new)
{
lxb_dom_node_t *node = nl_rb_node_unwrap(self);
lxb_dom_document_t *doc = node->owner_document;
int insert_after;
if (rb_eql(rb_String(next_or_previous), rb_str_new_literal("next")))
{
insert_after = 1;
}
else if (rb_eql(rb_String(next_or_previous), rb_str_new_literal("previous")))
{
insert_after = 0;
}
else
{
rb_raise(rb_eArgError, "Unsupported inserting position");
}
if (TYPE(new) == T_STRING)
{
lxb_dom_node_t *frag_root = nl_node_parse_fragment(doc, (lxb_char_t *)RSTRING_PTR(new), RSTRING_LEN(new));
while (frag_root->first_child != NULL)
{
lxb_dom_node_t *child = frag_root->first_child;
lxb_dom_node_remove(child);
insert_after ? lxb_dom_node_insert_after(node, child) : lxb_dom_node_insert_before(node, child);
}
lxb_dom_node_destroy(frag_root);
}
else if (rb_obj_class(new) == cNokolexborNode)
{
lxb_dom_node_t *node_new = nl_rb_node_unwrap(new);
lxb_dom_node_remove(node_new);
insert_after ? lxb_dom_node_insert_after(node, node_new) : lxb_dom_node_insert_before(node, node_new);
}
else
{
rb_raise(rb_eArgError, "Unsupported node type");
}
return Qnil;
}
|
#ancestors(selector = nil) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/nokolexbor/node.rb', line 48 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) ⇒ Object
179 180 181 |
# File 'lib/nokolexbor/node.rb', line 179 def append_class(names) kwattr_append("class", names) end |
#at(*args) ⇒ Object Also known as: %
159 160 161 162 163 164 165 166 167 |
# File 'lib/nokolexbor/node.rb', line 159 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) ⇒ Object
133 134 135 |
# File 'lib/nokolexbor/node.rb', line 133 def at_css(*args) at_css_impl(args.join(', ')) end |
#at_css_impl(selector) ⇒ Object
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 |
# File 'ext/nokolexbor/nl_node.c', line 308
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();
nl_node_find(self, selector, nl_node_at_css_callback, array);
if (array->length == 0)
{
return Qnil;
}
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) ⇒ Object
143 144 145 |
# File 'lib/nokolexbor/node.rb', line 143 def at_xpath(*args) xpath(*args).first end |
#attribute(name) ⇒ Object
94 95 96 97 |
# File 'lib/nokolexbor/node.rb', line 94 def attribute(name) return nil unless key?(name) Attribute.new(name, attr(name)) end |
#attributes ⇒ Object
99 100 101 |
# File 'lib/nokolexbor/node.rb', line 99 def attributes attrs.map { |k, v| [k, Attribute.new(k, v)] }.to_h end |
#attrs ⇒ Object
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 |
# File 'ext/nokolexbor/nl_node.c', line 469
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;
}
|
#cdata? ⇒ Boolean
28 29 30 |
# File 'lib/nokolexbor/node.rb', line 28 def cdata? type == CDATA_SECTION_NODE end |
#child ⇒ Object
566 567 568 569 570 571 572 |
# File 'ext/nokolexbor/nl_node.c', line 566
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;
}
|
#children ⇒ Object
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 |
# File 'ext/nokolexbor/nl_node.c', line 550
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=
112 113 114 115 116 117 118 119 |
# File 'lib/nokolexbor/node.rb', line 112 def children=(node) children.remove if node.is_a?(NodeSet) node.each { |n| add_child(n) } else add_child(node) end end |
#classes ⇒ Object
171 172 173 |
# File 'lib/nokolexbor/node.rb', line 171 def classes kwattr_values("class") end |
#clone ⇒ Object Also known as: dup
796 797 798 799 800 801 802 |
# File 'ext/nokolexbor/nl_node.c', line 796
static VALUE
nl_node_clone(VALUE self)
{
lxb_dom_node_t *node = nl_rb_node_unwrap(self);
lxb_dom_node_t *clone = lxb_dom_node_clone(node, 1);
return nl_rb_node_create(clone, nl_rb_document_get(self));
}
|
#comment? ⇒ Boolean
24 25 26 |
# File 'lib/nokolexbor/node.rb', line 24 def comment? type == COMMENT_NODE end |
#content ⇒ Object Also known as: text, inner_text, to_str
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'ext/nokolexbor/nl_node.c', line 88
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;
}
|
#css(*args) ⇒ Object
129 130 131 |
# File 'lib/nokolexbor/node.rb', line 129 def css(*args) css_impl(args.join(', ')) end |
#css_impl(selector) ⇒ Object
330 331 332 333 334 335 336 337 338 339 340 341 |
# File 'ext/nokolexbor/nl_node.c', line 330
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();
nl_node_find(self, selector, nl_node_css_callback, array);
sort_nodes_if_necessary(selector, node->owner_document, array);
return nl_rb_node_set_create_with_data(array, nl_rb_document_get(self));
}
|
#destroy ⇒ Object
582 583 584 585 586 587 588 |
# File 'ext/nokolexbor/nl_node.c', line 582
static VALUE
nl_node_destroy(VALUE self)
{
lxb_dom_node_t *node = nl_rb_node_unwrap(self);
lxb_dom_node_destroy(node);
return Qnil;
}
|
#each ⇒ Object
121 122 123 124 125 |
# File 'lib/nokolexbor/node.rb', line 121 def each attributes.each do |name, node| yield [name, node.value] end end |
#element? ⇒ Boolean
44 45 46 |
# File 'lib/nokolexbor/node.rb', line 44 def element? type == ELEMENT_NODE end |
#first_element_child ⇒ Object
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 |
# File 'ext/nokolexbor/nl_node.c', line 734
static VALUE
nl_node_first_element_child(VALUE self)
{
lxb_dom_node_t *parent = nl_rb_node_unwrap(self);
lxb_dom_node_t *cur;
if (parent == NULL)
{
return Qnil;
}
switch (parent->type)
{
case LXB_DOM_NODE_TYPE_ELEMENT:
case LXB_DOM_NODE_TYPE_ENTITY:
case LXB_DOM_NODE_TYPE_DOCUMENT:
cur = parent->first_child;
break;
default:
return Qnil;
}
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(html) ⇒ Object
641 642 643 644 645 646 647 648 649 650 |
# File 'ext/nokolexbor/nl_node.c', line 641
static VALUE
nl_node_fragment(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_char_t *)RSTRING_PTR(html), RSTRING_LEN(html));
return nl_rb_node_create(frag_root, nl_rb_document_get(self));
}
|
#fragment? ⇒ Boolean
40 41 42 |
# File 'lib/nokolexbor/node.rb', line 40 def fragment? type == DOCUMENT_FRAG_NODE end |
#inner_html ⇒ Object
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 |
# File 'ext/nokolexbor/nl_node.c', line 343
static VALUE
nl_node_inner_html(VALUE self)
{
lxb_dom_node_t *node = nl_rb_node_unwrap(self);
lexbor_str_t str = {0};
lxb_status_t status = lxb_html_serialize_deep_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;
}
|
#key?(rb_attr) ⇒ Boolean
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 |
# File 'ext/nokolexbor/nl_node.c', line 393
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;
}
|
#keys ⇒ Object
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 |
# File 'ext/nokolexbor/nl_node.c', line 412
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;
}
|
#keywordify(keywords) ⇒ Object
224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/nokolexbor/node.rb', line 224 def keywordify(keywords) case keywords when Enumerable keywords when String keywords.scan(/\S+/) else raise ArgumentError, "Keyword attributes must be passed as either a String or an Enumerable, but received #{keywords.class}" end end |
#kwattr_add(attribute_name, keywords) ⇒ Object
191 192 193 194 195 196 197 |
# File 'lib/nokolexbor/node.rb', line 191 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) ⇒ Object
199 200 201 202 203 204 205 |
# File 'lib/nokolexbor/node.rb', line 199 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) ⇒ Object
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/nokolexbor/node.rb', line 207 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) ⇒ Object
187 188 189 |
# File 'lib/nokolexbor/node.rb', line 187 def kwattr_values(attribute_name) keywordify(attr(attribute_name) || []) end |
#last_element_child ⇒ Object
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 |
# File 'ext/nokolexbor/nl_node.c', line 765
static VALUE
nl_node_last_element_child(VALUE self)
{
lxb_dom_node_t *parent = nl_rb_node_unwrap(self);
lxb_dom_node_t *cur;
if (parent == NULL)
{
return Qnil;
}
switch (parent->type)
{
case LXB_DOM_NODE_TYPE_ELEMENT:
case LXB_DOM_NODE_TYPE_ENTITY:
case LXB_DOM_NODE_TYPE_DOCUMENT:
cur = parent->last_child;
break;
default:
return Qnil;
}
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
90 91 92 |
# File 'lib/nokolexbor/node.rb', line 90 def matches?(selector) ancestors.last.css(selector).any? { |node| node == self } end |
#name ⇒ Object
609 610 611 612 613 614 615 616 |
# File 'ext/nokolexbor/nl_node.c', line 609
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);
}
|
#next ⇒ Object
528 529 530 531 532 533 |
# File 'ext/nokolexbor/nl_node.c', line 528
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_element ⇒ Object
535 536 537 538 539 540 541 542 543 544 545 546 547 548 |
# File 'ext/nokolexbor/nl_node.c', line 535
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_type ⇒ Object Also known as: type
728 729 730 731 732 |
# File 'ext/nokolexbor/nl_node.c', line 728
static VALUE
nl_node_get_type(VALUE self)
{
return INT2NUM(nl_rb_node_unwrap(self)->type);
}
|
#outer_html ⇒ Object Also known as: to_html, to_s
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 |
# File 'ext/nokolexbor/nl_node.c', line 368
static VALUE
nl_node_outer_html(VALUE self)
{
lxb_dom_node_t *node = nl_rb_node_unwrap(self);
lexbor_str_t str = {0};
lxb_status_t 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;
}
|
#parent ⇒ Object
499 500 501 502 503 504 |
# File 'ext/nokolexbor/nl_node.c', line 499
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;
}
|
#previous ⇒ Object
506 507 508 509 510 511 |
# File 'ext/nokolexbor/nl_node.c', line 506
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_element ⇒ Object
513 514 515 516 517 518 519 520 521 522 523 524 525 526 |
# File 'ext/nokolexbor/nl_node.c', line 513
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
32 33 34 |
# File 'lib/nokolexbor/node.rb', line 32 def processing_instruction? type == PI_NODE end |
#remove ⇒ Object Also known as: unlink
574 575 576 577 578 579 580 |
# File 'ext/nokolexbor/nl_node.c', line 574
static VALUE
nl_node_remove(VALUE self)
{
lxb_dom_node_t *node = nl_rb_node_unwrap(self);
lxb_dom_node_remove(node);
return Qnil;
}
|
#remove_attr(rb_attr) ⇒ Object Also known as: delete
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'ext/nokolexbor/nl_node.c', line 157
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 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);
return lxb_dom_element_remove_attribute(element, (const lxb_char_t *)attr_c, attr_len) == LXB_STATUS_OK ? Qtrue : Qfalse;
}
|
#remove_class(names = nil) ⇒ Object
183 184 185 |
# File 'lib/nokolexbor/node.rb', line 183 def remove_class(names = nil) kwattr_remove("class", names) end |
#replace(node) ⇒ Object
103 104 105 106 107 108 109 110 |
# File 'lib/nokolexbor/node.rb', line 103 def replace(node) if node.is_a?(NodeSet) node.each { |n| add_sibling(:previous, n) } else add_sibling(:previous, node) end remove end |
#search(*args) ⇒ Object Also known as: /
147 148 149 150 151 152 153 154 155 |
# File 'lib/nokolexbor/node.rb', line 147 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 |
#values ⇒ Object
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 |
# File 'ext/nokolexbor/nl_node.c', line 437
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) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/nokolexbor/node.rb', line 70 def wrap(node) case node when String new_parent = fragment(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 |
#xpath(*args) ⇒ Object
137 138 139 140 141 |
# File 'lib/nokolexbor/node.rb', line 137 def xpath(*args) paths, handler, ns, binds = extract_params(args) xpath_internal(self, paths, handler, ns, binds) end |