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,810 @@
1
+ // Copyright 2009, Squish Tech, LLC.
2
+
3
+ #include <node.h>
4
+ #include <node_buffer.h>
5
+
6
+ #include <cstring>
7
+
8
+ //#include <libxml/tree.h>
9
+ #include <libxml/HTMLparser.h>
10
+ #include <libxml/HTMLtree.h>
11
+ #include <libxml/relaxng.h>
12
+ #include <libxml/schematron.h>
13
+ #include <libxml/xinclude.h>
14
+ #include <libxml/xmlsave.h>
15
+ #include <libxml/xmlschemas.h>
16
+
17
+ #include "xml_document.h"
18
+ #include "xml_element.h"
19
+ #include "xml_namespace.h"
20
+ #include "xml_node.h"
21
+ #include "xml_syntax_error.h"
22
+
23
+ using namespace v8;
24
+
25
+ namespace libxmljs {
26
+
27
+ Nan::Persistent<FunctionTemplate> XmlDocument::constructor_template;
28
+
29
+ NAN_METHOD(XmlDocument::Encoding) {
30
+ Nan::HandleScope scope;
31
+ XmlDocument *document = Nan::ObjectWrap::Unwrap<XmlDocument>(info.Holder());
32
+ assert(document);
33
+
34
+ // if no args, get the encoding
35
+ if (info.Length() == 0 || info[0]->IsUndefined()) {
36
+ if (document->xml_obj->encoding)
37
+ return info.GetReturnValue().Set(
38
+ Nan::New<String>(
39
+ (const char *)document->xml_obj->encoding,
40
+ xmlStrlen((const xmlChar *)document->xml_obj->encoding))
41
+ .ToLocalChecked());
42
+
43
+ return info.GetReturnValue().Set(Nan::Null());
44
+ }
45
+
46
+ // set the encoding otherwise
47
+ Nan::Utf8String encoding(info[0]);
48
+ document->setEncoding(*encoding);
49
+ return info.GetReturnValue().Set(info.Holder());
50
+ }
51
+
52
+ void XmlDocument::setEncoding(const char *encoding) {
53
+ if (xml_obj->encoding != NULL) {
54
+ xmlFree((xmlChar *)xml_obj->encoding);
55
+ }
56
+
57
+ xml_obj->encoding = xmlStrdup((const xmlChar *)encoding);
58
+ }
59
+
60
+ NAN_METHOD(XmlDocument::Version) {
61
+ Nan::HandleScope scope;
62
+ XmlDocument *document = Nan::ObjectWrap::Unwrap<XmlDocument>(info.Holder());
63
+ assert(document);
64
+
65
+ if (document->xml_obj->version)
66
+ return info.GetReturnValue().Set(
67
+ Nan::New<String>((const char *)document->xml_obj->version,
68
+ xmlStrlen((const xmlChar *)document->xml_obj->version))
69
+ .ToLocalChecked());
70
+
71
+ return info.GetReturnValue().Set(Nan::Null());
72
+ }
73
+
74
+ NAN_METHOD(XmlDocument::Root) {
75
+ Nan::HandleScope scope;
76
+ XmlDocument *document = Nan::ObjectWrap::Unwrap<XmlDocument>(info.Holder());
77
+ assert(document);
78
+
79
+ xmlNode *root = xmlDocGetRootElement(document->xml_obj);
80
+
81
+ if (info.Length() == 0 || info[0]->IsUndefined()) {
82
+ if (!root) {
83
+ return info.GetReturnValue().Set(Nan::Null());
84
+ }
85
+ return info.GetReturnValue().Set(XmlElement::New(root));
86
+ }
87
+
88
+ if (root != NULL) {
89
+ return Nan::ThrowError("Holder document already has a root node");
90
+ }
91
+
92
+ // set the element as the root element for the document
93
+ // allows for proper retrieval of root later
94
+ XmlElement *element = Nan::ObjectWrap::Unwrap<XmlElement>(
95
+ Nan::To<Object>(info[0]).ToLocalChecked());
96
+ assert(element);
97
+ xmlDocSetRootElement(document->xml_obj, element->xml_obj);
98
+ element->ref_wrapped_ancestor();
99
+ return info.GetReturnValue().Set(info[0]);
100
+ }
101
+
102
+ NAN_METHOD(XmlDocument::GetDtd) {
103
+ Nan::HandleScope scope;
104
+ XmlDocument *document = Nan::ObjectWrap::Unwrap<XmlDocument>(info.Holder());
105
+ assert(document);
106
+
107
+ xmlDtdPtr dtd = xmlGetIntSubset(document->xml_obj);
108
+
109
+ if (!dtd) {
110
+ return info.GetReturnValue().Set(Nan::Null());
111
+ }
112
+
113
+ const char *name = (const char *)dtd->name;
114
+ const char *extId = (const char *)dtd->ExternalID;
115
+ const char *sysId = (const char *)dtd->SystemID;
116
+
117
+ Local<Object> dtdObj = Nan::New<Object>();
118
+ Local<Value> nameValue = Nan::Null();
119
+ Local<Value> extValue = Nan::Null();
120
+ Local<Value> sysValue = Nan::Null();
121
+
122
+ if (name != NULL) {
123
+ nameValue = Nan::New<String>(name, strlen(name)).ToLocalChecked();
124
+ }
125
+
126
+ if (extId != NULL) {
127
+ extValue = Nan::New<String>(extId, strlen(extId)).ToLocalChecked();
128
+ }
129
+
130
+ if (sysId != NULL) {
131
+ sysValue = Nan::New<String>(sysId, strlen(sysId)).ToLocalChecked();
132
+ }
133
+
134
+ Nan::Set(dtdObj, Nan::New<String>("name").ToLocalChecked(), nameValue);
135
+
136
+ Nan::Set(dtdObj, Nan::New<String>("externalId").ToLocalChecked(), extValue);
137
+
138
+ Nan::Set(dtdObj, Nan::New<String>("systemId").ToLocalChecked(), sysValue);
139
+
140
+ return info.GetReturnValue().Set(dtdObj);
141
+ }
142
+
143
+ NAN_METHOD(XmlDocument::SetDtd) {
144
+ Nan::HandleScope scope;
145
+
146
+ XmlDocument *document = Nan::ObjectWrap::Unwrap<XmlDocument>(info.Holder());
147
+ assert(document);
148
+
149
+ Nan::Utf8String name(info[0]);
150
+
151
+ Local<Value> extIdOpt;
152
+ Local<Value> sysIdOpt;
153
+ if (info.Length() > 1 && info[1]->IsString()) {
154
+ extIdOpt = info[1];
155
+ }
156
+ if (info.Length() > 2 && info[2]->IsString()) {
157
+ sysIdOpt = info[2];
158
+ }
159
+
160
+ Nan::Utf8String extIdRaw(extIdOpt);
161
+ Nan::Utf8String sysIdRaw(sysIdOpt);
162
+
163
+ // must be set to null in order for xmlCreateIntSubset to ignore them
164
+ const char *extId = (extIdRaw.length()) ? *extIdRaw : NULL;
165
+ const char *sysId = (sysIdRaw.length()) ? *sysIdRaw : NULL;
166
+
167
+ // No good way of unsetting the doctype if it is previously set...this allows
168
+ // us to.
169
+ xmlDtdPtr dtd = xmlGetIntSubset(document->xml_obj);
170
+
171
+ xmlUnlinkNode((xmlNodePtr)dtd);
172
+ xmlFreeNode((xmlNodePtr)dtd);
173
+
174
+ xmlCreateIntSubset(document->xml_obj, (const xmlChar *)*name,
175
+ (const xmlChar *)extId, (const xmlChar *)sysId);
176
+
177
+ return info.GetReturnValue().Set(info.Holder());
178
+ }
179
+
180
+ NAN_METHOD(XmlDocument::ToString) {
181
+ Nan::HandleScope scope;
182
+
183
+ XmlDocument *document = Nan::ObjectWrap::Unwrap<XmlDocument>(info.Holder());
184
+ assert(document);
185
+
186
+ int options = 0;
187
+ const char *encoding = "UTF-8";
188
+ Local<Value> encodingOpt = Nan::Null();
189
+
190
+ if (info[0]->IsObject()) {
191
+ Local<Object> obj = Nan::To<Object>(info[0]).ToLocalChecked();
192
+
193
+ // drop the xml declaration
194
+ if (Nan::Get(obj, Nan::New<String>("declaration").ToLocalChecked())
195
+ .ToLocalChecked()
196
+ ->IsFalse()) {
197
+ options |= XML_SAVE_NO_DECL;
198
+ }
199
+
200
+ // format save output
201
+ if (Nan::Get(obj, Nan::New<String>("format").ToLocalChecked())
202
+ .ToLocalChecked()
203
+ ->IsTrue()) {
204
+ options |= XML_SAVE_FORMAT;
205
+ }
206
+
207
+ // no empty tags (only works with XML) ex: <title></title> becomes <title/>
208
+ if (Nan::Get(obj, Nan::New<String>("selfCloseEmpty").ToLocalChecked())
209
+ .ToLocalChecked()
210
+ ->IsFalse()) {
211
+ options |= XML_SAVE_NO_EMPTY;
212
+ }
213
+
214
+ // format with non-significant whitespace
215
+ if (Nan::Get(obj, Nan::New<String>("whitespace").ToLocalChecked())
216
+ .ToLocalChecked()
217
+ ->IsTrue()) {
218
+ options |= XML_SAVE_WSNONSIG;
219
+ }
220
+
221
+ encodingOpt =
222
+ Nan::Get(obj, Nan::New<String>("encoding").ToLocalChecked())
223
+ .ToLocalChecked();
224
+
225
+ Local<Value> type = Nan::Get(obj, Nan::New<String>("type").ToLocalChecked())
226
+ .ToLocalChecked();
227
+ if (Nan::Equals(type, Nan::New<String>("XML").ToLocalChecked())
228
+ .ToChecked() ||
229
+ Nan::Equals(type, Nan::New<String>("xml").ToLocalChecked())
230
+ .ToChecked()) {
231
+ options |= XML_SAVE_AS_XML; // force XML serialization on HTML doc
232
+ } else if (Nan::Equals(type, Nan::New<String>("HTML").ToLocalChecked())
233
+ .ToChecked() ||
234
+ Nan::Equals(type, Nan::New<String>("html").ToLocalChecked())
235
+ .ToChecked()) {
236
+ options |= XML_SAVE_AS_HTML; // force HTML serialization on XML doc
237
+ // if the document is XML and we want formatted HTML output
238
+ // we must use the XHTML serializer because the default HTML
239
+ // serializer only formats node->type = HTML_NODE and not XML_NODEs
240
+ if ((options & XML_SAVE_FORMAT) && (options & XML_SAVE_XHTML) == false) {
241
+ options |= XML_SAVE_XHTML;
242
+ }
243
+ } else if (Nan::Equals(type, Nan::New<String>("XHTML").ToLocalChecked())
244
+ .ToChecked() ||
245
+ Nan::Equals(type, Nan::New<String>("xhtml").ToLocalChecked())
246
+ .ToChecked()) {
247
+ options |= XML_SAVE_XHTML; // force XHTML serialization
248
+ }
249
+ } else if (info.Length() == 0 || Nan::To<bool>(info[0]).FromMaybe(true)) {
250
+ options |= XML_SAVE_FORMAT;
251
+ }
252
+
253
+ if (encodingOpt->IsString()) {
254
+ Nan::Utf8String encoding_(Nan::To<String>(encodingOpt).ToLocalChecked());
255
+ encoding = *encoding_;
256
+ }
257
+
258
+ xmlBuffer *buf = xmlBufferCreate();
259
+ xmlSaveCtxt *savectx = xmlSaveToBuffer(buf, encoding, options);
260
+ xmlSaveTree(savectx, (xmlNode *)document->xml_obj);
261
+ xmlSaveFlush(savectx);
262
+ xmlSaveClose(savectx);
263
+ Local<Value> ret = Nan::Null();
264
+ if (xmlBufferLength(buf) > 0)
265
+ ret = Nan::New<String>((char *)xmlBufferContent(buf), xmlBufferLength(buf))
266
+ .ToLocalChecked();
267
+ xmlBufferFree(buf);
268
+
269
+ return info.GetReturnValue().Set(ret);
270
+ }
271
+
272
+ NAN_METHOD(XmlDocument::type) {
273
+ return info.GetReturnValue().Set(
274
+ Nan::New<String>("document").ToLocalChecked());
275
+ }
276
+
277
+ // not called from node
278
+ // private api
279
+ Local<Object> XmlDocument::New(xmlDoc *doc) {
280
+ Nan::EscapableHandleScope scope;
281
+
282
+ if (doc->_private) {
283
+ return scope.Escape(static_cast<XmlDocument *>(doc->_private)->handle());
284
+ }
285
+
286
+ Local<Object> obj =
287
+ Nan::NewInstance(
288
+ Nan::GetFunction(Nan::New(constructor_template)).ToLocalChecked())
289
+ .ToLocalChecked();
290
+
291
+ XmlDocument *document = Nan::ObjectWrap::Unwrap<XmlDocument>(obj);
292
+
293
+ // replace the document we created
294
+ document->xml_obj->_private = NULL;
295
+ xmlFreeDoc(document->xml_obj);
296
+ document->xml_obj = doc;
297
+
298
+ // store ourselves in the document
299
+ // this is how we can get instances or already existing v8 objects
300
+ doc->_private = document;
301
+
302
+ return scope.Escape(obj);
303
+ }
304
+
305
+ int getParserOption(Local<Object> props, const char *key, int value,
306
+ bool defaultValue = true) {
307
+ Nan::HandleScope scope;
308
+ Local<Value> prop =
309
+ Nan::Get(props, Nan::New<String>(key).ToLocalChecked()).ToLocalChecked();
310
+ return !prop->IsUndefined() && Nan::To<bool>(prop).ToChecked() == defaultValue
311
+ ? value
312
+ : 0;
313
+ }
314
+
315
+ xmlParserOption getParserOptions(Local<Object> props) {
316
+ int ret = 0;
317
+
318
+ // http://xmlsoft.org/html/libxml-parser.html#xmlParserOption
319
+ // http://www.xmlsoft.org/html/libxml-HTMLparser.html#htmlParserOption
320
+
321
+ ret |= getParserOption(props, "recover",
322
+ XML_PARSE_RECOVER); // 1: recover on errors
323
+ /*ret |= getParserOption(props, "recover", HTML_PARSE_RECOVER); //
324
+ * 1: Relaxed parsing*/
325
+
326
+ ret |= getParserOption(props, "noent",
327
+ XML_PARSE_NOENT); // 2: substitute entities
328
+
329
+ ret |= getParserOption(props, "dtdload",
330
+ XML_PARSE_DTDLOAD); // 4: load the external subset
331
+ ret |= getParserOption(props, "doctype", HTML_PARSE_NODEFDTD,
332
+ false); // 4: do not default a doctype if not found
333
+
334
+ ret |= getParserOption(props, "dtdattr",
335
+ XML_PARSE_DTDATTR); // 8: default DTD attributes
336
+ ret |= getParserOption(props, "dtdvalid",
337
+ XML_PARSE_DTDVALID); // 16: validate with the DTD
338
+
339
+ ret |= getParserOption(props, "noerror",
340
+ XML_PARSE_NOERROR); // 32: suppress error reports
341
+ ret |= getParserOption(props, "errors", HTML_PARSE_NOERROR,
342
+ false); // 32: suppress error reports
343
+
344
+ ret |= getParserOption(props, "nowarning",
345
+ XML_PARSE_NOWARNING); // 64: suppress warning reports
346
+ ret |= getParserOption(props, "warnings", HTML_PARSE_NOWARNING,
347
+ false); // 64: suppress warning reports
348
+
349
+ ret |= getParserOption(props, "pedantic",
350
+ XML_PARSE_PEDANTIC); // 128: pedantic error reporting
351
+ /*ret |= getParserOption(props, "pedantic", HTML_PARSE_PEDANTIC); //
352
+ * 128: pedantic error reporting*/
353
+
354
+ ret |= getParserOption(props, "noblanks",
355
+ XML_PARSE_NOBLANKS); // 256: remove blank nodes
356
+ ret |= getParserOption(props, "blanks", HTML_PARSE_NOBLANKS,
357
+ false); // 256: remove blank nodes
358
+
359
+ ret |= getParserOption(
360
+ props, "sax1", XML_PARSE_SAX1); // 512: use the SAX1 interface internally
361
+ ret |= getParserOption(
362
+ props, "xinclude",
363
+ XML_PARSE_XINCLUDE); // 1024: Implement XInclude substitition
364
+
365
+ ret |= getParserOption(props, "nonet",
366
+ XML_PARSE_NONET); // 2048: Forbid network access
367
+ ret |= getParserOption(props, "net", HTML_PARSE_NONET,
368
+ false); // 2048: Forbid network access
369
+
370
+ ret |= getParserOption(
371
+ props, "nodict",
372
+ XML_PARSE_NODICT); // 4096: Do not reuse the context dictionnary
373
+ ret |= getParserOption(props, "dict", XML_PARSE_NODICT,
374
+ false); // 4096: Do not reuse the context dictionnary
375
+
376
+ ret |= getParserOption(
377
+ props, "nsclean",
378
+ XML_PARSE_NSCLEAN); // 8192: remove redundant namespaces declarations
379
+ ret |= getParserOption(props, "implied", HTML_PARSE_NOIMPLIED,
380
+ false); // 8192: Do not add implied html/body elements
381
+
382
+ ret |= getParserOption(props, "nocdata",
383
+ XML_PARSE_NOCDATA); // 16384: merge CDATA as text nodes
384
+ ret |= getParserOption(props, "cdata", XML_PARSE_NOCDATA,
385
+ false); // 16384: merge CDATA as text nodes
386
+
387
+ ret |= getParserOption(
388
+ props, "noxincnode",
389
+ XML_PARSE_NOXINCNODE); // 32768: do not generate XINCLUDE START/END nodes
390
+ ret |=
391
+ getParserOption(props, "xincnode", XML_PARSE_NOXINCNODE,
392
+ false); // 32768: do not generate XINCLUDE START/END nodes
393
+
394
+ ret |= getParserOption(
395
+ props, "compact",
396
+ XML_PARSE_COMPACT); // 65536: compact small text nodes; no modification of
397
+ // the tree allowed afterwards (will possibly crash if
398
+ // you try to modify the tree)
399
+ /*ret |= getParserOption(props, "compact", HTML_PARSE_COMPACT , false); //
400
+ * 65536: compact small text nodes*/
401
+
402
+ ret |= getParserOption(
403
+ props, "old10",
404
+ XML_PARSE_OLD10); // 131072: parse using XML-1.0 before update 5
405
+
406
+ ret |= getParserOption(
407
+ props, "nobasefix",
408
+ XML_PARSE_NOBASEFIX); // 262144: do not fixup XINCLUDE xml:base uris
409
+ ret |= getParserOption(props, "basefix", XML_PARSE_NOBASEFIX,
410
+ false); // 262144: do not fixup XINCLUDE xml:base uris
411
+
412
+ ret |= getParserOption(
413
+ props, "huge",
414
+ XML_PARSE_HUGE); // 524288: relax any hardcoded limit from the parser
415
+ ret |= getParserOption(
416
+ props, "oldsax",
417
+ XML_PARSE_OLDSAX); // 1048576: parse using SAX2 interface before 2.7.0
418
+
419
+ ret |= getParserOption(
420
+ props, "ignore_enc",
421
+ XML_PARSE_IGNORE_ENC); // 2097152: ignore internal document encoding hint
422
+ /*ret |= getParserOption(props, "ignore_enc", HTML_PARSE_IGNORE_ENC); //
423
+ * 2097152: ignore internal document encoding hint*/
424
+
425
+ ret |= getParserOption(props, "big_lines",
426
+ XML_PARSE_BIG_LINES); // 4194304: Store big lines
427
+ // numbers in text PSVI field
428
+
429
+ return (xmlParserOption)ret;
430
+ }
431
+
432
+ NAN_METHOD(XmlDocument::FromHtml) {
433
+ Nan::HandleScope scope;
434
+
435
+ Local<Object> options = Nan::To<Object>(info[1]).ToLocalChecked();
436
+ Local<Value> baseUrlOpt =
437
+ Nan::Get(options, Nan::New<String>("baseUrl").ToLocalChecked())
438
+ .ToLocalChecked();
439
+ Local<Value> encodingOpt =
440
+ Nan::Get(options, Nan::New<String>("encoding").ToLocalChecked())
441
+ .ToLocalChecked();
442
+ Local<Value> excludeImpliedElementsOpt =
443
+ Nan::Get(options,
444
+ Nan::New<String>("excludeImpliedElements").ToLocalChecked())
445
+ .ToLocalChecked();
446
+
447
+ // the base URL that will be used for this HTML parsed document
448
+ Nan::Utf8String baseUrl_(Nan::To<String>(baseUrlOpt).ToLocalChecked());
449
+ const char *baseUrl = *baseUrl_;
450
+ if (!baseUrlOpt->IsString()) {
451
+ baseUrl = NULL;
452
+ }
453
+
454
+ // the encoding to be used for this document
455
+ // (leave NULL for libxml to autodetect)
456
+ Nan::Utf8String encoding_(Nan::To<String>(encodingOpt).ToLocalChecked());
457
+ const char *encoding = *encoding_;
458
+
459
+ if (!encodingOpt->IsString()) {
460
+ encoding = NULL;
461
+ }
462
+
463
+ Local<Array> errors = Nan::New<Array>();
464
+ xmlResetLastError();
465
+ xmlSetStructuredErrorFunc(reinterpret_cast<void *>(&errors),
466
+ XmlSyntaxError::PushToArray);
467
+
468
+ int opts = (int)getParserOptions(options);
469
+ if (Nan::To<bool>(excludeImpliedElementsOpt).ToChecked())
470
+ opts |= HTML_PARSE_NOIMPLIED | HTML_PARSE_NODEFDTD;
471
+
472
+ htmlDocPtr doc;
473
+ if (!node::Buffer::HasInstance(info[0])) {
474
+ // Parse a string
475
+ Nan::Utf8String str(Nan::To<String>(info[0]).ToLocalChecked());
476
+ doc = htmlReadMemory(*str, str.length(), baseUrl, encoding, opts);
477
+ } else {
478
+ // Parse a buffer
479
+ Local<Object> buf = Nan::To<Object>(info[0]).ToLocalChecked();
480
+ doc = htmlReadMemory(node::Buffer::Data(buf), node::Buffer::Length(buf),
481
+ baseUrl, encoding, opts);
482
+ }
483
+
484
+ xmlSetStructuredErrorFunc(NULL, NULL);
485
+
486
+ if (!doc) {
487
+ xmlError *error = xmlGetLastError();
488
+ if (error) {
489
+ return Nan::ThrowError(XmlSyntaxError::BuildSyntaxError(error));
490
+ }
491
+ return Nan::ThrowError("Could not parse XML string");
492
+ }
493
+
494
+ Local<Object> doc_handle = XmlDocument::New(doc);
495
+ Nan::Set(doc_handle, Nan::New<String>("errors").ToLocalChecked(), errors);
496
+
497
+ // create the xml document handle to return
498
+ return info.GetReturnValue().Set(doc_handle);
499
+ }
500
+
501
+ // FIXME: this method is almost identical to FromHtml above.
502
+ // The two should be refactored to use a common function for most
503
+ // of the work
504
+ NAN_METHOD(XmlDocument::FromXml) {
505
+ Nan::HandleScope scope;
506
+
507
+ Local<Array> errors = Nan::New<Array>();
508
+ xmlResetLastError();
509
+ xmlSetStructuredErrorFunc(reinterpret_cast<void *>(&errors),
510
+ XmlSyntaxError::PushToArray);
511
+
512
+ Local<Object> options = Nan::To<Object>(info[1]).ToLocalChecked();
513
+ Local<Value> baseUrlOpt =
514
+ Nan::Get(options, Nan::New<String>("baseUrl").ToLocalChecked())
515
+ .ToLocalChecked();
516
+ Local<Value> encodingOpt =
517
+ Nan::Get(options, Nan::New<String>("encoding").ToLocalChecked())
518
+ .ToLocalChecked();
519
+
520
+ // the base URL that will be used for this document
521
+ Nan::Utf8String baseUrl_(baseUrlOpt);
522
+ const char *baseUrl = *baseUrl_;
523
+ if (!baseUrlOpt->IsString()) {
524
+ baseUrl = NULL;
525
+ }
526
+
527
+ // the encoding to be used for this document
528
+ // (leave NULL for libxml to autodetect)
529
+ Nan::Utf8String encoding_(encodingOpt);
530
+ const char *encoding = *encoding_;
531
+ if (!encodingOpt->IsString()) {
532
+ encoding = NULL;
533
+ }
534
+
535
+ int opts = (int)getParserOptions(options);
536
+ xmlDocPtr doc;
537
+ if (!node::Buffer::HasInstance(info[0])) {
538
+ // Parse a string
539
+ Nan::Utf8String str(Nan::To<String>(info[0]).ToLocalChecked());
540
+ doc = xmlReadMemory(*str, str.length(), baseUrl, "UTF-8", opts);
541
+ } else {
542
+ // Parse a buffer
543
+ Local<Object> buf = Nan::To<Object>(info[0]).ToLocalChecked();
544
+ doc = xmlReadMemory(node::Buffer::Data(buf), node::Buffer::Length(buf),
545
+ baseUrl, encoding, opts);
546
+ }
547
+
548
+ xmlSetStructuredErrorFunc(NULL, NULL);
549
+
550
+ if (!doc) {
551
+ xmlError *error = xmlGetLastError();
552
+ if (error) {
553
+ return Nan::ThrowError(XmlSyntaxError::BuildSyntaxError(error));
554
+ }
555
+ return Nan::ThrowError("Could not parse XML string");
556
+ }
557
+
558
+ if (opts & XML_PARSE_XINCLUDE) {
559
+ xmlSetStructuredErrorFunc(reinterpret_cast<void *>(&errors),
560
+ XmlSyntaxError::PushToArray);
561
+ int ret = xmlXIncludeProcessFlags(doc, opts);
562
+ xmlSetStructuredErrorFunc(NULL, NULL);
563
+
564
+ if (ret < 0) {
565
+ xmlError *error = xmlGetLastError();
566
+ if (error) {
567
+ return Nan::ThrowError(XmlSyntaxError::BuildSyntaxError(error));
568
+ }
569
+ return Nan::ThrowError("Could not perform XInclude substitution");
570
+ }
571
+ }
572
+
573
+ Local<Object> doc_handle = XmlDocument::New(doc);
574
+ Nan::Set(doc_handle, Nan::New<String>("errors").ToLocalChecked(), errors);
575
+
576
+ xmlNode *root_node = xmlDocGetRootElement(doc);
577
+ if (root_node == NULL) {
578
+ return Nan::ThrowError("parsed document has no root element");
579
+ }
580
+
581
+ // create the xml document handle to return
582
+ return info.GetReturnValue().Set(doc_handle);
583
+ }
584
+
585
+ NAN_METHOD(XmlDocument::Validate) {
586
+ if (info.Length() == 0 || info[0]->IsNullOrUndefined()) {
587
+ Nan::ThrowError("Must pass xsd");
588
+ return;
589
+ }
590
+ if (!XmlDocument::constructor_template.Get(Isolate::GetCurrent())
591
+ ->HasInstance(info[0])) {
592
+ Nan::ThrowError("Must pass XmlDocument");
593
+ return;
594
+ }
595
+
596
+ Nan::HandleScope scope;
597
+
598
+ Local<Array> errors = Nan::New<Array>();
599
+ xmlResetLastError();
600
+ xmlSetStructuredErrorFunc(reinterpret_cast<void *>(&errors),
601
+ XmlSyntaxError::PushToArray);
602
+
603
+ XmlDocument *document = Nan::ObjectWrap::Unwrap<XmlDocument>(info.Holder());
604
+ XmlDocument *documentSchema = Nan::ObjectWrap::Unwrap<XmlDocument>(
605
+ Nan::To<Object>(info[0]).ToLocalChecked());
606
+
607
+ xmlSchemaParserCtxtPtr parser_ctxt =
608
+ xmlSchemaNewDocParserCtxt(documentSchema->xml_obj);
609
+ if (parser_ctxt == NULL) {
610
+ return Nan::ThrowError("Could not create context for schema parser");
611
+ }
612
+ xmlSchemaPtr schema = xmlSchemaParse(parser_ctxt);
613
+ if (schema == NULL) {
614
+ return Nan::ThrowError("Invalid XSD schema");
615
+ }
616
+ xmlSchemaValidCtxtPtr valid_ctxt = xmlSchemaNewValidCtxt(schema);
617
+ if (valid_ctxt == NULL) {
618
+ return Nan::ThrowError(
619
+ "Unable to create a validation context for the schema");
620
+ }
621
+ bool valid = xmlSchemaValidateDoc(valid_ctxt, document->xml_obj) == 0;
622
+
623
+ xmlSetStructuredErrorFunc(NULL, NULL);
624
+ Nan::Set(info.Holder(), Nan::New<String>("validationErrors").ToLocalChecked(),
625
+ errors)
626
+ .Check();
627
+
628
+ xmlSchemaFreeValidCtxt(valid_ctxt);
629
+ xmlSchemaFree(schema);
630
+ xmlSchemaFreeParserCtxt(parser_ctxt);
631
+
632
+ return info.GetReturnValue().Set(Nan::New<Boolean>(valid));
633
+ }
634
+
635
+ NAN_METHOD(XmlDocument::RngValidate) {
636
+ if (info.Length() == 0 || info[0]->IsNullOrUndefined()) {
637
+ Nan::ThrowError("Must pass xsd");
638
+ return;
639
+ }
640
+
641
+ if (!XmlDocument::constructor_template.Get(Isolate::GetCurrent())
642
+ ->HasInstance(info[0])) {
643
+ Nan::ThrowError("Must pass XmlDocument");
644
+ return;
645
+ }
646
+
647
+ Nan::HandleScope scope;
648
+
649
+ Local<Array> errors = Nan::New<Array>();
650
+ xmlResetLastError();
651
+ xmlSetStructuredErrorFunc(reinterpret_cast<void *>(&errors),
652
+ XmlSyntaxError::PushToArray);
653
+
654
+ XmlDocument *document = Nan::ObjectWrap::Unwrap<XmlDocument>(info.Holder());
655
+ XmlDocument *documentSchema = Nan::ObjectWrap::Unwrap<XmlDocument>(
656
+ Nan::To<Object>(info[0]).ToLocalChecked());
657
+
658
+ xmlRelaxNGParserCtxtPtr parser_ctxt =
659
+ xmlRelaxNGNewDocParserCtxt(documentSchema->xml_obj);
660
+ if (parser_ctxt == NULL) {
661
+ return Nan::ThrowError(
662
+ "Could not create context for RELAX NG schema parser");
663
+ }
664
+
665
+ xmlRelaxNGPtr schema = xmlRelaxNGParse(parser_ctxt);
666
+ if (schema == NULL) {
667
+ return Nan::ThrowError("Invalid RELAX NG schema");
668
+ }
669
+
670
+ xmlRelaxNGValidCtxtPtr valid_ctxt = xmlRelaxNGNewValidCtxt(schema);
671
+ if (valid_ctxt == NULL) {
672
+ return Nan::ThrowError(
673
+ "Unable to create a validation context for the RELAX NG schema");
674
+ }
675
+ bool valid = xmlRelaxNGValidateDoc(valid_ctxt, document->xml_obj) == 0;
676
+
677
+ xmlSetStructuredErrorFunc(NULL, NULL);
678
+ Nan::Set(info.Holder(), Nan::New<String>("validationErrors").ToLocalChecked(),
679
+ errors)
680
+ .Check();
681
+
682
+ xmlRelaxNGFreeValidCtxt(valid_ctxt);
683
+ xmlRelaxNGFree(schema);
684
+ xmlRelaxNGFreeParserCtxt(parser_ctxt);
685
+
686
+ return info.GetReturnValue().Set(Nan::New<Boolean>(valid));
687
+ }
688
+
689
+ NAN_METHOD(XmlDocument::SchematronValidate) {
690
+ if (info.Length() == 0 || info[0]->IsNullOrUndefined()) {
691
+ Nan::ThrowError("Must pass schema");
692
+ return;
693
+ }
694
+
695
+ if (!XmlDocument::constructor_template.Get(Isolate::GetCurrent())
696
+ ->HasInstance(info[0])) {
697
+ Nan::ThrowError("Must pass XmlDocument");
698
+ return;
699
+ }
700
+
701
+ Nan::HandleScope scope;
702
+
703
+ Local<Array> errors = Nan::New<Array>();
704
+ xmlResetLastError();
705
+
706
+ XmlDocument *document = Nan::ObjectWrap::Unwrap<XmlDocument>(info.Holder());
707
+ XmlDocument *documentSchema = Nan::ObjectWrap::Unwrap<XmlDocument>(
708
+ Nan::To<Object>(info[0]).ToLocalChecked());
709
+
710
+ xmlSchematronParserCtxtPtr parser_ctxt =
711
+ xmlSchematronNewDocParserCtxt(documentSchema->xml_obj);
712
+ if (parser_ctxt == NULL) {
713
+ return Nan::ThrowError(
714
+ "Could not create context for Schematron schema parser");
715
+ }
716
+
717
+ xmlSchematronPtr schema = xmlSchematronParse(parser_ctxt);
718
+ if (schema == NULL) {
719
+ return Nan::ThrowError("Invalid Schematron schema");
720
+ }
721
+
722
+ xmlSchematronValidCtxtPtr valid_ctxt = xmlSchematronNewValidCtxt(schema, XML_SCHEMATRON_OUT_ERROR);
723
+ if (valid_ctxt == NULL) {
724
+ return Nan::ThrowError(
725
+ "Unable to create a validation context for the Schematron schema");
726
+ }
727
+ xmlSchematronSetValidStructuredErrors(valid_ctxt,
728
+ XmlSyntaxError::PushToArray,
729
+ reinterpret_cast<void *>(&errors));
730
+
731
+ bool valid = xmlSchematronValidateDoc(valid_ctxt, document->xml_obj) == 0;
732
+
733
+ xmlSchematronSetValidStructuredErrors(valid_ctxt, NULL, NULL);
734
+ Nan::Set(info.Holder(), Nan::New<String>("validationErrors").ToLocalChecked(),
735
+ errors)
736
+ .Check();
737
+
738
+ xmlSchematronFreeValidCtxt(valid_ctxt);
739
+ xmlSchematronFree(schema);
740
+ xmlSchematronFreeParserCtxt(parser_ctxt);
741
+
742
+ return info.GetReturnValue().Set(Nan::New<Boolean>(valid));
743
+ }
744
+
745
+ /// this is a blank object with prototype methods
746
+ /// not exposed to the user and not called from js
747
+ NAN_METHOD(XmlDocument::New) {
748
+ NAN_CONSTRUCTOR_CHECK(XmlDocument)
749
+
750
+ const char *version = info.Length() > 0 && info[0]->IsString()
751
+ ? *Nan::Utf8String(info[0])
752
+ : "1.0";
753
+ xmlDoc *doc = xmlNewDoc((const xmlChar *)(version));
754
+
755
+ const char *encoding = info.Length() > 1 && info[1]->IsString()
756
+ ? *Nan::Utf8String(info[1])
757
+ : "utf8";
758
+
759
+ XmlDocument *document = new XmlDocument(doc);
760
+ document->setEncoding(encoding);
761
+ document->Wrap(info.Holder());
762
+
763
+ return info.GetReturnValue().Set(info.Holder());
764
+ }
765
+
766
+ XmlDocument::XmlDocument(xmlDoc *doc) : xml_obj(doc) {
767
+ xml_obj->_private = this;
768
+ }
769
+
770
+ XmlDocument::~XmlDocument() {
771
+ xml_obj->_private = NULL;
772
+ xmlFreeDoc(xml_obj);
773
+ }
774
+
775
+ void XmlDocument::Initialize(Local<Object> target) {
776
+ Nan::HandleScope scope;
777
+
778
+ Local<FunctionTemplate> tmpl = Nan::New<FunctionTemplate>(New);
779
+ tmpl->SetClassName(Nan::New<String>("Document").ToLocalChecked());
780
+
781
+ constructor_template.Reset(tmpl);
782
+ tmpl->InstanceTemplate()->SetInternalFieldCount(1);
783
+
784
+ /// setup internal methods for bindings
785
+ Nan::SetPrototypeMethod(tmpl, "root", XmlDocument::Root);
786
+
787
+ Nan::SetPrototypeMethod(tmpl, "version", XmlDocument::Version);
788
+
789
+ Nan::SetPrototypeMethod(tmpl, "encoding", XmlDocument::Encoding);
790
+
791
+ Nan::SetPrototypeMethod(tmpl, "toString", XmlDocument::ToString);
792
+
793
+ Nan::SetPrototypeMethod(tmpl, "validate", XmlDocument::Validate);
794
+ Nan::SetPrototypeMethod(tmpl, "rngValidate", XmlDocument::RngValidate);
795
+ Nan::SetPrototypeMethod(tmpl, "schematronValidate", XmlDocument::SchematronValidate);
796
+ Nan::SetPrototypeMethod(tmpl, "_setDtd", XmlDocument::SetDtd);
797
+ Nan::SetPrototypeMethod(tmpl, "getDtd", XmlDocument::GetDtd);
798
+ Nan::SetPrototypeMethod(tmpl, "type", XmlDocument::type);
799
+
800
+ Nan::SetMethod(target, "fromXml", XmlDocument::FromXml);
801
+ Nan::SetMethod(target, "fromHtml", XmlDocument::FromHtml);
802
+
803
+ // used to create new document handles
804
+ Nan::Set(target, Nan::New<String>("Document").ToLocalChecked(),
805
+ Nan::GetFunction(tmpl).ToLocalChecked());
806
+
807
+ XmlNode::Initialize(target);
808
+ XmlNamespace::Initialize(target);
809
+ }
810
+ } // namespace libxmljs