JS: Implement field.valueAsString

Adobe's docs say:
  For example, for a field with a value of “020”, value returns the integer 20,
   while valueAsString returns the string “020”.

Share most of the fieldGetValue code by splitting it out into a 'Core'
variant and then using it in both GetValue and GetValueAsString.

BUGS: 468036
(cherry picked from commit 24a9d097d7)
remotes/origin/release/23.04
Dr. David Alan Gilbert 3 years ago committed by Albert Astals Cid
parent f6b432e1fa
commit a747625c79
  1. 20
      core/script/kjs_field.cpp

@ -119,8 +119,7 @@ static KJSObject fieldGetType(KJSContext *, void *object)
return KJSString(fieldGetTypeHelper(field));
}
// Field.value (getter)
static KJSObject fieldGetValue(KJSContext *context, void *object)
static KJSObject fieldGetValueCore(KJSContext *context, bool asString, void *object)
{
FormField *field = reinterpret_cast<FormField *>(object);
KJSObject result = KJSUndefined();
@ -140,7 +139,7 @@ static KJSObject fieldGetValue(KJSContext *context, void *object)
const QLocale locale;
bool ok;
const double textAsNumber = locale.toDouble(text->text(), &ok);
if (ok) {
if (ok && !asString) {
result = KJSNumber(textAsNumber);
} else {
result = KJSString(text->text());
@ -160,9 +159,15 @@ static KJSObject fieldGetValue(KJSContext *context, void *object)
}
}
qCDebug(OkularCoreDebug) << "fieldGetValue: Field: " << field->fullyQualifiedName() << " Type: " << fieldGetTypeHelper(field) << " Value: " << result.toString(context) << (result.isString() ? "(as string)" : "");
qCDebug(OkularCoreDebug) << "fieldGetValueCore:"
<< " Field: " << field->fullyQualifiedName() << " Type: " << fieldGetTypeHelper(field) << " Value: " << result.toString(context) << (result.isString() ? "(as string)" : "");
return result;
}
// Field.value (getter)
static KJSObject fieldGetValue(KJSContext *context, void *object)
{
return fieldGetValueCore(context, /*asString*/ false, object);
}
// Field.value (setter)
static void fieldSetValue(KJSContext *context, void *object, KJSObject value)
@ -203,6 +208,12 @@ static void fieldSetValue(KJSContext *context, void *object, KJSObject value)
}
}
// Field.valueAsString (getter)
static KJSObject fieldGetValueAsString(KJSContext *context, void *object)
{
return fieldGetValueCore(context, /*asString*/ true, object);
}
// Field.hidden (getter)
static KJSObject fieldGetHidden(KJSContext *, void *object)
{
@ -305,6 +316,7 @@ void JSField::initType(KJSContext *ctx)
g_fieldProto->defineProperty(ctx, QStringLiteral("readonly"), fieldGetReadOnly, fieldSetReadOnly);
g_fieldProto->defineProperty(ctx, QStringLiteral("type"), fieldGetType);
g_fieldProto->defineProperty(ctx, QStringLiteral("value"), fieldGetValue, fieldSetValue);
g_fieldProto->defineProperty(ctx, QStringLiteral("valueAsString"), fieldGetValueAsString);
g_fieldProto->defineProperty(ctx, QStringLiteral("hidden"), fieldGetHidden, fieldSetHidden);
g_fieldProto->defineProperty(ctx, QStringLiteral("display"), fieldGetDisplay, fieldSetDisplay);

Loading…
Cancel
Save