File tree Expand file tree Collapse file tree 2 files changed +27
-2
lines changed Expand file tree Collapse file tree 2 files changed +27
-2
lines changed Original file line number Diff line number Diff line change 1
1
module . exports = Stream ;
2
2
3
3
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 ;
5
7
6
8
function Stream ( cbs , options ) {
7
9
var parser = this . _parser = new Parser ( cbs , options ) ;
10
+ var decoder = this . _decoder = new StringDecoder ( ) ;
8
11
9
12
WritableStream . call ( this , { decodeStrings : false } ) ;
10
13
11
14
this . once ( "finish" , function ( ) {
12
- parser . end ( ) ;
15
+ parser . end ( decoder . end ( ) ) ;
13
16
} ) ;
14
17
}
15
18
16
19
require ( "inherits" ) ( Stream , WritableStream ) ;
17
20
18
21
WritableStream . prototype . _write = function ( chunk , encoding , cb ) {
22
+ if ( chunk instanceof Buffer ) chunk = this . _decoder . write ( chunk ) ;
19
23
this . _parser . write ( chunk ) ;
20
24
cb ( ) ;
21
25
} ;
Original file line number Diff line number Diff line change
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
+ } ) ;
You can’t perform that action at this time.
0 commit comments