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
package/src/xml_node.h ADDED
@@ -0,0 +1,73 @@
1
+ // Copyright 2009, Squish Tech, LLC.
2
+ #ifndef SRC_XML_NODE_H_
3
+ #define SRC_XML_NODE_H_
4
+
5
+ #include "nan.h"
6
+ #include <libxml/tree.h>
7
+
8
+ namespace libxmljs {
9
+
10
+ class XmlNode : public Nan::ObjectWrap {
11
+ public:
12
+ xmlNode *xml_obj;
13
+
14
+ // reference to a parent XmlNode or XmlElement
15
+ xmlNode *ancestor;
16
+
17
+ // referencing functions
18
+ void ref_wrapped_ancestor();
19
+ void unref_wrapped_ancestor();
20
+ xmlNode *get_wrapped_ancestor();
21
+ int refs() { return refs_; };
22
+
23
+ // the doc ref'd by this proxy
24
+ xmlDoc *doc;
25
+
26
+ explicit XmlNode(xmlNode *node);
27
+ virtual ~XmlNode();
28
+
29
+ static void Initialize(v8::Local<v8::Object> target);
30
+ static Nan::Persistent<v8::FunctionTemplate> constructor_template;
31
+
32
+ // create new XmlElement, XmlAttribute, etc. to wrap a libxml xmlNode
33
+ static v8::Local<v8::Value> New(xmlNode *node);
34
+
35
+ protected:
36
+ static NAN_METHOD(Doc);
37
+ static NAN_METHOD(Namespace);
38
+ static NAN_METHOD(Namespaces);
39
+ static NAN_METHOD(Parent);
40
+ static NAN_METHOD(NextSibling);
41
+ static NAN_METHOD(PrevSibling);
42
+ static NAN_METHOD(LineNumber);
43
+ static NAN_METHOD(Type);
44
+ static NAN_METHOD(ToString);
45
+ static NAN_METHOD(Remove);
46
+ static NAN_METHOD(Clone);
47
+
48
+ v8::Local<v8::Value> get_doc();
49
+ v8::Local<v8::Value> remove_namespace();
50
+ v8::Local<v8::Value> get_namespace();
51
+ void set_namespace(xmlNs *ns);
52
+ xmlNs *find_namespace(const char *search_str);
53
+ v8::Local<v8::Value> get_all_namespaces();
54
+ v8::Local<v8::Value> get_local_namespaces();
55
+ v8::Local<v8::Value> get_parent();
56
+ v8::Local<v8::Value> get_prev_sibling();
57
+ v8::Local<v8::Value> get_next_sibling();
58
+ v8::Local<v8::Value> get_line_number();
59
+ v8::Local<v8::Value> clone(bool recurse);
60
+ v8::Local<v8::Value> get_type();
61
+ v8::Local<v8::Value> to_string(int options = 0);
62
+ void remove();
63
+ void add_child(xmlNode *child);
64
+ void add_prev_sibling(xmlNode *element);
65
+ void add_next_sibling(xmlNode *element);
66
+ void replace_element(xmlNode *element);
67
+ void replace_text(const char *content);
68
+ xmlNode *import_node(xmlNode *node);
69
+ };
70
+
71
+ } // namespace libxmljs
72
+
73
+ #endif // SRC_XML_NODE_H_
package/src/xml_pi.cc ADDED
@@ -0,0 +1,161 @@
1
+ #include <node.h>
2
+
3
+ #include <cstring>
4
+
5
+ #include "libxmljs.h"
6
+
7
+ #include "xml_attribute.h"
8
+ #include "xml_document.h"
9
+ #include "xml_pi.h"
10
+ #include "xml_xpath_context.h"
11
+
12
+ using namespace v8;
13
+
14
+ namespace libxmljs {
15
+
16
+ Nan::Persistent<FunctionTemplate>
17
+ XmlProcessingInstruction::constructor_template;
18
+
19
+ // doc, content
20
+ NAN_METHOD(XmlProcessingInstruction::New) {
21
+ NAN_CONSTRUCTOR_CHECK(ProcessingInstruction)
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
+ DOCUMENT_ARG_CHECK
31
+ if (!info[1]->IsString()) {
32
+ Nan::ThrowError("name argument must be of type string");
33
+ return;
34
+ }
35
+
36
+ XmlDocument *document = Nan::ObjectWrap::Unwrap<XmlDocument>(doc);
37
+ assert(document);
38
+
39
+ Nan::Utf8String name(info[1]);
40
+
41
+ Local<Value> contentOpt;
42
+ if (info[2]->IsString()) {
43
+ contentOpt = info[2];
44
+ } else if (!info[2]->IsNullOrUndefined()) {
45
+ Nan::ThrowError("content argument must be of type string");
46
+ return;
47
+ }
48
+ Nan::Utf8String contentRaw(contentOpt);
49
+ const char *content = (contentRaw.length()) ? *contentRaw : NULL;
50
+
51
+ xmlNode *pi = xmlNewDocPI(document->xml_obj, (const xmlChar *)*name,
52
+ (xmlChar *)content);
53
+
54
+ XmlProcessingInstruction *processing_instruction =
55
+ new XmlProcessingInstruction(pi);
56
+ pi->_private = processing_instruction;
57
+ processing_instruction->Wrap(info.Holder());
58
+
59
+ // this prevents the document from going away
60
+ Nan::Set(info.Holder(), Nan::New<String>("document").ToLocalChecked(),
61
+ info[0])
62
+ .Check();
63
+
64
+ return info.GetReturnValue().Set(info.Holder());
65
+ }
66
+
67
+ NAN_METHOD(XmlProcessingInstruction::Name) {
68
+ Nan::HandleScope scope;
69
+ XmlProcessingInstruction *processing_instruction =
70
+ Nan::ObjectWrap::Unwrap<XmlProcessingInstruction>(info.Holder());
71
+ assert(processing_instruction);
72
+
73
+ if (info.Length() == 0)
74
+ return info.GetReturnValue().Set(processing_instruction->get_name());
75
+
76
+ Nan::Utf8String name(Nan::To<String>(info[0]).ToLocalChecked());
77
+ processing_instruction->set_name(*name);
78
+ return info.GetReturnValue().Set(info.Holder());
79
+ }
80
+
81
+ NAN_METHOD(XmlProcessingInstruction::Text) {
82
+ Nan::HandleScope scope;
83
+ XmlProcessingInstruction *processing_instruction =
84
+ Nan::ObjectWrap::Unwrap<XmlProcessingInstruction>(info.Holder());
85
+ assert(processing_instruction);
86
+
87
+ if (info.Length() == 0) {
88
+ return info.GetReturnValue().Set(processing_instruction->get_content());
89
+ } else {
90
+ processing_instruction->set_content(*Nan::Utf8String(info[0]));
91
+ }
92
+
93
+ return info.GetReturnValue().Set(info.Holder());
94
+ }
95
+
96
+ void XmlProcessingInstruction::set_name(const char *name) {
97
+ xmlNodeSetName(xml_obj, (const xmlChar *)name);
98
+ }
99
+
100
+ Local<Value> XmlProcessingInstruction::get_name() {
101
+ Nan::EscapableHandleScope scope;
102
+ if (xml_obj->name)
103
+ return scope.Escape(
104
+ Nan::New<String>((const char *)xml_obj->name).ToLocalChecked());
105
+ else
106
+ return scope.Escape(Nan::Undefined());
107
+ }
108
+
109
+ void XmlProcessingInstruction::set_content(const char *content) {
110
+ xmlNodeSetContent(xml_obj, (xmlChar *)content);
111
+ }
112
+
113
+ Local<Value> XmlProcessingInstruction::get_content() {
114
+ Nan::EscapableHandleScope scope;
115
+ xmlChar *content = xmlNodeGetContent(xml_obj);
116
+ if (content) {
117
+ Local<String> ret_content =
118
+ Nan::New<String>((const char *)content).ToLocalChecked();
119
+ xmlFree(content);
120
+ return scope.Escape(ret_content);
121
+ }
122
+
123
+ return scope.Escape(Nan::New<String>("").ToLocalChecked());
124
+ }
125
+
126
+ Local<Object> XmlProcessingInstruction::New(xmlNode *node) {
127
+ Nan::EscapableHandleScope scope;
128
+ if (node->_private) {
129
+ return scope.Escape(static_cast<XmlNode *>(node->_private)->handle());
130
+ }
131
+
132
+ XmlProcessingInstruction *processing_instruction =
133
+ new XmlProcessingInstruction(node);
134
+ Local<Object> obj =
135
+ Nan::NewInstance(
136
+ Nan::GetFunction(Nan::New(constructor_template)).ToLocalChecked())
137
+ .ToLocalChecked();
138
+ processing_instruction->Wrap(obj);
139
+ return scope.Escape(obj);
140
+ }
141
+
142
+ XmlProcessingInstruction::XmlProcessingInstruction(xmlNode *node)
143
+ : XmlNode(node) {}
144
+
145
+ void XmlProcessingInstruction::Initialize(Local<Object> target) {
146
+ Nan::HandleScope scope;
147
+ Local<FunctionTemplate> t =
148
+ Nan::New<FunctionTemplate>(static_cast<NAN_METHOD((*))>(New));
149
+ t->Inherit(Nan::New(XmlNode::constructor_template));
150
+ t->InstanceTemplate()->SetInternalFieldCount(1);
151
+ constructor_template.Reset(t);
152
+
153
+ Nan::SetPrototypeMethod(t, "name", XmlProcessingInstruction::Name);
154
+
155
+ Nan::SetPrototypeMethod(t, "text", XmlProcessingInstruction::Text);
156
+
157
+ Nan::Set(target, Nan::New<String>("ProcessingInstruction").ToLocalChecked(),
158
+ Nan::GetFunction(t).ToLocalChecked());
159
+ }
160
+
161
+ } // namespace libxmljs
package/src/xml_pi.h ADDED
@@ -0,0 +1,34 @@
1
+ #ifndef SRC_XML_PI_H_
2
+ #define SRC_XML_PI_H_
3
+
4
+ #include "libxmljs.h"
5
+ #include "xml_node.h"
6
+
7
+ namespace libxmljs {
8
+
9
+ class XmlProcessingInstruction : public XmlNode {
10
+ public:
11
+ explicit XmlProcessingInstruction(xmlNode *node);
12
+
13
+ static void Initialize(v8::Local<v8::Object> target);
14
+
15
+ static Nan::Persistent<v8::FunctionTemplate> constructor_template;
16
+
17
+ // create new xml comment to wrap the node
18
+ static v8::Local<v8::Object> New(xmlNode *node);
19
+
20
+ protected:
21
+ static NAN_METHOD(New);
22
+ static NAN_METHOD(Name);
23
+ static NAN_METHOD(Text);
24
+
25
+ void set_name(const char *name);
26
+
27
+ v8::Local<v8::Value> get_name();
28
+ void set_content(const char *content);
29
+ v8::Local<v8::Value> get_content();
30
+ };
31
+
32
+ } // namespace libxmljs
33
+
34
+ #endif // SRC_XML_PI_H_
@@ -0,0 +1,424 @@
1
+ // Copyright 2009, Squish Tech, LLC.
2
+
3
+ #include <node.h>
4
+
5
+ #include <libxml/parserInternals.h>
6
+
7
+ #include "libxmljs.h"
8
+
9
+ #include "xml_sax_parser.h"
10
+
11
+ libxmljs::XmlSaxParser *LXJS_GET_PARSER_FROM_CONTEXT(void *context) {
12
+ _xmlParserCtxt *the_context = static_cast<_xmlParserCtxt *>(context);
13
+ return static_cast<libxmljs::XmlSaxParser *>(the_context->_private);
14
+ }
15
+
16
+ #define EMIT_SYMBOL_STRING "emit"
17
+
18
+ using namespace v8;
19
+ Nan::Persistent<String> emit_symbol;
20
+
21
+ namespace libxmljs {
22
+
23
+ XmlSaxParser::XmlSaxParser() : context_(NULL) {
24
+ xmlSAXHandler tmp = {
25
+ 0, // internalSubset;
26
+ 0, // isStandalone;
27
+ 0, // hasInternalSubset;
28
+ 0, // hasExternalSubset;
29
+ 0, // resolveEntity;
30
+ 0, // getEntity;
31
+ 0, // entityDecl;
32
+ 0, // notationDecl;
33
+ 0, // attributeDecl;
34
+ 0, // elementDecl;
35
+ 0, // unparsedEntityDecl;
36
+ 0, // setDocumentLocator;
37
+ XmlSaxParser::start_document,
38
+ XmlSaxParser::end_document, // endDocument;
39
+ 0, // startElement;
40
+ 0, // endElement;
41
+ 0, // reference;
42
+ XmlSaxParser::characters, // characters;
43
+ 0, // ignorableWhitespace;
44
+ 0, // processingInstruction;
45
+ XmlSaxParser::comment, // comment;
46
+ XmlSaxParser::warning, // warning;
47
+ XmlSaxParser::error, // error;
48
+ 0, // fatalError; /* unused error() get all the errors */
49
+ 0, // getParameterEntity;
50
+ XmlSaxParser::cdata_block, // cdataBlock;
51
+ 0, // externalSubset;
52
+ XML_SAX2_MAGIC, /* force SAX2 */
53
+ this, /* _private */
54
+ XmlSaxParser::start_element_ns, // startElementNs;
55
+ XmlSaxParser::end_element_ns, // endElementNs;
56
+ 0 // SaxParserCallback::structured_error // serror
57
+ };
58
+
59
+ sax_handler_ = tmp;
60
+ }
61
+
62
+ XmlSaxParser::~XmlSaxParser() { this->releaseContext(); }
63
+
64
+ void XmlSaxParser::initializeContext() {
65
+ assert(context_);
66
+ context_->validate = 0;
67
+ context_->_private = this;
68
+ }
69
+
70
+ void XmlSaxParser::releaseContext() {
71
+ if (context_) {
72
+ context_->_private = 0;
73
+ if (context_->myDoc != NULL) {
74
+ xmlFreeDoc(context_->myDoc);
75
+ context_->myDoc = NULL;
76
+ }
77
+
78
+ xmlFreeParserCtxt(context_);
79
+ context_ = 0;
80
+ }
81
+ }
82
+
83
+ NAN_METHOD(XmlSaxParser::NewParser) {
84
+ Nan::HandleScope scope;
85
+ XmlSaxParser *parser = new XmlSaxParser();
86
+ parser->Wrap(info.Holder());
87
+
88
+ return info.GetReturnValue().Set(info.Holder());
89
+ }
90
+
91
+ NAN_METHOD(XmlSaxParser::NewPushParser) {
92
+ Nan::HandleScope scope;
93
+ XmlSaxParser *parser = new XmlSaxParser();
94
+ parser->initialize_push_parser();
95
+ parser->Wrap(info.Holder());
96
+
97
+ return info.GetReturnValue().Set(info.Holder());
98
+ }
99
+
100
+ void XmlSaxParser::Callback(const char *what, int argc, Local<Value> argv[]) {
101
+ Nan::HandleScope scope;
102
+
103
+ // new arguments array with first argument being the event name
104
+ Local<Value> *args = new Local<Value>[argc + 1];
105
+ args[0] = Nan::New<String>(what).ToLocalChecked();
106
+ for (int i = 1; i <= argc; ++i) {
107
+ args[i] = argv[i - 1];
108
+ }
109
+
110
+ // get the 'emit' function from ourselves
111
+ Local<Value> emit_v =
112
+ Nan::Get(this->handle(), Nan::New(emit_symbol)).ToLocalChecked();
113
+ assert(emit_v->IsFunction());
114
+
115
+ // trigger the event
116
+ Nan::AsyncResource res("nan:makeCallback");
117
+ res.runInAsyncScope(this->handle(), Local<Function>::Cast(emit_v), argc + 1,
118
+ args);
119
+
120
+ delete[] args;
121
+ }
122
+
123
+ NAN_METHOD(XmlSaxParser::Push) {
124
+ Nan::HandleScope scope;
125
+ LIBXMLJS_ARGUMENT_TYPE_CHECK(info[0], IsString,
126
+ "Bad Argument: parseString requires a string");
127
+
128
+ XmlSaxParser *parser = Nan::ObjectWrap::Unwrap<XmlSaxParser>(info.Holder());
129
+
130
+ Nan::Utf8String parsable(Nan::To<String>(info[0]).ToLocalChecked());
131
+
132
+ bool terminate = info.Length() > 1
133
+ ? Nan::To<Boolean>(info[1]).ToLocalChecked()->Value()
134
+ : false;
135
+
136
+ parser->push(*parsable, parsable.length(), terminate);
137
+
138
+ return info.GetReturnValue().Set(Nan::True());
139
+ }
140
+
141
+ void XmlSaxParser::initialize_push_parser() {
142
+ context_ = xmlCreatePushParserCtxt(&sax_handler_, NULL, NULL, 0, "");
143
+ context_->replaceEntities = 1;
144
+ initializeContext();
145
+ }
146
+
147
+ void XmlSaxParser::push(const char *str, unsigned int size,
148
+ bool terminate = false) {
149
+ xmlParseChunk(context_, str, size, terminate);
150
+ }
151
+
152
+ NAN_METHOD(XmlSaxParser::ParseString) {
153
+ Nan::HandleScope scope;
154
+ LIBXMLJS_ARGUMENT_TYPE_CHECK(info[0], IsString,
155
+ "Bad Argument: parseString requires a string");
156
+
157
+ XmlSaxParser *parser = Nan::ObjectWrap::Unwrap<XmlSaxParser>(info.Holder());
158
+
159
+ Nan::Utf8String parsable(info[0]);
160
+ parser->parse_string(*parsable, parsable.length());
161
+
162
+ // TODO(sprsquish): return based on the parser
163
+ return info.GetReturnValue().Set(Nan::True());
164
+ }
165
+
166
+ void XmlSaxParser::parse_string(const char *str, unsigned int size) {
167
+ context_ = xmlCreateMemoryParserCtxt(str, size);
168
+ initializeContext();
169
+ context_->replaceEntities = 1;
170
+ xmlSAXHandler *old_sax = context_->sax;
171
+ context_->sax = &sax_handler_;
172
+ xmlParseDocument(context_);
173
+ context_->sax = old_sax;
174
+ releaseContext();
175
+ }
176
+
177
+ void XmlSaxParser::start_document(void *context) {
178
+ libxmljs::XmlSaxParser *parser = LXJS_GET_PARSER_FROM_CONTEXT(context);
179
+ parser->Callback("startDocument");
180
+ }
181
+
182
+ void XmlSaxParser::end_document(void *context) {
183
+ libxmljs::XmlSaxParser *parser = LXJS_GET_PARSER_FROM_CONTEXT(context);
184
+ parser->Callback("endDocument");
185
+ }
186
+
187
+ void XmlSaxParser::start_element_ns(void *context, const xmlChar *localname,
188
+ const xmlChar *prefix, const xmlChar *uri,
189
+ int nb_namespaces,
190
+ const xmlChar **namespaces,
191
+ int nb_attributes, int nb_defaulted,
192
+ const xmlChar **attributes) {
193
+ Nan::HandleScope scope;
194
+ libxmljs::XmlSaxParser *parser = LXJS_GET_PARSER_FROM_CONTEXT(context);
195
+
196
+ const int argc = 5;
197
+ const xmlChar *nsPref, *nsUri, *attrLocal, *attrPref, *attrUri, *attrVal;
198
+ int i, j;
199
+
200
+ Local<Array> elem;
201
+
202
+ // Initialize argv with localname, prefix, and uri
203
+ Local<Value> argv[argc] = {
204
+ Nan::New<String>((const char *)localname).ToLocalChecked()};
205
+
206
+ // Build attributes list
207
+ // Each attribute is an array of [localname, prefix, URI, value, end]
208
+ Local<Array> attrList = Nan::New<Array>(nb_attributes);
209
+ if (attributes) {
210
+ for (i = 0, j = 0; j < nb_attributes; i += 5, j++) {
211
+ attrLocal = attributes[i + 0];
212
+ attrPref = attributes[i + 1];
213
+ attrUri = attributes[i + 2];
214
+ attrVal = attributes[i + 3];
215
+
216
+ elem = Nan::New<Array>(4);
217
+
218
+ Nan::Set(elem, Nan::New<Integer>(0),
219
+ Nan::New<String>((const char *)attrLocal, xmlStrlen(attrLocal))
220
+ .ToLocalChecked());
221
+
222
+ Nan::Set(elem, Nan::New<Integer>(1),
223
+ Nan::New<String>((const char *)attrPref, xmlStrlen(attrPref))
224
+ .ToLocalChecked());
225
+
226
+ Nan::Set(elem, Nan::New<Integer>(2),
227
+ Nan::New<String>((const char *)attrUri, xmlStrlen(attrUri))
228
+ .ToLocalChecked());
229
+
230
+ Nan::Set(
231
+ elem, Nan::New<Integer>(3),
232
+ Nan::New<String>((const char *)attrVal, attributes[i + 4] - attrVal)
233
+ .ToLocalChecked());
234
+
235
+ Nan::Set(attrList, Nan::New<Integer>(j), elem);
236
+ }
237
+ }
238
+ argv[1] = attrList;
239
+
240
+ if (prefix) {
241
+ argv[2] = Nan::New<String>((const char *)prefix).ToLocalChecked();
242
+ } else {
243
+ argv[2] = Nan::Null();
244
+ }
245
+
246
+ if (uri) {
247
+ argv[3] = Nan::New<String>((const char *)uri).ToLocalChecked();
248
+ } else {
249
+ argv[3] = Nan::Null();
250
+ }
251
+
252
+ // Build namespace array of arrays [[prefix, ns], [prefix, ns]]
253
+ Local<Array> nsList = Nan::New<Array>(nb_namespaces);
254
+ if (namespaces) {
255
+ for (i = 0, j = 0; j < nb_namespaces; j++) {
256
+ nsPref = namespaces[i++];
257
+ nsUri = namespaces[i++];
258
+
259
+ elem = Nan::New<Array>(2);
260
+ if (xmlStrlen(nsPref) == 0) {
261
+ Nan::Set(elem, Nan::New<Integer>(0), Nan::Null());
262
+ } else {
263
+ Nan::Set(elem, Nan::New<Integer>(0),
264
+ Nan::New<String>((const char *)nsPref, xmlStrlen(nsPref))
265
+ .ToLocalChecked());
266
+ }
267
+
268
+ Nan::Set(elem, Nan::New<Integer>(1),
269
+ Nan::New<String>((const char *)nsUri, xmlStrlen(nsUri))
270
+ .ToLocalChecked());
271
+
272
+ Nan::Set(nsList, Nan::New<Integer>(j), elem);
273
+ }
274
+ }
275
+ argv[4] = nsList;
276
+
277
+ parser->Callback("startElementNS", argc, argv);
278
+ }
279
+
280
+ void XmlSaxParser::end_element_ns(void *context, const xmlChar *localname,
281
+ const xmlChar *prefix, const xmlChar *uri) {
282
+ Nan::HandleScope scope;
283
+ libxmljs::XmlSaxParser *parser = LXJS_GET_PARSER_FROM_CONTEXT(context);
284
+
285
+ Local<Value> argv[3];
286
+ argv[0] = Nan::New<String>((const char *)localname).ToLocalChecked();
287
+
288
+ if (prefix) {
289
+ argv[1] = Nan::New<String>((const char *)prefix).ToLocalChecked();
290
+ } else {
291
+ argv[1] = Nan::Null();
292
+ }
293
+
294
+ if (uri) {
295
+ argv[2] = Nan::New<String>((const char *)uri).ToLocalChecked();
296
+ } else {
297
+ argv[2] = Nan::Null();
298
+ }
299
+
300
+ parser->Callback("endElementNS", 3, argv);
301
+ }
302
+
303
+ void XmlSaxParser::characters(void *context, const xmlChar *ch, int len) {
304
+ Nan::HandleScope scope;
305
+ libxmljs::XmlSaxParser *parser = LXJS_GET_PARSER_FROM_CONTEXT(context);
306
+
307
+ Local<Value> argv[1] = {
308
+ Nan::New<String>((const char *)ch, len).ToLocalChecked()};
309
+ parser->Callback("characters", 1, argv);
310
+ }
311
+
312
+ void XmlSaxParser::comment(void *context, const xmlChar *value) {
313
+ Nan::HandleScope scope;
314
+ libxmljs::XmlSaxParser *parser = LXJS_GET_PARSER_FROM_CONTEXT(context);
315
+ Local<Value> argv[1] = {
316
+ Nan::New<String>((const char *)value).ToLocalChecked()};
317
+ parser->Callback("comment", 1, argv);
318
+ }
319
+
320
+ void XmlSaxParser::cdata_block(void *context, const xmlChar *value, int len) {
321
+ Nan::HandleScope scope;
322
+ libxmljs::XmlSaxParser *parser = LXJS_GET_PARSER_FROM_CONTEXT(context);
323
+ Local<Value> argv[1] = {
324
+ Nan::New<String>((const char *)value, len).ToLocalChecked()};
325
+ parser->Callback("cdata", 1, argv);
326
+ }
327
+
328
+ #ifdef WIN32
329
+ // https://github.com/json-c/json-c/blob/master/printbuf.c
330
+ // Copyright (c) 2004, 2005 Metaparadigm Pte Ltd
331
+ static int vasprintf(char **buf, const char *fmt, va_list ap) {
332
+ int chars;
333
+ char *b;
334
+
335
+ if (!buf) {
336
+ return -1;
337
+ }
338
+
339
+ chars = _vscprintf(fmt, ap) + 1;
340
+
341
+ b = (char *)malloc(sizeof(char) * chars);
342
+ if (!b) {
343
+ return -1;
344
+ }
345
+
346
+ if ((chars = vsprintf(b, fmt, ap)) < 0) {
347
+ free(b);
348
+ } else {
349
+ *buf = b;
350
+ }
351
+
352
+ return chars;
353
+ }
354
+ #endif /* WIN32 */
355
+
356
+ void XmlSaxParser::warning(void *context, const char *msg, ...) {
357
+ Nan::HandleScope scope;
358
+ libxmljs::XmlSaxParser *parser = LXJS_GET_PARSER_FROM_CONTEXT(context);
359
+
360
+ char *message;
361
+
362
+ va_list args;
363
+ va_start(args, msg);
364
+ if (vasprintf(&message, msg, args) >= 0) {
365
+ Local<Value> argv[1] = {
366
+ Nan::New<String>((const char *)message).ToLocalChecked()};
367
+ parser->Callback("warning", 1, argv);
368
+ }
369
+
370
+ va_end(args);
371
+ free(message);
372
+ }
373
+
374
+ void XmlSaxParser::error(void *context, const char *msg, ...) {
375
+ Nan::HandleScope scope;
376
+ libxmljs::XmlSaxParser *parser = LXJS_GET_PARSER_FROM_CONTEXT(context);
377
+
378
+ char *message;
379
+
380
+ va_list args;
381
+ va_start(args, msg);
382
+ if (vasprintf(&message, msg, args) >= 0) {
383
+ Local<Value> argv[1] = {
384
+ Nan::New<String>((const char *)message).ToLocalChecked()};
385
+ parser->Callback("error", 1, argv);
386
+ }
387
+
388
+ va_end(args);
389
+ free(message);
390
+ }
391
+
392
+ void XmlSaxParser::Initialize(Local<Object> target) {
393
+ Nan::HandleScope scope;
394
+
395
+ emit_symbol.Reset(Nan::New<String>(EMIT_SYMBOL_STRING).ToLocalChecked());
396
+
397
+ // SAX Parser
398
+ Local<FunctionTemplate> parser_t = Nan::New<FunctionTemplate>(NewParser);
399
+
400
+ Nan::Persistent<FunctionTemplate> sax_parser_template;
401
+ sax_parser_template.Reset(parser_t);
402
+
403
+ parser_t->InstanceTemplate()->SetInternalFieldCount(1);
404
+
405
+ Nan::SetPrototypeMethod(parser_t, "parseString", XmlSaxParser::ParseString);
406
+
407
+ Nan::Set(target, Nan::New<String>("SaxParser").ToLocalChecked(),
408
+ Nan::GetFunction(parser_t).ToLocalChecked());
409
+
410
+ Local<FunctionTemplate> push_parser_t =
411
+ Nan::New<FunctionTemplate>(NewPushParser);
412
+
413
+ // Push Parser
414
+ Nan::Persistent<FunctionTemplate> sax_push_parser_template;
415
+ sax_push_parser_template.Reset(push_parser_t);
416
+
417
+ push_parser_t->InstanceTemplate()->SetInternalFieldCount(1);
418
+
419
+ Nan::SetPrototypeMethod(push_parser_t, "push", XmlSaxParser::Push);
420
+
421
+ Nan::Set(target, Nan::New<String>("SaxPushParser").ToLocalChecked(),
422
+ Nan::GetFunction(push_parser_t).ToLocalChecked());
423
+ }
424
+ } // namespace libxmljs