nokolexbor 0.3.3 → 0.3.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/ext/nokolexbor/nl_attribute.c +155 -0
- data/ext/nokolexbor/nl_node.c +66 -3
- data/ext/nokolexbor/nokolexbor.c +1 -0
- data/ext/nokolexbor/nokolexbor.h +1 -0
- data/lib/nokolexbor/node.rb +3 -6
- data/lib/nokolexbor/version.rb +1 -1
- data/lib/nokolexbor.rb +0 -1
- metadata +3 -3
- data/lib/nokolexbor/attribute.rb +0 -18
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 53cb1f54e475a0b10fc7773259866fe675e761d0ca204c5f685f4ed8dd60294a
|
4
|
+
data.tar.gz: 0e697320eae75b41e16ac431c6c679159507013b5e3a9858959377eb969355b2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
+
}
|
data/ext/nokolexbor/nl_node.c
CHANGED
@@ -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
|
-
|
35
|
-
|
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
|
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);
|
data/ext/nokolexbor/nokolexbor.c
CHANGED
data/ext/nokolexbor/nokolexbor.h
CHANGED
@@ -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);
|
data/lib/nokolexbor/node.rb
CHANGED
@@ -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
|
-
|
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)
|
data/lib/nokolexbor/version.rb
CHANGED
data/lib/nokolexbor.rb
CHANGED
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.
|
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-
|
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
|
data/lib/nokolexbor/attribute.rb
DELETED
@@ -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
|