Class: Nokolexbor::Document

Inherits:
Node
  • Object
show all
Defined in:
lib/nokolexbor/document.rb,
ext/nokolexbor/nl_document.c

Constant Summary

Constants inherited from Node

Node::ATTRIBUTE_NODE, Node::CDATA_SECTION_NODE, Node::COMMENT_NODE, Node::DOCUMENT_FRAG_NODE, Node::DOCUMENT_NODE, Node::DOCUMENT_TYPE_NODE, Node::ELEMENT_NODE, Node::ENTITY_NODE, Node::ENTITY_REF_NODE, Node::LOOKS_LIKE_XPATH, Node::NOTATION_NODE, Node::PI_NODE, Node::TEXT_NODE

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Node

#<<, #==, #[], #[]=, #add_child, #add_class, #add_next_sibling, #add_previous_sibling, #add_sibling, #after, #ancestors, #append_class, #at, #at_css, #at_css_impl, #at_xpath, #attribute, #attributes, #attrs, #before, #cdata?, #child, #children, #children=, #classes, #clone, #comment?, #content, #content=, #css, #css_impl, #destroy, #document?, #each, #element?, #first_element_child, #fragment, #fragment?, #inner_html, #key?, #keys, #keywordify, #kwattr_add, #kwattr_append, #kwattr_remove, #kwattr_values, #last_element_child, #matches?, #name, #next, #next_element, #node_type, #nokogiri_at_css, #nokogiri_css, #outer_html, #parent, #parent=, #parse, #prepend_child, #previous, #previous_element, #processing_instruction?, #remove, #remove_attr, #remove_class, #replace, #search, #text?, #traverse, #values, #wrap, #write_to, #xpath

Class Method Details

.newObject



54
55
56
57
58
# File 'ext/nokolexbor/nl_document.c', line 54

static VALUE
nl_document_new(VALUE self)
{
  return nl_document_parse(self, rb_str_new("", 0));
}

.parse(rb_string_or_io) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'ext/nokolexbor/nl_document.c', line 24

static VALUE
nl_document_parse(VALUE self, VALUE rb_string_or_io)
{
  VALUE id_read = rb_intern("read");
  VALUE rb_html;
  if (rb_respond_to(rb_string_or_io, id_read)) {
    rb_html = rb_funcall(rb_string_or_io, id_read, 0);
  } else {
    rb_html = rb_string_or_io;
  }
  const char *html_c = StringValuePtr(rb_html);
  size_t html_len = RSTRING_LEN(rb_html);

  lxb_html_document_t *document;

  document = lxb_html_document_create();
  if (document == NULL) {
    rb_raise(rb_eRuntimeError, "Error creating document");
  }

  lxb_dom_document_scripting_set(lxb_dom_interface_document(document), true);

  lxb_status_t status = lxb_html_document_parse(document, (const lxb_char_t *)html_c, html_len);
  if (status != LXB_STATUS_OK) {
    nl_raise_lexbor_error(status);
  }

  return TypedData_Wrap_Struct(cNokolexborDocument, &nl_document_type, document);
}

Instance Method Details

#create_cdata(string, &block) ⇒ Object

Create a CDATA Node containing string



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

def create_cdata(string, &block)
  Nokolexbor::CDATA.new(string.to_s, self, &block)
end

#create_comment(string, &block) ⇒ Object

Create a Comment Node containing string



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

def create_comment(string, &block)
  Nokolexbor::Comment.new(string.to_s, self, &block)
end

#create_element(name, *contents_or_attrs, &block) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/nokolexbor/document.rb', line 5

def create_element(name, *contents_or_attrs, &block)
  elm = Nokolexbor::Element.new(name, self, &block)
  contents_or_attrs.each do |arg|
    case arg
    when Hash
      arg.each do |k, v|
        elm[k.to_s] = v.to_s
      end
    else
      elm.content = arg
    end
  end
  elm
end

#create_text_node(string, &block) ⇒ Object

Create a Text Node with string



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

def create_text_node(string, &block)
  Nokolexbor::Text.new(string.to_s, self, &block)
end

#documentObject

A reference to self



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

def document
  self
end

#meta_encodingObject



40
41
42
43
44
45
46
# File 'lib/nokolexbor/document.rb', line 40

def meta_encoding
  if (meta = at_css("meta[charset]"))
    meta[:charset]
  elsif (meta = meta_content_type)
    meta["content"][/charset\s*=\s*([\w-]+)/i, 1]
  end
end

#meta_encoding=(encoding) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/nokolexbor/document.rb', line 48

def meta_encoding=(encoding)
  if (meta = meta_content_type)
    meta["content"] = format("text/html; charset=%s", encoding)
    encoding
  elsif (meta = at_css("meta[charset]"))
    meta["charset"] = encoding
  else
    meta = Nokolexbor::Node.new("meta", self)
    meta["charset"] = encoding

    if (head = at_css("head"))
      head.prepend_child(meta)
    else
      (meta)
    end
    encoding
  end
end

#set_metadata_element(element) ⇒ Object



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

def (element)
  if (head = at_css("head"))
    head << element
  elsif (html = at_css("html"))
    head = html.prepend_child(Nokolexbor::Node.new("head", self))
    head.prepend_child(element)
  elsif (first = children.find do |node|
           case node
           when Nokolexbor::Node
             true
           end
         end)
    # We reach here only if the underlying document model
    # allows <html>/<head> elements to be omitted and does not
    # automatically supply them.
    first.add_previous_sibling(element)
  else
    html = add_child(Nokolexbor::Node.new("html", self))
    head = html.add_child(Nokolexbor::Node.new("head", self))
    head.prepend_child(element)
  end
end

#titleObject



68
69
70
71
72
73
74
# File 'ext/nokolexbor/nl_document.c', line 68

VALUE
nl_document_get_title(VALUE rb_doc)
{
  size_t len;
  lxb_char_t *str = lxb_html_document_title(nl_rb_document_unwrap(rb_doc), &len);
  return str == NULL ? rb_str_new("", 0) : rb_utf8_str_new(str, len);
}

#title=(rb_title) ⇒ Object



76
77
78
79
80
81
82
83
# File 'ext/nokolexbor/nl_document.c', line 76

VALUE
nl_document_set_title(VALUE rb_doc, VALUE rb_title)
{
  const char *c_title = StringValuePtr(rb_title);
  size_t len = RSTRING_LEN(rb_title);
  lxb_char_t *str = lxb_html_document_title_set(nl_rb_document_unwrap(rb_doc), (const lxb_char_t *)c_title, len);
  return Qnil;
}