diff --git a/CHANGELOG.md b/CHANGELOG.md index d56a1cff..28ffef72 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ v0.16.0 (xx dec 2018) ---------------------- ### Added - Add setting Chart Title and Legend visibility @Tom-Magill #1433 +- Add ability to pass a Style object in Section constructor @ndench #1416 - Add support for hidden text @Alexmg86 #1527 ### Fixed diff --git a/src/PhpWord/Element/Section.php b/src/PhpWord/Element/Section.php index d612fc01..b495ef7b 100644 --- a/src/PhpWord/Element/Section.php +++ b/src/PhpWord/Element/Section.php @@ -59,14 +59,16 @@ class Section extends AbstractContainer * Create new instance * * @param int $sectionCount - * @param array $style + * @param null|array|\PhpOffice\PhpWord\Style $style */ public function __construct($sectionCount, $style = null) { $this->sectionId = $sectionCount; $this->setDocPart($this->container, $this->sectionId); - $this->style = new SectionStyle(); - $this->setStyle($style); + if (null === $style) { + $style = new SectionStyle(); + } + $this->style = $this->setNewStyle(new SectionStyle(), $style); } /** diff --git a/tests/PhpWord/Element/SectionTest.php b/tests/PhpWord/Element/SectionTest.php index 265307d7..83d1214e 100644 --- a/tests/PhpWord/Element/SectionTest.php +++ b/tests/PhpWord/Element/SectionTest.php @@ -19,6 +19,7 @@ namespace PhpOffice\PhpWord\Element; use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\Style; +use PhpOffice\PhpWord\Style\Section as SectionStyle; /** * @covers \PhpOffice\PhpWord\Element\Section @@ -27,6 +28,27 @@ use PhpOffice\PhpWord\Style; */ class SectionTest extends \PHPUnit\Framework\TestCase { + public function testConstructorWithDefaultStyle() + { + $section = new Section(0); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Section', $section->getStyle()); + } + + public function testConstructorWithArrayStyle() + { + $section = new Section(0, array('orientation' => 'landscape')); + $style = $section->getStyle(); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Section', $style); + $this->assertEquals('landscape', $style->getOrientation()); + } + + public function testConstructorWithObjectStyle() + { + $style = new SectionStyle(); + $section = new Section(0, $style); + $this->assertSame($style, $section->getStyle()); + } + /** * @covers ::setStyle */ diff --git a/tests/PhpWord/Writer/Word2007/ElementTest.php b/tests/PhpWord/Writer/Word2007/ElementTest.php index 25c62ecc..dc75a335 100644 --- a/tests/PhpWord/Writer/Word2007/ElementTest.php +++ b/tests/PhpWord/Writer/Word2007/ElementTest.php @@ -492,4 +492,19 @@ class ElementTest extends \PHPUnit\Framework\TestCase $this->assertTrue($doc->elementExists('/w:document/w:body/w:p[2]/w:pPr/w:pStyle')); $this->assertEquals('Heading1', $doc->getElementAttribute('/w:document/w:body/w:p[2]/w:pPr/w:pStyle', 'w:val')); } + + /** + * Test correct writing of text with ampersant in it + */ + public function testTextWithAmpersant() + { + \PhpOffice\PhpWord\Settings::setOutputEscapingEnabled(true); + $phpWord = new PhpWord(); + $section = $phpWord->addSection(); + $section->addText('this text contains an & (ampersant)'); + + $doc = TestHelperDOCX::getDocument($phpWord); + $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); + } }