Fix for bug 22594. Implemented window.escape() and unescape(). R=ftang

This commit is contained in:
vidur%netscape.com 2000-01-15 02:26:10 +00:00
Родитель 70ac93177d
Коммит 61085e3a2a
7 изменённых файлов: 283 добавлений и 5 удалений

Просмотреть файл

@ -16,8 +16,6 @@
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
*/
/* AUTO-GENERATED. DO NOT EDIT!!! */
@ -193,6 +191,10 @@ public:
NS_IMETHOD Close()=0;
NS_IMETHOD Close(JSContext* cx, jsval* argv, PRUint32 argc)=0;
NS_IMETHOD Escape(const nsString& aStr, nsString& aReturn)=0;
NS_IMETHOD Unescape(const nsString& aStr, nsString& aReturn)=0;
};
@ -276,6 +278,8 @@ public:
NS_IMETHOD OpenDialog(JSContext* cx, jsval* argv, PRUint32 argc, nsIDOMWindow** aReturn); \
NS_IMETHOD Close(); \
NS_IMETHOD Close(JSContext* cx, jsval* argv, PRUint32 argc); \
NS_IMETHOD Escape(const nsString& aStr, nsString& aReturn); \
NS_IMETHOD Unescape(const nsString& aStr, nsString& aReturn); \
@ -359,6 +363,8 @@ public:
NS_IMETHOD OpenDialog(JSContext* cx, jsval* argv, PRUint32 argc, nsIDOMWindow** aReturn) { return _to OpenDialog(cx, argv, argc, aReturn); } \
NS_IMETHOD Close() { return _to Close(); } \
NS_IMETHOD Close(JSContext* cx, jsval* argv, PRUint32 argc) { return _to Close(cx, argv, argc); } \
NS_IMETHOD Escape(const nsString& aStr, nsString& aReturn) { return _to Escape(aStr, aReturn); } \
NS_IMETHOD Unescape(const nsString& aStr, nsString& aReturn) { return _to Unescape(aStr, aReturn); } \
extern nsresult NS_InitWindowClass(nsIScriptContext *aContext, nsIScriptGlobalObject *aGlobal);

Просмотреть файл

@ -77,6 +77,9 @@
Window openDialog(/* ... */);
noscript void close();
void close(/* ... */);
DOMString escape(in DOMString str);
DOMString unescape(in DOMString str);
};
interface EventTarget {

Просмотреть файл

@ -818,6 +818,7 @@ enum nsDOMProp {
NS_DOM_PROP_WINDOW_DOCUMENT,
NS_DOM_PROP_WINDOW_DUMP,
NS_DOM_PROP_WINDOW_ENABLEEXTERNALCAPTURE,
NS_DOM_PROP_WINDOW_ESCAPE,
NS_DOM_PROP_WINDOW_FOCUS,
NS_DOM_PROP_WINDOW_FORWARD,
NS_DOM_PROP_WINDOW_FRAMES,
@ -865,6 +866,7 @@ enum nsDOMProp {
NS_DOM_PROP_WINDOW_STOP,
NS_DOM_PROP_WINDOW_TOOLBAR,
NS_DOM_PROP_WINDOW_TOP,
NS_DOM_PROP_WINDOW_UNESCAPE,
NS_DOM_PROP_WINDOW_WINDOW,
NS_DOM_PROP_WINDOWCOLLECTION_ITEM,
NS_DOM_PROP_WINDOWCOLLECTION_LENGTH,

Просмотреть файл

@ -817,6 +817,7 @@
"window.document", \
"window.dump", \
"window.enableexternalcapture", \
"window.escape", \
"window.focus", \
"window.forward", \
"window.frames", \
@ -864,6 +865,7 @@
"window.stop", \
"window.toolbar", \
"window.top", \
"window.unescape", \
"window.window", \
"windowcollection.item", \
"windowcollection.length", \

Просмотреть файл

@ -86,6 +86,9 @@
#include "nsDOMPropEnums.h"
#include "nsXPIDLString.h"
#include "nsICookieService.h"
#include "nsICharsetConverterManager.h"
#include "nsEscape.h"
static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID);
#include "nsIJVMManager.h"
@ -123,6 +126,7 @@ static NS_DEFINE_CID(kPrefServiceCID, NS_PREF_CID);
static NS_DEFINE_IID(kCookieServiceCID, NS_COOKIESERVICE_CID);
static NS_DEFINE_CID(kXULControllersCID, NS_XULCONTROLLERS_CID);
static NS_DEFINE_CID(kCharsetConverterManagerCID, NS_ICHARSETCONVERTERMANAGER_CID);
//STATIC METHOD
//one MORE implementation of this function
@ -1406,6 +1410,162 @@ GlobalWindowImpl::Close(JSContext* cx, jsval* argv, PRUint32 argc)
return result;
}
NS_IMETHODIMP
GlobalWindowImpl::Escape(const nsString& aStr, nsString& aReturn)
{
nsresult result;
nsCOMPtr<nsIUnicodeEncoder> encoder;
nsAutoString charset;
NS_WITH_SERVICE(nsICharsetConverterManager, ccm, kCharsetConverterManagerCID, &result);
if (NS_FAILED(result) || !ccm) {
return result;
}
// Get the document character set
charset = "UTF-8"; // default to utf-8
if (mDocument) {
nsCOMPtr<nsIDocument> doc = do_QueryInterface(mDocument);
if (doc) {
result = doc->GetDocumentCharacterSet(charset);
}
}
if (NS_FAILED(result)) {
return result;
}
// Get an encoder for the character set
result = ccm->GetUnicodeEncoder(&charset, getter_AddRefs(encoder));
if (NS_FAILED(result)) {
return result;
}
result = encoder->Reset();
if (NS_FAILED(result)) {
return result;
}
PRInt32 maxByteLen, srcLen;
srcLen = aStr.Length();
const PRUnichar* src = aStr.GetUnicode();
// Get the expected length of result string
result = encoder->GetMaxLength(src, srcLen, &maxByteLen);
if (NS_FAILED(result)) {
return result;
}
// Allocate a buffer of the maximum length
char* dest = (char*)nsAllocator::Alloc(maxByteLen+1);
PRInt32 destLen2, destLen = maxByteLen;
if (nsnull == dest) {
return NS_ERROR_OUT_OF_MEMORY;
}
// Convert from unicode to the character set
result = encoder->Convert(src, &srcLen, dest, &destLen);
if (NS_FAILED(result)) {
nsAllocator::Free(dest);
return result;
}
// Allow the encoder to finish the conversion
destLen2 = maxByteLen - destLen;
encoder->Finish(dest+destLen, &destLen2);
dest[destLen+destLen2] = '\0';
// Escape the string
char* outBuf = nsEscape(dest, nsEscapeMask(url_XAlphas | url_XPAlphas | url_Path));
aReturn.SetString(outBuf);
nsAllocator::Free(outBuf);
nsAllocator::Free(dest);
return result;
}
NS_IMETHODIMP
GlobalWindowImpl::Unescape(const nsString& aStr, nsString& aReturn)
{
nsresult result;
nsCOMPtr<nsIUnicodeDecoder> decoder;
nsAutoString charset;
NS_WITH_SERVICE(nsICharsetConverterManager, ccm, kCharsetConverterManagerCID, &result);
if (NS_FAILED(result) || !ccm) {
return result;
}
// Get the document character set
charset = "UTF-8"; // default to utf-8
if (mDocument) {
nsCOMPtr<nsIDocument> doc = do_QueryInterface(mDocument);
if (doc) {
result = doc->GetDocumentCharacterSet(charset);
}
}
if (NS_FAILED(result)) {
return result;
}
// Get an decoder for the character set
result = ccm->GetUnicodeDecoder(&charset, getter_AddRefs(decoder));
if (NS_FAILED(result)) {
return result;
}
result = decoder->Reset();
if (NS_FAILED(result)) {
return result;
}
// Need to copy to do the two-byte to one-byte deflation
char* inBuf = aStr.ToNewCString();
if (nsnull == inBuf) {
return NS_ERROR_OUT_OF_MEMORY;
}
// Unescape the string
char* src = nsUnescape(inBuf);
nsAllocator::Free(inBuf);
if (nsnull == src) {
return NS_ERROR_OUT_OF_MEMORY;
}
PRInt32 maxLength, srcLen;
srcLen = aStr.Length();
// Get the expected length of the result string
result = decoder->GetMaxLength(src, srcLen, &maxLength);
if (NS_FAILED(result)) {
nsAllocator::Free(src);
return result;
}
// Allocate a buffer of the maximum length
PRUnichar* dest = (PRUnichar*)nsAllocator::Alloc(sizeof(PRUnichar)*maxLength);
PRInt32 destLen = maxLength;
if (nsnull == dest) {
nsAllocator::Free(src);
return NS_ERROR_OUT_OF_MEMORY;
}
// Convert from character set to unicode
result = decoder->Convert(src, &srcLen, dest, &destLen);
nsAllocator::Free(src);
if (NS_FAILED(result)) {
nsAllocator::Free(dest);
return result;
}
aReturn.SetString(dest, destLen);
nsAllocator::Free(dest);
return NS_OK;
}
NS_IMETHODIMP
GlobalWindowImpl::Forward()
{

Просмотреть файл

@ -186,6 +186,9 @@ public:
NS_IMETHOD Close();
NS_IMETHOD Close(JSContext* cx, jsval* argv, PRUint32 argc);
NS_IMETHOD Escape(const nsString& aStr, nsString& aReturn);
NS_IMETHOD Unescape(const nsString& aStr, nsString& aReturn);
NS_IMETHOD CaptureEvents(PRInt32 aEventFlags);
NS_IMETHOD ReleaseEvents(PRInt32 aEventFlags);
NS_IMETHOD RouteEvent(nsIDOMEvent* aEvt);

Просмотреть файл

@ -16,8 +16,6 @@
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
*/
/* AUTO-GENERATED. DO NOT EDIT!!! */
@ -480,7 +478,7 @@ GetWindowProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
result = a->GetControllers(&prop);
if (NS_SUCCEEDED(result)) {
// get the js object; n.b., this will do a release on 'prop'
nsJSUtils::nsConvertXPCObjectToJSVal(prop, nsIControllers::GetIID(), cx, obj, vp);
nsJSUtils::nsConvertXPCObjectToJSVal(prop, NS_GET_IID(nsIControllers), cx, obj, vp);
}
else {
return nsJSUtils::nsReportError(cx, obj, result);
@ -2662,6 +2660,108 @@ WindowClose(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
}
//
// Native method Escape
//
PR_STATIC_CALLBACK(JSBool)
WindowEscape(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
nsIDOMWindow *nativeThis = (nsIDOMWindow*)nsJSUtils::nsGetNativeThis(cx, obj);
nsresult result = NS_OK;
nsAutoString nativeRet;
nsAutoString b0;
// If there's no private data, this must be the prototype, so ignore
if (nsnull == nativeThis) {
return JS_TRUE;
}
{
*rval = JSVAL_NULL;
{
PRBool ok;
nsresult rv;
NS_WITH_SERVICE(nsIScriptSecurityManager, secMan,
NS_SCRIPTSECURITYMANAGER_PROGID, &rv);
if (NS_FAILED(rv)) {
return nsJSUtils::nsReportError(cx, obj, NS_ERROR_DOM_SECMAN_ERR);
}
secMan->CheckScriptAccess(cx, obj, NS_DOM_PROP_WINDOW_ESCAPE, PR_FALSE, &ok);
if (!ok) {
return nsJSUtils::nsReportError(cx, obj, NS_ERROR_DOM_SECURITY_ERR);
}
}
if (argc < 1) {
return nsJSUtils::nsReportError(cx, obj, NS_ERROR_DOM_TOO_FEW_PARAMETERS_ERR);
}
nsJSUtils::nsConvertJSValToString(b0, cx, argv[0]);
result = nativeThis->Escape(b0, nativeRet);
if (NS_FAILED(result)) {
return nsJSUtils::nsReportError(cx, obj, result);
}
nsJSUtils::nsConvertStringToJSVal(nativeRet, cx, rval);
}
return JS_TRUE;
}
//
// Native method Unescape
//
PR_STATIC_CALLBACK(JSBool)
WindowUnescape(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
nsIDOMWindow *nativeThis = (nsIDOMWindow*)nsJSUtils::nsGetNativeThis(cx, obj);
nsresult result = NS_OK;
nsAutoString nativeRet;
nsAutoString b0;
// If there's no private data, this must be the prototype, so ignore
if (nsnull == nativeThis) {
return JS_TRUE;
}
{
*rval = JSVAL_NULL;
{
PRBool ok;
nsresult rv;
NS_WITH_SERVICE(nsIScriptSecurityManager, secMan,
NS_SCRIPTSECURITYMANAGER_PROGID, &rv);
if (NS_FAILED(rv)) {
return nsJSUtils::nsReportError(cx, obj, NS_ERROR_DOM_SECMAN_ERR);
}
secMan->CheckScriptAccess(cx, obj, NS_DOM_PROP_WINDOW_UNESCAPE, PR_FALSE, &ok);
if (!ok) {
return nsJSUtils::nsReportError(cx, obj, NS_ERROR_DOM_SECURITY_ERR);
}
}
if (argc < 1) {
return nsJSUtils::nsReportError(cx, obj, NS_ERROR_DOM_TOO_FEW_PARAMETERS_ERR);
}
nsJSUtils::nsConvertJSValToString(b0, cx, argv[0]);
result = nativeThis->Unescape(b0, nativeRet);
if (NS_FAILED(result)) {
return nsJSUtils::nsReportError(cx, obj, result);
}
nsJSUtils::nsConvertStringToJSVal(nativeRet, cx, rval);
}
return JS_TRUE;
}
//
// Native method AddEventListener
//
@ -2892,6 +2992,8 @@ static JSFunctionSpec WindowMethods[] =
{"open", WindowOpen, 0},
{"openDialog", WindowOpenDialog, 0},
{"close", WindowClose, 0},
{"escape", WindowEscape, 1},
{"unescape", WindowUnescape, 1},
{"addEventListener", EventTargetAddEventListener, 3},
{"removeEventListener", EventTargetRemoveEventListener, 3},
{0}