From f8763bb449273667ef5ff11a0fa491e3e8b46136 Mon Sep 17 00:00:00 2001 From: Ryan Harrison Date: Thu, 31 Aug 2017 14:22:39 -0400 Subject: Properly handle \n, \r, \r\n when inserting text BUG=pdfium:877 Change-Id: I6aa3e53057c27700c2d3c0c1692fa86ae9b3a03c Reviewed-on: https://pdfium-review.googlesource.com/12711 Reviewed-by: Tom Sepez Commit-Queue: Ryan Harrison --- fpdfsdk/pwl/cpwl_edit_embeddertest.cpp | 48 ++++++++++++++++++++++++++++++++++ fpdfsdk/pwl/cpwl_edit_impl.cpp | 4 +-- 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/fpdfsdk/pwl/cpwl_edit_embeddertest.cpp b/fpdfsdk/pwl/cpwl_edit_embeddertest.cpp index 3574de9345..b3f0d5dc0e 100644 --- a/fpdfsdk/pwl/cpwl_edit_embeddertest.cpp +++ b/fpdfsdk/pwl/cpwl_edit_embeddertest.cpp @@ -371,3 +371,51 @@ TEST_F(CPWLEditEmbeddertest, GetCPWLEdit()->ReplaceSelection(L"Hippopotamus"); EXPECT_STREQ(L"ElepHippop", GetCPWLEdit()->GetText().c_str()); } + +TEST_F(CPWLEditEmbeddertest, SetTextWithEndCarriageFeed) { + FormFillerAndWindowSetup(GetCPDFSDKAnnot()); + GetCPWLEdit()->SetText(L"Foo\r"); + EXPECT_STREQ(L"Foo", GetCPWLEdit()->GetText().c_str()); +} + +TEST_F(CPWLEditEmbeddertest, SetTextWithEndNewline) { + FormFillerAndWindowSetup(GetCPDFSDKAnnot()); + GetCPWLEdit()->SetText(L"Foo\n"); + EXPECT_STREQ(L"Foo", GetCPWLEdit()->GetText().c_str()); +} + +TEST_F(CPWLEditEmbeddertest, SetTextWithEndCarriageFeedAndNewLine) { + FormFillerAndWindowSetup(GetCPDFSDKAnnot()); + GetCPWLEdit()->SetText(L"Foo\r\n"); + EXPECT_STREQ(L"Foo", GetCPWLEdit()->GetText().c_str()); +} + +TEST_F(CPWLEditEmbeddertest, SetTextWithEndNewLineAndCarriageFeed) { + FormFillerAndWindowSetup(GetCPDFSDKAnnot()); + GetCPWLEdit()->SetText(L"Foo\n\r"); + EXPECT_STREQ(L"Foo", GetCPWLEdit()->GetText().c_str()); +} + +TEST_F(CPWLEditEmbeddertest, SetTextWithBodyCarriageFeed) { + FormFillerAndWindowSetup(GetCPDFSDKAnnot()); + GetCPWLEdit()->SetText(L"Foo\rBar"); + EXPECT_STREQ(L"FooBar", GetCPWLEdit()->GetText().c_str()); +} + +TEST_F(CPWLEditEmbeddertest, SetTextWithBodyNewline) { + FormFillerAndWindowSetup(GetCPDFSDKAnnot()); + GetCPWLEdit()->SetText(L"Foo\nBar"); + EXPECT_STREQ(L"FooBar", GetCPWLEdit()->GetText().c_str()); +} + +TEST_F(CPWLEditEmbeddertest, SetTextWithBodyCarriageFeedAndNewLine) { + FormFillerAndWindowSetup(GetCPDFSDKAnnot()); + GetCPWLEdit()->SetText(L"Foo\r\nBar"); + EXPECT_STREQ(L"FooBar", GetCPWLEdit()->GetText().c_str()); +} + +TEST_F(CPWLEditEmbeddertest, SetTextWithBodyNewLineAndCarriageFeed) { + FormFillerAndWindowSetup(GetCPDFSDKAnnot()); + GetCPWLEdit()->SetText(L"Foo\n\rBar"); + EXPECT_STREQ(L"FooBar", GetCPWLEdit()->GetText().c_str()); +} diff --git a/fpdfsdk/pwl/cpwl_edit_impl.cpp b/fpdfsdk/pwl/cpwl_edit_impl.cpp index 42eaa16667..c44301f212 100644 --- a/fpdfsdk/pwl/cpwl_edit_impl.cpp +++ b/fpdfsdk/pwl/cpwl_edit_impl.cpp @@ -1846,13 +1846,11 @@ CPVT_WordPlace CPWL_EditImpl::DoInsertText(const CPVT_WordPlace& place, switch (word) { case 0x0D: wp = m_pVT->InsertSection(wp, nullptr, nullptr); - if (sText[i + 1] == 0x0A) + if (i + 1 < sz && sText[i + 1] == 0x0A) i++; break; case 0x0A: wp = m_pVT->InsertSection(wp, nullptr, nullptr); - if (sText[i + 1] == 0x0D) - i++; break; case 0x09: word = 0x20; -- cgit v1.2.3