[Pkg-javascript-commits] [node-expat] 64/371: Imported Upstream version 1.3.0
Jonas Smedegaard
dr at jones.dk
Sun Feb 28 09:59:45 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 4ca9ec73d1477d9cd190583a57203fa9f8f5a94a
Author: dr at jones.dk <dr at jones.dk>
Date: Sat Apr 16 00:32:53 2011 +0200
Imported Upstream version 1.3.0
---
README.markdown | 3 +++
node-expat.cc | 36 ++++++++++++++++++++++++++
package.json | 3 ++-
test.js | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 121 insertions(+), 1 deletion(-)
diff --git a/README.markdown b/README.markdown
index 573daaa..82dc256 100644
--- a/README.markdown
+++ b/README.markdown
@@ -33,6 +33,9 @@ Important events emitted by a parser:
There are more. Use `test.js` for reference.
+It's possible to stop and resume the parser from within element handlers using the parsers
+stop() and resume() methods.
+
## Error handling ##
We don't emit an error event because libexpat doesn't use a callback
diff --git a/node-expat.cc b/node-expat.cc
index 3f16237..9917043 100644
--- a/node-expat.cc
+++ b/node-expat.cc
@@ -27,6 +27,8 @@ public:
NODE_SET_PROTOTYPE_METHOD(t, "parse", Parse);
NODE_SET_PROTOTYPE_METHOD(t, "setEncoding", SetEncoding);
NODE_SET_PROTOTYPE_METHOD(t, "getError", GetError);
+ NODE_SET_PROTOTYPE_METHOD(t, "stop", Stop);
+ NODE_SET_PROTOTYPE_METHOD(t, "resume", Resume);
target->Set(String::NewSymbol("Parser"), t->GetFunction());
@@ -189,7 +191,41 @@ protected:
else
return scope.Close(Null());
}
+
+ /*** stop() ***/
+
+ static Handle<Value> Stop(const Arguments& args)
+ {
+ Parser *parser = ObjectWrap::Unwrap<Parser>(args.This());
+ HandleScope scope;
+
+ int status = parser->stop();
+
+ return scope.Close(status ? True() : False());
+ }
+
+ int stop()
+ {
+ return XML_StopParser(parser, XML_TRUE) != 0;
+ }
+
+ /*** resume() ***/
+
+ static Handle<Value> Resume(const Arguments& args)
+ {
+ Parser *parser = ObjectWrap::Unwrap<Parser>(args.This());
+ HandleScope scope;
+ int status = parser->resume();
+
+ return scope.Close(status ? True() : False());
+ }
+
+ int resume()
+ {
+ return XML_ResumeParser(parser) != 0;
+ }
+
const XML_LChar *getError()
{
enum XML_Error code;
diff --git a/package.json b/package.json
index a47a9c8..6122ae1 100644
--- a/package.json
+++ b/package.json
@@ -1,5 +1,5 @@
{ "name": "node-expat"
-,"version": "1.2.0"
+,"version": "1.3.0"
,"main": "./build/default/node-expat"
,"description": "NodeJS binding for fast XML parsing."
,"scripts" : { "install" : "./install.sh" }
@@ -13,6 +13,7 @@
,"email": "astro at spaceboyz.net"
,"web": "http://spaceboyz.net/~astro/"
}]
+,"contributors": ["Stephan Maka", "Derek Hammer", "Iein Valdez", "Peter Körner"]
,"licenses": [{ "type": "MIT" }]
,"engine": "node"
}
diff --git a/test.js b/test.js
index 4367f7e..8148ab1 100644
--- a/test.js
+++ b/test.js
@@ -74,6 +74,80 @@ function expect(s, evs_expected) {
}
}
+function testStopResume(cb)
+{
+ var p = new expat.Parser("UTF-8");
+
+ var input = '\
+ <wrap> \
+ <short /> \
+ <short></short> \
+ <long /> \
+ <short /> \
+ <long>foo</long> \
+ </wrap>';
+
+ var expected = ['wrap', 'short', 'short', 'long', 'short', 'long'];
+ var received = [];
+
+ var tolerance = 10/100;
+ var expectedRuntime = 1000;
+ var start = new Date();
+
+ p.addListener('startElement', function(name, attrs) {
+ received.push(name);
+
+ // suspend parser for 1/2 second
+ if(name == 'long') {
+ p.stop();
+
+ setTimeout(function() {
+ p.resume();
+ }, 500);
+ }
+ })
+
+ p.addListener('endElement', function(name) {
+ // finished parsing
+ if(name == 'wrap') {
+ // test elements received (count. naming, order)
+ if(JSON.stringify(expected) != JSON.stringify(received)) {
+ sys.puts("Failed Stop/Resume test");
+ sys.puts("Expected: " + expected);
+ sys.puts("Received: " + received);
+ return cb(false);
+ }
+
+ // test timing (+-5%)
+ var now = new Date();
+ var diff = now.getTime() - start.getTime();
+ var max = expectedRuntime + expectedRuntime * tolerance,
+ min = expectedRuntime - expectedRuntime * tolerance;
+
+ if(diff > max) {
+ sys.puts("Failed Stop/Resume test");
+ sys.puts("Expected Runtime < " + max);
+ sys.puts("Taken Runtime: " + diff);
+ return cb(false);
+ }
+
+ if(diff < min) {
+ sys.puts("Failed Stop/Resume test");
+ sys.puts("Expected Runtime > " + min);
+ sys.puts("Taken Runtime: " + diff);
+ return cb(false);
+ }
+
+ return cb(true);
+ }
+ });
+
+ if(!p.parse(input)) {
+ sys.puts("Failed Stop/Resume test: parse returned error: "+p.getError());
+ return cb(false);
+ }
+}
+
expect("<r/>",
[['startElement', 'r', {}],
['endElement', 'r']]);
@@ -131,3 +205,9 @@ expect(new Buffer('<foo><![CDATA[bar]]></foo>'),
['endElement', 'foo']]);
sys.puts("Ran "+tests+" tests with "+iterations+" iterations: "+fails+" failures.");
+
+sys.puts("Starting Stop/Resume Test (wait for result...)");
+testStopResume(function(success) {
+ sys.puts("Stop/Resume Test "+(success ? 'succeeded' : 'failed'));
+});
+
--
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