From c7432cdae68b4201f15dce4f14a970e039532c00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Netto?= Date: Sat, 27 Jul 2019 16:37:20 -0300 Subject: [PATCH] Implemented support for AFSpecial_Format and AFSpecial_Keystroke --- autotests/CMakeLists.txt | 1 + autotests/formattest.cpp | 74 +++++++++++++++++++++++++++++++++ core/script/builtin.js | 89 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 164 insertions(+) diff --git a/autotests/CMakeLists.txt b/autotests/CMakeLists.txt index d40d56650..fe161bd77 100644 --- a/autotests/CMakeLists.txt +++ b/autotests/CMakeLists.txt @@ -22,6 +22,7 @@ if(Poppler_Qt5_FOUND) TEST_NAME "kjsfunctionstest" LINK_LIBRARIES Qt5::Widgets Qt5::Test okularcore ) + ecm_add_test(formattest.cpp TEST_NAME "formattest" LINK_LIBRARIES Qt5::Widgets Qt5::Test okularcore diff --git a/autotests/formattest.cpp b/autotests/formattest.cpp index e5b8c5813..525f24abd 100644 --- a/autotests/formattest.cpp +++ b/autotests/formattest.cpp @@ -28,6 +28,7 @@ private slots: void initTestCase(); void cleanupTestCase(); void testTimeFormat(); + void testSpecialFormat(); private: Okular::Document *m_document; @@ -135,6 +136,79 @@ void FormatTest::testTimeFormat() QCOMPARE( QStringLiteral( "1:20:00 pm" ), m_formattedText ); } +void FormatTest::testSpecialFormat() +{ + Okular::FormFieldText *fft = reinterpret_cast< Okular::FormFieldText * >( m_fields[ QStringLiteral( "CEP" ) ] ); + + // This field will just become itself, so we test only if keystroke worked. + fft->setText( QStringLiteral( "12345" ) ); + bool ok = false; + m_document->processKeystrokeAction( fft->additionalAction( Okular::FormField::FieldModified ), fft, ok ); + + QCOMPARE( true, ok ); + + fft->setText( QStringLiteral( "123456" ) ); + ok = false; + m_document->processKeystrokeAction( fft->additionalAction( Okular::FormField::FieldModified ), fft, ok ); + m_document->processFormatAction( fft->additionalAction( Okular::FormField::FormatField ), fft ); + + QCOMPARE( false, ok ); + + fft = reinterpret_cast< Okular::FormFieldText * >( m_fields[ QStringLiteral( "8Digits" ) ] ); + + fft->setText( QStringLiteral( "123456789" ) ); + ok = false; + m_document->processKeystrokeAction( fft->additionalAction( Okular::FormField::FieldModified ), fft, ok ); + m_document->processFormatAction( fft->additionalAction( Okular::FormField::FormatField ), fft ); + + QCOMPARE( true, ok ); + QCOMPARE( m_formattedText, QStringLiteral( "12345-6789" ) ); + + fft->setText( QStringLiteral( "1234567890" ) ); + ok = false; + m_document->processKeystrokeAction( fft->additionalAction( Okular::FormField::FieldModified ), fft, ok ); + m_document->processFormatAction( fft->additionalAction( Okular::FormField::FormatField ), fft ); + + QCOMPARE( false, ok ); + QCOMPARE( m_formattedText, QStringLiteral( "12345-6789" ) ); + + fft = reinterpret_cast< Okular::FormFieldText * >( m_fields[ QStringLiteral( "telefone" ) ] ); + + fft->setText( QStringLiteral( "1234567890" ) ); + ok = false; + m_document->processKeystrokeAction( fft->additionalAction( Okular::FormField::FieldModified ), fft, ok ); + m_document->processFormatAction( fft->additionalAction( Okular::FormField::FormatField ), fft ); + + QCOMPARE( true, ok ); + QCOMPARE( m_formattedText, QStringLiteral("(123) 456-7890" ) ); + + fft->setText( QStringLiteral( "12345678900" ) ); + ok = false; + m_document->processKeystrokeAction( fft->additionalAction( Okular::FormField::FieldModified ), fft, ok ); + m_document->processFormatAction( fft->additionalAction( Okular::FormField::FormatField ), fft ); + + QCOMPARE( false, ok ); + QCOMPARE( m_formattedText, QStringLiteral("(123) 456-7890" ) ); + + fft = reinterpret_cast< Okular::FormFieldText * >( m_fields[ QStringLiteral( "CPF" ) ] ); + + fft->setText( QStringLiteral( "123456789" ) ); + ok = false; + m_document->processKeystrokeAction( fft->additionalAction( Okular::FormField::FieldModified ), fft, ok ); + m_document->processFormatAction( fft->additionalAction( Okular::FormField::FormatField ), fft ); + + QCOMPARE( true, ok ); + QCOMPARE( m_formattedText, QStringLiteral( "123-45-6789" ) ); + + fft->setText( QStringLiteral( "1234567890" ) ); + ok = false; + m_document->processKeystrokeAction( fft->additionalAction( Okular::FormField::FieldModified ), fft, ok ); + m_document->processFormatAction( fft->additionalAction( Okular::FormField::FormatField ), fft ); + + QCOMPARE( false, ok ); + QCOMPARE( m_formattedText, QStringLiteral( "123-45-6789" ) ); +} + void FormatTest::cleanupTestCase() { m_document->closeDocument(); diff --git a/core/script/builtin.js b/core/script/builtin.js index a5dcb3904..878ab49aa 100644 --- a/core/script/builtin.js +++ b/core/script/builtin.js @@ -156,3 +156,92 @@ function AFTime_Keystroke( ptf ) { return; } + +/** AFSpecial_Format + * psf is the type of formatting to use: + * 0 = zip code + * 1 = zip + 4 + * 2 = phone + * 3 = SSN + * + * These are all in the US format. +*/ +function AFSpecial_Format( psf ) +{ + if( !event.value || psf == 0 ) + { + return; + } + + var ret = event.value; + + if( psf === 1 ) + ret = ret.substr( 0, 5 ) + '-' + ret.substr( 5, 4 ); + + else if( psf === 2 ) + ret = '(' + ret.substr( 0, 3 ) + ') ' + ret.substr( 3, 3 ) + '-' + ret.substr( 6, 4 ); + + else if( psf === 3 ) + ret = ret.substr( 0, 3 ) + '-' + ret.substr( 3, 2 ) + '-' + ret.substr( 5, 4 ); + + event.value = ret; +} + +/** AFSpecial_Keystroke + * + * Checks if the String in event.value is valid. + * + * Parameter description based on Acrobat Help: + * + * psf is the type of formatting to use: + * 0 = zip code + * 1 = zip + 4 + * 2 = phone + * 3 = SSN + * + * These are all in the US format. We check to see if only numbers are inserted and the length of the string. +*/ +function AFSpecial_Keystroke( psf ) +{ + if ( !event.value ) + { + return; + } + + var str = event.value; + if( psf === 0 ) + { + if( str.length > 5 ) + { + event.rc = false; + return; + } + } + + else if( psf === 1 || psf === 3 ) + { + if( str.length > 9 ) + { + event.rc = false; + return; + } + } + + else if( psf === 2 ) + { + if( str.length > 10 ) + { + event.rc = false; + return; + } + } + + for( i = 0 ; i < str.length ; ++i ) + { + if( !( str[i] <= '9' && str[i] >= '0' ) ) + { + event.rc = false; + return; + } + } +} -- GitLab