Class: Makiri::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/makiri/node.rb,
ext/makiri/makiri.c

Overview

Base class for every DOM node (element, attribute, text, comment, …). The bulk of the API lives in the C extension; this file defines the Ruby-only conveniences.

Instance Method Summary collapse

Instance Method Details

#<<(rb_child) ⇒ Object

node << child -> node (chainable).



95
96
97
98
99
100
# File 'ext/makiri/glue/ruby_mutate.c', line 95

static VALUE
mkr_node_append(VALUE self, VALUE rb_child)
{
    mkr_node_add_child(self, rb_child);
    return self;
}

#==(other) ⇒ Object

Pointer identity: equal iff both wrap the same lxb_dom_node_t.



543
544
545
546
547
548
549
550
# File 'ext/makiri/glue/ruby_node.c', line 543

static VALUE
mkr_node_equals(VALUE self, VALUE other)
{
    if (!rb_obj_is_kind_of(other, mkr_cNode)) {
        return Qfalse;
    }
    return mkr_node_unwrap(self) == mkr_node_unwrap(other) ? Qtrue : Qfalse;
}

#[](rb_name) ⇒ Object Also known as: attr, get_attribute

node -> String or nil (nil when not an element or absent).



407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# File 'ext/makiri/glue/ruby_node.c', line 407

static VALUE
mkr_node_aref(VALUE self, VALUE rb_name)
{
    lxb_dom_node_t *node = mkr_node_unwrap(self);
    if (node->type != LXB_DOM_NODE_TYPE_ELEMENT) {
        return Qnil;
    }

    mkr_ruby_borrowed_text_t nv = mkr_ruby_verified_text(rb_name, "attribute name");
    const lxb_char_t *nm = (const lxb_char_t *)nv.ptr;
    size_t nlen = nv.len;

    lxb_dom_element_t *el = lxb_dom_interface_element(node);
    if (!lxb_dom_element_has_attribute(el, nm, nlen)) {
        return Qnil;
    }

    size_t vlen = 0;
    const lxb_char_t *val = lxb_dom_element_get_attribute(el, nm, nlen, &vlen);
    RB_GC_GUARD(nv.value);
    return mkr_ruby_str_from_borrowed(mkr_borrowed_text((const char *)val, vlen));
}

#[]=(rb_name, rb_value) ⇒ Object

element = value -> value



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'ext/makiri/glue/ruby_mutate.c', line 191

static VALUE
mkr_node_aset(VALUE self, VALUE rb_name, VALUE rb_value)
{
    lxb_dom_node_t *node = mkr_node_unwrap(self);
    if (node->type != LXB_DOM_NODE_TYPE_ELEMENT) {
        rb_raise(mkr_eError, "cannot set an attribute on a non-element node");
    }
    mkr_ruby_borrowed_text_t nv = mkr_ruby_verified_text(rb_name, "attribute name");
    mkr_ruby_borrowed_text_t vv = mkr_ruby_verified_text(rb_value, "attribute value");
    lxb_dom_attr_t *attr = lxb_dom_element_set_attribute(
        lxb_dom_interface_element(node),
        (const lxb_char_t *)nv.ptr, nv.len,
        (const lxb_char_t *)vv.ptr, vv.len);
    RB_GC_GUARD(nv.value);
    RB_GC_GUARD(vv.value);
    if (attr == NULL) {
        rb_raise(mkr_eError, "failed to set attribute");
    }
    mkr_invalidate_index(self);
    return rb_value;
}

#add_child(rb_child) ⇒ Object

node.add_child(child) -> child. Appends child as the last child. A document fragment contributes its children rather than itself.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'ext/makiri/glue/ruby_mutate.c', line 75

static VALUE
mkr_node_add_child(VALUE self, VALUE rb_child)
{
    lxb_dom_node_t *parent = mkr_node_unwrap(self);
    lxb_dom_node_t *child  = mkr_arg_node(rb_child);
    mkr_prepare_insert(parent, child);
    if (mkr_is_fragment(child)) {
        lxb_dom_node_t *c;
        while ((c = child->first_child) != NULL) {
            lxb_dom_node_remove(c);
            lxb_dom_node_insert_child(parent, c);
        }
    } else {
        lxb_dom_node_insert_child(parent, child);
    }
    mkr_invalidate_index(self);
    return rb_child;
}

#add_class(names) ⇒ self

Add each class in names (space-separated) that is not already present.

Returns:

  • (self)


83
84
85
86
87
88
# File 'lib/makiri/node.rb', line 83

def add_class(names)
  have = classes
  have.concat(names.to_s.split(/\s+/).reject { |c| c.empty? || have.include?(c) })
  self["class"] = have.join(" ")
  self
end

#add_next_sibling(rb_node) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'ext/makiri/glue/ruby_mutate.c', line 124

static VALUE
mkr_node_add_next_sibling(VALUE self, VALUE rb_node)
{
    lxb_dom_node_t *ref  = mkr_node_unwrap(self);
    lxb_dom_node_t *node = mkr_arg_node(rb_node);
    if (ref->parent == NULL) {
        rb_raise(mkr_eError, "cannot add a sibling to a node with no parent");
    }
    mkr_prepare_insert(ref, node);
    if (mkr_is_fragment(node)) {
        lxb_dom_node_t *anchor = ref, *c;
        while ((c = node->first_child) != NULL) {
            lxb_dom_node_remove(c);
            lxb_dom_node_insert_after(anchor, c);
            anchor = c; /* keep document order after ref */
        }
    } else {
        lxb_dom_node_insert_after(ref, node);
    }
    mkr_invalidate_index(self);
    return rb_node;
}

#add_previous_sibling(rb_node) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'ext/makiri/glue/ruby_mutate.c', line 102

static VALUE
mkr_node_add_previous_sibling(VALUE self, VALUE rb_node)
{
    lxb_dom_node_t *ref  = mkr_node_unwrap(self);
    lxb_dom_node_t *node = mkr_arg_node(rb_node);
    if (ref->parent == NULL) {
        rb_raise(mkr_eError, "cannot add a sibling to a node with no parent");
    }
    mkr_prepare_insert(ref, node);
    if (mkr_is_fragment(node)) {
        lxb_dom_node_t *c;
        while ((c = node->first_child) != NULL) {
            lxb_dom_node_remove(c);
            lxb_dom_node_insert_before(ref, c);
        }
    } else {
        lxb_dom_node_insert_before(ref, node);
    }
    mkr_invalidate_index(self);
    return rb_node;
}

#after(rb_node) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'ext/makiri/glue/ruby_mutate.c', line 124

static VALUE
mkr_node_add_next_sibling(VALUE self, VALUE rb_node)
{
    lxb_dom_node_t *ref  = mkr_node_unwrap(self);
    lxb_dom_node_t *node = mkr_arg_node(rb_node);
    if (ref->parent == NULL) {
        rb_raise(mkr_eError, "cannot add a sibling to a node with no parent");
    }
    mkr_prepare_insert(ref, node);
    if (mkr_is_fragment(node)) {
        lxb_dom_node_t *anchor = ref, *c;
        while ((c = node->first_child) != NULL) {
            lxb_dom_node_remove(c);
            lxb_dom_node_insert_after(anchor, c);
            anchor = c; /* keep document order after ref */
        }
    } else {
        lxb_dom_node_insert_after(ref, node);
    }
    mkr_invalidate_index(self);
    return rb_node;
}

#ancestorsObject

Ancestor elements, nearest first (parent, grandparent, … root).



368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'ext/makiri/glue/ruby_node.c', line 368

static VALUE
mkr_node_ancestors(VALUE self)
{
    lxb_dom_node_t *node = mkr_node_unwrap(self);
    VALUE document = mkr_node_document(self);
    VALUE set = mkr_node_set_new(document);
    for (lxb_dom_node_t *p = node->parent; p != NULL; p = p->parent) {
        if (p->type == LXB_DOM_NODE_TYPE_ELEMENT) {
            mkr_node_set_push(set, p);
        }
    }
    return set;
}

#append_class(names) ⇒ self

Append each class in names unconditionally (duplicates allowed).

Returns:

  • (self)


92
93
94
95
# File 'lib/makiri/node.rb', line 92

def append_class(names)
  self["class"] = (classes + names.to_s.split(/\s+/).reject(&:empty?)).join(" ")
  self
end

#at(path) ⇒ Object

First result of #search: the first node for a node-set, else the value.



146
147
148
149
# File 'lib/makiri/node.rb', line 146

def at(path)
  result = search(path)
  result.is_a?(NodeSet) ? result.first : result
end

#at_css(rb_selector) ⇒ Object



172
173
174
175
176
177
# File 'ext/makiri/glue/ruby_css.c', line 172

static VALUE
mkr_node_at_css(VALUE self, VALUE rb_selector)
{
    VALUE set = mkr_css_query(self, rb_selector, 1);
    return rb_funcall(set, rb_intern("first"), 0);
}

#at_xpath(*args) ⇒ Object

First matching node (for a node-set), or the scalar value otherwise.



690
691
692
693
694
695
696
697
698
699
700
# File 'ext/makiri/glue/ruby_xpath.c', line 690

static VALUE
mkr_node_at_xpath(int argc, VALUE *argv, VALUE self)
{
    VALUE rb_expr, handler, opts;
    rb_scan_args(argc, argv, "11:", &rb_expr, &handler, &opts);
    VALUE result = mkr_node_xpath_run(self, rb_expr, handler, mkr_ns_matching_lax(opts), 1);
    if (rb_obj_is_kind_of(result, mkr_cNodeSet)) {
        return rb_funcall(result, rb_intern("first"), 0);
    }
    return result;
}

#attribute(name) ⇒ Makiri::Attribute?

The Attribute node named name, or nil (cf. #[], which returns the value).

Returns:



70
71
72
# File 'lib/makiri/node.rb', line 70

def attribute(name)
  attributes[name.to_s]
end

#attribute?Boolean

Returns:

  • (Boolean)


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

def attribute?
  is_a?(Attribute)
end

#attribute_nodesObject

element.attribute_nodes -> NodeSet of Attribute nodes (document order). Empty for non-elements. These wrap the bare lxb_dom_attr_t; navigating back with Attribute#parent goes through the compat attr->owner index.



490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
# File 'ext/makiri/glue/ruby_node.c', line 490

static VALUE
mkr_node_attribute_nodes(VALUE self)
{
    lxb_dom_node_t *node = mkr_node_unwrap(self);
    VALUE document = mkr_node_document(self);
    VALUE set = mkr_node_set_new(document);
    if (node->type != LXB_DOM_NODE_TYPE_ELEMENT) {
        return set;
    }
    lxb_dom_attr_t *attr =
        lxb_dom_element_first_attribute(lxb_dom_interface_element(node));
    while (attr != NULL) {
        mkr_node_set_push(set, lxb_dom_interface_node(attr));
        attr = lxb_dom_element_next_attribute(attr);
    }
    return set;
}

#attributesHash{String => Makiri::Attribute}

Attributes as a name => Attribute Hash (empty for non-elements).

Returns:



127
128
129
# File 'lib/makiri/node.rb', line 127

def attributes
  attribute_nodes.each_with_object({}) { |attr, h| h[attr.name] = attr }
end

#before(rb_node) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'ext/makiri/glue/ruby_mutate.c', line 102

static VALUE
mkr_node_add_previous_sibling(VALUE self, VALUE rb_node)
{
    lxb_dom_node_t *ref  = mkr_node_unwrap(self);
    lxb_dom_node_t *node = mkr_arg_node(rb_node);
    if (ref->parent == NULL) {
        rb_raise(mkr_eError, "cannot add a sibling to a node with no parent");
    }
    mkr_prepare_insert(ref, node);
    if (mkr_is_fragment(node)) {
        lxb_dom_node_t *c;
        while ((c = node->first_child) != NULL) {
            lxb_dom_node_remove(c);
            lxb_dom_node_insert_before(ref, c);
        }
    } else {
        lxb_dom_node_insert_before(ref, node);
    }
    mkr_invalidate_index(self);
    return rb_node;
}

#blank?Boolean

Returns true for a blank/whitespace-only text or CDATA node.

Returns:

  • (Boolean)

    true for a blank/whitespace-only text or CDATA node.



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

def blank?
  (text? || is_a?(CData)) && content.strip.empty?
end

#childObject

First child node (any type), or nil.



332
333
334
335
336
337
# File 'ext/makiri/glue/ruby_node.c', line 332

static VALUE
mkr_node_child(VALUE self)
{
    lxb_dom_node_t *node = mkr_node_unwrap(self);
    return mkr_wrap_node(node->first_child, mkr_node_document(self));
}

#childrenObject

All child nodes as a NodeSet.



340
341
342
343
344
345
346
347
348
349
350
# File 'ext/makiri/glue/ruby_node.c', line 340

static VALUE
mkr_node_children(VALUE self)
{
    lxb_dom_node_t *node = mkr_node_unwrap(self);
    VALUE document = mkr_node_document(self);
    VALUE set = mkr_node_set_new(document);
    for (lxb_dom_node_t *c = node->first_child; c != NULL; c = c->next) {
        mkr_node_set_push(set, c);
    }
    return set;
}

#classesArray<String>

Returns the element’s class names.

Returns:

  • (Array<String>)

    the element’s class names.



77
78
79
# File 'lib/makiri/node.rb', line 77

def classes
  self["class"].to_s.split(/\s+/).reject(&:empty?)
end

#comment?Boolean

Returns:

  • (Boolean)


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

def comment?
  is_a?(Comment)
end

#contentObject

Concatenated text content of this node and its descendants. The DOM spec makes a Document’s textContent null; we instead return the text of the root element (matching the intuitive, Nokogiri-like Document#text).



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'ext/makiri/glue/ruby_node.c', line 207

static VALUE
mkr_node_content(VALUE self)
{
    lxb_dom_node_t *node = mkr_node_unwrap(self);
    if (node->type == LXB_DOM_NODE_TYPE_DOCUMENT) {
        node = lxb_dom_document_root((lxb_dom_document_t *)node);
        if (node == NULL) {
            return rb_utf8_str_new("", 0);
        }
    }

    /* Fast path for elements / fragments (the common case, incl. document text).
     *
     * Preferred: the per-document text index (lexbor_compat/text_index.c) maps
     * this node to the contiguous, document-order run of its descendants' text
     * slices, so we serve a single pre-sized memcpy run with no per-extraction
     * tree walk — the walk is otherwise the dominant, cache-bound cost. Built
     * lazily on first use and dropped on any mutation, so a slice can never
     * point at reallocated/detached storage.
     *
     * Fallback (index unavailable — node outside the indexed tree, e.g. a
     * fragment, or a build OOM): stream each descendant text/CDATA node's data
     * straight into the Ruby string via an iterative pre-order walk (stack-safe;
     * skips Lexbor's intermediate arena buffer + copy). */
    if (node->type == LXB_DOM_NODE_TYPE_ELEMENT
        || node->type == LXB_DOM_NODE_TYPE_DOCUMENT_FRAGMENT) {
        mkr_parsed_t *parsed = mkr_doc_parsed(mkr_node_document(self));
        const mkr_borrowed_text_t *slices;
        size_t nslices, total;
        if (parsed != NULL
            && mkr_parsed_text_slices(parsed, node, &slices, &nslices, &total)) {
            return mkr_ruby_str_from_slices(slices, nslices, total);
        }

        VALUE str = rb_utf8_str_new(NULL, 0);
        for (lxb_dom_node_t *n = node->first_child; n != NULL;) {
            if (n->type == LXB_DOM_NODE_TYPE_TEXT
                || n->type == LXB_DOM_NODE_TYPE_CDATA_SECTION) {
                const lexbor_str_t *d = &lxb_dom_interface_character_data(n)->data;
                if (d->data != NULL && d->length != 0) {
                    rb_str_cat(str, (const char *)d->data, (long)d->length);
                }
            }
            if (n->first_child != NULL) { n = n->first_child; continue; }
            while (n != node && n->next == NULL) { n = n->parent; }
            if (n == node) { break; }
            n = n->next;
        }
        return str;
    }

    /* Character-data and other node kinds keep the general (proven) path. */
    size_t len = 0;
    lxb_char_t *text = lxb_dom_node_text_content(node, &len);
    if (text == NULL) {
        return rb_utf8_str_new("", 0);
    }
    VALUE str = rb_utf8_str_new((const char *)text, len);
    lxb_dom_document_destroy_text(node->owner_document, text);
    return str;
}

#content=(rb_text) ⇒ Object

node.content = text -> text. DOM textContent setter: for an element this replaces all children with a single text node; for a text/comment/cdata node it sets the data.



245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'ext/makiri/glue/ruby_mutate.c', line 245

static VALUE
mkr_node_set_content(VALUE self, VALUE rb_text)
{
    lxb_dom_node_t *node = mkr_node_unwrap(self);
    mkr_ruby_borrowed_text_t tv = mkr_ruby_verified_text(rb_text, "node content");
    lxb_status_t st = lxb_dom_node_text_content_set(
        node, (const lxb_char_t *)tv.ptr, tv.len);
    RB_GC_GUARD(tv.value);
    if (st != LXB_STATUS_OK) {
        rb_raise(mkr_eError, "failed to set node content");
    }
    mkr_invalidate_index(self);
    return rb_text;
}

#css(rb_selector) ⇒ Object



166
167
168
169
170
# File 'ext/makiri/glue/ruby_css.c', line 166

static VALUE
mkr_node_css(VALUE self, VALUE rb_selector)
{
    return mkr_css_query(self, rb_selector, 0);
}

#delete(rb_name) ⇒ Object Also known as: remove_attribute

element.delete(name) -> self. Removes the attribute if present.



261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'ext/makiri/glue/ruby_mutate.c', line 261

static VALUE
mkr_node_delete(VALUE self, VALUE rb_name)
{
    lxb_dom_node_t *node = mkr_node_unwrap(self);
    if (node->type != LXB_DOM_NODE_TYPE_ELEMENT) {
        return self;
    }
    mkr_ruby_borrowed_text_t nv = mkr_ruby_verified_text(rb_name, "attribute name");
    lxb_dom_element_remove_attribute(
        lxb_dom_interface_element(node), (const lxb_char_t *)nv.ptr, nv.len);
    RB_GC_GUARD(nv.value);
    mkr_invalidate_index(self);
    return self;
}

#documentObject




273
274
275
276
277
# File 'ext/makiri/glue/ruby_node.c', line 273

static VALUE
mkr_node_get_document(VALUE self)
{
    return mkr_node_document(self);
}

#document?Boolean

Returns:

  • (Boolean)


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

def document?
  is_a?(Document)
end

#document_fragment?Boolean Also known as: fragment?

Returns:

  • (Boolean)


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

def document_fragment?
  is_a?(DocumentFragment)
end

#element?Boolean Also known as: elem?

Returns:

  • (Boolean)


11
12
13
# File 'lib/makiri/node.rb', line 11

def element?
  is_a?(Element)
end

#element_childrenObject

Child elements only, as a NodeSet.



353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'ext/makiri/glue/ruby_node.c', line 353

static VALUE
mkr_node_element_children(VALUE self)
{
    lxb_dom_node_t *node = mkr_node_unwrap(self);
    VALUE document = mkr_node_document(self);
    VALUE set = mkr_node_set_new(document);
    for (lxb_dom_node_t *c = node->first_child; c != NULL; c = c->next) {
        if (c->type == LXB_DOM_NODE_TYPE_ELEMENT) {
            mkr_node_set_push(set, c);
        }
    }
    return set;
}

#elementsObject

Child elements only, as a NodeSet.



353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'ext/makiri/glue/ruby_node.c', line 353

static VALUE
mkr_node_element_children(VALUE self)
{
    lxb_dom_node_t *node = mkr_node_unwrap(self);
    VALUE document = mkr_node_document(self);
    VALUE set = mkr_node_set_new(document);
    for (lxb_dom_node_t *c = node->first_child; c != NULL; c = c->next) {
        if (c->type == LXB_DOM_NODE_TYPE_ELEMENT) {
            mkr_node_set_push(set, c);
        }
    }
    return set;
}

#eql?(other) ⇒ Boolean

Pointer identity: equal iff both wrap the same lxb_dom_node_t.

Returns:

  • (Boolean)


543
544
545
546
547
548
549
550
# File 'ext/makiri/glue/ruby_node.c', line 543

static VALUE
mkr_node_equals(VALUE self, VALUE other)
{
    if (!rb_obj_is_kind_of(other, mkr_cNode)) {
        return Qfalse;
    }
    return mkr_node_unwrap(self) == mkr_node_unwrap(other) ? Qtrue : Qfalse;
}

#first_element_childObject



382
383
384
385
386
387
388
389
390
# File 'ext/makiri/glue/ruby_node.c', line 382

static VALUE
mkr_node_first_element_child(VALUE self)
{
    lxb_dom_node_t *c = mkr_node_unwrap(self)->first_child;
    while (c != NULL && c->type != LXB_DOM_NODE_TYPE_ELEMENT) {
        c = c->next;
    }
    return mkr_wrap_node(c, mkr_node_document(self));
}

#hashObject

Stable hash derived from the node pointer, so a == b implies a.hash == b.hash even across separately-created wrappers.



554
555
556
557
558
559
# File 'ext/makiri/glue/ruby_node.c', line 554

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

#inner_html(*args) ⇒ Object

Inner HTML: the node’s children, without the node’s own tag.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'ext/makiri/glue/ruby_serialize.c', line 69

static VALUE
mkr_node_inner_html(int argc, VALUE *argv, VALUE self)
{
    int pretty = mkr_serialize_pretty_opt(argc, argv);
    lxb_dom_node_t *node = mkr_node_unwrap(self);
    VALUE str = rb_utf8_str_new(NULL, 0);
    lxb_status_t st = pretty
        ? lxb_html_serialize_pretty_deep_cb(node, LXB_HTML_SERIALIZE_OPT_UNDEF,
                                            0, mkr_serialize_cb, (void *)str)
        : lxb_html_serialize_deep_cb(node, mkr_serialize_cb, (void *)str);
    if (st != LXB_STATUS_OK) {
        rb_raise(mkr_eError, "HTML serialization failed");
    }
    return str;
}

#inner_html=(rb_html) ⇒ Object

element.inner_html = html -> html. Replaces the element’s children.



327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'ext/makiri/glue/ruby_mutate.c', line 327

static VALUE
mkr_node_set_inner_html(VALUE self, VALUE rb_html)
{
    lxb_dom_node_t *node = mkr_node_unwrap(self);
    if (node->type != LXB_DOM_NODE_TYPE_ELEMENT) {
        rb_raise(mkr_eError, "inner_html= requires an element");
    }

    /* Detach existing children (arena reclaims them at document destroy). */
    lxb_dom_node_t *c;
    while ((c = node->first_child) != NULL) {
        lxb_dom_node_remove(c);
    }

    mkr_parse_fragment_into(node, rb_html, node->owner_document,
                            mkr_emit_append, node);
    mkr_invalidate_index(self);
    return rb_html;
}

#inner_textObject

Concatenated text content of this node and its descendants. The DOM spec makes a Document’s textContent null; we instead return the text of the root element (matching the intuitive, Nokogiri-like Document#text).



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'ext/makiri/glue/ruby_node.c', line 207

static VALUE
mkr_node_content(VALUE self)
{
    lxb_dom_node_t *node = mkr_node_unwrap(self);
    if (node->type == LXB_DOM_NODE_TYPE_DOCUMENT) {
        node = lxb_dom_document_root((lxb_dom_document_t *)node);
        if (node == NULL) {
            return rb_utf8_str_new("", 0);
        }
    }

    /* Fast path for elements / fragments (the common case, incl. document text).
     *
     * Preferred: the per-document text index (lexbor_compat/text_index.c) maps
     * this node to the contiguous, document-order run of its descendants' text
     * slices, so we serve a single pre-sized memcpy run with no per-extraction
     * tree walk — the walk is otherwise the dominant, cache-bound cost. Built
     * lazily on first use and dropped on any mutation, so a slice can never
     * point at reallocated/detached storage.
     *
     * Fallback (index unavailable — node outside the indexed tree, e.g. a
     * fragment, or a build OOM): stream each descendant text/CDATA node's data
     * straight into the Ruby string via an iterative pre-order walk (stack-safe;
     * skips Lexbor's intermediate arena buffer + copy). */
    if (node->type == LXB_DOM_NODE_TYPE_ELEMENT
        || node->type == LXB_DOM_NODE_TYPE_DOCUMENT_FRAGMENT) {
        mkr_parsed_t *parsed = mkr_doc_parsed(mkr_node_document(self));
        const mkr_borrowed_text_t *slices;
        size_t nslices, total;
        if (parsed != NULL
            && mkr_parsed_text_slices(parsed, node, &slices, &nslices, &total)) {
            return mkr_ruby_str_from_slices(slices, nslices, total);
        }

        VALUE str = rb_utf8_str_new(NULL, 0);
        for (lxb_dom_node_t *n = node->first_child; n != NULL;) {
            if (n->type == LXB_DOM_NODE_TYPE_TEXT
                || n->type == LXB_DOM_NODE_TYPE_CDATA_SECTION) {
                const lexbor_str_t *d = &lxb_dom_interface_character_data(n)->data;
                if (d->data != NULL && d->length != 0) {
                    rb_str_cat(str, (const char *)d->data, (long)d->length);
                }
            }
            if (n->first_child != NULL) { n = n->first_child; continue; }
            while (n != node && n->next == NULL) { n = n->parent; }
            if (n == node) { break; }
            n = n->next;
        }
        return str;
    }

    /* Character-data and other node kinds keep the general (proven) path. */
    size_t len = 0;
    lxb_char_t *text = lxb_dom_node_text_content(node, &len);
    if (text == NULL) {
        return rb_utf8_str_new("", 0);
    }
    VALUE str = rb_utf8_str_new((const char *)text, len);
    lxb_dom_document_destroy_text(node->owner_document, text);
    return str;
}

#inspectObject

Inspect representation. Avoids dumping the whole subtree.



169
170
171
172
173
# File 'lib/makiri/node.rb', line 169

def inspect
  "#<#{self.class.name} name=#{name.inspect}>"
rescue NoMethodError
  "#<#{self.class.name}>"
end

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

node.key?(name) -> true/false

Returns:

  • (Boolean)


431
432
433
434
435
436
437
438
439
440
441
442
443
# File 'ext/makiri/glue/ruby_node.c', line 431

static VALUE
mkr_node_has_key(VALUE self, VALUE rb_name)
{
    lxb_dom_node_t *node = mkr_node_unwrap(self);
    if (node->type != LXB_DOM_NODE_TYPE_ELEMENT) {
        return Qfalse;
    }
    mkr_ruby_borrowed_text_t nv = mkr_ruby_verified_text(rb_name, "attribute name");
    lxb_dom_element_t *el = lxb_dom_interface_element(node);
    bool has = lxb_dom_element_has_attribute(el, (const lxb_char_t *)nv.ptr, nv.len);
    RB_GC_GUARD(nv.value);
    return has ? Qtrue : Qfalse;
}

#keysObject

node.keys -> [String, …] of attribute names (document order).



446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
# File 'ext/makiri/glue/ruby_node.c', line 446

static VALUE
mkr_node_keys(VALUE self)
{
    lxb_dom_node_t *node = mkr_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));
    while (attr != NULL) {
        size_t len = 0;
        const lxb_char_t *name = lxb_dom_attr_qualified_name(attr, &len);
        rb_ary_push(ary, mkr_ruby_str_from_borrowed(
                             mkr_borrowed_text((const char *)name, len)));
        attr = lxb_dom_element_next_attribute(attr);
    }
    return ary;
}

#last_element_childObject



392
393
394
395
396
397
398
399
400
# File 'ext/makiri/glue/ruby_node.c', line 392

static VALUE
mkr_node_last_element_child(VALUE self)
{
    lxb_dom_node_t *c = mkr_node_unwrap(self)->last_child;
    while (c != NULL && c->type != LXB_DOM_NODE_TYPE_ELEMENT) {
        c = c->prev;
    }
    return mkr_wrap_node(c, mkr_node_document(self));
}

#lineObject

node.line -> 1-based source line, or nil when unknown.

The line comes from the byte offset stamped onto the node at parse time (source-location tracking) resolved against the document’s line table. Returns nil for nodes the tracker could not place (e.g. parser-inserted implicit <html>/<head>/<body>, or any node when tracking was disabled).



529
530
531
532
533
534
535
536
# File 'ext/makiri/glue/ruby_node.c', line 529

static VALUE
mkr_node_line(VALUE self)
{
    lxb_dom_node_t *node = mkr_node_unwrap(self);
    mkr_parsed_t   *p    = mkr_doc_parsed(mkr_node_document(self));
    size_t line = mkr_parsed_node_line(p, node);
    return line == 0 ? Qnil : ULONG2NUM(line);
}

#matches?(rb_selector) ⇒ Boolean

Node#matches?(selector): does THIS node match the CSS selector? (Like Nokogiri — tested against the node itself, not its descendants.) A malformed selector raises Makiri::CSS::SyntaxError.

Returns:

  • (Boolean)


157
158
159
160
161
162
163
164
# File 'ext/makiri/glue/ruby_css.c', line 157

static VALUE
mkr_node_matches(VALUE self, VALUE rb_selector)
{
    lxb_dom_node_t *node = mkr_node_unwrap(self);
    int matched = 0;
    mkr_with_compiled_selector(rb_selector, node, mkr_run_match, &matched);
    return matched ? Qtrue : Qfalse;
}

#nameObject Also known as: node_name

Node name. Matches Nokogiri: lowercase tag name for HTML elements (Lexbor lowercases during tokenization), and the un-prefixed DOM names “text”/“comment”/“#cdata-section”/“document” for the other kinds.



103
104
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
# File 'ext/makiri/glue/ruby_node.c', line 103

static VALUE
mkr_node_name(VALUE self)
{
    lxb_dom_node_t *node = mkr_node_unwrap(self);
    size_t len = 0;
    const lxb_char_t *name;

    switch (node->type) {
    case LXB_DOM_NODE_TYPE_ELEMENT:
        name = lxb_dom_element_qualified_name(lxb_dom_interface_element(node), &len);
        return mkr_ruby_str_from_borrowed(mkr_borrowed_text((const char *)name, len));
    case LXB_DOM_NODE_TYPE_ATTRIBUTE:
        name = lxb_dom_attr_qualified_name(lxb_dom_interface_attr(node), &len);
        return mkr_ruby_str_from_borrowed(mkr_borrowed_text((const char *)name, len));
    case LXB_DOM_NODE_TYPE_TEXT:
        return rb_utf8_str_new_cstr("text");
    case LXB_DOM_NODE_TYPE_COMMENT:
        return rb_utf8_str_new_cstr("comment");
    case LXB_DOM_NODE_TYPE_CDATA_SECTION:
        return rb_utf8_str_new_cstr("#cdata-section");
    case LXB_DOM_NODE_TYPE_DOCUMENT:
        return rb_utf8_str_new_cstr("document");
    default:
        name = lxb_dom_node_name(node, &len);
        return mkr_ruby_str_from_borrowed(mkr_borrowed_text((const char *)name, len));
    }
}

#name=(rb_name) ⇒ Object Also known as: node_name=

element.name = new_name -> new_name. Renames the element in place (identity preserved): create a throwaway element with the new name so the document interns it, copy its name fields onto this node, then discard it.



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'ext/makiri/glue/ruby_mutate.c', line 216

static VALUE
mkr_node_set_name(VALUE self, VALUE rb_name)
{
    lxb_dom_node_t *node = mkr_node_unwrap(self);
    if (node->type != LXB_DOM_NODE_TYPE_ELEMENT) {
        rb_raise(mkr_eError, "name= is only supported on elements");
    }
    mkr_ruby_borrowed_text_t nv = mkr_ruby_verified_text(rb_name, "element name");
    lxb_dom_element_t *fresh = lxb_dom_document_create_element(
        node->owner_document, (const lxb_char_t *)nv.ptr, nv.len, NULL);
    RB_GC_GUARD(nv.value);
    if (fresh == NULL) {
        rb_raise(mkr_eError, "failed to rename element");
    }

    lxb_dom_element_t *el = lxb_dom_interface_element(node);
    el->node.local_name = fresh->node.local_name;
    el->node.prefix     = fresh->node.prefix;
    el->node.ns         = fresh->node.ns;
    el->upper_name      = fresh->upper_name;
    el->qualified_name  = fresh->qualified_name;

    lxb_dom_node_destroy(lxb_dom_interface_node(fresh));
    return rb_name;
}

#nextObject



297
298
299
300
301
302
# File 'ext/makiri/glue/ruby_node.c', line 297

static VALUE
mkr_node_next(VALUE self)
{
    lxb_dom_node_t *node = mkr_node_unwrap(self);
    return mkr_wrap_node(node->next, mkr_node_document(self));
}

#next_elementObject



311
312
313
314
315
316
317
318
319
# File 'ext/makiri/glue/ruby_node.c', line 311

static VALUE
mkr_node_next_element(VALUE self)
{
    lxb_dom_node_t *node = mkr_node_unwrap(self)->next;
    while (node != NULL && node->type != LXB_DOM_NODE_TYPE_ELEMENT) {
        node = node->next;
    }
    return mkr_wrap_node(node, mkr_node_document(self));
}

#next_siblingObject



297
298
299
300
301
302
# File 'ext/makiri/glue/ruby_node.c', line 297

static VALUE
mkr_node_next(VALUE self)
{
    lxb_dom_node_t *node = mkr_node_unwrap(self);
    return mkr_wrap_node(node->next, mkr_node_document(self));
}

#node_typeObject Also known as: type

Numeric DOM node type (LXB_DOM_NODE_TYPE_*).



132
133
134
135
136
# File 'ext/makiri/glue/ruby_node.c', line 132

static VALUE
mkr_node_get_type(VALUE self)
{
    return INT2NUM((int)mkr_node_unwrap(self)->type);
}

#outer_html(*args) ⇒ Object

Outer HTML: the node itself plus its descendants. Pass ‘pretty: true` for indented output.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'ext/makiri/glue/ruby_serialize.c', line 39

static VALUE
mkr_node_to_html(int argc, VALUE *argv, VALUE self)
{
    int pretty = mkr_serialize_pretty_opt(argc, argv);
    lxb_dom_node_t *node = mkr_node_unwrap(self);
    VALUE str = rb_utf8_str_new(NULL, 0);

    /* A document fragment has no tag of its own; "outer" == its children, so
     * the deep (children) serializer is the right one (the tree serializer
     * rejects a fragment node). */
    int deep = (node->type == LXB_DOM_NODE_TYPE_DOCUMENT_FRAGMENT);
    lxb_status_t st;
    if (deep) {
        st = pretty
            ? lxb_html_serialize_pretty_deep_cb(node, LXB_HTML_SERIALIZE_OPT_UNDEF,
                                                0, mkr_serialize_cb, (void *)str)
            : lxb_html_serialize_deep_cb(node, mkr_serialize_cb, (void *)str);
    } else {
        st = pretty
            ? lxb_html_serialize_pretty_tree_cb(node, LXB_HTML_SERIALIZE_OPT_UNDEF,
                                                0, mkr_serialize_cb, (void *)str)
            : lxb_html_serialize_tree_cb(node, mkr_serialize_cb, (void *)str);
    }
    if (st != LXB_STATUS_OK) {
        rb_raise(mkr_eError, "HTML serialization failed");
    }
    return str;
}

#outer_html=(rb_html) ⇒ Object

node.outer_html = html -> html. Replaces the node itself with the parse.



348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'ext/makiri/glue/ruby_mutate.c', line 348

static VALUE
mkr_node_set_outer_html(VALUE self, VALUE rb_html)
{
    lxb_dom_node_t *node   = mkr_node_unwrap(self);
    lxb_dom_node_t *parent = node->parent;
    if (parent == NULL || parent->type != LXB_DOM_NODE_TYPE_ELEMENT) {
        rb_raise(mkr_eError, "outer_html= requires a node with a parent element");
    }

    /* Parse in the parent's context, splice imported nodes before self. */
    mkr_parse_fragment_into(parent, rb_html, node->owner_document,
                            mkr_emit_before, node);
    lxb_dom_node_remove(node);
    mkr_invalidate_index(self);
    return rb_html;
}

#parentObject



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'ext/makiri/glue/ruby_node.c', line 279

static VALUE
mkr_node_parent(VALUE self)
{
    lxb_dom_node_t *node = mkr_node_unwrap(self);
    VALUE document = mkr_node_document(self);

    /* Lexbor never links an attribute back to its element, so node->parent is
     * NULL for attributes. Resolve via the compat attr->owner index. */
    if (node->type == LXB_DOM_NODE_TYPE_ATTRIBUTE) {
        lxb_dom_node_t *owner =
            mkr_parsed_attr_owner(mkr_doc_parsed(document),
                                  lxb_dom_interface_attr(node));
        return mkr_wrap_node(owner, document);
    }

    return mkr_wrap_node(node->parent, document);
}

#parse(rb_html) ⇒ Object

node.parse(html) -> NodeSet of nodes parsed as a fragment in this element’s context (its own tag + namespace). Matches Nokogiri’s Node#parse and is the way to reach a foreign (SVG/MathML) fragment context.



341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'ext/makiri/glue/ruby_doc.c', line 341

static VALUE
mkr_node_parse(VALUE self, VALUE rb_html)
{
    lxb_dom_node_t *node = mkr_node_unwrap(self);
    if (node->type != LXB_DOM_NODE_TYPE_ELEMENT) {
        rb_raise(rb_eArgError, "Node#parse requires an element context");
    }
    VALUE document = mkr_node_document(self);
    VALUE frag = mkr_build_fragment_ctx(document, rb_html,
                                        (lxb_tag_id_t)node->local_name,
                                        (lxb_ns_id_t)node->ns);
    return rb_funcall(frag, rb_intern("children"), 0);
}

#pathString

An absolute XPath that locates this node, e.g. “/html/body/p”. Element/text/comment steps carry a 1-based position among same-kind siblings (omitted when unique); attributes use “@name”. Round-trips through #at_xpath.

Returns:

  • (String)


156
157
158
159
160
161
162
163
164
165
166
# File 'lib/makiri/node.rb', line 156

def path
  return "/" if document?

  segments = []
  node = self
  until node.nil? || node.document?
    segments.unshift(node.send(:path_segment))
    node = node.parent
  end
  "/#{segments.join("/")}"
end

#previousObject



304
305
306
307
308
309
# File 'ext/makiri/glue/ruby_node.c', line 304

static VALUE
mkr_node_previous(VALUE self)
{
    lxb_dom_node_t *node = mkr_node_unwrap(self);
    return mkr_wrap_node(node->prev, mkr_node_document(self));
}

#previous_elementObject



321
322
323
324
325
326
327
328
329
# File 'ext/makiri/glue/ruby_node.c', line 321

static VALUE
mkr_node_previous_element(VALUE self)
{
    lxb_dom_node_t *node = mkr_node_unwrap(self)->prev;
    while (node != NULL && node->type != LXB_DOM_NODE_TYPE_ELEMENT) {
        node = node->prev;
    }
    return mkr_wrap_node(node, mkr_node_document(self));
}

#previous_siblingObject



304
305
306
307
308
309
# File 'ext/makiri/glue/ruby_node.c', line 304

static VALUE
mkr_node_previous(VALUE self)
{
    lxb_dom_node_t *node = mkr_node_unwrap(self);
    return mkr_wrap_node(node->prev, mkr_node_document(self));
}

#processing_instruction?Boolean

Returns:

  • (Boolean)


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

def processing_instruction?
  is_a?(ProcessingInstruction)
end

#removeObject

node.remove / node.unlink -> node. Detaches from the tree (still usable).



148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'ext/makiri/glue/ruby_mutate.c', line 148

static VALUE
mkr_node_remove(VALUE self)
{
    lxb_dom_node_t *node = mkr_node_unwrap(self);
    if (node->type == LXB_DOM_NODE_TYPE_ATTRIBUTE) {
        rb_raise(mkr_eError, "use delete(name) to remove an attribute");
    }
    if (node->parent != NULL) {
        lxb_dom_node_remove(node);
        mkr_invalidate_index(self);
    }
    return self;
}

#remove_class(names = nil) ⇒ self

Remove each class in names (or every class when names is nil); drops the ‘class` attribute entirely when none remain.

Returns:

  • (self)


100
101
102
103
104
105
106
107
108
# File 'lib/makiri/node.rb', line 100

def remove_class(names = nil)
  if names.nil?
    delete("class")
  else
    remaining = classes - names.to_s.split(/\s+/)
    remaining.empty? ? delete("class") : (self["class"] = remaining.join(" "))
  end
  self
end

#replace(rb_other) ⇒ Object

node.replace(other) -> other. Puts other where node is, detaches node.



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'ext/makiri/glue/ruby_mutate.c', line 163

static VALUE
mkr_node_replace(VALUE self, VALUE rb_other)
{
    lxb_dom_node_t *ref   = mkr_node_unwrap(self);
    lxb_dom_node_t *other = mkr_arg_node(rb_other);
    if (ref->parent == NULL) {
        rb_raise(mkr_eError, "cannot replace a node with no parent");
    }
    mkr_prepare_insert(ref, other);
    if (mkr_is_fragment(other)) {
        lxb_dom_node_t *c;
        while ((c = other->first_child) != NULL) {
            lxb_dom_node_remove(c);
            lxb_dom_node_insert_before(ref, c);
        }
    } else {
        lxb_dom_node_insert_before(ref, other);
    }
    lxb_dom_node_remove(ref);
    mkr_invalidate_index(self);
    return rb_other;
}

#rootMakiri::Element?

The root element of the owning document (e.g. <html>).

Returns:



121
122
123
# File 'lib/makiri/node.rb', line 121

def root
  document.root
end

#search(path) ⇒ Makiri::NodeSet, ...

Query with CSS or XPath, auto-detecting which from the string shape. Strings that look like a location path (start with “/”, “./”, “..”, “.//”, “(”, “@” or contain “::”) are treated as XPath; everything else as CSS.

Returns:



141
142
143
# File 'lib/makiri/node.rb', line 141

def search(path)
  xpath?(path) ? xpath(path) : css(path)
end

#set_attribute(name, value) ⇒ Object

Set an attribute (alias for #[]=). @return [String]



64
65
66
# File 'lib/makiri/node.rb', line 64

def set_attribute(name, value)
  self[name] = value
end

#textObject

Concatenated text content of this node and its descendants. The DOM spec makes a Document’s textContent null; we instead return the text of the root element (matching the intuitive, Nokogiri-like Document#text).



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'ext/makiri/glue/ruby_node.c', line 207

static VALUE
mkr_node_content(VALUE self)
{
    lxb_dom_node_t *node = mkr_node_unwrap(self);
    if (node->type == LXB_DOM_NODE_TYPE_DOCUMENT) {
        node = lxb_dom_document_root((lxb_dom_document_t *)node);
        if (node == NULL) {
            return rb_utf8_str_new("", 0);
        }
    }

    /* Fast path for elements / fragments (the common case, incl. document text).
     *
     * Preferred: the per-document text index (lexbor_compat/text_index.c) maps
     * this node to the contiguous, document-order run of its descendants' text
     * slices, so we serve a single pre-sized memcpy run with no per-extraction
     * tree walk — the walk is otherwise the dominant, cache-bound cost. Built
     * lazily on first use and dropped on any mutation, so a slice can never
     * point at reallocated/detached storage.
     *
     * Fallback (index unavailable — node outside the indexed tree, e.g. a
     * fragment, or a build OOM): stream each descendant text/CDATA node's data
     * straight into the Ruby string via an iterative pre-order walk (stack-safe;
     * skips Lexbor's intermediate arena buffer + copy). */
    if (node->type == LXB_DOM_NODE_TYPE_ELEMENT
        || node->type == LXB_DOM_NODE_TYPE_DOCUMENT_FRAGMENT) {
        mkr_parsed_t *parsed = mkr_doc_parsed(mkr_node_document(self));
        const mkr_borrowed_text_t *slices;
        size_t nslices, total;
        if (parsed != NULL
            && mkr_parsed_text_slices(parsed, node, &slices, &nslices, &total)) {
            return mkr_ruby_str_from_slices(slices, nslices, total);
        }

        VALUE str = rb_utf8_str_new(NULL, 0);
        for (lxb_dom_node_t *n = node->first_child; n != NULL;) {
            if (n->type == LXB_DOM_NODE_TYPE_TEXT
                || n->type == LXB_DOM_NODE_TYPE_CDATA_SECTION) {
                const lexbor_str_t *d = &lxb_dom_interface_character_data(n)->data;
                if (d->data != NULL && d->length != 0) {
                    rb_str_cat(str, (const char *)d->data, (long)d->length);
                }
            }
            if (n->first_child != NULL) { n = n->first_child; continue; }
            while (n != node && n->next == NULL) { n = n->parent; }
            if (n == node) { break; }
            n = n->next;
        }
        return str;
    }

    /* Character-data and other node kinds keep the general (proven) path. */
    size_t len = 0;
    lxb_char_t *text = lxb_dom_node_text_content(node, &len);
    if (text == NULL) {
        return rb_utf8_str_new("", 0);
    }
    VALUE str = rb_utf8_str_new((const char *)text, len);
    lxb_dom_document_destroy_text(node->owner_document, text);
    return str;
}

#text?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/makiri/node.rb', line 16

def text?
  is_a?(Text)
end

#to_hHash{String => String}

Attributes as a plain name => value Hash (empty for non-elements).

Returns:

  • (Hash{String => String})


133
134
135
# File 'lib/makiri/node.rb', line 133

def to_h
  attribute_nodes.each_with_object({}) { |attr, h| h[attr.name] = attr.value }
end

#to_html(*args) ⇒ Object

Outer HTML: the node itself plus its descendants. Pass ‘pretty: true` for indented output.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'ext/makiri/glue/ruby_serialize.c', line 39

static VALUE
mkr_node_to_html(int argc, VALUE *argv, VALUE self)
{
    int pretty = mkr_serialize_pretty_opt(argc, argv);
    lxb_dom_node_t *node = mkr_node_unwrap(self);
    VALUE str = rb_utf8_str_new(NULL, 0);

    /* A document fragment has no tag of its own; "outer" == its children, so
     * the deep (children) serializer is the right one (the tree serializer
     * rejects a fragment node). */
    int deep = (node->type == LXB_DOM_NODE_TYPE_DOCUMENT_FRAGMENT);
    lxb_status_t st;
    if (deep) {
        st = pretty
            ? lxb_html_serialize_pretty_deep_cb(node, LXB_HTML_SERIALIZE_OPT_UNDEF,
                                                0, mkr_serialize_cb, (void *)str)
            : lxb_html_serialize_deep_cb(node, mkr_serialize_cb, (void *)str);
    } else {
        st = pretty
            ? lxb_html_serialize_pretty_tree_cb(node, LXB_HTML_SERIALIZE_OPT_UNDEF,
                                                0, mkr_serialize_cb, (void *)str)
            : lxb_html_serialize_tree_cb(node, mkr_serialize_cb, (void *)str);
    }
    if (st != LXB_STATUS_OK) {
        rb_raise(mkr_eError, "HTML serialization failed");
    }
    return str;
}

#to_s(*args) ⇒ Object

Outer HTML: the node itself plus its descendants. Pass ‘pretty: true` for indented output.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'ext/makiri/glue/ruby_serialize.c', line 39

static VALUE
mkr_node_to_html(int argc, VALUE *argv, VALUE self)
{
    int pretty = mkr_serialize_pretty_opt(argc, argv);
    lxb_dom_node_t *node = mkr_node_unwrap(self);
    VALUE str = rb_utf8_str_new(NULL, 0);

    /* A document fragment has no tag of its own; "outer" == its children, so
     * the deep (children) serializer is the right one (the tree serializer
     * rejects a fragment node). */
    int deep = (node->type == LXB_DOM_NODE_TYPE_DOCUMENT_FRAGMENT);
    lxb_status_t st;
    if (deep) {
        st = pretty
            ? lxb_html_serialize_pretty_deep_cb(node, LXB_HTML_SERIALIZE_OPT_UNDEF,
                                                0, mkr_serialize_cb, (void *)str)
            : lxb_html_serialize_deep_cb(node, mkr_serialize_cb, (void *)str);
    } else {
        st = pretty
            ? lxb_html_serialize_pretty_tree_cb(node, LXB_HTML_SERIALIZE_OPT_UNDEF,
                                                0, mkr_serialize_cb, (void *)str)
            : lxb_html_serialize_tree_cb(node, mkr_serialize_cb, (void *)str);
    }
    if (st != LXB_STATUS_OK) {
        rb_raise(mkr_eError, "HTML serialization failed");
    }
    return str;
}

#traverse(&block) ⇒ self

Yield this node and every descendant, depth-first, children before self (post-order, matching Nokogiri).

Returns:

  • (self)


113
114
115
116
117
# File 'lib/makiri/node.rb', line 113

def traverse(&block)
  children.each { |child| child.traverse(&block) }
  block.call(self)
  self
end

node.remove / node.unlink -> node. Detaches from the tree (still usable).



148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'ext/makiri/glue/ruby_mutate.c', line 148

static VALUE
mkr_node_remove(VALUE self)
{
    lxb_dom_node_t *node = mkr_node_unwrap(self);
    if (node->type == LXB_DOM_NODE_TYPE_ATTRIBUTE) {
        rb_raise(mkr_eError, "use delete(name) to remove an attribute");
    }
    if (node->parent != NULL) {
        lxb_dom_node_remove(node);
        mkr_invalidate_index(self);
    }
    return self;
}

#valueObject

attr.value -> the attribute’s value String. For non-attribute nodes, falls back to text content (matching the loose Nokogiri-ish meaning of #value).



510
511
512
513
514
515
516
517
518
519
520
521
# File 'ext/makiri/glue/ruby_node.c', line 510

static VALUE
mkr_node_value(VALUE self)
{
    lxb_dom_node_t *node = mkr_node_unwrap(self);
    if (node->type == LXB_DOM_NODE_TYPE_ATTRIBUTE) {
        size_t len = 0;
        const lxb_char_t *val =
            lxb_dom_attr_value(lxb_dom_interface_attr(node), &len);
        return mkr_ruby_str_from_borrowed(mkr_borrowed_text((const char *)val, len));
    }
    return mkr_node_content(self);
}

#valuesObject

node.values -> [String, …] of attribute values (document order).



467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
# File 'ext/makiri/glue/ruby_node.c', line 467

static VALUE
mkr_node_values(VALUE self)
{
    lxb_dom_node_t *node = mkr_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));
    while (attr != NULL) {
        size_t len = 0;
        const lxb_char_t *val = lxb_dom_attr_value(attr, &len);
        rb_ary_push(ary, mkr_ruby_str_from_borrowed(
                             mkr_borrowed_text((const char *)val, len)));
        attr = lxb_dom_element_next_attribute(attr);
    }
    return ary;
}

#xpath(*args) ⇒ Object



681
682
683
684
685
686
687
# File 'ext/makiri/glue/ruby_xpath.c', line 681

static VALUE
mkr_node_xpath(int argc, VALUE *argv, VALUE self)
{
    VALUE rb_expr, handler, opts;
    rb_scan_args(argc, argv, "11:", &rb_expr, &handler, &opts);
    return mkr_node_xpath_run(self, rb_expr, handler, mkr_ns_matching_lax(opts), 0);
}