[Pkg-javascript-commits] [node-expat] 233/371: hook an UnknownEncoding handler, setUnknownEncoding(map)

Jonas Smedegaard dr at jones.dk
Sun Feb 28 10:00:10 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 bbfc2a0a59ea0b2784fd75471176cc099c9c721c
Author: Astro <astro at spaceboyz.net>
Date:   Tue Jan 7 01:18:36 2014 +0100

    hook an UnknownEncoding handler, setUnknownEncoding(map)
---
 README.markdown   |  1 +
 lib/node-expat.js |  4 ++++
 node-expat.cc     | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 test.js           | 31 +++++++++++++++++++++++++++++--
 4 files changed, 85 insertions(+), 2 deletions(-)

diff --git a/README.markdown b/README.markdown
index 4762ffa..41c5095 100644
--- a/README.markdown
+++ b/README.markdown
@@ -49,6 +49,7 @@ Important events emitted by a parser:
     , parser
     ;
 
+  /* pass encoding only if not specified in XML document */
   parser = new expat.Parser("UTF-8");
   parser.on('startElement', function (name, attrs) {
     console.log(name, attrs);
diff --git a/lib/node-expat.js b/lib/node-expat.js
index 8fcf9eb..fb1b39b 100644
--- a/lib/node-expat.js
+++ b/lib/node-expat.js
@@ -22,6 +22,10 @@ Parser.prototype.setEncoding = function(encoding) {
   return this.parser.setEncoding(encoding);
 };
 
+Parser.prototype.setUnknownEncoding = function(map, convert) {
+  return this.parser.setUnknownEncoding(map, convert);
+};
+
 Parser.prototype.getError = function() {
   return this.parser.getError();
 };
diff --git a/node-expat.cc b/node-expat.cc
index 21e7cd5..69187f0 100644
--- a/node-expat.cc
+++ b/node-expat.cc
@@ -17,6 +17,7 @@ public:
 
     NODE_SET_PROTOTYPE_METHOD(t, "parse", Parse);
     NODE_SET_PROTOTYPE_METHOD(t, "setEncoding", SetEncoding);
+    NODE_SET_PROTOTYPE_METHOD(t, "setUnknownEncoding", SetUnknownEncoding);
     NODE_SET_PROTOTYPE_METHOD(t, "getError", GetError);
     NODE_SET_PROTOTYPE_METHOD(t, "stop", Stop);
     NODE_SET_PROTOTYPE_METHOD(t, "resume", Resume);
@@ -72,6 +73,7 @@ protected:
     XML_SetCommentHandler(parser, Comment);
     XML_SetXmlDeclHandler(parser, XmlDecl);
     XML_SetEntityDeclHandler(parser, EntityDecl);
+    XML_SetUnknownEncodingHandler(parser, UnknownEncoding, this);
   }
     
   /*** parse() ***/
@@ -415,6 +417,55 @@ private:
     parser->Emit(8, argv);
   }
 
+  XML_Encoding *xmlEncodingInfo;
+
+  static int UnknownEncoding(void *encodingHandlerData, const XML_Char *name, XML_Encoding *info)
+  {
+    NanScope();
+    Parser *parser = reinterpret_cast<Parser *>(encodingHandlerData);
+
+    /* Trigger event */
+    parser->xmlEncodingInfo = info;
+    Handle<Value> argv[2] = { NanSymbol("unknownEncoding"),
+                              name ? NanNewLocal<Value>(String::New(name))
+                                   : NanNewLocal<Value>(Null())
+    };
+    parser->Emit(2, argv);
+
+    /* Did no event handler invoke setUnknownEncoding()? */
+    if (parser->xmlEncodingInfo) {
+      parser->xmlEncodingInfo = NULL;
+      return XML_STATUS_ERROR;
+    } else {
+      return XML_STATUS_OK;
+    }
+  }
+
+  /**
+   * Fills xmlEncodingInfo
+   */
+  static NAN_METHOD(SetUnknownEncoding)
+  {
+    Parser *parser = ObjectWrap::Unwrap<Parser>(args.This());
+    NanScope();
+
+    if (args.Length() >= 1 && args[0]->IsArray()) {
+      Local<Array> map = args[0].As<Array>();
+      /* Copy map */
+      for(int i = 0; i < 256; i++) {
+        Local<Value> m = map->Get(Integer::New(i));
+        if (m->IsInt32())
+          parser->xmlEncodingInfo->map[i] = m->Int32Value();
+        else
+          NanThrowTypeError("UnknownEncoding map must consist of 256 ints");
+      }
+    } else
+      NanThrowTypeError("SetUnknownEncoding expects a map array");
+
+    parser->xmlEncodingInfo = NULL;
+    NanReturnUndefined();
+  }
+
   void Emit(int argc, Handle<Value> argv[])
   {
     NanScope();
diff --git a/test.js b/test.js
index affc2a9..95e1e5d 100644
--- a/test.js
+++ b/test.js
@@ -24,13 +24,12 @@ function collapseTexts(evs) {
 
 function expect(s, evs_expected) {
   for(var step = s.length; step > 0; step--) {
-    expectWithParserAndStep(s, evs_expected, new expat.Parser("UTF-8"), step);
+    expectWithParserAndStep(s, evs_expected, new expat.Parser(), step);
   }
 }
 
 function expectWithParserAndStep(s, evs_expected, p, step) {
 	var evs_received = [];
-	//p.setEncoding("UTF-8");
 	p.addListener('startElement', function(name, attrs) {
 	    evs_received.push(['startElement', name, attrs]);
 	});
@@ -171,6 +170,34 @@ vows.describe('node-expat').addBatch({
 		[['comment', ' no comment ']]);
 	}
     },
+    'unknownEncoding with single-byte map': {
+        'Windows-1252': function() {
+	    var p = new expat.Parser();
+            var encodingName;
+            p.addListener('unknownEncoding', function(name) {
+                encodingName = name;
+                var map = [];
+                for(var i = 0; i < 256; i++)
+                    map[i] = i;
+                map[165] = 0x00A5;  // ¥
+                map[128] = 0x20AC;  // €
+                map[ 36] = 0x0024;  // $
+                p.setUnknownEncoding(map);
+            });
+            var text = "";
+            p.addListener('text', function(s) {
+                text += s;
+            });
+            p.addListener('error', function(e) {
+                assert.fail(e);
+            });
+            p.parse("<?xml version='1.0' encoding='Windows-1252'?><r>");
+            p.parse(new Buffer([165, 128, 36]));
+            p.parse("</r>");
+            assert.equal(encodingName, "Windows-1252");
+            assert.equal(text, "¥€$");
+        }
+    },
     'error': {
 	'tag name starting with ampersand': function() {
 	    expect("<&", [['error', "not well-formed (invalid token)"]]);

-- 
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