Piotr Caban : jscript: Added Global.encodeURIComponent implementation.

Alexandre Julliard julliard at winehq.org
Wed Oct 14 09:01:59 CDT 2009


Module: wine
Branch: master
Commit: daf4da06461ebc6e323a207b123f9ee94b5dd688
URL:    http://source.winehq.org/git/wine.git/?a=commit;h=daf4da06461ebc6e323a207b123f9ee94b5dd688

Author: Piotr Caban <piotr.caban at gmail.com>
Date:   Tue Oct 13 22:39:20 2009 +0200

 jscript: Added Global.encodeURIComponent implementation.

---

 dlls/jscript/global.c     |   90 ++++++++++++++++++++++++++++++++++++++++++--
 dlls/jscript/tests/api.js |   21 ++++++++++
 2 files changed, 106 insertions(+), 5 deletions(-)

diff --git a/dlls/jscript/global.c b/dlls/jscript/global.c
index 4282d06..9670949 100644
--- a/dlls/jscript/global.c
+++ b/dlls/jscript/global.c
@@ -334,8 +334,10 @@ static HRESULT JSGlobal_escape(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, D
     }
 
     ret = SysAllocStringLen(NULL, len);
-    if(!ret)
+    if(!ret) {
+        SysFreeString(str);
         return E_OUTOFMEMORY;
+    }
 
     len = 0;
     for(ptr=str; *ptr; ptr++) {
@@ -357,6 +359,8 @@ static HRESULT JSGlobal_escape(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, D
         }
     }
 
+    SysFreeString(str);
+
     if(retv) {
         V_VT(retv) = VT_BSTR;
         V_BSTR(retv) = ret;
@@ -690,8 +694,10 @@ static HRESULT JSGlobal_unescape(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
     }
 
     ret = SysAllocStringLen(NULL, len);
-    if(!ret)
+    if(!ret) {
+        SysFreeString(str);
         return E_OUTOFMEMORY;
+    }
 
     len = 0;
     for(ptr=str; *ptr; ptr++) {
@@ -715,6 +721,8 @@ static HRESULT JSGlobal_unescape(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
         len++;
     }
 
+    SysFreeString(str);
+
     if(retv) {
         V_VT(retv) = VT_BSTR;
         V_BSTR(retv) = ret;
@@ -802,6 +810,7 @@ static HRESULT JSGlobal_encodeURI(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags
         }else {
             i = WideCharToMultiByte(CP_UTF8, 0, ptr, 1, NULL, 0, NULL, NULL)*3;
             if(!i) {
+                SysFreeString(str);
                 FIXME("throw URIError\n");
                 return E_FAIL;
             }
@@ -811,8 +820,10 @@ static HRESULT JSGlobal_encodeURI(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags
     }
 
     rptr = ret = SysAllocStringLen(NULL, len);
-    if(!ret)
+    if(!ret) {
+        SysFreeString(str);
         return E_OUTOFMEMORY;
+    }
 
     for(ptr = str; *ptr; ptr++) {
         if(is_uri_unescaped(*ptr) || is_uri_reserved(*ptr) || *ptr == '#') {
@@ -827,6 +838,8 @@ static HRESULT JSGlobal_encodeURI(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags
         }
     }
 
+    SysFreeString(str);
+
     TRACE("%s -> %s\n", debugstr_w(str), debugstr_w(ret));
     if(retv) {
         V_VT(retv) = VT_BSTR;
@@ -847,8 +860,75 @@ static HRESULT JSGlobal_decodeURI(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags
 static HRESULT JSGlobal_encodeURIComponent(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
 {
-    FIXME("\n");
-    return E_NOTIMPL;
+    BSTR str, ret;
+    char buf[4];
+    const WCHAR *ptr;
+    DWORD len = 0, size, i;
+    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(ctx, get_arg(dp, 0), ei, &str);
+    if(FAILED(hres))
+        return hres;
+
+    for(ptr=str; *ptr; ptr++) {
+        if(is_uri_unescaped(*ptr))
+            len++;
+        else {
+            size = WideCharToMultiByte(CP_UTF8, 0, ptr, 1, NULL, 0, NULL, NULL);
+            if(!size) {
+                SysFreeString(str);
+                FIXME("throw Error\n");
+                return E_FAIL;
+            }
+            len += size*3;
+        }
+    }
+
+    ret = SysAllocStringLen(NULL, len);
+    if(!ret) {
+        SysFreeString(str);
+        return E_OUTOFMEMORY;
+    }
+
+    len = 0;
+    for(ptr=str; *ptr; ptr++) {
+        if(is_uri_unescaped(*ptr))
+            ret[len++] = *ptr;
+        else {
+            size = WideCharToMultiByte(CP_UTF8, 0, ptr, 1, buf, sizeof(buf), NULL, NULL);
+            for(i=0; i<size; i++) {
+                ret[len++] = '%';
+                ret[len++] = int_to_char((BYTE)buf[i] >> 4);
+                ret[len++] = int_to_char(buf[i] & 0x0f);
+            }
+        }
+    }
+
+    SysFreeString(str);
+
+    if(retv) {
+        V_VT(retv) = VT_BSTR;
+        V_BSTR(retv) = ret;
+    } else {
+        SysFreeString(ret);
+    }
+
+    return S_OK;
 }
 
 static HRESULT JSGlobal_decodeURIComponent(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
diff --git a/dlls/jscript/tests/api.js b/dlls/jscript/tests/api.js
index 7963016..d7626f1 100644
--- a/dlls/jscript/tests/api.js
+++ b/dlls/jscript/tests/api.js
@@ -58,6 +58,27 @@ ok(tmp === "undefined", "encodeURI() = " + tmp);
 tmp = encodeURI("abc", "test");
 ok(tmp === "abc", "encodeURI('abc') = " + tmp);
 
+tmp = encodeURIComponent("abc");
+ok(tmp === "abc", "encodeURIComponent('abc') = " + tmp);
+tmp = encodeURIComponent("{abc}");
+ok(tmp === "%7Babc%7D", "encodeURIComponent('{abc}') = " + tmp);
+tmp = encodeURIComponent("");
+ok(tmp === "", "encodeURIComponent('') = " + tmp);
+tmp = encodeURIComponent("\01\02\03\04");
+ok(tmp === "%01%02%03%04", "encodeURIComponent('\\01\\02\\03\\04') = " + tmp);
+tmp = encodeURIComponent("{#@}");
+ok(tmp === "%7B%23%40%7D", "encodeURIComponent('{#@}') = " + tmp);
+tmp = encodeURIComponent("\xa1 ");
+ok(tmp === "%C2%A1%20", "encodeURIComponent(\\xa1 ) = " + tmp);
+tmp = encodeURIComponent("\xffff");
+ok(tmp.length === 8, "encodeURIComponent('\\xffff').length = " + tmp.length);
+tmp = encodeURIComponent("abcABC123;/?:@&=+$,-_.!~*'()");
+ok(tmp === "abcABC123%3B%2F%3F%3A%40%26%3D%2B%24%2C-_.!~*'()", "encodeURIComponent('abcABC123;/?:@&=+$,-_.!~*'()') = " + tmp);
+tmp = encodeURIComponent();
+ok(tmp === "undefined", "encodeURIComponent() = " + tmp);
+tmp = encodeURIComponent("abc", "test");
+ok(tmp === "abc", "encodeURIComponent('abc') = " + tmp);
+
 tmp = escape("abc");
 ok(tmp === "abc", "escape('abc') = " + tmp);
 tmp = escape("");




More information about the wine-cvs mailing list