Class: Nokolexbor::Node
- Inherits:
-
Object
- Object
- Nokolexbor::Node
- Includes:
- Enumerable
- Defined in:
- lib/nokolexbor/node.rb,
ext/nokolexbor/nl_node.c
Direct Known Subclasses
CharacterData, Document, DocumentFragment, Element, NodeSet, ProcessingInstruction
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
- #<<(node_or_tags) ⇒ Object
- #==(other) ⇒ Object
- #[](rb_attr) ⇒ Object (also: #attr, #get_attribute)
- #[]=(rb_attr, rb_value) ⇒ Object (also: #set_attr, #set_attribute)
- #add_child(new) ⇒ Object
- #add_class(names) ⇒ Object
- #add_next_sibling(node_or_tags) ⇒ Object (also: #next=)
- #add_previous_sibling(node_or_tags) ⇒ Object (also: #previous=)
- #add_sibling(next_or_previous, new) ⇒ Object
- #after(node_or_tags) ⇒ 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
- #before(node_or_tags) ⇒ 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)
- #content=(content) ⇒ Object
- #css(*args) ⇒ Object
- #css_impl(selector) ⇒ Object
- #destroy ⇒ Object
- #document? ⇒ Boolean
- #each ⇒ Object
- #element? ⇒ Boolean
- #first_element_child ⇒ Object
- #fragment(tags) ⇒ Object
- #fragment? ⇒ Boolean
- #inner_html(*args) ⇒ Object
- #key?(rb_attr) ⇒ Boolean (also: #has_attribute?)
- #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 (also: #next_sibling)
- #next_element ⇒ Object
- #node_type ⇒ Object (also: #type)
- #nokogiri_at_css(*args) ⇒ Object
- #nokogiri_css(*args) ⇒ Object
- #outer_html(*args) ⇒ Object (also: #to_html, #serialize, #to_s)
- #parent ⇒ Object
- #parent=(parent_node) ⇒ Object
- #parse(html) ⇒ Object
- #prepend_child(node) ⇒ Object
- #previous ⇒ Object (also: #previous_sibling)
- #previous_element ⇒ Object
- #processing_instruction? ⇒ Boolean
- #remove ⇒ Object (also: #unlink)
- #remove_attr(rb_attr) ⇒ Object (also: #delete, #remove_attribute)
- #remove_class(names = nil) ⇒ Object
- #replace(node) ⇒ Object
- #search(*args) ⇒ Object (also: #/)
- #text? ⇒ Boolean
- #traverse {|_self| ... } ⇒ Object
- #values ⇒ Object
- #wrap(node) ⇒ Object
- #write_to(io, *options) ⇒ Object (also: #write_html_to)
- #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
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'ext/nokolexbor/nl_node.c', line 82
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);
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) ⇒ Object
123 124 125 126 |
# File 'lib/nokolexbor/node.rb', line 123 def <<() add_child() self end |
#==(other) ⇒ Object
636 637 638 639 640 641 642 |
# File 'ext/nokolexbor/nl_node.c', line 636
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, get_attribute
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'ext/nokolexbor/nl_node.c', line 144
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, set_attribute
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'ext/nokolexbor/nl_node.c', line 169
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
744 745 746 747 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 |
# File 'ext/nokolexbor/nl_node.c', line 744
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, NULL, (lxb_char_t *)RSTRING_PTR(new), RSTRING_LEN(new));
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);
lxb_dom_node_insert_child(node, 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));
} else if (rb_obj_is_kind_of(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);
return new;
} else {
rb_raise(rb_eArgError, "Unsupported node type");
}
return Qnil;
}
|
#add_class(names) ⇒ Object
247 248 249 |
# File 'lib/nokolexbor/node.rb', line 247 def add_class(names) kwattr_add("class", names) end |
#add_next_sibling(node_or_tags) ⇒ Object Also known as: next=
101 102 103 104 105 106 |
# File 'lib/nokolexbor/node.rb', line 101 def add_next_sibling() raise ArgumentError, "A document may not have multiple root nodes." if parent&.document? && !(.comment? || .processing_instruction?) add_sibling(:next, ) end |
#add_previous_sibling(node_or_tags) ⇒ Object Also known as: previous=
94 95 96 97 98 99 |
# File 'lib/nokolexbor/node.rb', line 94 def add_previous_sibling() raise ArgumentError, "A document may not have multiple root nodes." if parent&.document? && !(.comment? || .processing_instruction?) add_sibling(:previous, ) end |
#add_sibling(next_or_previous, new) ⇒ Object
704 705 706 707 708 709 710 711 712 713 714 715 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 742 |
# File 'ext/nokolexbor/nl_node.c', line 704
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, NULL, (lxb_char_t *)RSTRING_PTR(new), RSTRING_LEN(new));
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);
insert_after ? lxb_dom_node_insert_after(node, child) : lxb_dom_node_insert_before(node, 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));
} else if (rb_obj_is_kind_of(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);
return new;
} else {
rb_raise(rb_eArgError, "Unsupported node type");
}
return Qnil;
}
|
#after(node_or_tags) ⇒ Object
113 114 115 116 |
# File 'lib/nokolexbor/node.rb', line 113 def after() add_next_sibling() self end |
#ancestors(selector = nil) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/nokolexbor/node.rb', line 52 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
251 252 253 |
# File 'lib/nokolexbor/node.rb', line 251 def append_class(names) kwattr_append("class", names) end |
#at(*args) ⇒ Object Also known as: %
231 232 233 234 235 236 237 238 239 |
# File 'lib/nokolexbor/node.rb', line 231 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
195 196 197 |
# File 'lib/nokolexbor/node.rb', line 195 def at_css(*args) at_css_impl(args.join(', ')) end |
#at_css_impl(selector) ⇒ Object
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 |
# File 'ext/nokolexbor/nl_node.c', line 336
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) ⇒ Object
215 216 217 |
# File 'lib/nokolexbor/node.rb', line 215 def at_xpath(*args) xpath(*args).first end |
#attribute(name) ⇒ Object
148 149 150 151 |
# File 'lib/nokolexbor/node.rb', line 148 def attribute(name) return nil unless key?(name) Attribute.new(name, attr(name)) end |
#attributes ⇒ Object
153 154 155 |
# File 'lib/nokolexbor/node.rb', line 153 def attributes attrs.map { |k, v| [k, Attribute.new(k, v)] }.to_h end |
#attrs ⇒ Object
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 |
# File 'ext/nokolexbor/nl_node.c', line 522
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) ⇒ Object
108 109 110 111 |
# File 'lib/nokolexbor/node.rb', line 108 def before() add_previous_sibling() self end |
#cdata? ⇒ Boolean
28 29 30 |
# File 'lib/nokolexbor/node.rb', line 28 def cdata? type == CDATA_SECTION_NODE end |
#child ⇒ Object
612 613 614 615 616 617 618 |
# File 'ext/nokolexbor/nl_node.c', line 612
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
597 598 599 600 601 602 603 604 605 606 607 608 609 610 |
# File 'ext/nokolexbor/nl_node.c', line 597
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=
166 167 168 169 170 171 172 173 |
# File 'lib/nokolexbor/node.rb', line 166 def children=(node) children.remove if node.is_a?(NodeSet) node.each { |n| add_child(n) } else add_child(node) end end |
#classes ⇒ Object
243 244 245 |
# File 'lib/nokolexbor/node.rb', line 243 def classes kwattr_values("class") end |
#clone ⇒ Object Also known as: dup
835 836 837 838 839 840 841 |
# File 'ext/nokolexbor/nl_node.c', line 835
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
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'ext/nokolexbor/nl_node.c', line 114
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) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'ext/nokolexbor/nl_node.c', line 130
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);
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) ⇒ Object
191 192 193 |
# File 'lib/nokolexbor/node.rb', line 191 def css(*args) css_impl(args.join(', ')) end |
#css_impl(selector) ⇒ Object
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 |
# File 'ext/nokolexbor/nl_node.c', line 363
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));
}
|
#destroy ⇒ Object
628 629 630 631 632 633 634 |
# File 'ext/nokolexbor/nl_node.c', line 628
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
48 49 50 |
# File 'lib/nokolexbor/node.rb', line 48 def document? is_a?(Nokolexbor::Document) end |
#each ⇒ Object
179 180 181 182 183 |
# File 'lib/nokolexbor/node.rb', line 179 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
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 |
# File 'ext/nokolexbor/nl_node.c', line 781
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(tags) ⇒ Object
185 186 187 |
# File 'lib/nokolexbor/node.rb', line 185 def fragment() Nokolexbor::DocumentFragment.new(document, , self) end |
#fragment? ⇒ Boolean
40 41 42 |
# File 'lib/nokolexbor/node.rb', line 40 def fragment? type == DOCUMENT_FRAG_NODE end |
#inner_html(*args) ⇒ Object
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 |
# File 'ext/nokolexbor/nl_node.c', line 380
static VALUE
nl_node_inner_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_deep_str(node, 0, 0, &str);
} else {
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 Also known as: has_attribute?
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 |
# File 'ext/nokolexbor/nl_node.c', line 454
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
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 |
# File 'ext/nokolexbor/nl_node.c', line 472
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
296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/nokolexbor/node.rb', line 296 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
263 264 265 266 267 268 269 |
# File 'lib/nokolexbor/node.rb', line 263 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
271 272 273 274 275 276 277 |
# File 'lib/nokolexbor/node.rb', line 271 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
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/nokolexbor/node.rb', line 279 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
259 260 261 |
# File 'lib/nokolexbor/node.rb', line 259 def kwattr_values(attribute_name) keywordify(attr(attribute_name) || []) end |
#last_element_child ⇒ Object
808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 |
# File 'ext/nokolexbor/nl_node.c', line 808
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
144 145 146 |
# File 'lib/nokolexbor/node.rb', line 144 def matches?(selector) ancestors.last.css(selector).any? { |node| node == self } end |
#name ⇒ Object
654 655 656 657 658 659 660 661 |
# File 'ext/nokolexbor/nl_node.c', line 654
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 Also known as: next_sibling
577 578 579 580 581 582 |
# File 'ext/nokolexbor/nl_node.c', line 577
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
584 585 586 587 588 589 590 591 592 593 594 595 |
# File 'ext/nokolexbor/nl_node.c', line 584
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
775 776 777 778 779 |
# File 'ext/nokolexbor/nl_node.c', line 775
static VALUE
nl_node_get_type(VALUE self)
{
return INT2NUM(nl_rb_node_unwrap(self)->type);
}
|
#nokogiri_at_css(*args) ⇒ Object
205 206 207 |
# File 'lib/nokolexbor/node.rb', line 205 def nokogiri_at_css(*args) nokogiri_css(*args).first end |
#nokogiri_css(*args) ⇒ Object
199 200 201 202 203 |
# File 'lib/nokolexbor/node.rb', line 199 def nokogiri_css(*args) rules, handler, ns, _ = extract_params(args) nokogiri_css_internal(self, rules, handler, ns) end |
#outer_html(*args) ⇒ Object Also known as: to_html, serialize, to_s
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 |
# File 'ext/nokolexbor/nl_node.c', line 417
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;
}
|
#parent ⇒ Object
550 551 552 553 554 555 |
# File 'ext/nokolexbor/nl_node.c', line 550
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
175 176 177 |
# File 'lib/nokolexbor/node.rb', line 175 def parent=(parent_node) parent_node.add_child(self) end |
#parse(html) ⇒ Object
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 |
# File 'ext/nokolexbor/nl_node.c', line 685
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));
}
|
#prepend_child(node) ⇒ Object
128 129 130 131 132 133 134 135 136 137 |
# File 'lib/nokolexbor/node.rb', line 128 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 |
#previous ⇒ Object Also known as: previous_sibling
557 558 559 560 561 562 |
# File 'ext/nokolexbor/nl_node.c', line 557
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
564 565 566 567 568 569 570 571 572 573 574 575 |
# File 'ext/nokolexbor/nl_node.c', line 564
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
620 621 622 623 624 625 626 |
# File 'ext/nokolexbor/nl_node.c', line 620
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, remove_attribute
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'ext/nokolexbor/nl_node.c', line 193
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
255 256 257 |
# File 'lib/nokolexbor/node.rb', line 255 def remove_class(names = nil) kwattr_remove("class", names) end |
#replace(node) ⇒ Object
157 158 159 160 161 162 163 164 |
# File 'lib/nokolexbor/node.rb', line 157 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: /
219 220 221 222 223 224 225 226 227 |
# File 'lib/nokolexbor/node.rb', line 219 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 |
#text? ⇒ Boolean
36 37 38 |
# File 'lib/nokolexbor/node.rb', line 36 def text? type == TEXT_NODE end |
#traverse {|_self| ... } ⇒ Object
139 140 141 142 |
# File 'lib/nokolexbor/node.rb', line 139 def traverse(&block) children.each { |j| j.traverse(&block) } yield(self) end |
#values ⇒ Object
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 |
# File 'ext/nokolexbor/nl_node.c', line 495
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
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/nokolexbor/node.rb', line 74 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 |
#write_to(io, *options) ⇒ Object Also known as: write_html_to
308 309 310 |
# File 'lib/nokolexbor/node.rb', line 308 def write_to(io, *) io.write(to_html(*)) end |
#xpath(*args) ⇒ Object
209 210 211 212 213 |
# File 'lib/nokolexbor/node.rb', line 209 def xpath(*args) paths, handler, ns, binds = extract_params(args) xpath_internal(self, paths, handler, ns, binds) end |