Skip to content

No nested w:pPr elements in ListItemRun. #1628

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 38 additions & 15 deletions src/PhpWord/Writer/Word2007/Element/ListItemRun.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

namespace PhpOffice\PhpWord\Writer\Word2007\Element;

use PhpOffice\PhpWord\Element\ListItemRun as ListItemRunElement;
use PhpOffice\PhpWord\Writer\Word2007\Style\Paragraph as ParagraphStyleWriter;

/**
Expand All @@ -31,34 +32,56 @@ class ListItemRun extends AbstractElement
*/
public function write()
{
$xmlWriter = $this->getXmlWriter();
$element = $this->getElement();
if (!$element instanceof \PhpOffice\PhpWord\Element\ListItemRun) {

if (!$element instanceof ListItemRunElement) {
return;
}

$this->writeParagraph($element);
}

private function writeParagraph(ListItemRunElement $element)
{
$xmlWriter = $this->getXmlWriter();
$xmlWriter->startElement('w:p');

$this->writeParagraphProperties($element);

$containerWriter = new Container($xmlWriter, $element);
$containerWriter->write();

$xmlWriter->endElement(); // w:p
}

private function writeParagraphProperties(ListItemRunElement $element)
{
$xmlWriter = $this->getXmlWriter();
$xmlWriter->startElement('w:pPr');
$paragraphStyle = $element->getParagraphStyle();
$styleWriter = new ParagraphStyleWriter($xmlWriter, $paragraphStyle);

$styleWriter = new ParagraphStyleWriter($xmlWriter, $element->getParagraphStyle());
$styleWriter->setIsInline(true);
$styleWriter->setWithoutPPR(true);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The magic.

$styleWriter->write();

$xmlWriter->startElement('w:numPr');
$xmlWriter->startElement('w:ilvl');
$xmlWriter->writeAttribute('w:val', $element->getDepth());
$xmlWriter->endElement(); // w:ilvl
$xmlWriter->startElement('w:numId');
$xmlWriter->writeAttribute('w:val', $element->getStyle()->getNumId());
$xmlWriter->endElement(); // w:numId
$xmlWriter->endElement(); // w:numPr
$this->writeParagraphPropertiesNumbering($element);

$xmlWriter->endElement(); // w:pPr
}

$containerWriter = new Container($xmlWriter, $element);
$containerWriter->write();
private function writeParagraphPropertiesNumbering(ListItemRunElement $element)
{
$xmlWriter = $this->getXmlWriter();
$xmlWriter->startElement('w:numPr');

$xmlWriter->endElement(); // w:p
$xmlWriter->writeElementBlock('w:ilvl', array(
'w:val' => $element->getDepth(),
));

$xmlWriter->writeElementBlock('w:numId', array(
'w:val' => $element->getStyle()->getNumId(),
));

$xmlWriter->endElement(); // w:numPr
}
}
21 changes: 21 additions & 0 deletions tests/PhpWord/Writer/Word2007/ElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -510,4 +510,25 @@ public function testTextWithAmpersant()
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:r/w:t'));
$this->assertEquals('this text contains an & (ampersant)', $doc->getElement('/w:document/w:body/w:p/w:r/w:t')->nodeValue);
}

/**
* Test ListItemRun paragraph style writing
*/
public function testListItemRunStyleWriting()
{
$phpWord = new PhpWord();
$phpWord->addParagraphStyle('MyParagraphStyle', array('spaceBefore' => 400));

$section = $phpWord->addSection();
$listItemRun = $section->addListItemRun(0, null, 'MyParagraphStyle');
$listItemRun->addText('List item');
$listItemRun->addText(' in bold', array('bold' => true));

$doc = TestHelperDOCX::getDocument($phpWord);
$this->assertFalse($doc->elementExists('/w:document/w:body/w:p/w:pPr/w:pPr'));
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:pPr/w:pStyle'));
$this->assertEquals('List item', $doc->getElement('/w:document/w:body/w:p/w:r[1]/w:t')->nodeValue);
$this->assertEquals(' in bold', $doc->getElement('/w:document/w:body/w:p/w:r[2]/w:t')->nodeValue);
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:r[2]/w:rPr/w:b'));
}
}