From 00a5b88371945df945ef10623a863cbe1a19a983 Mon Sep 17 00:00:00 2001 From: Kalle Olavi Niemitalo Date: Sun, 20 Aug 2006 13:37:03 +0300 Subject: [PATCH] ECMAScript: Preserve all of Unicode when reading the accessKey property. Setting the property does not yet support Unicode. --- src/ecmascript/see/form.c | 10 ++++---- src/ecmascript/see/input.c | 22 ++++++++++++++++ src/ecmascript/see/input.h | 2 ++ src/ecmascript/spidermonkey/form.c | 41 ++++++++++++++++++++++++++---- 4 files changed, 65 insertions(+), 10 deletions(-) diff --git a/src/ecmascript/see/form.c b/src/ecmascript/see/form.c index 604a0ef68..c42ef025b 100644 --- a/src/ecmascript/see/form.c +++ b/src/ecmascript/see/form.c @@ -182,15 +182,15 @@ input_get(struct SEE_interpreter *interp, struct SEE_object *o, SEE_SET_UNDEFINED(res); if (p == s_accessKey) { - struct string keystr; + struct SEE_string *keystr; if (!link) return; - init_string(&keystr); - add_accesskey_to_string(&keystr, link->accesskey); - str = string_to_SEE_string(interp, keystr.source); + keystr = SEE_string_new(interp, 0); + if (link->accesskey) + append_unicode_to_SEE_string(interp, keystr, + link->accesskey); SEE_SET_STRING(res, str); - done_string(&keystr); } else if (p == s_alt) { str = string_to_SEE_string(interp, fc->alt); SEE_SET_STRING(res, str); diff --git a/src/ecmascript/see/input.c b/src/ecmascript/see/input.c index 34b0f0bc9..38048ac21 100644 --- a/src/ecmascript/see/input.c +++ b/src/ecmascript/see/input.c @@ -101,3 +101,25 @@ string_to_SEE_string(struct SEE_interpreter *interp, unsigned char *s) str->data[i] = s[i]; return str; } + +void +append_unicode_to_SEE_string(struct SEE_interpreter *interp, + struct SEE_string *str, + unicode_val_T u) +{ + if (u <= 0xFFFF) { + /* TODO: Should this reject code points in the + * surrogate range? */ + SEE_string_addch(str, u); + } else if (u <= 0x10FFFF) { + SEE_string_addch(0xD800 + ((u - 0x10000) >> 10)); + SEE_string_addch(0xDC00 + (u & 0x3FF)); + } else { + /* str->interpreter exists but is not documented, so don't + * use it; use a separate @interp parameter instead. + * Also, SEE does not support "%lX". */ + SEE_error_throw(interp, interp->RangeError, + "UTF-16 cannot encode U+%.4X", + (unsigned int) u); + } +} diff --git a/src/ecmascript/see/input.h b/src/ecmascript/see/input.h index 78236e526..05e355864 100644 --- a/src/ecmascript/see/input.h +++ b/src/ecmascript/see/input.h @@ -9,5 +9,7 @@ struct SEE_input *SEE_input_elinks(struct SEE_interpreter *, unsigned char *); unsigned char *SEE_string_to_unsigned_char(struct SEE_string *); unsigned char *SEE_value_to_unsigned_char(struct SEE_interpreter *, struct SEE_value *); struct SEE_string *string_to_SEE_string(struct SEE_interpreter *, unsigned char *); +void append_unicode_to_SEE_string(struct SEE_interpreter *, struct SEE_string *, + unicode_val_T); #endif diff --git a/src/ecmascript/spidermonkey/form.c b/src/ecmascript/spidermonkey/form.c index 7234f82f5..d57f912e2 100644 --- a/src/ecmascript/spidermonkey/form.c +++ b/src/ecmascript/spidermonkey/form.c @@ -117,6 +117,8 @@ static const JSFunctionSpec input_funcs[] = { { NULL } }; +static JSString *unicode_to_jsstring(JSContext *ctx, unicode_val_T u); + static JSBool input_get_property(JSContext *ctx, JSObject *obj, jsval id, jsval *vp) { @@ -146,14 +148,19 @@ input_get_property(JSContext *ctx, JSObject *obj, jsval id, jsval *vp) switch (JSVAL_TO_INT(id)) { case JSP_INPUT_ACCESSKEY: { - struct string keystr; + JSString *keystr; if (!link) break; - init_string(&keystr); - add_accesskey_to_string(&keystr, link->accesskey); - string_to_jsval(ctx, vp, keystr.source); - done_string(&keystr); + if (!link->accesskey) { + *vp = JS_GetEmptyStringValue(ctx); + } else { + keystr = unicode_to_jsstring(ctx, link->accesskey); + if (keystr) + *vp = STRING_TO_JSVAL(keystr); + else + return JS_FALSE; + } break; } case JSP_INPUT_ALT: @@ -970,3 +977,27 @@ forms_namedItem(JSContext *ctx, JSObject *obj, uintN argc, jsval *argv, jsval *r return JS_TRUE; } + + +static JSString * +unicode_to_jsstring(JSContext *ctx, unicode_val_T u) +{ + jschar buf[2]; + + /* If JS_NewUCStringCopyN hits a null character, it truncates + * the string there and pads it with more nulls. However, + * that is not a problem here, because if there is a null + * character in buf[], then it must be the only character. */ + if (u <= 0xFFFF) { + /* TODO: Should this reject code points in the + * surrogate range? */ + buf[0] = u; + return JS_NewUCStringCopyN(ctx, buf, 1); + } else if (u <= 0x10FFFF) { + buf[0] = 0xD800 + ((u - 0x10000) >> 10); + buf[1] = 0xDC00 + (u & 0x3FF); + return JS_NewUCStringCopyN(ctx, buf, 2); + } else { + return NULL; + } +}