[Project_owners] XMLHttpRequest characterset weirdness

Neil neil at parkwaycc.co.uk
Mon Oct 11 01:28:06 EDT 2004


Matthew Wilson wrote:

>     var serializer = new XMLSerializer();
>
>     // This is a BIG HACK but works OK at the moment
>     var str = Components.classes["@mozilla.org/supports-string;1"].
>           createInstance(Components.interfaces.nsISupportsString);
>     str.data = '';
>     var outputStream = {
>         write: function(buf, count) {
>            str.data += buf.substring(0,count);
>            return count;
>        }
>     };
>     serializer.serializeToStream (xmlhttp.responseXML, outputStream, 
> "UTF-8");
>     var targetDocument = "<?xml version='1.0' encoding='UTF-8'?>" + 
> str.data;
>
>
> The BIG HACK is to work around the lack of a scriptable output stream 
> in Mozilla (as far as I am aware).

Is there any reason you used an nsISupportsString rather than a JS 
string, i.e.:

    var serializer = new XMLSerializer();

    // This is a BIG HACK but works OK at the moment
    var str = '';
    var outputStream = {
        write: function(buf, count) {
           str += buf.substring(0,count);
           return count;
       }
    };
    serializer.serializeToStream (xmlhttp.responseXML, outputStream, 
"UTF-8");
    var targetDocument = "<?xml version='1.0' encoding='UTF-8'?>" + str;

You could even encapsulate it:
function StringOutputStream(initialValue) {
    this.data = initialValue || '';
}
StringOutputStream.prototype.write = function write(buf, count) {
    this.data += buf.substring(0, count);
    return count;
}

var outputStream = new StringOutputStream('<?xml version="1.0" 
encoding="UTF-8"?>');
serializer.serializeToStream (xmlhttp.responseXML, outputStream, "UTF-8");

Does jslib not have this (yet)?


More information about the Project_owners mailing list