-
Notifications
You must be signed in to change notification settings - Fork 254
/
xml_attribute.cc
168 lines (132 loc) · 4.42 KB
/
xml_attribute.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright 2009, Squish Tech, LLC.
#include "xml_attribute.h"
namespace libxmljs {
Nan::Persistent<v8::FunctionTemplate> XmlAttribute::constructor_template;
NAN_METHOD(XmlAttribute::New) {
Nan::HandleScope scope;
return info.GetReturnValue().Set(info.Holder());
}
v8::Local<v8::Object>
XmlAttribute::New(xmlNode* xml_obj, const xmlChar* name, const xmlChar* value)
{
xmlAttr* attr = xmlSetProp(xml_obj, name, value);
assert(attr);
if (attr->_private) {
return static_cast<XmlNode*>(xml_obj->_private)->handle();
}
XmlAttribute* attribute = new XmlAttribute(attr);
v8::Local<v8::Object> obj = Nan::New(constructor_template)->GetFunction()->NewInstance();
attribute->Wrap(obj);
return obj;
}
v8::Local<v8::Object>
XmlAttribute::New(xmlAttr* attr)
{
assert(attr->type == XML_ATTRIBUTE_NODE);
if (attr->_private) {
return static_cast<XmlNode*>(attr->_private)->handle();
}
XmlAttribute* attribute = new XmlAttribute(attr);
v8::Local<v8::Object> obj = Nan::New(constructor_template)->GetFunction()->NewInstance();
attribute->Wrap(obj);
return obj;
}
NAN_METHOD(XmlAttribute::Name) {
Nan::HandleScope scope;
XmlAttribute *attr = Nan::ObjectWrap::Unwrap<XmlAttribute>(info.Holder());
assert(attr);
return info.GetReturnValue().Set(attr->get_name());
}
NAN_METHOD(XmlAttribute::Value) {
Nan::HandleScope scope;
XmlAttribute *attr = Nan::ObjectWrap::Unwrap<XmlAttribute>(info.Holder());
assert(attr);
// attr.value('new value');
if (info.Length() > 0) {
attr->set_value(*v8::String::Utf8Value(info[0]));
return info.GetReturnValue().Set(info.Holder());
}
// attr.value();
return info.GetReturnValue().Set(attr->get_value());
}
NAN_METHOD(XmlAttribute::Node) {
Nan::HandleScope scope;
XmlAttribute *attr = Nan::ObjectWrap::Unwrap<XmlAttribute>(info.Holder());
assert(attr);
return info.GetReturnValue().Set(attr->get_element());
}
NAN_METHOD(XmlAttribute::Namespace) {
Nan::HandleScope scope;
XmlAttribute *attr = Nan::ObjectWrap::Unwrap<XmlAttribute>(info.Holder());
assert(attr);
return info.GetReturnValue().Set(attr->get_namespace());
}
v8::Local<v8::Value>
XmlAttribute::get_name() {
if (xml_obj->name)
return Nan::New<v8::String>((const char*)xml_obj->name,
xmlStrlen(xml_obj->name)).ToLocalChecked();
return Nan::Null();
}
v8::Local<v8::Value>
XmlAttribute::get_value() {
Nan::EscapableHandleScope scope;
xmlChar* value = xmlNodeGetContent(xml_obj);
if (value != NULL) {
v8::Local<v8::String> ret_value = Nan::New<v8::String>((const char*)value,
xmlStrlen(value)).ToLocalChecked();
xmlFree(value);
return scope.Escape(ret_value);
}
return Nan::Null();
}
void
XmlAttribute::set_value(const char* value) {
if (xml_obj->children)
xmlFreeNodeList(xml_obj->children);
xml_obj->children = xml_obj->last = NULL;
if (value) {
xmlChar *buffer;
xmlNode *tmp;
// Encode our content
buffer = xmlEncodeEntitiesReentrant(xml_obj->doc, (const xmlChar*)value);
xml_obj->children = xmlStringGetNodeList(xml_obj->doc, buffer);
xml_obj->last = NULL;
tmp = xml_obj->children;
// Loop through the children
for (tmp = xml_obj->children; tmp; tmp = tmp->next) {
tmp->parent = reinterpret_cast<xmlNode *>(xml_obj);
tmp->doc = xml_obj->doc;
if (tmp->next == NULL) xml_obj->last = tmp;
}
// Free up memory
xmlFree(buffer);
}
}
v8::Local<v8::Value>
XmlAttribute::get_element() {
return XmlElement::New(xml_obj->parent);
}
v8::Local<v8::Value>
XmlAttribute::get_namespace() {
if (!xml_obj->ns) {
return Nan::Null();
}
return XmlNamespace::New(xml_obj->ns);
}
void
XmlAttribute::Initialize(v8::Handle<v8::Object> target) {
Nan::HandleScope scope;
v8::Local<v8::FunctionTemplate> tmpl =
Nan::New<v8::FunctionTemplate>(XmlAttribute::New);
constructor_template.Reset( tmpl);
tmpl->Inherit(Nan::New(XmlNode::constructor_template));
tmpl->InstanceTemplate()->SetInternalFieldCount(1);
Nan::SetPrototypeMethod(tmpl, "name", XmlAttribute::Name);
Nan::SetPrototypeMethod(tmpl, "value", XmlAttribute::Value);
Nan::SetPrototypeMethod(tmpl, "node", XmlAttribute::Node);
Nan::SetPrototypeMethod(tmpl, "namespace", XmlAttribute::Namespace);
Nan::Set(target, Nan::New<v8::String>("Attribute").ToLocalChecked(),
tmpl->GetFunction());
}
} // namespace libxmljs