From 51d40d823a151435ade261d01b0b6ec9fcfbcbe0 Mon Sep 17 00:00:00 2001 From: Piotr Caban Date: Wed, 2 Sep 2009 12:33:39 +0200 Subject: [PATCH] jscript: Added JSGlobal_unescape implementation. --- dlls/jscript/global.c | 78 +++++++++++++++++++++++++++++++++++++++++++++-- dlls/jscript/tests/api.js | 11 +++++++ 2 files changed, 87 insertions(+), 2 deletions(-) diff --git a/dlls/jscript/global.c b/dlls/jscript/global.c index e843fac02f7..6e932efddb6 100644 --- a/dlls/jscript/global.c +++ b/dlls/jscript/global.c @@ -576,11 +576,85 @@ static HRESULT JSGlobal_parseFloat(DispatchEx *dispex, LCID lcid, WORD flags, DI return S_OK; } +static inline int hex_to_int(const WCHAR wch) { + if(toupperW(wch)>='A' && toupperW(wch)<='F') return toupperW(wch)-'A'+10; + if(isdigitW(wch)) return wch-'0'; + return -1; +} + static HRESULT JSGlobal_unescape(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp) { - FIXME("\n"); - return E_NOTIMPL; + BSTR ret, str; + const WCHAR *ptr; + DWORD len = 0; + HRESULT hres; + + TRACE("\n"); + + if(!arg_cnt(dp)) { + if(retv) { + ret = SysAllocString(undefinedW); + if(!ret) + return E_OUTOFMEMORY; + + V_VT(retv) = VT_BSTR; + V_BSTR(retv) = ret; + } + + return S_OK; + } + + hres = to_string(dispex->ctx, get_arg(dp, 0), ei, &str); + if(FAILED(hres)) + return hres; + + for(ptr=str; *ptr; ptr++) { + if(*ptr == '%') { + if(hex_to_int(*(ptr+1))!=-1 && hex_to_int(*(ptr+2))!=-1) + ptr += 2; + else if(*(ptr+1)=='u' && hex_to_int(*(ptr+2))!=-1 && hex_to_int(*(ptr+3))!=-1 + && hex_to_int(*(ptr+4))!=-1 && hex_to_int(*(ptr+5))!=-1) + ptr += 5; + } + + len++; + } + + ret = SysAllocStringLen(NULL, len); + if(!ret) + return E_OUTOFMEMORY; + + len = 0; + for(ptr=str; *ptr; ptr++) { + if(*ptr == '%') { + if(hex_to_int(*(ptr+1))!=-1 && hex_to_int(*(ptr+2))!=-1) { + ret[len] = (hex_to_int(*(ptr+1))<<4) + hex_to_int(*(ptr+2)); + ptr += 2; + } + else if(*(ptr+1)=='u' && hex_to_int(*(ptr+2))!=-1 && hex_to_int(*(ptr+3))!=-1 + && hex_to_int(*(ptr+4))!=-1 && hex_to_int(*(ptr+5))!=-1) { + ret[len] = (hex_to_int(*(ptr+2))<<12) + (hex_to_int(*(ptr+3))<<8) + + (hex_to_int(*(ptr+4))<<4) + hex_to_int(*(ptr+5)); + ptr += 5; + } + else + ret[len] = *ptr; + } + else + ret[len] = *ptr; + + len++; + } + + if(retv) { + V_VT(retv) = VT_BSTR; + V_BSTR(retv) = ret; + } + else + SysFreeString(ret); + + return S_OK; } static HRESULT JSGlobal_GetObject(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp, diff --git a/dlls/jscript/tests/api.js b/dlls/jscript/tests/api.js index d6e882752b3..136db614bd8 100644 --- a/dlls/jscript/tests/api.js +++ b/dlls/jscript/tests/api.js @@ -58,6 +58,17 @@ ok(tmp === "undefined", "encodeURI() = " + tmp); tmp = encodeURI("abc", "test"); ok(tmp === "abc", "encodeURI('abc') = " + tmp); +tmp = unescape("abc"); +ok(tmp === "abc", "unescape('abc') = " + tmp); +tmp = unescape(""); +ok(tmp === "", "unescape('') = " + tmp); +tmp = unescape("%%%"); +ok(tmp === "%%%", "unescape('%%%') = " + tmp); +tmp = unescape(); +ok(tmp === "undefined", "unescape() = " + tmp); +tmp = unescape("%54%65s%u0074"); +ok(tmp === "Test", "unescape('%54%65s%u0074') = " + tmp); + tmp = "" + new Object(); ok(tmp === "[object Object]", "'' + new Object() = " + tmp); (tmp = new Array).f = Object.prototype.toString; -- 2.11.4.GIT