[Pkg-javascript-commits] [node-expat] 02/371: StartElement & EndElement events + tests

Jonas Smedegaard dr at jones.dk
Sun Feb 28 09:59:39 UTC 2016


This is an automated email from the git hooks/post-receive script.

js pushed a commit to branch master
in repository node-expat.

commit 417e1f2bd1736cbee45e7fd2a6636c07df47299d
Author: Astro <astro at spaceboyz.net>
Date:   Sat May 22 15:04:33 2010 +0200

    StartElement & EndElement events + tests
---
 expat.cc | 40 +++++++++++++++++++++++++++++++++++++---
 test.js  | 42 ++++++++++++++++++++++++++++++++++++++----
 2 files changed, 75 insertions(+), 7 deletions(-)

diff --git a/expat.cc b/expat.cc
index c66c596..bae878e 100644
--- a/expat.cc
+++ b/expat.cc
@@ -7,6 +7,11 @@ extern "C" {
 using namespace v8;
 using namespace node;
 
+static void StartElement(void *userData, const XML_Char *name, const XML_Char **atts);
+static void EndElement(void *userData, const XML_Char *name);
+
+static Persistent<String> sym_startElement, sym_endElement;
+
 class Parser : public EventEmitter {
 public:
   static void Initialize(Handle<Object> target)
@@ -21,18 +26,19 @@ public:
 
     target->Set(String::NewSymbol("Parser"), t->GetFunction());
     printf("initialized %i\n", t->GetFunction()->IsFunction());
+
+    sym_startElement = NODE_PSYMBOL("startElement");
+    sym_endElement = NODE_PSYMBOL("endElement");
   }
 
 protected:
   static Handle<Value> New(const Arguments& args)
   {
     HandleScope scope;
-    printf("new\n");
 
     Parser *parser = new Parser();
     parser->Wrap(args.This());
     
-    printf("newed\n");
     return args.This();
   }
 
@@ -41,11 +47,15 @@ protected:
   {
     parser = XML_ParserCreate(/*encoding*/ "UTF-8");
     assert(parser != NULL);
+
+    XML_SetUserData(parser, this);
+    XML_SetElementHandler(parser, StartElement, EndElement);
   }
 
   ~Parser()
   {
-    //assert(parser == NULL);
+    printf("dtor\n");
+    XML_ParserFree(parser);
   }
 
   static Handle<Value> Parse(const Arguments& args)
@@ -91,6 +101,30 @@ private:
 };
 
 
+static void StartElement(void *userData, const XML_Char *name, const XML_Char **atts)
+{
+  Parser *parser = reinterpret_cast<Parser *>(userData);
+
+  /* Collect atts into JS object */
+  Local<Object> attr = Object::New();
+  for(const XML_Char **atts1 = atts; *atts1; atts1 += 2)
+    attr->Set(String::New(atts1[0]), String::New(atts1[1]));
+
+  /* Trigger event */
+  Handle<Value> argv[2] = { String::New(name), attr };
+  parser->Emit(sym_startElement, 2, argv);
+}
+
+static void EndElement(void *userData, const XML_Char *name)
+{
+  Parser *parser = reinterpret_cast<Parser *>(userData);
+
+  /* Trigger event */
+  Handle<Value> argv[1] = { String::New(name) };
+  parser->Emit(sym_endElement, 1, argv);
+}
+
+
 extern "C" void init(Handle<Object> target)
 {
   HandleScope scope;
diff --git a/test.js b/test.js
index a6c0589..fc4db89 100644
--- a/test.js
+++ b/test.js
@@ -1,7 +1,41 @@
 var sys = require('sys');
 var expat = require('./build/default/expat');
 
-sys.puts("newing");
-var p = new expat.Parser();
-sys.puts("newed");
-p.parse("<foo bar='baz'/>");
+function expect(s, evs_expected) {
+    var evs_received = [];
+    var p = new expat.Parser();
+    p.addListener('startElement', function(name, attrs) {
+	evs_received.push(['startElement', name, attrs]);
+    });
+    p.addListener('endElement', function(name) {
+	evs_received.push(['endElement', name]);
+    });
+    p.addListener('text', function(s) {
+	evs_received.push(['text', s]);
+    });
+    p.addListener('cdata', function(s) {
+	evs_received.push(['cdata', s]);
+    });
+    p.parse(s, true);
+
+    var expected = JSON.stringify(evs_expected);
+    var received = JSON.stringify(evs_received);
+    if (expected != received) {
+	sys.puts("Fail for: " + s);
+	sys.puts("Expected: " + expected);
+	sys.puts("Received: " + received);
+    }
+}
+
+expect("<r/>",
+       [['startElement', 'r', {}],
+	['endElement', 'r']]);
+expect("<r foo='bar'/>",
+       [['startElement', 'r', {foo: 'bar'}],
+	['endElement', 'r']]);
+expect("<r foo='bar' baz=\"quux\" test=\"tset\"/>",
+       [['startElement', 'r', {foo: 'bar', baz: 'quux', test: 'tset'}],
+	['endElement', 'r']]);
+expect("<r xmlns='http://localhost/' xmlns:x=\"http://example.com/\"></r>",
+       [['startElement', 'r', {xmlns: 'http://localhost/', 'xmlns:x': 'http://example.com/'}],
+	['endElement', 'r']]);

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-expat.git



More information about the Pkg-javascript-commits mailing list