Skip to content

Commit 2aad069

Browse files
ajaffffb55
authored andcommitted
1 parent 9770f24 commit 2aad069

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

lib/WritableStream.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
module.exports = Stream;
22

33
var Parser = require("./Parser.js"),
4-
WritableStream = require("stream").Writable || require("readable-stream").Writable;
4+
WritableStream = require("stream").Writable || require("readable-stream").Writable,
5+
StringDecoder = require("string_decoder").StringDecoder,
6+
Buffer = require("buffer").Buffer;
57

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

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

1114
this.once("finish", function(){
12-
parser.end();
15+
parser.end(decoder.end());
1316
});
1417
}
1518

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

1821
WritableStream.prototype._write = function(chunk, encoding, cb){
22+
if(chunk instanceof Buffer) chunk = this._decoder.write(chunk);
1923
this._parser.write(chunk);
2024
cb();
2125
};

test/unicode.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
var htmlparser2 = require(".."),
2+
assert = require("assert");
3+
4+
describe("WritableStream", function(){
5+
6+
it("should decode fragmented unicode characters", function(){
7+
var processed = false;
8+
var stream = new htmlparser2.WritableStream({
9+
ontext: function(text){
10+
assert.equal(text, "€");
11+
processed = true;
12+
}
13+
});
14+
15+
stream.write(new Buffer([0xE2, 0x82]));
16+
stream.write(new Buffer([0xAC]));
17+
stream.end();
18+
19+
assert(processed);
20+
});
21+
});

0 commit comments

Comments
 (0)