Class: Makiri::DocumentFragment

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

Overview

A detached group of sibling nodes. Build one with DocumentFragment.parse (its own backing document) or Makiri::Document#fragment (bound to an existing document, so its nodes can be spliced in with Node#add_child and friends). Inserting a fragment contributes its children, not the fragment node itself.

Both .parse and Document#fragment accept a Nokogiri-compatible context: keyword naming the element the HTML is parsed inside of (the fragment-parsing algorithm is context-sensitive). It may be:

* a tag-name String (HTML namespace), e.g. <tt>context: "tr"</tt>; the
  bare strings <tt>"svg"</tt> / <tt>"math"</tt> name the foreign roots;
* a {Makiri::Node} element — its tag and namespace are used (the way to
  reach a foreign non-root context such as an SVG <desc>).

The default context is <body>. See also Node#parse.

.parse is defined in C (ext/makiri/glue/ruby_doc.c).

Class Method Summary collapse

Methods inherited from Node

#<<, #==, #[], #[]=, #add_child, #add_class, #add_next_sibling, #add_previous_sibling, #after, #ancestors, #append_class, #at, #at_css, #at_xpath, #attribute, #attribute?, #attribute_nodes, #attributes, #before, #blank?, #child, #children, #classes, #comment?, #content, #content=, #css, #delete, #document, #document?, #document_fragment?, #element?, #element_children, #elements, #eql?, #first_element_child, #hash, #inner_html, #inner_html=, #inner_text, #inspect, #key?, #keys, #last_element_child, #line, #matches?, #name, #name=, #next, #next_element, #next_sibling, #node_type, #outer_html, #outer_html=, #parent, #parse, #path, #previous, #previous_element, #previous_sibling, #processing_instruction?, #remove, #remove_class, #replace, #root, #search, #set_attribute, #text, #text?, #to_h, #to_html, #to_s, #traverse, #unlink, #value, #values, #xpath

Class Method Details

.parse(*args) ⇒ Object

DocumentFragment.parse(html, context: …) -> standalone fragment with its own backing document (kept alive by the fragment’s wrapper).



317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'ext/makiri/glue/ruby_doc.c', line 317

static VALUE
mkr_frag_s_parse(int argc, VALUE *argv, VALUE klass)
{
    (void)klass;
    VALUE html, opts;
    rb_scan_args(argc, argv, "1:", &html, &opts);
    VALUE context = NIL_P(opts) ? Qnil
                                : rb_hash_aref(opts, ID2SYM(rb_intern("context")));

    static const lxb_char_t shell[] = "<html><body></body></html>";
    mkr_parsed_t *parsed = mkr_parse_html(shell, sizeof(shell) - 1);
    if (parsed == NULL) {
        rb_raise(mkr_eError, "failed to create fragment document");
    }
    VALUE document = mkr_wrap_document(parsed); /* GC now owns parsed */
    lxb_tag_id_t tag;
    lxb_ns_id_t  ns;
    mkr_resolve_fragment_context(mkr_doc_unwrap(document), context, &tag, &ns);
    return mkr_build_fragment_ctx(document, html, tag, ns);
}