From bb06b07439a1c9f865b65d8fc075b1f91b03b639 Mon Sep 17 00:00:00 2001 From: "mattwillis%gmail.com" Date: Thu, 14 Sep 2006 22:31:59 +0000 Subject: [PATCH] bug 315672 - Manually unfold lines so as to not split UTF-8 character. patch by mvl, r=dmose --- calendar/import-export/calIcsImportExport.js | 30 ++++++++++++++------ 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/calendar/import-export/calIcsImportExport.js b/calendar/import-export/calIcsImportExport.js index c187af1fbb59..af19865b4886 100644 --- a/calendar/import-export/calIcsImportExport.js +++ b/calendar/import-export/calIcsImportExport.js @@ -66,17 +66,31 @@ calIcsImporter.prototype.importFromStream = function ics_importFromStream(aStream, aCount) { var items = new Array(); + // Read in the string. Note that it isn't a real string at this point, because + // likely, the file is utf8. The multibyte chars show up as multiple 'chars' + // in this string. So call it an array of octets for now. + var octetArray = ""; + var scriptableInputStream = Components.classes["@mozilla.org/scriptableinputstream;1"] + .createInstance(Components.interfaces.nsIScriptableInputStream); + scriptableInputStream.init(aStream); + octetArray = scriptableInputStream.read(-1); + + // Some other apps (most notably, sunbird 0.2) happily splits an utf8 character + // between the octets, and adds a newline and space between them, for ics + // folding. Unfold manually before parsing the file as utf8. + // This is utf8 safe, because octets with the first bit 0 are always one-octet + // characters. So the space or the newline never can be part of a multi-byte + // char. + octetArray = octetArray.replace(/\n /, ""); + // Interpret the byte-array as an utf8 string, and convert into a // javascript string. - var convStream = Components.classes["@mozilla.org/intl/converter-input-stream;1"] - .getService(Components.interfaces.nsIConverterInputStream); - convStream.init(aStream, 'UTF-8', 0, 0x0000); + var unicodeConverter = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"] + .createInstance(Components.interfaces.nsIScriptableUnicodeConverter); + // ics files are always utf8 + unicodeConverter.charset = "UTF-8"; + var str = unicodeConverter.ConvertFromUnicode(octetArray); - var tmpStr = {}; - var str = ""; - while (convStream.readString(-1, tmpStr)) { - str += tmpStr.value; - } icssrv = Components.classes["@mozilla.org/calendar/ics-service;1"] .getService(Components.interfaces.calIICSService);