[Pkg-javascript-commits] [node-expat] 16/371: pass encoding optionally to new Parser() or use setEncoding()
Jonas Smedegaard
dr at jones.dk
Sun Feb 28 09:59:40 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 01abaaad27ddae259a5623ccd92eb5a23b3a61a4
Author: Astro <astro at spaceboyz.net>
Date: Sun May 23 16:32:38 2010 +0200
pass encoding optionally to new Parser() or use setEncoding()
---
expat.cc | 45 +++++++++++++++++++++++++++++++++++++++++----
test.js | 3 ++-
2 files changed, 43 insertions(+), 5 deletions(-)
diff --git a/expat.cc b/expat.cc
index 292c0f1..3c7423d 100644
--- a/expat.cc
+++ b/expat.cc
@@ -21,6 +21,7 @@ public:
t->InstanceTemplate()->SetInternalFieldCount(1);
NODE_SET_PROTOTYPE_METHOD(t, "parse", Parse);
+ NODE_SET_PROTOTYPE_METHOD(t, "setEncoding", SetEncoding);
target->Set(String::NewSymbol("Parser"), t->GetFunction());
@@ -35,17 +36,24 @@ protected:
static Handle<Value> New(const Arguments& args)
{
HandleScope scope;
+ XML_Char *encoding = NULL;
+ if (args.Length() == 1 && args[0]->IsString())
+ {
+ encoding = new XML_Char[32];
+ args[0]->ToString()->WriteAscii(encoding, 0, 32);
+ }
- Parser *parser = new Parser();
+ Parser *parser = new Parser(encoding);
+ if (encoding)
+ delete[] encoding;
parser->Wrap(args.This());
-
return args.This();
}
- Parser()
+ Parser(const XML_Char *encoding)
: EventEmitter()
{
- parser = XML_ParserCreate(/*encoding*/ "UTF-8");
+ parser = XML_ParserCreate(encoding);
assert(parser != NULL);
XML_SetUserData(parser, this);
@@ -98,9 +106,38 @@ protected:
return true;
}
+ static Handle<Value> SetEncoding(const Arguments& args)
+ {
+ Parser *parser = ObjectWrap::Unwrap<Parser>(args.This());
+ HandleScope scope;
+
+ if (args.Length() == 1 && args[0]->IsString())
+ {
+ XML_Char *encoding = new XML_Char[32];
+ args[0]->ToString()->WriteAscii(encoding, 0, 32);
+
+ int status = parser->setEncoding(encoding);
+
+ delete[] encoding;
+
+ return scope.Close(status != 0 ? True() : False());
+ }
+ else
+ return False();
+ }
+
+ int setEncoding(XML_Char *encoding)
+ {
+ return XML_SetEncoding(parser, encoding);
+ }
+
private:
+ /* expat instance */
XML_Parser parser;
+ /* no default ctor */
+ Parser();
+
/*** SAX callbacks ***/
/* Should a local HandleScope be used in those callbacks? */
diff --git a/test.js b/test.js
index 0ba7af1..82f626c 100644
--- a/test.js
+++ b/test.js
@@ -25,7 +25,8 @@ function expect(s, evs_expected) {
for(var step = s.length; step > 0; step--) {
iterations++;
var evs_received = [];
- var p = new expat.Parser();
+ var p = new expat.Parser("ISO-8859-1");
+ p.setEncoding("UTF-8");
p.addListener('startElement', function(name, attrs) {
evs_received.push(['startElement', name, attrs]);
});
--
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