From 24a9d097d7be00128d4d65903f5c013730be1c2c Mon Sep 17 00:00:00 2001 From: "Dr. David Alan Gilbert" Date: Fri, 7 Apr 2023 20:48:41 +0100 Subject: [PATCH] JS: Implement field.valueAsString MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- core/script/kjs_field.cpp | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/core/script/kjs_field.cpp b/core/script/kjs_field.cpp index 31a89e5a8..e27924f2e 100644 --- a/core/script/kjs_field.cpp +++ b/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(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);