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
remotes/origin/work/sune/use-parsed-distinguished-name
Dr. David Alan Gilbert 3 years ago committed by Albert Astals Cid
parent 074c3dc3c2
commit 24a9d097d7
  1. 20
      core/script/kjs_field.cpp

@ -119,8 +119,7 @@ static KJSObject fieldGetType(KJSContext *, void *object)
return KJSString(fieldGetTypeHelper(field)); return KJSString(fieldGetTypeHelper(field));
} }
// Field.value (getter) static KJSObject fieldGetValueCore(KJSContext *context, bool asString, void *object)
static KJSObject fieldGetValue(KJSContext *context, void *object)
{ {
FormField *field = reinterpret_cast<FormField *>(object); FormField *field = reinterpret_cast<FormField *>(object);
KJSObject result = KJSUndefined(); KJSObject result = KJSUndefined();
@ -140,7 +139,7 @@ static KJSObject fieldGetValue(KJSContext *context, void *object)
const QLocale locale; const QLocale locale;
bool ok; bool ok;
const double textAsNumber = locale.toDouble(text->text(), &ok); const double textAsNumber = locale.toDouble(text->text(), &ok);
if (ok) { if (ok && !asString) {
result = KJSNumber(textAsNumber); result = KJSNumber(textAsNumber);
} else { } else {
result = KJSString(text->text()); 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; return result;
} }
// Field.value (getter)
static KJSObject fieldGetValue(KJSContext *context, void *object)
{
return fieldGetValueCore(context, /*asString*/ false, object);
}
// Field.value (setter) // Field.value (setter)
static void fieldSetValue(KJSContext *context, void *object, KJSObject value) 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) // Field.hidden (getter)
static KJSObject fieldGetHidden(KJSContext *, void *object) 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("readonly"), fieldGetReadOnly, fieldSetReadOnly);
g_fieldProto->defineProperty(ctx, QStringLiteral("type"), fieldGetType); g_fieldProto->defineProperty(ctx, QStringLiteral("type"), fieldGetType);
g_fieldProto->defineProperty(ctx, QStringLiteral("value"), fieldGetValue, fieldSetValue); 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("hidden"), fieldGetHidden, fieldSetHidden);
g_fieldProto->defineProperty(ctx, QStringLiteral("display"), fieldGetDisplay, fieldSetDisplay); g_fieldProto->defineProperty(ctx, QStringLiteral("display"), fieldGetDisplay, fieldSetDisplay);

Loading…
Cancel
Save