pocxxeci 0.30.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of pocxxeci might be problematic. Click here for more details.

Files changed (160) hide show
  1. package/LICENSE +19 -0
  2. package/Makefile +18 -0
  3. package/README.md +52 -0
  4. package/binding.gyp +81 -0
  5. package/index.d.ts +273 -0
  6. package/index.js +45 -0
  7. package/lib/bindings.js +1 -0
  8. package/lib/document.js +122 -0
  9. package/lib/element.js +82 -0
  10. package/lib/sax_parser.js +38 -0
  11. package/package.json +70 -0
  12. package/src/html_document.cc +7 -0
  13. package/src/html_document.h +18 -0
  14. package/src/libxmljs.cc +252 -0
  15. package/src/libxmljs.h +53 -0
  16. package/src/xml_attribute.cc +173 -0
  17. package/src/xml_attribute.h +40 -0
  18. package/src/xml_comment.cc +117 -0
  19. package/src/xml_comment.h +30 -0
  20. package/src/xml_document.cc +810 -0
  21. package/src/xml_document.h +67 -0
  22. package/src/xml_element.cc +565 -0
  23. package/src/xml_element.h +61 -0
  24. package/src/xml_namespace.cc +158 -0
  25. package/src/xml_namespace.h +39 -0
  26. package/src/xml_node.cc +761 -0
  27. package/src/xml_node.h +73 -0
  28. package/src/xml_pi.cc +161 -0
  29. package/src/xml_pi.h +34 -0
  30. package/src/xml_sax_parser.cc +424 -0
  31. package/src/xml_sax_parser.h +73 -0
  32. package/src/xml_syntax_error.cc +66 -0
  33. package/src/xml_syntax_error.h +25 -0
  34. package/src/xml_text.cc +320 -0
  35. package/src/xml_text.h +48 -0
  36. package/src/xml_textwriter.cc +315 -0
  37. package/src/xml_textwriter.h +62 -0
  38. package/src/xml_xpath_context.cc +70 -0
  39. package/src/xml_xpath_context.h +23 -0
  40. package/vendor/libxml/Copyright +23 -0
  41. package/vendor/libxml/DOCBparser.c +305 -0
  42. package/vendor/libxml/HTMLparser.c +7287 -0
  43. package/vendor/libxml/HTMLtree.c +1200 -0
  44. package/vendor/libxml/Makefile +2983 -0
  45. package/vendor/libxml/SAX.c +180 -0
  46. package/vendor/libxml/SAX2.c +3036 -0
  47. package/vendor/libxml/buf.c +1351 -0
  48. package/vendor/libxml/buf.h +72 -0
  49. package/vendor/libxml/c14n.c +2234 -0
  50. package/vendor/libxml/catalog.c +3828 -0
  51. package/vendor/libxml/chvalid.c +336 -0
  52. package/vendor/libxml/config.h +294 -0
  53. package/vendor/libxml/config.h.gch +0 -0
  54. package/vendor/libxml/debugXML.c +3423 -0
  55. package/vendor/libxml/dict.c +1298 -0
  56. package/vendor/libxml/elfgcchack.h +17818 -0
  57. package/vendor/libxml/enc.h +32 -0
  58. package/vendor/libxml/encoding.c +3975 -0
  59. package/vendor/libxml/entities.c +1163 -0
  60. package/vendor/libxml/error.c +998 -0
  61. package/vendor/libxml/globals.c +1126 -0
  62. package/vendor/libxml/hash.c +1146 -0
  63. package/vendor/libxml/include/libxml/DOCBparser.h +96 -0
  64. package/vendor/libxml/include/libxml/HTMLparser.h +306 -0
  65. package/vendor/libxml/include/libxml/HTMLtree.h +147 -0
  66. package/vendor/libxml/include/libxml/Makefile +725 -0
  67. package/vendor/libxml/include/libxml/Makefile.am +54 -0
  68. package/vendor/libxml/include/libxml/Makefile.in +725 -0
  69. package/vendor/libxml/include/libxml/SAX.h +173 -0
  70. package/vendor/libxml/include/libxml/SAX2.h +178 -0
  71. package/vendor/libxml/include/libxml/c14n.h +128 -0
  72. package/vendor/libxml/include/libxml/catalog.h +182 -0
  73. package/vendor/libxml/include/libxml/chvalid.h +230 -0
  74. package/vendor/libxml/include/libxml/debugXML.h +217 -0
  75. package/vendor/libxml/include/libxml/dict.h +79 -0
  76. package/vendor/libxml/include/libxml/encoding.h +245 -0
  77. package/vendor/libxml/include/libxml/entities.h +151 -0
  78. package/vendor/libxml/include/libxml/globals.h +508 -0
  79. package/vendor/libxml/include/libxml/hash.h +236 -0
  80. package/vendor/libxml/include/libxml/list.h +137 -0
  81. package/vendor/libxml/include/libxml/nanoftp.h +163 -0
  82. package/vendor/libxml/include/libxml/nanohttp.h +81 -0
  83. package/vendor/libxml/include/libxml/parser.h +1243 -0
  84. package/vendor/libxml/include/libxml/parserInternals.h +644 -0
  85. package/vendor/libxml/include/libxml/pattern.h +100 -0
  86. package/vendor/libxml/include/libxml/relaxng.h +217 -0
  87. package/vendor/libxml/include/libxml/schemasInternals.h +958 -0
  88. package/vendor/libxml/include/libxml/schematron.h +142 -0
  89. package/vendor/libxml/include/libxml/threads.h +89 -0
  90. package/vendor/libxml/include/libxml/tree.h +1311 -0
  91. package/vendor/libxml/include/libxml/uri.h +94 -0
  92. package/vendor/libxml/include/libxml/valid.h +458 -0
  93. package/vendor/libxml/include/libxml/xinclude.h +129 -0
  94. package/vendor/libxml/include/libxml/xlink.h +189 -0
  95. package/vendor/libxml/include/libxml/xmlIO.h +368 -0
  96. package/vendor/libxml/include/libxml/xmlautomata.h +146 -0
  97. package/vendor/libxml/include/libxml/xmlerror.h +945 -0
  98. package/vendor/libxml/include/libxml/xmlexports.h +77 -0
  99. package/vendor/libxml/include/libxml/xmlmemory.h +224 -0
  100. package/vendor/libxml/include/libxml/xmlmodule.h +57 -0
  101. package/vendor/libxml/include/libxml/xmlreader.h +428 -0
  102. package/vendor/libxml/include/libxml/xmlregexp.h +222 -0
  103. package/vendor/libxml/include/libxml/xmlsave.h +88 -0
  104. package/vendor/libxml/include/libxml/xmlschemas.h +246 -0
  105. package/vendor/libxml/include/libxml/xmlschemastypes.h +151 -0
  106. package/vendor/libxml/include/libxml/xmlstring.h +140 -0
  107. package/vendor/libxml/include/libxml/xmlunicode.h +202 -0
  108. package/vendor/libxml/include/libxml/xmlversion.h +484 -0
  109. package/vendor/libxml/include/libxml/xmlwin32version.h +239 -0
  110. package/vendor/libxml/include/libxml/xmlwriter.h +488 -0
  111. package/vendor/libxml/include/libxml/xpath.h +564 -0
  112. package/vendor/libxml/include/libxml/xpathInternals.h +632 -0
  113. package/vendor/libxml/include/libxml/xpointer.h +114 -0
  114. package/vendor/libxml/include/win32config.h +122 -0
  115. package/vendor/libxml/include/wsockcompat.h +54 -0
  116. package/vendor/libxml/legacy.c +1343 -0
  117. package/vendor/libxml/libxml.h +134 -0
  118. package/vendor/libxml/list.c +779 -0
  119. package/vendor/libxml/nanoftp.c +2118 -0
  120. package/vendor/libxml/nanohttp.c +1899 -0
  121. package/vendor/libxml/parser.c +15553 -0
  122. package/vendor/libxml/parserInternals.c +2164 -0
  123. package/vendor/libxml/pattern.c +2621 -0
  124. package/vendor/libxml/relaxng.c +11101 -0
  125. package/vendor/libxml/rngparser.c +1595 -0
  126. package/vendor/libxml/runsuite.c +1157 -0
  127. package/vendor/libxml/save.h +36 -0
  128. package/vendor/libxml/schematron.c +1787 -0
  129. package/vendor/libxml/threads.c +1049 -0
  130. package/vendor/libxml/timsort.h +601 -0
  131. package/vendor/libxml/tree.c +10183 -0
  132. package/vendor/libxml/trio.c +6895 -0
  133. package/vendor/libxml/trio.h +230 -0
  134. package/vendor/libxml/triodef.h +228 -0
  135. package/vendor/libxml/trionan.c +914 -0
  136. package/vendor/libxml/trionan.h +84 -0
  137. package/vendor/libxml/triop.h +150 -0
  138. package/vendor/libxml/triostr.c +2112 -0
  139. package/vendor/libxml/triostr.h +144 -0
  140. package/vendor/libxml/uri.c +2561 -0
  141. package/vendor/libxml/valid.c +7138 -0
  142. package/vendor/libxml/xinclude.c +2657 -0
  143. package/vendor/libxml/xlink.c +183 -0
  144. package/vendor/libxml/xmlIO.c +4135 -0
  145. package/vendor/libxml/xmlcatalog.c +624 -0
  146. package/vendor/libxml/xmllint.c +3796 -0
  147. package/vendor/libxml/xmlmemory.c +1163 -0
  148. package/vendor/libxml/xmlmodule.c +468 -0
  149. package/vendor/libxml/xmlreader.c +6033 -0
  150. package/vendor/libxml/xmlregexp.c +8271 -0
  151. package/vendor/libxml/xmlsave.c +2735 -0
  152. package/vendor/libxml/xmlschemas.c +29173 -0
  153. package/vendor/libxml/xmlschemastypes.c +6276 -0
  154. package/vendor/libxml/xmlstring.c +1050 -0
  155. package/vendor/libxml/xmlunicode.c +3179 -0
  156. package/vendor/libxml/xmlwriter.c +4738 -0
  157. package/vendor/libxml/xpath.c +14734 -0
  158. package/vendor/libxml/xpointer.c +2969 -0
  159. package/vendor/libxml/xzlib.c +815 -0
  160. package/vendor/libxml/xzlib.h +19 -0
@@ -0,0 +1,67 @@
1
+ // Copyright 2009, Squish Tech, LLC.
2
+ #ifndef SRC_XML_DOCUMENT_H_
3
+ #define SRC_XML_DOCUMENT_H_
4
+
5
+ #include <libxml/tree.h>
6
+
7
+ #include "libxmljs.h"
8
+
9
+ namespace libxmljs {
10
+
11
+ class XmlDocument : public Nan::ObjectWrap {
12
+
13
+ public:
14
+ // used to create new instanced of a document handle
15
+ static Nan::Persistent<v8::FunctionTemplate> constructor_template;
16
+
17
+ // TODO make private with accessor
18
+ xmlDoc *xml_obj;
19
+
20
+ virtual ~XmlDocument();
21
+
22
+ // setup the document handle bindings and internal constructor
23
+ static void Initialize(v8::Local<v8::Object> target);
24
+
25
+ // create a new document handle initialized with the
26
+ // given xmlDoc object, intended for use in c++ space
27
+ static v8::Local<v8::Object> New(xmlDoc *doc);
28
+
29
+ // publicly expose ref functions
30
+ using Nan::ObjectWrap::Ref;
31
+ using Nan::ObjectWrap::Unref;
32
+
33
+ // expose ObjectWrap::refs_ (for testing)
34
+ int refs() { return refs_; }
35
+
36
+ protected:
37
+ // initialize a new document
38
+ explicit XmlDocument(xmlDoc *doc);
39
+
40
+ static NAN_METHOD(New);
41
+ static NAN_METHOD(FromHtml);
42
+ static NAN_METHOD(FromXml);
43
+ static NAN_METHOD(SetDtd);
44
+
45
+ // document handle methods
46
+ static NAN_METHOD(Root);
47
+ static NAN_METHOD(GetDtd);
48
+ static NAN_METHOD(Encoding);
49
+ static NAN_METHOD(Version);
50
+ static NAN_METHOD(Doc);
51
+ static NAN_METHOD(Errors);
52
+ static NAN_METHOD(ToString);
53
+ static NAN_METHOD(Validate);
54
+ static NAN_METHOD(RngValidate);
55
+ static NAN_METHOD(SchematronValidate);
56
+ static NAN_METHOD(type);
57
+
58
+ // Static member variables
59
+ static const int DEFAULT_PARSING_OPTS;
60
+ static const int EXCLUDE_IMPLIED_ELEMENTS;
61
+
62
+ void setEncoding(const char *encoding);
63
+ };
64
+
65
+ } // namespace libxmljs
66
+
67
+ #endif // SRC_XML_DOCUMENT_H_
@@ -0,0 +1,565 @@
1
+ // Copyright 2009, Squish Tech, LLC.
2
+
3
+ #include <node.h>
4
+
5
+ #include <cstring>
6
+
7
+ #include "libxmljs.h"
8
+
9
+ #include "xml_attribute.h"
10
+ #include "xml_document.h"
11
+ #include "xml_element.h"
12
+ #include "xml_xpath_context.h"
13
+
14
+ using namespace v8;
15
+
16
+ namespace libxmljs {
17
+
18
+ Nan::Persistent<FunctionTemplate> XmlElement::constructor_template;
19
+
20
+ // doc, name, content
21
+ NAN_METHOD(XmlElement::New) {
22
+ Nan::HandleScope scope;
23
+
24
+ // if we were created for an existing xml node, then we don't need
25
+ // to create a new node on the document
26
+ if (info.Length() == 0) {
27
+ return info.GetReturnValue().Set(info.Holder());
28
+ }
29
+
30
+ XmlDocument *document = Nan::ObjectWrap::Unwrap<XmlDocument>(
31
+ Nan::To<Object>(info[0]).ToLocalChecked());
32
+ assert(document);
33
+
34
+ Nan::Utf8String name(info[1]);
35
+
36
+ Local<Value> contentOpt;
37
+ if (info[2]->IsString()) {
38
+ contentOpt = info[2];
39
+ }
40
+ Nan::Utf8String contentRaw(contentOpt);
41
+ const char *content = (contentRaw.length()) ? *contentRaw : NULL;
42
+
43
+ xmlChar *encodedContent =
44
+ content
45
+ ? xmlEncodeSpecialChars(document->xml_obj, (const xmlChar *)content)
46
+ : NULL;
47
+ xmlNode *elem = xmlNewDocNode(document->xml_obj, NULL, (const xmlChar *)*name,
48
+ encodedContent);
49
+ if (encodedContent)
50
+ xmlFree(encodedContent);
51
+
52
+ XmlElement *element = new XmlElement(elem);
53
+ elem->_private = element;
54
+ element->Wrap(info.Holder());
55
+
56
+ // this prevents the document from going away
57
+ Nan::Set(info.Holder(), Nan::New<String>("document").ToLocalChecked(),
58
+ info[0])
59
+ .Check();
60
+
61
+ return info.GetReturnValue().Set(info.Holder());
62
+ }
63
+
64
+ NAN_METHOD(XmlElement::Name) {
65
+ Nan::HandleScope scope;
66
+ XmlElement *element = Nan::ObjectWrap::Unwrap<XmlElement>(info.Holder());
67
+ assert(element);
68
+
69
+ if (info.Length() == 0)
70
+ return info.GetReturnValue().Set(element->get_name());
71
+
72
+ Nan::Utf8String name(Nan::To<String>(info[0]).ToLocalChecked());
73
+ element->set_name(*name);
74
+ return info.GetReturnValue().Set(info.Holder());
75
+ }
76
+
77
+ NAN_METHOD(XmlElement::Attr) {
78
+ Nan::HandleScope scope;
79
+ XmlElement *element = Nan::ObjectWrap::Unwrap<XmlElement>(info.Holder());
80
+ assert(element);
81
+
82
+ // getter
83
+ if (info.Length() == 1) {
84
+ Nan::Utf8String name(info[0]);
85
+ return info.GetReturnValue().Set(element->get_attr(*name));
86
+ }
87
+
88
+ // setter
89
+ Nan::Utf8String name(info[0]);
90
+ Nan::Utf8String value(info[1]);
91
+ element->set_attr(*name, *value);
92
+
93
+ return info.GetReturnValue().Set(info.Holder());
94
+ }
95
+
96
+ NAN_METHOD(XmlElement::Attrs) {
97
+ Nan::HandleScope scope;
98
+ XmlElement *element = Nan::ObjectWrap::Unwrap<XmlElement>(info.Holder());
99
+ assert(element);
100
+
101
+ return info.GetReturnValue().Set(element->get_attrs());
102
+ }
103
+
104
+ NAN_METHOD(XmlElement::AddChild) {
105
+ XmlElement *element = Nan::ObjectWrap::Unwrap<XmlElement>(info.Holder());
106
+ assert(element);
107
+
108
+ XmlNode *child = Nan::ObjectWrap::Unwrap<XmlNode>(
109
+ Nan::To<Object>(info[0]).ToLocalChecked());
110
+ assert(child);
111
+
112
+ xmlNode *imported_child = element->import_node(child->xml_obj);
113
+ if (imported_child == NULL) {
114
+ return Nan::ThrowError(
115
+ "Could not add child. Failed to copy node to new Document.");
116
+ }
117
+
118
+ bool will_merge = element->child_will_merge(imported_child);
119
+ if ((child->xml_obj == imported_child) && will_merge) {
120
+ // merged child will be free, so ensure it is a copy
121
+ imported_child = xmlCopyNode(imported_child, 0);
122
+ }
123
+
124
+ element->add_child(imported_child);
125
+
126
+ if (!will_merge && (imported_child->_private != NULL)) {
127
+ static_cast<XmlNode *>(imported_child->_private)->ref_wrapped_ancestor();
128
+ }
129
+
130
+ return info.GetReturnValue().Set(info.Holder());
131
+ }
132
+
133
+ NAN_METHOD(XmlElement::AddCData) {
134
+ Nan::HandleScope scope;
135
+ XmlElement *element = Nan::ObjectWrap::Unwrap<XmlElement>(info.Holder());
136
+ assert(element);
137
+
138
+ Local<Value> contentOpt;
139
+ if (info[0]->IsString()) {
140
+ contentOpt = info[0];
141
+ }
142
+ Nan::Utf8String contentRaw(contentOpt);
143
+ const char *content = (contentRaw.length()) ? *contentRaw : NULL;
144
+
145
+ xmlNode *elem =
146
+ xmlNewCDataBlock(element->xml_obj->doc, (const xmlChar *)content,
147
+ xmlStrlen((const xmlChar *)content));
148
+
149
+ element->add_cdata(elem);
150
+ return info.GetReturnValue().Set(info.Holder());
151
+ }
152
+
153
+ NAN_METHOD(XmlElement::Find) {
154
+ Nan::HandleScope scope;
155
+ XmlElement *element = Nan::ObjectWrap::Unwrap<XmlElement>(info.Holder());
156
+ assert(element);
157
+
158
+ Nan::Utf8String xpath(info[0]);
159
+
160
+ XmlXpathContext ctxt(element->xml_obj);
161
+
162
+ if (info.Length() == 2) {
163
+ if (info[1]->IsString()) {
164
+ Nan::Utf8String uri(info[1]);
165
+ ctxt.register_ns((const xmlChar *)"xmlns", (const xmlChar *)*uri);
166
+ } else if (info[1]->IsObject()) {
167
+ Local<Object> namespaces = Nan::To<Object>(info[1]).ToLocalChecked();
168
+ Local<Array> properties =
169
+ Nan::GetPropertyNames(namespaces).ToLocalChecked();
170
+ for (unsigned int i = 0; i < properties->Length(); i++) {
171
+ Local<String> prop_name =
172
+ Nan::To<String>(
173
+ Nan::Get(properties, Nan::New<Number>(i)).ToLocalChecked())
174
+ .ToLocalChecked();
175
+ Nan::Utf8String prefix(prop_name);
176
+ Nan::Utf8String uri(Nan::Get(namespaces, prop_name).ToLocalChecked());
177
+ ctxt.register_ns((const xmlChar *)*prefix, (const xmlChar *)*uri);
178
+ }
179
+ }
180
+ }
181
+
182
+ return info.GetReturnValue().Set(ctxt.evaluate((const xmlChar *)*xpath));
183
+ }
184
+
185
+ NAN_METHOD(XmlElement::NextElement) {
186
+ Nan::HandleScope scope;
187
+ XmlElement *element = Nan::ObjectWrap::Unwrap<XmlElement>(info.Holder());
188
+ assert(element);
189
+
190
+ return info.GetReturnValue().Set(element->get_next_element());
191
+ }
192
+
193
+ NAN_METHOD(XmlElement::PrevElement) {
194
+ Nan::HandleScope scope;
195
+ XmlElement *element = Nan::ObjectWrap::Unwrap<XmlElement>(info.Holder());
196
+ assert(element);
197
+
198
+ return info.GetReturnValue().Set(element->get_prev_element());
199
+ }
200
+
201
+ NAN_METHOD(XmlElement::Text) {
202
+ Nan::HandleScope scope;
203
+ XmlElement *element = Nan::ObjectWrap::Unwrap<XmlElement>(info.Holder());
204
+ assert(element);
205
+
206
+ if (info.Length() == 0) {
207
+ return info.GetReturnValue().Set(element->get_content());
208
+ } else {
209
+ element->set_content(*Nan::Utf8String(info[0]));
210
+ }
211
+
212
+ return info.GetReturnValue().Set(info.Holder());
213
+ }
214
+
215
+ NAN_METHOD(XmlElement::Child) {
216
+ Nan::HandleScope scope;
217
+ XmlElement *element = Nan::ObjectWrap::Unwrap<XmlElement>(info.Holder());
218
+ assert(element);
219
+
220
+ if (info.Length() != 1 || !info[0]->IsInt32()) {
221
+ return Nan::ThrowError("Bad argument: must provide #child() with a number");
222
+ }
223
+
224
+ const int32_t idx = Nan::To<int32_t>(info[0]).ToChecked();
225
+ return info.GetReturnValue().Set(element->get_child(idx));
226
+ }
227
+
228
+ NAN_METHOD(XmlElement::ChildNodes) {
229
+ Nan::HandleScope scope;
230
+ XmlElement *element = Nan::ObjectWrap::Unwrap<XmlElement>(info.Holder());
231
+ assert(element);
232
+
233
+ if (info[0]->IsInt32())
234
+ return info.GetReturnValue().Set(
235
+ element->get_child(Nan::To<int32_t>(info[0]).ToChecked()));
236
+
237
+ return info.GetReturnValue().Set(element->get_child_nodes());
238
+ }
239
+
240
+ NAN_METHOD(XmlElement::Path) {
241
+ Nan::HandleScope scope;
242
+ XmlElement *element = Nan::ObjectWrap::Unwrap<XmlElement>(info.Holder());
243
+ assert(element);
244
+
245
+ return info.GetReturnValue().Set(element->get_path());
246
+ }
247
+
248
+ NAN_METHOD(XmlElement::AddPrevSibling) {
249
+ XmlElement *element = Nan::ObjectWrap::Unwrap<XmlElement>(info.Holder());
250
+ assert(element);
251
+
252
+ XmlNode *new_sibling = Nan::ObjectWrap::Unwrap<XmlNode>(
253
+ Nan::To<Object>(info[0]).ToLocalChecked());
254
+ assert(new_sibling);
255
+
256
+ xmlNode *imported_sibling = element->import_node(new_sibling->xml_obj);
257
+ if (imported_sibling == NULL) {
258
+ return Nan::ThrowError(
259
+ "Could not add sibling. Failed to copy node to new Document.");
260
+ }
261
+
262
+ element->add_prev_sibling(imported_sibling);
263
+
264
+ if (imported_sibling->_private != NULL) {
265
+ static_cast<XmlNode *>(imported_sibling->_private)->ref_wrapped_ancestor();
266
+ }
267
+
268
+ return info.GetReturnValue().Set(info[0]);
269
+ }
270
+
271
+ NAN_METHOD(XmlElement::AddNextSibling) {
272
+ XmlElement *element = Nan::ObjectWrap::Unwrap<XmlElement>(info.Holder());
273
+ assert(element);
274
+
275
+ XmlNode *new_sibling = Nan::ObjectWrap::Unwrap<XmlNode>(
276
+ Nan::To<Object>(info[0]).ToLocalChecked());
277
+ assert(new_sibling);
278
+
279
+ xmlNode *imported_sibling = element->import_node(new_sibling->xml_obj);
280
+ if (imported_sibling == NULL) {
281
+ return Nan::ThrowError(
282
+ "Could not add sibling. Failed to copy node to new Document.");
283
+ }
284
+
285
+ element->add_next_sibling(imported_sibling);
286
+
287
+ if (imported_sibling->_private != NULL) {
288
+ static_cast<XmlNode *>(imported_sibling->_private)->ref_wrapped_ancestor();
289
+ }
290
+
291
+ return info.GetReturnValue().Set(info[0]);
292
+ }
293
+
294
+ NAN_METHOD(XmlElement::Replace) {
295
+ XmlElement *element = Nan::ObjectWrap::Unwrap<XmlElement>(info.Holder());
296
+ assert(element);
297
+
298
+ if (info[0]->IsString()) {
299
+ element->replace_text(*Nan::Utf8String(info[0]));
300
+ } else {
301
+ XmlElement *new_sibling = Nan::ObjectWrap::Unwrap<XmlElement>(
302
+ Nan::To<Object>(info[0]).ToLocalChecked());
303
+ assert(new_sibling);
304
+
305
+ xmlNode *imported_sibling = element->import_node(new_sibling->xml_obj);
306
+ if (imported_sibling == NULL) {
307
+ return Nan::ThrowError(
308
+ "Could not replace. Failed to copy node to new Document.");
309
+ }
310
+ element->replace_element(imported_sibling);
311
+ }
312
+
313
+ return info.GetReturnValue().Set(info[0]);
314
+ }
315
+
316
+ void XmlElement::set_name(const char *name) {
317
+ xmlNodeSetName(xml_obj, (const xmlChar *)name);
318
+ }
319
+
320
+ Local<Value> XmlElement::get_name() {
321
+ Nan::EscapableHandleScope scope;
322
+ if (xml_obj->name)
323
+ return scope.Escape(
324
+ Nan::New<String>((const char *)xml_obj->name).ToLocalChecked());
325
+ else
326
+ return scope.Escape(Nan::Undefined());
327
+ }
328
+
329
+ // TODO(sprsquish) make these work with namespaces
330
+ Local<Value> XmlElement::get_attr(const char *name) {
331
+ Nan::EscapableHandleScope scope;
332
+ xmlAttr *attr = xmlHasProp(xml_obj, (const xmlChar *)name);
333
+
334
+ // why do we need a reference to the element here?
335
+ if (attr) {
336
+ return scope.Escape(XmlAttribute::New(attr));
337
+ }
338
+
339
+ return scope.Escape(Nan::Null());
340
+ }
341
+
342
+ // TODO(sprsquish) make these work with namespaces
343
+ void XmlElement::set_attr(const char *name, const char *value) {
344
+ Nan::HandleScope scope;
345
+ XmlAttribute::New(xml_obj, (const xmlChar *)name, (const xmlChar *)value);
346
+ }
347
+
348
+ Local<Value> XmlElement::get_attrs() {
349
+ Nan::EscapableHandleScope scope;
350
+ xmlAttr *attr = xml_obj->properties;
351
+
352
+ if (!attr)
353
+ return scope.Escape(Nan::New<Array>(0));
354
+
355
+ Local<Array> attributes = Nan::New<Array>();
356
+ Local<Function> push = Local<Function>::Cast(
357
+ Nan::Get(attributes, Nan::New<String>("push").ToLocalChecked())
358
+ .ToLocalChecked());
359
+ Local<Value> argv[1];
360
+ do {
361
+ argv[0] = XmlAttribute::New(attr);
362
+ Nan::Call(push, attributes, 1, argv);
363
+ } while ((attr = attr->next));
364
+
365
+ return scope.Escape(attributes);
366
+ }
367
+
368
+ void XmlElement::add_cdata(xmlNode *cdata) { xmlAddChild(xml_obj, cdata); }
369
+
370
+ Local<Value> XmlElement::get_child(int32_t idx) {
371
+ Nan::EscapableHandleScope scope;
372
+ xmlNode *child = xml_obj->children;
373
+
374
+ int32_t i = 0;
375
+ while (child && i < idx) {
376
+ child = child->next;
377
+ ++i;
378
+ }
379
+
380
+ if (!child)
381
+ return scope.Escape(Nan::Null());
382
+
383
+ return scope.Escape(XmlNode::New(child));
384
+ }
385
+
386
+ Local<Value> XmlElement::get_child_nodes() {
387
+ Nan::EscapableHandleScope scope;
388
+
389
+ xmlNode *child = xml_obj->children;
390
+ if (!child)
391
+ return scope.Escape(Nan::New<Array>(0));
392
+
393
+ uint32_t len = 0;
394
+ do {
395
+ ++len;
396
+ } while ((child = child->next));
397
+
398
+ Local<Array> children = Nan::New<Array>(len);
399
+ child = xml_obj->children;
400
+
401
+ uint32_t i = 0;
402
+ do {
403
+ Nan::Set(children, i, XmlNode::New(child));
404
+ } while ((child = child->next) && ++i < len);
405
+
406
+ return scope.Escape(children);
407
+ }
408
+
409
+ Local<Value> XmlElement::get_path() {
410
+ Nan::EscapableHandleScope scope;
411
+ xmlChar *path = xmlGetNodePath(xml_obj);
412
+ const char *return_path = path ? reinterpret_cast<char *>(path) : "";
413
+ int str_len = xmlStrlen((const xmlChar *)return_path);
414
+ Local<String> js_obj =
415
+ Nan::New<String>(return_path, str_len).ToLocalChecked();
416
+ xmlFree(path);
417
+ return scope.Escape(js_obj);
418
+ }
419
+
420
+ void XmlElement::unlink_children() {
421
+ xmlNode *cur = xml_obj->children;
422
+ while (cur != NULL) {
423
+ xmlNode *next = cur->next;
424
+ if (cur->_private != NULL) {
425
+ static_cast<XmlNode *>(cur->_private)->unref_wrapped_ancestor();
426
+ }
427
+ xmlUnlinkNode(cur);
428
+ cur = next;
429
+ }
430
+ }
431
+
432
+ void XmlElement::set_content(const char *content) {
433
+ xmlChar *encoded =
434
+ xmlEncodeSpecialChars(xml_obj->doc, (const xmlChar *)content);
435
+ this->unlink_children();
436
+ xmlNodeSetContent(xml_obj, encoded);
437
+ xmlFree(encoded);
438
+ }
439
+
440
+ Local<Value> XmlElement::get_content() {
441
+ Nan::EscapableHandleScope scope;
442
+ xmlChar *content = xmlNodeGetContent(xml_obj);
443
+ if (content) {
444
+ Local<String> ret_content =
445
+ Nan::New<String>((const char *)content).ToLocalChecked();
446
+ xmlFree(content);
447
+ return scope.Escape(ret_content);
448
+ }
449
+
450
+ return scope.Escape(Nan::New<String>("").ToLocalChecked());
451
+ }
452
+
453
+ Local<Value> XmlElement::get_next_element() {
454
+ Nan::EscapableHandleScope scope;
455
+
456
+ xmlNode *sibling = xml_obj->next;
457
+ if (!sibling)
458
+ return scope.Escape(Nan::Null());
459
+
460
+ while (sibling && sibling->type != XML_ELEMENT_NODE)
461
+ sibling = sibling->next;
462
+
463
+ if (sibling) {
464
+ return scope.Escape(XmlElement::New(sibling));
465
+ }
466
+
467
+ return scope.Escape(Nan::Null());
468
+ }
469
+
470
+ Local<Value> XmlElement::get_prev_element() {
471
+ Nan::EscapableHandleScope scope;
472
+
473
+ xmlNode *sibling = xml_obj->prev;
474
+ if (!sibling)
475
+ return scope.Escape(Nan::Null());
476
+
477
+ while (sibling && sibling->type != XML_ELEMENT_NODE) {
478
+ sibling = sibling->prev;
479
+ }
480
+
481
+ if (sibling) {
482
+ return scope.Escape(XmlElement::New(sibling));
483
+ }
484
+
485
+ return scope.Escape(Nan::Null());
486
+ }
487
+
488
+ Local<Object> XmlElement::New(xmlNode *node) {
489
+ Nan::EscapableHandleScope scope;
490
+ if (node->_private) {
491
+ return scope.Escape(static_cast<XmlNode *>(node->_private)->handle());
492
+ }
493
+
494
+ XmlElement *element = new XmlElement(node);
495
+ Local<Object> obj =
496
+ Nan::NewInstance(
497
+ Nan::GetFunction(Nan::New(constructor_template)).ToLocalChecked())
498
+ .ToLocalChecked();
499
+ element->Wrap(obj);
500
+ return scope.Escape(obj);
501
+ }
502
+
503
+ XmlElement::XmlElement(xmlNode *node) : XmlNode(node) {}
504
+
505
+ void XmlElement::replace_element(xmlNode *element) {
506
+ xmlReplaceNode(xml_obj, element);
507
+ if (element->_private != NULL) {
508
+ XmlNode *node = static_cast<XmlNode *>(element->_private);
509
+ node->ref_wrapped_ancestor();
510
+ }
511
+ }
512
+
513
+ void XmlElement::replace_text(const char *content) {
514
+ xmlNodePtr txt = xmlNewDocText(xml_obj->doc, (const xmlChar *)content);
515
+ xmlReplaceNode(xml_obj, txt);
516
+ }
517
+
518
+ bool XmlElement::child_will_merge(xmlNode *child) {
519
+ return ((child->type == XML_TEXT_NODE) && (xml_obj->last != NULL) &&
520
+ (xml_obj->last->type == XML_TEXT_NODE) &&
521
+ (xml_obj->last->name == child->name) && (xml_obj->last != child));
522
+ }
523
+
524
+ void XmlElement::Initialize(Local<Object> target) {
525
+ Nan::HandleScope scope;
526
+ Local<FunctionTemplate> tmpl = Nan::New<FunctionTemplate>(New);
527
+ constructor_template.Reset(tmpl);
528
+ tmpl->Inherit(Nan::New(XmlNode::constructor_template));
529
+ tmpl->InstanceTemplate()->SetInternalFieldCount(1);
530
+
531
+ Nan::SetPrototypeMethod(tmpl, "addChild", XmlElement::AddChild);
532
+
533
+ Nan::SetPrototypeMethod(tmpl, "cdata", XmlElement::AddCData);
534
+
535
+ Nan::SetPrototypeMethod(tmpl, "_attr", XmlElement::Attr);
536
+
537
+ Nan::SetPrototypeMethod(tmpl, "attrs", XmlElement::Attrs);
538
+
539
+ Nan::SetPrototypeMethod(tmpl, "child", XmlElement::Child);
540
+
541
+ Nan::SetPrototypeMethod(tmpl, "childNodes", XmlElement::ChildNodes);
542
+
543
+ Nan::SetPrototypeMethod(tmpl, "find", XmlElement::Find);
544
+
545
+ Nan::SetPrototypeMethod(tmpl, "nextElement", XmlElement::NextElement);
546
+
547
+ Nan::SetPrototypeMethod(tmpl, "prevElement", XmlElement::PrevElement);
548
+
549
+ Nan::SetPrototypeMethod(tmpl, "name", XmlElement::Name);
550
+
551
+ Nan::SetPrototypeMethod(tmpl, "path", XmlElement::Path);
552
+
553
+ Nan::SetPrototypeMethod(tmpl, "text", XmlElement::Text);
554
+
555
+ Nan::SetPrototypeMethod(tmpl, "addPrevSibling", XmlElement::AddPrevSibling);
556
+
557
+ Nan::SetPrototypeMethod(tmpl, "addNextSibling", XmlElement::AddNextSibling);
558
+
559
+ Nan::SetPrototypeMethod(tmpl, "replace", XmlElement::Replace);
560
+
561
+ Nan::Set(target, Nan::New<String>("Element").ToLocalChecked(),
562
+ Nan::GetFunction(tmpl).ToLocalChecked());
563
+ }
564
+
565
+ } // namespace libxmljs
@@ -0,0 +1,61 @@
1
+ // Copyright 2009, Squish Tech, LLC.
2
+ #ifndef SRC_XML_ELEMENT_H_
3
+ #define SRC_XML_ELEMENT_H_
4
+
5
+ #include "libxmljs.h"
6
+ #include "xml_node.h"
7
+
8
+ namespace libxmljs {
9
+
10
+ class XmlElement : public XmlNode {
11
+ public:
12
+ explicit XmlElement(xmlNode *node);
13
+
14
+ static void Initialize(v8::Local<v8::Object> target);
15
+
16
+ static Nan::Persistent<v8::FunctionTemplate> constructor_template;
17
+
18
+ // create new xml element to wrap the node
19
+ static v8::Local<v8::Object> New(xmlNode *node);
20
+
21
+ protected:
22
+ static NAN_METHOD(New);
23
+ static NAN_METHOD(Name);
24
+ static NAN_METHOD(Attr);
25
+ static NAN_METHOD(Attrs);
26
+ static NAN_METHOD(Find);
27
+ static NAN_METHOD(Text);
28
+ static NAN_METHOD(Path);
29
+ static NAN_METHOD(Child);
30
+ static NAN_METHOD(ChildNodes);
31
+ static NAN_METHOD(AddChild);
32
+ static NAN_METHOD(AddCData);
33
+ static NAN_METHOD(NextElement);
34
+ static NAN_METHOD(PrevElement);
35
+ static NAN_METHOD(AddPrevSibling);
36
+ static NAN_METHOD(AddNextSibling);
37
+ static NAN_METHOD(Replace);
38
+
39
+ void set_name(const char *name);
40
+
41
+ v8::Local<v8::Value> get_name();
42
+ v8::Local<v8::Value> get_child(int32_t idx);
43
+ v8::Local<v8::Value> get_child_nodes();
44
+ v8::Local<v8::Value> get_path();
45
+ v8::Local<v8::Value> get_attr(const char *name);
46
+ v8::Local<v8::Value> get_attrs();
47
+ void set_attr(const char *name, const char *value);
48
+ void add_cdata(xmlNode *cdata);
49
+ void unlink_children();
50
+ void set_content(const char *content);
51
+ v8::Local<v8::Value> get_content();
52
+ v8::Local<v8::Value> get_next_element();
53
+ v8::Local<v8::Value> get_prev_element();
54
+ void replace_element(xmlNode *element);
55
+ void replace_text(const char *content);
56
+ bool child_will_merge(xmlNode *child);
57
+ };
58
+
59
+ } // namespace libxmljs
60
+
61
+ #endif // SRC_XML_ELEMENT_H_