nokolexbor 0.3.3 → 0.3.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 042127a2ed4027c52d3718c148921604cae07c274dddb7f3702c7caf0708863f
4
- data.tar.gz: 60230b55b773ef41abdca313adc3ffdba38a0b8462c9d23c3365e57c68b63bd5
3
+ metadata.gz: 53cb1f54e475a0b10fc7773259866fe675e761d0ca204c5f685f4ed8dd60294a
4
+ data.tar.gz: 0e697320eae75b41e16ac431c6c679159507013b5e3a9858959377eb969355b2
5
5
  SHA512:
6
- metadata.gz: f06063b84e62e23ba093a397ba6ffdbd802480e711ca24966d5b78be8bc8a7299754ab8bb4cd5d77d6dfe39efa7c21c705b105fe9d626d62eddc5e2e5a6ec9fb
7
- data.tar.gz: 86ddfdb650b78827071385cfca708ddbffcd35204a71f439315f800e8e60ee73c2d49ce46e28192ac454df8bab7ef8cbc937b97830e0b488290dbadccd8e51d6
6
+ metadata.gz: 609b453f596331b53ab13b403c274cde9472e119307c0af63da19be28f0475603705ef285332c94f8b39a47b6850dfc138694ccee0ede531acffd67e31e915a9
7
+ data.tar.gz: 9f6d2c3ceba6d98bc8f87d59c846bd6c41fc2758681b66481c72de1027078d30e62344267cfe63ce58209e23f6749502750fa05f4365112275d79cee11641e9d
@@ -0,0 +1,155 @@
1
+ #include "nokolexbor.h"
2
+
3
+ VALUE cNokolexborAttribute;
4
+ extern VALUE mNokolexbor;
5
+ extern VALUE cNokolexborNode;
6
+
7
+ static VALUE
8
+ nl_attribute_new(int argc, VALUE *argv, VALUE klass)
9
+ {
10
+ lxb_dom_document_t *document;
11
+ VALUE rb_document;
12
+ VALUE rb_name;
13
+ VALUE rest;
14
+
15
+ rb_scan_args(argc, argv, "2*", &rb_document, &rb_name, &rest);
16
+
17
+ if (!rb_obj_is_kind_of(rb_document, cNokolexborDocument)) {
18
+ rb_raise(rb_eArgError, "Document must be a Nokolexbor::Document");
19
+ }
20
+
21
+ document = nl_rb_document_unwrap(rb_document);
22
+
23
+ const char *c_name = StringValuePtr(rb_name);
24
+ size_t name_len = RSTRING_LEN(rb_name);
25
+ lxb_dom_attr_t *attr = lxb_dom_attr_interface_create(document);
26
+ if (attr == NULL) {
27
+ rb_raise(rb_eRuntimeError, "Error creating attribute");
28
+ }
29
+
30
+ lxb_dom_attr_set_name(attr, (const lxb_char_t *)c_name, name_len, false);
31
+
32
+ VALUE rb_node = nl_rb_node_create(&attr->node, rb_document);
33
+
34
+ if (rb_block_given_p()) {
35
+ rb_yield(rb_node);
36
+ }
37
+
38
+ return rb_node;
39
+ }
40
+
41
+ static VALUE
42
+ nl_attribute_name(VALUE self)
43
+ {
44
+ lxb_dom_node_t *node = nl_rb_node_unwrap(self);
45
+ lxb_dom_attr_t *attr = lxb_dom_interface_attr(node);
46
+
47
+ size_t len;
48
+ lxb_char_t *name = lxb_dom_attr_qualified_name(attr, &len);
49
+
50
+ return rb_utf8_str_new(name, len);
51
+ }
52
+
53
+ static VALUE
54
+ nl_attribute_set_name(VALUE self, VALUE rb_name)
55
+ {
56
+ lxb_dom_node_t *node = nl_rb_node_unwrap(self);
57
+ lxb_dom_attr_t *attr = lxb_dom_interface_attr(node);
58
+
59
+ const char *c_name = StringValuePtr(rb_name);
60
+ size_t name_len = RSTRING_LEN(rb_name);
61
+
62
+ lxb_status_t status = lxb_dom_attr_set_name(attr, (const lxb_char_t *)c_name, name_len, false);
63
+ if (status != LXB_STATUS_OK) {
64
+ nl_raise_lexbor_error(status);
65
+ }
66
+
67
+ return rb_name;
68
+ }
69
+
70
+ static VALUE
71
+ nl_attribute_value(VALUE self)
72
+ {
73
+ lxb_dom_node_t *node = nl_rb_node_unwrap(self);
74
+ lxb_dom_attr_t *attr = lxb_dom_interface_attr(node);
75
+
76
+ size_t len;
77
+ lxb_char_t *value = lxb_dom_attr_value(attr, &len);
78
+
79
+ return rb_utf8_str_new(value, len);
80
+ }
81
+
82
+ static VALUE
83
+ nl_attribute_set_value(VALUE self, VALUE rb_content)
84
+ {
85
+ lxb_dom_node_t *node = nl_rb_node_unwrap(self);
86
+ lxb_dom_attr_t *attr = lxb_dom_interface_attr(node);
87
+
88
+ const char *c_content = StringValuePtr(rb_content);
89
+ size_t content_len = RSTRING_LEN(rb_content);
90
+
91
+ lxb_status_t status = lxb_dom_attr_set_value(attr, (const lxb_char_t *)c_content, content_len);
92
+ if (status != LXB_STATUS_OK) {
93
+ nl_raise_lexbor_error(status);
94
+ }
95
+
96
+ return rb_content;
97
+ }
98
+
99
+ static VALUE
100
+ nl_attribute_parent(VALUE self)
101
+ {
102
+ lxb_dom_node_t *node = nl_rb_node_unwrap(self);
103
+ lxb_dom_attr_t *attr = lxb_dom_interface_attr(node);
104
+
105
+ if (attr->owner == NULL) {
106
+ return Qnil;
107
+ }
108
+ return nl_rb_node_create(attr->owner, nl_rb_document_get(self));
109
+ }
110
+
111
+ static VALUE
112
+ nl_attribute_previous(VALUE self)
113
+ {
114
+ lxb_dom_node_t *node = nl_rb_node_unwrap(self);
115
+ lxb_dom_attr_t *attr = lxb_dom_interface_attr(node);
116
+
117
+ if (attr->prev == NULL) {
118
+ return Qnil;
119
+ }
120
+ return nl_rb_node_create(attr->prev, nl_rb_document_get(self));
121
+ }
122
+
123
+ static VALUE
124
+ nl_attribute_next(VALUE self)
125
+ {
126
+ lxb_dom_node_t *node = nl_rb_node_unwrap(self);
127
+ lxb_dom_attr_t *attr = lxb_dom_interface_attr(node);
128
+
129
+ if (attr->next == NULL) {
130
+ return Qnil;
131
+ }
132
+ return nl_rb_node_create(attr->next, nl_rb_document_get(self));
133
+ }
134
+
135
+ void Init_nl_attribute(void)
136
+ {
137
+ cNokolexborAttribute = rb_define_class_under(mNokolexbor, "Attribute", cNokolexborNode);
138
+
139
+ rb_define_singleton_method(cNokolexborAttribute, "new", nl_attribute_new, -1);
140
+
141
+ rb_define_method(cNokolexborAttribute, "name", nl_attribute_name, 0);
142
+ rb_define_method(cNokolexborAttribute, "name=", nl_attribute_set_name, 1);
143
+ rb_define_method(cNokolexborAttribute, "value", nl_attribute_value, 0);
144
+ rb_define_method(cNokolexborAttribute, "value=", nl_attribute_set_value, 1);
145
+ rb_define_method(cNokolexborAttribute, "parent", nl_attribute_parent, 0);
146
+ rb_define_method(cNokolexborAttribute, "previous", nl_attribute_previous, 0);
147
+ rb_define_method(cNokolexborAttribute, "next", nl_attribute_next, 0);
148
+
149
+ rb_define_alias(cNokolexborAttribute, "node_name", "name");
150
+ rb_define_alias(cNokolexborAttribute, "node_name=", "name=");
151
+ rb_define_alias(cNokolexborAttribute, "text", "value");
152
+ rb_define_alias(cNokolexborAttribute, "content", "value");
153
+ rb_define_alias(cNokolexborAttribute, "to_s", "value");
154
+ rb_define_alias(cNokolexborAttribute, "to_str", "value");
155
+ }
@@ -12,6 +12,7 @@ extern VALUE cNokolexborComment;
12
12
  extern VALUE cNokolexborProcessingInstruction;
13
13
  extern VALUE cNokolexborNodeSet;
14
14
  extern VALUE cNokolexborDocumentFragment;
15
+ extern VALUE cNokolexborAttribute;
15
16
  extern VALUE eLexborError;
16
17
  VALUE cNokolexborNode;
17
18
  VALUE cNokolexborElement;
@@ -31,8 +32,9 @@ nl_rb_node_create(lxb_dom_node_t *node, VALUE rb_document)
31
32
  case LXB_DOM_NODE_TYPE_ELEMENT:
32
33
  rb_class = cNokolexborElement;
33
34
  break;
34
- // case LXB_DOM_NODE_TYPE_ATTRIBUTE:
35
- // break;
35
+ case LXB_DOM_NODE_TYPE_ATTRIBUTE:
36
+ rb_class = cNokolexborAttribute;
37
+ break;
36
38
  case LXB_DOM_NODE_TYPE_TEXT:
37
39
  rb_class = cNokolexborText;
38
40
  break;
@@ -111,6 +113,55 @@ nl_node_new(int argc, VALUE *argv, VALUE klass)
111
113
  return rb_node;
112
114
  }
113
115
 
116
+ static VALUE
117
+ nl_node_attribute(VALUE self, VALUE rb_name)
118
+ {
119
+ lxb_dom_node_t *node = nl_rb_node_unwrap(self);
120
+
121
+ const char *c_name = StringValuePtr(rb_name);
122
+ size_t name_len = RSTRING_LEN(rb_name);
123
+
124
+ if (node->type != LXB_DOM_NODE_TYPE_ELEMENT) {
125
+ return Qnil;
126
+ }
127
+
128
+ lxb_dom_attr_t *attr = lxb_dom_element_attr_by_name(lxb_dom_interface_element(node), (const lxb_char_t *)c_name, name_len);
129
+ if (attr == NULL) {
130
+ return Qnil;
131
+ }
132
+ if (attr->owner == NULL) {
133
+ attr->owner = node;
134
+ }
135
+ return nl_rb_node_create(attr, nl_rb_document_get(self));
136
+ }
137
+
138
+ static VALUE
139
+ nl_node_attribute_nodes(VALUE self)
140
+ {
141
+ lxb_dom_node_t *node = nl_rb_node_unwrap(self);
142
+ VALUE ary = rb_ary_new();
143
+ if (node->type != LXB_DOM_NODE_TYPE_ELEMENT) {
144
+ return ary;
145
+ }
146
+
147
+ lxb_dom_attr_t *attr = lxb_dom_element_first_attribute(lxb_dom_interface_element(node));
148
+
149
+ if (attr == NULL) {
150
+ return ary;
151
+ }
152
+
153
+ VALUE rb_doc = nl_rb_document_get(self);
154
+ while (attr != NULL) {
155
+ if (attr->owner == NULL) {
156
+ attr->owner = node;
157
+ }
158
+ rb_ary_push(ary, nl_rb_node_create(attr, rb_doc));
159
+ attr = attr->next;
160
+ }
161
+
162
+ return ary;
163
+ }
164
+
114
165
  static VALUE
115
166
  nl_node_content(VALUE self)
116
167
  {
@@ -836,7 +887,17 @@ static VALUE
836
887
  nl_node_clone(VALUE self)
837
888
  {
838
889
  lxb_dom_node_t *node = nl_rb_node_unwrap(self);
839
- lxb_dom_node_t *clone = lxb_dom_node_clone(node, 1);
890
+ lxb_dom_node_t *clone;
891
+
892
+ switch (node->type) {
893
+ case LXB_DOM_NODE_TYPE_ATTRIBUTE:
894
+ clone = lxb_dom_attr_interface_clone(node->owner_document, lxb_dom_interface_attr(node));
895
+ case LXB_DOM_NODE_TYPE_CDATA_SECTION:
896
+ clone = lxb_dom_cdata_section_interface_clone(node->owner_document, lxb_dom_interface_cdata_section(node));
897
+ default:
898
+ clone = lxb_dom_node_clone(node, true);
899
+ break;
900
+ }
840
901
  return nl_rb_node_create(clone, nl_rb_document_get(self));
841
902
  }
842
903
 
@@ -849,6 +910,8 @@ void Init_nl_node(void)
849
910
  cNokolexborCharacterData = rb_define_class_under(mNokolexbor, "CharacterData", cNokolexborNode);
850
911
 
851
912
  rb_define_singleton_method(cNokolexborNode, "new", nl_node_new, -1);
913
+ rb_define_method(cNokolexborNode, "attribute", nl_node_attribute, 1);
914
+ rb_define_method(cNokolexborNode, "attribute_nodes", nl_node_attribute_nodes, 0);
852
915
  rb_define_method(cNokolexborNode, "content", nl_node_content, 0);
853
916
  rb_define_method(cNokolexborNode, "content=", nl_node_content_set, 1);
854
917
  rb_define_method(cNokolexborNode, "[]", nl_node_get_attr, 1);
@@ -17,5 +17,6 @@ void Init_nokolexbor(void)
17
17
  Init_nl_processing_instruction();
18
18
  Init_nl_node_set();
19
19
  Init_nl_document_fragment();
20
+ Init_nl_attribute();
20
21
  Init_nl_xpath_context();
21
22
  }
@@ -18,6 +18,7 @@ void Init_nl_cdata(void);
18
18
  void Init_nl_processing_instruction(void);
19
19
  void Init_nl_node_set(void);
20
20
  void Init_nl_document_fragment(void);
21
+ void Init_nl_attribute(void);
21
22
  void Init_nl_xpath_context(void);
22
23
 
23
24
  void nl_raise_lexbor_error(lxb_status_t error);
@@ -145,13 +145,10 @@ module Nokolexbor
145
145
  ancestors.last.css(selector).any? { |node| node == self }
146
146
  end
147
147
 
148
- def attribute(name)
149
- return nil unless key?(name)
150
- Attribute.new(name, attr(name))
151
- end
152
-
153
148
  def attributes
154
- attrs.map { |k, v| [k, Attribute.new(k, v)] }.to_h
149
+ attribute_nodes.each_with_object({}) do |node, hash|
150
+ hash[node.name] = node
151
+ end
155
152
  end
156
153
 
157
154
  def replace(node)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Nokolexbor
4
- VERSION = '0.3.3'
4
+ VERSION = '0.3.4'
5
5
  end
data/lib/nokolexbor.rb CHANGED
@@ -26,7 +26,6 @@ require 'nokolexbor/node'
26
26
  require 'nokolexbor/document'
27
27
  require 'nokolexbor/node_set'
28
28
  require 'nokolexbor/document_fragment'
29
- require 'nokolexbor/attribute'
30
29
  require 'nokolexbor/xpath'
31
30
  require 'nokolexbor/xpath_context'
32
31
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nokolexbor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yicheng Zhou
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-01-09 00:00:00.000000000 Z
11
+ date: 2023-01-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake-compiler
@@ -78,6 +78,7 @@ files:
78
78
  - ext/nokolexbor/libxml/xpath.h
79
79
  - ext/nokolexbor/libxml/xpathInternals.h
80
80
  - ext/nokolexbor/libxml/xpointer.h
81
+ - ext/nokolexbor/nl_attribute.c
81
82
  - ext/nokolexbor/nl_cdata.c
82
83
  - ext/nokolexbor/nl_comment.c
83
84
  - ext/nokolexbor/nl_document.c
@@ -119,7 +120,6 @@ files:
119
120
  - ext/nokolexbor/xml_tree.c
120
121
  - ext/nokolexbor/xml_xpath.c
121
122
  - lib/nokolexbor.rb
122
- - lib/nokolexbor/attribute.rb
123
123
  - lib/nokolexbor/document.rb
124
124
  - lib/nokolexbor/document_fragment.rb
125
125
  - lib/nokolexbor/node.rb
@@ -1,18 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Nokolexbor
4
- class Attribute
5
- attr_accessor :name
6
- attr_accessor :value
7
-
8
- def initialize(name, value)
9
- @name = name
10
- @value = value
11
- end
12
-
13
- alias_method :text, :value
14
- alias_method :content, :value
15
- alias_method :to_s, :value
16
- alias_method :to_str, :value
17
- end
18
- end