Class: Nokolexbor::Attribute

Inherits:
Node
  • Object
show all
Defined in:
ext/nokolexbor/nl_attribute.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

Instance Attribute Summary

Attributes inherited from Node

#document

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, #attribute_nodes, #attributes, #attrs, #before, #cdata?, #child, #children, #children=, #classes, #clone, #comment?, #content=, #css, #css_impl, #css_path, #destroy, #document?, #each, #element?, #element_children, #first_element_child, #fragment, #fragment?, #inner_html, #key?, #keys, #kwattr_add, #kwattr_append, #kwattr_remove, #kwattr_values, #last_element_child, #matches?, #next_element, #node_type, #nokogiri_at_css, #nokogiri_css, #outer_html, #parent=, #parse, #path, #prepend_child, #previous_element, #processing_instruction?, #remove, #remove_attr, #remove_class, #replace, #search, #source_location, #swap, #text?, #traverse, #value?, #values, #wrap, #write_to, #xpath

Class Method Details

.new(document, name) ⇒ Attribute

Create a new Attribute on the document with name.

Returns:

Parameters:



16
17
18
19
20
21
22
23
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
# File 'ext/nokolexbor/nl_attribute.c', line 16

static VALUE
nl_attribute_new(int argc, VALUE *argv, VALUE klass)
{
  lxb_dom_document_t *document;
  VALUE rb_document;
  VALUE rb_name;
  VALUE rest;

  rb_scan_args(argc, argv, "2*", &rb_document, &rb_name, &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_attr_t *attr = lxb_dom_attr_interface_create(document);
  if (attr == NULL) {
    rb_raise(rb_eRuntimeError, "Error creating attribute");
  }

  lxb_dom_attr_set_name(attr, (const lxb_char_t *)c_name, name_len, false);

  VALUE rb_node = nl_rb_node_create(&attr->node, rb_document);

  if (rb_block_given_p()) {
    rb_yield(rb_node);
  }

  return rb_node;
}

Instance Method Details

#inspectObject



181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'ext/nokolexbor/nl_attribute.c', line 181

static VALUE
nl_attribute_inspect(VALUE self)
{
  VALUE c = rb_class_name(CLASS_OF(self));
  lxb_dom_node_t *node = nl_rb_node_unwrap(self);
  lxb_dom_attr_t *attr = lxb_dom_interface_attr(node);
  size_t len;
  lxb_char_t *attr_value = lxb_dom_attr_value(attr, &len);

  return rb_sprintf("#<%" PRIsVALUE " %s=\"%s\">", c,
                    lxb_dom_attr_qualified_name(attr, &len),
                    attr_value == NULL ? "" : attr_value);
}

#nameString Also known as: node_name

Get the name of the Attribute.

Returns:

  • (String)


55
56
57
58
59
60
61
62
63
64
65
# File 'ext/nokolexbor/nl_attribute.c', line 55

static VALUE
nl_attribute_name(VALUE self)
{
  lxb_dom_node_t *node = nl_rb_node_unwrap(self);
  lxb_dom_attr_t *attr = lxb_dom_interface_attr(node);

  size_t len;
  lxb_char_t *name = lxb_dom_attr_qualified_name(attr, &len);

  return rb_utf8_str_new(name, len);
}

#name=(name) ⇒ String Also known as: node_name=

Set the name of the Attribute.

Returns:

  • (String)


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'ext/nokolexbor/nl_attribute.c', line 73

static VALUE
nl_attribute_set_name(VALUE self, VALUE rb_name)
{
  lxb_dom_node_t *node = nl_rb_node_unwrap(self);
  lxb_dom_attr_t *attr = lxb_dom_interface_attr(node);

  const char *c_name = StringValuePtr(rb_name);
  size_t name_len = RSTRING_LEN(rb_name);

  lxb_status_t status = lxb_dom_attr_set_name(attr, (const lxb_char_t *)c_name, name_len, false);
  if (status != LXB_STATUS_OK) {
    nl_raise_lexbor_error(status);
  }

  return rb_name;
}

#nextAttribute

Get the next Attribute.

Returns:



169
170
171
172
173
174
175
176
177
178
179
# File 'ext/nokolexbor/nl_attribute.c', line 169

static VALUE
nl_attribute_next(VALUE self)
{
  lxb_dom_node_t *node = nl_rb_node_unwrap(self);
  lxb_dom_attr_t *attr = lxb_dom_interface_attr(node);

  if (attr->next == NULL) {
    return Qnil;
  }
  return nl_rb_node_create(attr->next, nl_rb_document_get(self));
}

#parentNode

Get the owner Node of the Attribute.

Returns:



135
136
137
138
139
140
141
142
143
144
145
# File 'ext/nokolexbor/nl_attribute.c', line 135

static VALUE
nl_attribute_parent(VALUE self)
{
  lxb_dom_node_t *node = nl_rb_node_unwrap(self);
  lxb_dom_attr_t *attr = lxb_dom_interface_attr(node);

  if (attr->owner == NULL) {
    return Qnil;
  }
  return nl_rb_node_create(attr->owner, nl_rb_document_get(self));
}

#previousAttribute

Get the previous Attribute.

Returns:



152
153
154
155
156
157
158
159
160
161
162
# File 'ext/nokolexbor/nl_attribute.c', line 152

static VALUE
nl_attribute_previous(VALUE self)
{
  lxb_dom_node_t *node = nl_rb_node_unwrap(self);
  lxb_dom_attr_t *attr = lxb_dom_interface_attr(node);

  if (attr->prev == NULL) {
    return Qnil;
  }
  return nl_rb_node_create(attr->prev, nl_rb_document_get(self));
}

#valueString Also known as: text, content, to_s, to_str

Get the value of the Attribute.

Returns:

  • (String)


95
96
97
98
99
100
101
102
103
104
105
# File 'ext/nokolexbor/nl_attribute.c', line 95

static VALUE
nl_attribute_value(VALUE self)
{
  lxb_dom_node_t *node = nl_rb_node_unwrap(self);
  lxb_dom_attr_t *attr = lxb_dom_interface_attr(node);

  size_t len;
  lxb_char_t *value = lxb_dom_attr_value(attr, &len);

  return rb_utf8_str_new(value, len);
}

#value=(value) ⇒ String

Set the value of the Attribute.

Returns:

  • (String)


113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'ext/nokolexbor/nl_attribute.c', line 113

static VALUE
nl_attribute_set_value(VALUE self, VALUE rb_content)
{
  lxb_dom_node_t *node = nl_rb_node_unwrap(self);
  lxb_dom_attr_t *attr = lxb_dom_interface_attr(node);

  const char *c_content = StringValuePtr(rb_content);
  size_t content_len = RSTRING_LEN(rb_content);

  lxb_status_t status = lxb_dom_attr_set_value(attr, (const lxb_char_t *)c_content, content_len);
  if (status != LXB_STATUS_OK) {
    nl_raise_lexbor_error(status);
  }

  return rb_content;
}