From 6ffaad12aa3c6fae0939cb19b3de7a6a2748dcbe Mon Sep 17 00:00:00 2001 From: Devon Adkisson Date: Sat, 23 Mar 2013 04:21:33 -0400 Subject: [PATCH] Fix Unicode support to cope with Javascript Unicode size limitations --- lib/eventstream.jison | 7 ++++++- lib/eventstream.js | 28 ++++++++++++++-------------- test/eventsource_test.js | 11 +++++++++++ 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/lib/eventstream.jison b/lib/eventstream.jison index cc18ce9..5e68ab1 100644 --- a/lib/eventstream.jison +++ b/lib/eventstream.jison @@ -5,7 +5,12 @@ \u0020 return 'space'; ":" return 'colon'; (\u000D\u000A|\u000D|\u000A) return 'eol'; -[\u0000-\u0009\u000B-\u000C\u000E-\u0019\u0021-\u0039\u003B-\u10FFFF] return 'char'; + +/* Javascript can't support \u10FFFF , it's too large. +\uFFFFF is only 1 less and works correctly, size limitations! */ + +[\u0000-\u0009\u000B-\u000C\u000E-\u0019\u0021-\u0039\u003B-\uFFFFF] return 'char'; + <> return 'EOF'; . return 'INVALID'; diff --git a/lib/eventstream.js b/lib/eventstream.js index f8149c7..09aa98c 100644 --- a/lib/eventstream.js +++ b/lib/eventstream.js @@ -9,11 +9,11 @@ performAction: function anonymous(yytext,yyleng,yylineno,yy,yystate,$$,_$) { var $0 = $$.length - 1; switch (yystate) { -case 1: return this.$; +case 1: return this.$; break; -case 2: this.$ = $$[$0-2]; this.$.push($$[$0-1]); +case 2: this.$ = $$[$0-2]; this.$.push($$[$0-1]); break; -case 3: this.$ = [$$[$0-1]]; +case 3: this.$ = [$$[$0-1]]; break; case 4: this.$ = $$[$0-1]; @@ -22,7 +22,7 @@ case 4: else if ($$[$0].name == 'event') this.$.event = $$[$0].value; else if ($$[$0].name == 'id') this.$.id = $$[$0].value; else if ($$[$0].name == 'retry') this.$.field = $$[$0].value; - + break; case 5: this.$ = { @@ -34,21 +34,21 @@ case 5: else if ($$[$0].name == 'event') this.$.event = $$[$0].value; else if ($$[$0].name == 'id') this.$.id = $$[$0].value; else if ($$[$0].name == 'retry') this.$.field = $$[$0].value; - + break; -case 8: this.$ = { name: 'comment', value: $$[$0] } +case 8: this.$ = { name: 'comment', value: $$[$0] } break; -case 9: this.$ = { name: 'comment', value: '' } +case 9: this.$ = { name: 'comment', value: '' } break; -case 10: this.$ = { name: $$[$0-3], value: $$[$0-1][0] === ' ' ? $$[$0-1].slice(1) : $$[$0-1] } +case 10: this.$ = { name: $$[$0-3], value: $$[$0-1][0] === ' ' ? $$[$0-1].slice(1) : $$[$0-1] } break; -case 11: this.$ = { name: $$[$0-2], value: '' } +case 11: this.$ = { name: $$[$0-2], value: '' } break; -case 12: this.$ = { name: $$[$0-1], value: '' } +case 12: this.$ = { name: $$[$0-1], value: '' } break; -case 14: this.$ = $$[$0-1] + $$[$0]; +case 14: this.$ = $$[$0-1] + $$[$0]; break; -case 16: this.$ = $$[$0-1] + $$[$0]; +case 16: this.$ = $$[$0-1] + $$[$0]; break; } }, @@ -350,7 +350,7 @@ case 6:return 'INVALID'; break; } }; -lexer.rules = [/^(?:^\uFEFF)/,/^(?:\u0020)/,/^(?::)/,/^(?:(\u000D\u000A|\u000D|\u000A))/,/^(?:[\u0000-\u0009\u000B-\u000C\u000E-\u0019\u0021-\u0039\u003B-\u10FFFF])/,/^(?:$)/,/^(?:.)/]; +lexer.rules = [/^(?:^\uFEFF)/,/^(?:\u0020)/,/^(?::)/,/^(?:(\u000D\u000A|\u000D|\u000A))/,/^(?:[\u0000-\u0009\u000B-\u000C\u000E-\u0019\u0021-\u0039\u003B-\uFFFFF])/,/^(?:$)/,/^(?:.)/]; lexer.conditions = {"INITIAL":{"rules":[0,1,2,3,4,5,6],"inclusive":true}}; return lexer;})() parser.lexer = lexer; @@ -375,4 +375,4 @@ exports.main = function commonjsMain(args) { if (typeof module !== 'undefined' && require.main === module) { exports.main(typeof process !== 'undefined' ? process.argv.slice(1) : require("system").args); } -} \ No newline at end of file +} diff --git a/test/eventsource_test.js b/test/eventsource_test.js index 7bd6fbc..f902898 100644 --- a/test/eventsource_test.js +++ b/test/eventsource_test.js @@ -42,6 +42,17 @@ exports['Messages'] = { done(); }, + 'Multibyte Characters': function(test) { + createServer(["id: 1\ndata: €豆腐\n\n"], function(close) { + var es = new EventSource('http://localhost:' + port); + es.onmessage = function(m) { + test.equal("€豆腐", m.data); + es.close(); + close(test.done); + }; + }); + }, + 'issue-18': function(test) { createServer(["id: 1\ndata: hello world\n\n"], function(close) { var es = new EventSource('http://localhost:' + port);