summaryrefslogtreecommitdiff
path: root/fpdfsdk/pdfwindow
diff options
context:
space:
mode:
authorDiana Gage <drgage@google.com>2017-06-20 11:17:11 -0700
committerChromium commit bot <commit-bot@chromium.org>2017-06-20 21:41:22 +0000
commitdce2d72f9fbc166ee8eed0e362ab26e1e1a33cdd (patch)
tree410816fa09ffea9d2db198b5138f45ea55253824 /fpdfsdk/pdfwindow
parentb5c5ec06396158232d7f6955dde7b3512df0ae6e (diff)
downloadpdfium-dce2d72f9fbc166ee8eed0e362ab26e1e1a33cdd.tar.xz
Add FORM_GetSelectedText() function.
This function copies the selected text from a form text field or form combobox text field into the buffer parameter and returns the length of the selected text string. When buffer is a nullptr or buflen is less than the length of the selected text, this function does not modify the buffer and only returns the selected text length. BUG=chromium:59266 Change-Id: Ie77de38e45bbe6f9ea033826c961435304eedfc7 Reviewed-on: https://pdfium-review.googlesource.com/6413 Reviewed-by: dsinclair <dsinclair@chromium.org> Reviewed-by: Lei Zhang <thestig@chromium.org> Commit-Queue: Lei Zhang <thestig@chromium.org>
Diffstat (limited to 'fpdfsdk/pdfwindow')
-rw-r--r--fpdfsdk/pdfwindow/cpwl_combo_box.cpp7
-rw-r--r--fpdfsdk/pdfwindow/cpwl_combo_box.h1
-rw-r--r--fpdfsdk/pdfwindow/cpwl_edit_ctrl.cpp7
-rw-r--r--fpdfsdk/pdfwindow/cpwl_edit_ctrl.h1
-rw-r--r--fpdfsdk/pdfwindow/cpwl_edit_embeddertest.cpp138
-rw-r--r--fpdfsdk/pdfwindow/cpwl_wnd.cpp4
-rw-r--r--fpdfsdk/pdfwindow/cpwl_wnd.h1
7 files changed, 122 insertions, 37 deletions
diff --git a/fpdfsdk/pdfwindow/cpwl_combo_box.cpp b/fpdfsdk/pdfwindow/cpwl_combo_box.cpp
index b69f98906e..2929aac90f 100644
--- a/fpdfsdk/pdfwindow/cpwl_combo_box.cpp
+++ b/fpdfsdk/pdfwindow/cpwl_combo_box.cpp
@@ -216,6 +216,13 @@ void CPWL_ComboBox::KillFocus() {
CPWL_Wnd::KillFocus();
}
+CFX_WideString CPWL_ComboBox::GetSelectedText() {
+ if (m_pEdit)
+ return m_pEdit->GetSelectedText();
+
+ return CFX_WideString();
+}
+
CFX_WideString CPWL_ComboBox::GetText() const {
if (m_pEdit) {
return m_pEdit->GetText();
diff --git a/fpdfsdk/pdfwindow/cpwl_combo_box.h b/fpdfsdk/pdfwindow/cpwl_combo_box.h
index e814ca5d40..549374f3e8 100644
--- a/fpdfsdk/pdfwindow/cpwl_combo_box.h
+++ b/fpdfsdk/pdfwindow/cpwl_combo_box.h
@@ -69,6 +69,7 @@ class CPWL_ComboBox : public CPWL_Wnd {
CFX_FloatRect GetFocusRect() const override;
void SetFocus() override;
void KillFocus() override;
+ CFX_WideString GetSelectedText() override;
void SetFillerNotify(IPWL_Filler_Notify* pNotify);
diff --git a/fpdfsdk/pdfwindow/cpwl_edit_ctrl.cpp b/fpdfsdk/pdfwindow/cpwl_edit_ctrl.cpp
index 9c22cead58..3390696e01 100644
--- a/fpdfsdk/pdfwindow/cpwl_edit_ctrl.cpp
+++ b/fpdfsdk/pdfwindow/cpwl_edit_ctrl.cpp
@@ -53,6 +53,13 @@ void CPWL_EditCtrl::SetCursor() {
}
}
+CFX_WideString CPWL_EditCtrl::GetSelectedText() {
+ if (m_pEdit)
+ return m_pEdit->GetSelText();
+
+ return CFX_WideString();
+}
+
void CPWL_EditCtrl::RePosChildWnd() {
m_pEdit->SetPlateRect(GetClientRect());
}
diff --git a/fpdfsdk/pdfwindow/cpwl_edit_ctrl.h b/fpdfsdk/pdfwindow/cpwl_edit_ctrl.h
index 59bb623776..1c4c92507a 100644
--- a/fpdfsdk/pdfwindow/cpwl_edit_ctrl.h
+++ b/fpdfsdk/pdfwindow/cpwl_edit_ctrl.h
@@ -61,6 +61,7 @@ class CPWL_EditCtrl : public CPWL_Wnd {
void SetFontSize(float fFontSize) override;
float GetFontSize() const override;
void SetCursor() override;
+ CFX_WideString GetSelectedText() override;
void IOnSetScrollInfoY(float fPlateMin,
float fPlateMax,
diff --git a/fpdfsdk/pdfwindow/cpwl_edit_embeddertest.cpp b/fpdfsdk/pdfwindow/cpwl_edit_embeddertest.cpp
index 4a14b65d0d..e206cd16e3 100644
--- a/fpdfsdk/pdfwindow/cpwl_edit_embeddertest.cpp
+++ b/fpdfsdk/pdfwindow/cpwl_edit_embeddertest.cpp
@@ -11,51 +11,115 @@
#include "testing/embedder_test.h"
#include "testing/gtest/include/gtest/gtest.h"
-class CPWLEditEmbeddertest : public EmbedderTest {};
+class CPWLEditEmbeddertest : public EmbedderTest {
+ protected:
+ void SetUp() override {
+ EmbedderTest::SetUp();
+ CreateAndInitializeFormPDF();
+ }
-TEST_F(CPWLEditEmbeddertest, TypeText) {
- EXPECT_TRUE(OpenDocument("text_form.pdf"));
- FPDF_PAGE page = LoadPage(0);
- ASSERT_TRUE(page);
-
- CPDFSDK_FormFillEnvironment* pFormFillEnv =
- static_cast<CPDFSDK_FormFillEnvironment*>(form_handle());
-
- CPDFSDK_Annot* pAnnot;
- {
- CBA_AnnotIterator iter(pFormFillEnv->GetPageView(0),
- CPDF_Annot::Subtype::WIDGET);
- pAnnot = iter.GetFirstAnnot();
- CPDFSDK_Annot* pLastAnnot = iter.GetLastAnnot();
- ASSERT_EQ(pAnnot, pLastAnnot);
- ASSERT_TRUE(pAnnot);
- ASSERT_EQ(CPDF_Annot::Subtype::WIDGET, pAnnot->GetAnnotSubtype());
+ void TearDown() override {
+ UnloadPage(GetPage());
+ EmbedderTest::TearDown();
}
- CFFL_InteractiveFormFiller* pInteractiveFormFiller =
- pFormFillEnv->GetInteractiveFormFiller();
- {
- CPDFSDK_Annot::ObservedPtr pObserved(pAnnot);
- EXPECT_TRUE(pInteractiveFormFiller->OnSetFocus(&pObserved, 0));
+ void CreateAndInitializeFormPDF() {
+ EXPECT_TRUE(OpenDocument("text_form.pdf"));
+ m_page = LoadPage(0);
+ ASSERT_TRUE(m_page);
+
+ CPDFSDK_FormFillEnvironment* pFormFillEnv =
+ static_cast<CPDFSDK_FormFillEnvironment*>(form_handle());
+
+ {
+ CBA_AnnotIterator iter(pFormFillEnv->GetPageView(0),
+ CPDF_Annot::Subtype::WIDGET);
+ m_pAnnot = iter.GetFirstAnnot();
+ CPDFSDK_Annot* pLastAnnot = iter.GetLastAnnot();
+ ASSERT_EQ(m_pAnnot, pLastAnnot);
+ ASSERT_TRUE(m_pAnnot);
+ ASSERT_EQ(CPDF_Annot::Subtype::WIDGET, m_pAnnot->GetAnnotSubtype());
+ }
+
+ CFFL_InteractiveFormFiller* pInteractiveFormFiller =
+ pFormFillEnv->GetInteractiveFormFiller();
+ {
+ CPDFSDK_Annot::ObservedPtr pObserved(m_pAnnot);
+ EXPECT_TRUE(pInteractiveFormFiller->OnSetFocus(&pObserved, 0));
+ }
+
+ m_pFormFiller = pInteractiveFormFiller->GetFormFiller(m_pAnnot, false);
+ ASSERT_TRUE(m_pFormFiller);
+
+ CPWL_Wnd* pWindow =
+ m_pFormFiller->GetPDFWindow(pFormFillEnv->GetPageView(0), false);
+ ASSERT_TRUE(pWindow);
+ ASSERT_EQ(PWL_CLASSNAME_EDIT, pWindow->GetClassName());
+
+ m_pEdit = static_cast<CPWL_Edit*>(pWindow);
}
- CFFL_FormFiller* pFormFiller =
- pInteractiveFormFiller->GetFormFiller(pAnnot, false);
- ASSERT_TRUE(pFormFiller);
+ FPDF_PAGE GetPage() { return m_page; }
+ CPWL_Edit* GetCPWLEdit() { return m_pEdit; }
+ CFFL_FormFiller* GetCFFLFormFiller() { return m_pFormFiller; }
+ CPDFSDK_Annot* GetCPDFSDKAnnot() { return m_pAnnot; }
+
+ private:
+ FPDF_PAGE m_page;
+ CPWL_Edit* m_pEdit;
+ CFFL_FormFiller* m_pFormFiller;
+ CPDFSDK_Annot* m_pAnnot;
+};
+
+TEST_F(CPWLEditEmbeddertest, TypeText) {
+ EXPECT_TRUE(GetCPWLEdit()->GetText().IsEmpty());
+ EXPECT_TRUE(GetCFFLFormFiller()->OnChar(GetCPDFSDKAnnot(), 'a', 0));
+ EXPECT_TRUE(GetCFFLFormFiller()->OnChar(GetCPDFSDKAnnot(), 'b', 0));
+ EXPECT_TRUE(GetCFFLFormFiller()->OnChar(GetCPDFSDKAnnot(), 'c', 0));
+
+ EXPECT_STREQ(L"abc", GetCPWLEdit()->GetText().c_str());
+}
+
+TEST_F(CPWLEditEmbeddertest, GetSelectedTextEmptyAndBasic) {
+ // Attempt to set selection before text has been typed to test that
+ // selection is identified as empty.
+ //
+ // Select from character index [0, 3) within form text field.
+ GetCPWLEdit()->SetSel(0, 3);
+ EXPECT_TRUE(GetCPWLEdit()->GetSelectedText().IsEmpty());
+
+ EXPECT_TRUE(GetCFFLFormFiller()->OnChar(GetCPDFSDKAnnot(), 'a', 0));
+ EXPECT_TRUE(GetCFFLFormFiller()->OnChar(GetCPDFSDKAnnot(), 'b', 0));
+ EXPECT_TRUE(GetCFFLFormFiller()->OnChar(GetCPDFSDKAnnot(), 'c', 0));
+ GetCPWLEdit()->SetSel(0, 2);
+
+ EXPECT_STREQ(L"ab", GetCPWLEdit()->GetSelectedText().c_str());
+}
+
+TEST_F(CPWLEditEmbeddertest, GetSelectedTextFragments) {
+ for (int i = 0; i < 50; ++i) {
+ EXPECT_TRUE(GetCFFLFormFiller()->OnChar(GetCPDFSDKAnnot(), i + 'A', 0));
+ }
+
+ GetCPWLEdit()->SetSel(0, 0);
+ EXPECT_TRUE(GetCPWLEdit()->GetSelectedText().IsEmpty());
+
+ GetCPWLEdit()->SetSel(0, 1);
+ EXPECT_STREQ(L"A", GetCPWLEdit()->GetSelectedText().c_str());
- CPWL_Wnd* pWindow =
- pFormFiller->GetPDFWindow(pFormFillEnv->GetPageView(0), false);
- ASSERT_TRUE(pWindow);
- ASSERT_EQ(PWL_CLASSNAME_EDIT, pWindow->GetClassName());
+ GetCPWLEdit()->SetSel(0, -1);
+ EXPECT_STREQ(L"ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqr",
+ GetCPWLEdit()->GetSelectedText().c_str());
- CPWL_Edit* pEdit = static_cast<CPWL_Edit*>(pWindow);
- EXPECT_TRUE(pEdit->GetText().IsEmpty());
+ GetCPWLEdit()->SetSel(-8, -1);
+ EXPECT_TRUE(GetCPWLEdit()->GetSelectedText().IsEmpty());
- EXPECT_TRUE(pFormFiller->OnChar(pAnnot, 'a', 0));
- EXPECT_TRUE(pFormFiller->OnChar(pAnnot, 'b', 0));
- EXPECT_TRUE(pFormFiller->OnChar(pAnnot, 'c', 0));
+ GetCPWLEdit()->SetSel(23, 12);
+ EXPECT_STREQ(L"MNOPQRSTUVW", GetCPWLEdit()->GetSelectedText().c_str());
- EXPECT_STREQ(L"abc", pEdit->GetText().c_str());
+ GetCPWLEdit()->SetSel(12, 23);
+ EXPECT_STREQ(L"MNOPQRSTUVW", GetCPWLEdit()->GetSelectedText().c_str());
- UnloadPage(page);
+ GetCPWLEdit()->SetSel(49, 50);
+ EXPECT_STREQ(L"r", GetCPWLEdit()->GetSelectedText().c_str());
}
diff --git a/fpdfsdk/pdfwindow/cpwl_wnd.cpp b/fpdfsdk/pdfwindow/cpwl_wnd.cpp
index 90a79305eb..d44b219110 100644
--- a/fpdfsdk/pdfwindow/cpwl_wnd.cpp
+++ b/fpdfsdk/pdfwindow/cpwl_wnd.cpp
@@ -390,6 +390,10 @@ PWL_IMPLEMENT_MOUSE_METHOD(OnRButtonUp)
PWL_IMPLEMENT_MOUSE_METHOD(OnMouseMove)
#undef PWL_IMPLEMENT_MOUSE_METHOD
+CFX_WideString CPWL_Wnd::GetSelectedText() {
+ return CFX_WideString();
+}
+
bool CPWL_Wnd::OnMouseWheel(short zDelta,
const CFX_PointF& point,
uint32_t nFlag) {
diff --git a/fpdfsdk/pdfwindow/cpwl_wnd.h b/fpdfsdk/pdfwindow/cpwl_wnd.h
index e7a4e23c38..704984932b 100644
--- a/fpdfsdk/pdfwindow/cpwl_wnd.h
+++ b/fpdfsdk/pdfwindow/cpwl_wnd.h
@@ -229,6 +229,7 @@ class CPWL_Wnd : public CPWL_TimerHandler {
virtual void SetFontSize(float fFontSize);
virtual float GetFontSize() const;
+ virtual CFX_WideString GetSelectedText();
virtual CFX_FloatRect GetFocusRect() const;
virtual CFX_FloatRect GetClientRect() const;