Skip to content

Commit

Permalink
Use StringDecoder for Buffers in WritableStream
Browse files Browse the repository at this point in the history
  • Loading branch information
ajafff committed Apr 1, 2016
1 parent 9770f24 commit d79e36d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/WritableStream.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
module.exports = Stream;

var Parser = require("./Parser.js"),
WritableStream = require("stream").Writable || require("readable-stream").Writable;
WritableStream = require("stream").Writable || require("readable-stream").Writable,
StringDecoder = require("string_decoder").StringDecoder,
Buffer = require("buffer").Buffer;

function Stream(cbs, options){
var parser = this._parser = new Parser(cbs, options);
var decoder = this._decoder = new StringDecoder();

WritableStream.call(this, {decodeStrings: false});

this.once("finish", function(){
parser.end();
parser.end(decoder.end());
});
}

require("inherits")(Stream, WritableStream);

WritableStream.prototype._write = function(chunk, encoding, cb){
if(chunk instanceof Buffer) chunk = this._decoder.write(chunk);
this._parser.write(chunk);
cb();
};
21 changes: 21 additions & 0 deletions test/unicode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
var htmlparser2 = require(".."),
assert = require("assert");

describe("WritableStream", function(){

it("should decode fragmented unicode characters", function(){
var processed = false;
var stream = new htmlparser2.WritableStream({
ontext: function(text){
assert.equal(text, "€");
processed = true;
}
});

stream.write(new Buffer([0xE2, 0x82]));
stream.write(new Buffer([0xAC]));
stream.end();

assert(processed);
});
});

0 comments on commit d79e36d

Please sign in to comment.