Skip to content

Conversation

rasamassen
Copy link

@rasamassen rasamassen commented Sep 1, 2025

Returns line return instead of paragraph return on textrun, footnote, and endnote.

Description

TextBreak was always creating a new paragraph, even when it should have just been making a line break.

Fixes # (issue)

Checklist:

  • My CI is 🟢
  • I have covered by unit tests my new code (check build/coverage for coverage report)
  • I have updated the documentation to describe the changes
  • I have updated the changelog

Returns line return instead of paragraph return on textrun, footnote, and endnote.
@rasamassen rasamassen changed the title Fix TextBreak.php RTF: Fix TextBreak.php Sep 1, 2025
@coveralls
Copy link

coveralls commented Sep 1, 2025

Coverage Status

coverage: 96.757%. remained the same
when pulling 6a28762 on rasamassen:TextBreak
into 0ab0b49 on PHPOffice:master.

Copy link
Member

@Progi1984 Progi1984 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rasamassen Thank you for your contribution.

Could you add an unit test, please ?

@rasamassen
Copy link
Author

@rasamassen Thank you for your contribution.

Could you add an unit test, please ?

Probably not. I don't program much anymore - it's been a decade or more - and it's been a lot of work to figure out the parts of the RTF writer that weren't working; figuring out how the tests work will be a lot more work I don't have time for. I can give you a sample below that shows the error if you compare the current code to my revisions. Hopefully that helps you create a test.

// Create PhpWord
$phpWord = new PhpOffice\PhpWord\PhpWord();

// Set default settings
$phpWord->setDefaultParagraphStyle(['align' => 'left']);

// Styles
$phpWord->addFontStyle('bold', ['bold' => true]);
$phpWord->addParagraphStyle('center', ['align' => 'center']);

// Build Document
$section = $phpWord->addSection();
$section->addText('The default style.');
$section->addTextBreak();
$textrun = $section->addTextRun('center');
$textrun->addText('This is a single sentence', 'bold');
$textrun->addTextBreak();
$textrun->addText('spread over multiple lines', 'bold');
$textrun->addTextBreak();
$textrun->addText('all centered', 'bold');
$section->addText('A new sentence in the default style.');

In the original code, there was a paragraph break, resulting in the additional lines losing their paragraph styling.

Original Output (lines 2 and 3 are left-align when they should be center; line 4 is center):
\pard\nowidctlpar {The default style.}\par
\pard\par
pard\nowidctlpar \qc{{\b This is a single sentence}\pard\par
{\b spread over multiple lines}\pard\par
{\b all centered}}\par
\pard\nowidctlpar {A new sentence in the default style.}\par

New Output (correct):
\pard\nowidctlpar {The default style.}\par
\pard\par
pard\nowidctlpar \qc{{\b This is a single sentence}\line
{\b spread over multiple lines}\line
{\b all centered}}\par
\pard\nowidctlpar {A new sentence in the default style.}\par

@Progi1984
Copy link
Member

@rasamassen Could you invite me on your repository and I will add unit test for your PRs ? Else I can't merge PR without linked unit tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants