1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-12-04 14:46:47 -05:00

ECMAScript: Preserve all of Unicode when reading the accessKey property.

Setting the property does not yet support Unicode.
This commit is contained in:
Kalle Olavi Niemitalo 2006-08-20 13:37:03 +03:00 committed by Kalle Olavi Niemitalo
parent 245c8cb020
commit 00a5b88371
4 changed files with 65 additions and 10 deletions

View File

@ -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);

View File

@ -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);
}
}

View File

@ -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

View File

@ -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;
}
}