-
Notifications
You must be signed in to change notification settings - Fork 254
/
xml_attribute.cc
154 lines (120 loc) · 4.12 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
// Copyright 2009, Squish Tech, LLC.
#include "./xml_attribute.h"
namespace libxmljs {
v8::Persistent<v8::FunctionTemplate> XmlAttribute::constructor_template;
v8::Handle<v8::Value>
XmlAttribute::New(const v8::Arguments& args) {
v8::HandleScope scope;
// was created by BUILD_NODE
if (args.Length() == 1 && args[0]->StrictEquals(v8::Null()))
return args.This();
XmlElement *element = LibXmlObj::Unwrap<XmlElement>(args[0]->ToObject());
v8::String::Utf8Value name(args[1]->ToString());
v8::String::Utf8Value value(args[2]->ToString());
xmlAttr *elem = xmlSetProp(element->xml_obj,
(const xmlChar*)*name,
(const xmlChar*)*value);
// namespace passed in
if (args.Length() == 4 && args[3]->IsObject()) {
XmlNamespace *ns = LibXmlObj::Unwrap<XmlNamespace>(args[3]->ToObject());
assert(ns);
v8::Persistent<v8::Object> js_attr =
LXJS_GET_MAYBE_BUILD(XmlAttribute, xmlAttr, elem);
XmlAttribute *attr = LibXmlObj::Unwrap<XmlAttribute>(js_attr);
attr->set_namespace(ns->xml_obj);
}
return LXJS_GET_MAYBE_BUILD(XmlAttribute, xmlAttr, elem);
}
v8::Handle<v8::Value>
XmlAttribute::Name(const v8::Arguments& args) {
v8::HandleScope scope;
XmlAttribute *attr = LibXmlObj::Unwrap<XmlAttribute>(args.This());
assert(attr);
return attr->get_name();
}
v8::Handle<v8::Value>
XmlAttribute::Value(const v8::Arguments& args) {
v8::HandleScope scope;
XmlAttribute *attr = LibXmlObj::Unwrap<XmlAttribute>(args.This());
assert(attr);
// attr.value('new value');
if (args.Length() > 0) {
attr->set_value(*v8::String::Utf8Value(args[0]));
return args.This();
}
// attr.value();
return attr->get_value();
}
v8::Handle<v8::Value>
XmlAttribute::Node(const v8::Arguments& args) {
v8::HandleScope scope;
XmlAttribute *attr = LibXmlObj::Unwrap<XmlAttribute>(args.This());
assert(attr);
return attr->get_element();
}
v8::Handle<v8::Value>
XmlAttribute::get_name() {
if (xml_obj->name)
return v8::String::New((const char*)xml_obj->name,
xmlStrlen(xml_obj->name));
return v8::Null();
}
v8::Handle<v8::Value>
XmlAttribute::get_value() {
xmlChar* value = xmlNodeGetContent(xml_obj);
if (value != NULL) {
v8::Handle<v8::String> ret_value = v8::String::New((const char*)value,
xmlStrlen(value));
xmlFree(value);
return ret_value;
}
return v8::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::Handle<v8::Value>
XmlAttribute::get_element() {
return JsObj::Unwrap<xmlNode>(xml_obj->parent);
}
void
XmlAttribute::Initialize(v8::Handle<v8::Object> target) {
v8::HandleScope scope;
v8::Local<v8::FunctionTemplate> t =
v8::FunctionTemplate::New(XmlAttribute::New);
constructor_template = v8::Persistent<v8::FunctionTemplate>::New(t);
constructor_template->Inherit(XmlNode::constructor_template);
constructor_template->InstanceTemplate()->SetInternalFieldCount(1);
LXJS_SET_PROTO_METHOD(constructor_template,
"name",
XmlAttribute::Name);
LXJS_SET_PROTO_METHOD(constructor_template,
"value",
XmlAttribute::Value);
LXJS_SET_PROTO_METHOD(constructor_template,
"node",
XmlAttribute::Node);
target->Set(v8::String::NewSymbol("Attribute"),
constructor_template->GetFunction());
}
} // namespace libxmljs