Skip to content
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

Support for padding in a table cell #2697

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions docs/changes/1.x/1.4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- Writer ODText: Support for ListItemRun by [@Progi1984](https://github.com/Progi1984) fixing [#2159](https://github.com/PHPOffice/PHPWord/issues/2159), [#2620](https://github.com/PHPOffice/PHPWord/issues/2620) in [#2669](https://github.com/PHPOffice/PHPWord/pull/2669)
- Writer HTML: Support for vAlign in Tables by [@SpraxDev](https://github.com/SpraxDev) in [#2675](https://github.com/PHPOffice/PHPWord/pull/2675)
- Writer Word2007: Support for padding in Table Cell by [@Azamat8405](https://github.com/Azamat8405) in [#2697](https://github.com/PHPOffice/PHPWord/pull/2697)

### Bug fixes

Expand Down
52 changes: 52 additions & 0 deletions src/PhpWord/Shared/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,58 @@ protected static function parseStyleDeclarations(array $selectors, array $styles
$styles['spaceAfter'] = Converter::cssToTwip($value);

break;

case 'padding':
$valueTop = $valueRight = $valueBottom = $valueLeft = null;
$cValue = preg_replace('# +#', ' ', trim($value));
$paddingArr = explode(' ', $cValue);
$countParams = count($paddingArr);
if ($countParams == 1) {
$valueTop = $valueRight = $valueBottom = $valueLeft = $paddingArr[0];
} elseif ($countParams == 2) {
$valueTop = $valueBottom = $paddingArr[0];
$valueRight = $valueLeft = $paddingArr[1];
} elseif ($countParams == 3) {
$valueTop = $paddingArr[0];
$valueRight = $valueLeft = $paddingArr[1];
$valueBottom = $paddingArr[2];
} elseif ($countParams == 4) {
$valueTop = $paddingArr[0];
$valueRight = $paddingArr[1];
$valueBottom = $paddingArr[2];
$valueLeft = $paddingArr[3];
}
if ($valueTop !== null) {
$styles['paddingTop'] = Converter::cssToTwip($valueTop);
}
if ($valueRight !== null) {
$styles['paddingRight'] = Converter::cssToTwip($valueRight);
}
if ($valueBottom !== null) {
$styles['paddingBottom'] = Converter::cssToTwip($valueBottom);
}
if ($valueLeft !== null) {
$styles['paddingLeft'] = Converter::cssToTwip($valueLeft);
}

break;
case 'padding-top':
$styles['paddingTop'] = Converter::cssToTwip($value);

break;
case 'padding-right':
$styles['paddingRight'] = Converter::cssToTwip($value);

break;
case 'padding-bottom':
$styles['paddingBottom'] = Converter::cssToTwip($value);

break;
case 'padding-left':
$styles['paddingLeft'] = Converter::cssToTwip($value);

break;

case 'border-color':
self::mapBorderColor($styles, $value);

Expand Down
108 changes: 108 additions & 0 deletions src/PhpWord/Style/Cell.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,26 @@ class Cell extends Border
*/
private $vAlign;

/**
* @var null|int
*/
private $paddingTop;

/**
* @var null|int
*/
private $paddingBottom;

/**
* @var null|int
*/
private $paddingLeft;

/**
* @var null|int
*/
private $paddingRight;

/**
* Text Direction.
*
Expand Down Expand Up @@ -356,4 +376,92 @@ public function getNoWrap(): bool
{
return $this->noWrap;
}

/**
* Get style padding-top.
*
* @return mixed
*/
public function getPaddingTop()
{
return $this->paddingTop;
}

/**
* Set style padding-top.
*
* @return $this
*/
public function setPaddingTop(int $value): self
{
$this->paddingTop = $value;

return $this;
}

/**
* Get style padding-bottom.
*
* @return mixed
*/
public function getPaddingBottom()
{
return $this->paddingBottom;
}

/**
* Set style padding-bottom.
*
* @return $this
*/
public function setPaddingBottom(int $value): self
{
$this->paddingBottom = $value;

return $this;
}

/**
* Get style padding-left.
*
* @return mixed
*/
public function getPaddingLeft()
{
return $this->paddingLeft;
}

/**
* Set style padding-left.
*
* @return $this
*/
public function setPaddingLeft(int $value): self
{
$this->paddingLeft = $value;

return $this;
}

/**
* Get style padding-right.
*
* @return mixed
*/
public function getPaddingRight()
{
return $this->paddingRight;
}

/**
* Set style padding-right.
*
* @return $this
*/
public function setPaddingRight(int $value): self
{
$this->paddingRight = $value;

return $this;
}
}
34 changes: 34 additions & 0 deletions src/PhpWord/Writer/Word2007/Style/Cell.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,40 @@ public function write(): void
$xmlWriter->endElement(); // w:tcW
}

$paddingTop = $style->getPaddingTop();
$paddingLeft = $style->getPaddingLeft();
$paddingBottom = $style->getPaddingBottom();
$paddingRight = $style->getPaddingRight();

if ($paddingTop !== null || $paddingLeft !== null || $paddingBottom !== null || $paddingRight !== null) {
$xmlWriter->startElement('w:tcMar');
if ($paddingTop !== null) {
$xmlWriter->startElement('w:top');
$xmlWriter->writeAttribute('w:w', $paddingTop);
$xmlWriter->writeAttribute('w:type', \PhpOffice\PhpWord\SimpleType\TblWidth::TWIP);
$xmlWriter->endElement(); // w:top
}
if ($paddingLeft !== null) {
$xmlWriter->startElement('w:start');
$xmlWriter->writeAttribute('w:w', $paddingLeft);
$xmlWriter->writeAttribute('w:type', \PhpOffice\PhpWord\SimpleType\TblWidth::TWIP);
$xmlWriter->endElement(); // w:start
}
if ($paddingBottom !== null) {
$xmlWriter->startElement('w:bottom');
$xmlWriter->writeAttribute('w:w', $paddingBottom);
$xmlWriter->writeAttribute('w:type', \PhpOffice\PhpWord\SimpleType\TblWidth::TWIP);
$xmlWriter->endElement(); // w:bottom
}
if ($paddingRight !== null) {
$xmlWriter->startElement('w:end');
$xmlWriter->writeAttribute('w:w', $paddingRight);
$xmlWriter->writeAttribute('w:type', \PhpOffice\PhpWord\SimpleType\TblWidth::TWIP);
$xmlWriter->endElement(); // w:end
}
$xmlWriter->endElement(); // w:tcMar
}

// Text direction
$textDir = $style->getTextDirection();
$xmlWriter->writeElementIf(null !== $textDir, 'w:textDirection', 'w:val', $textDir);
Expand Down
62 changes: 62 additions & 0 deletions tests/PhpWordTests/Shared/HtmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
use PhpOffice\PhpWord\Element\Section;
use PhpOffice\PhpWord\Element\Table;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Shared\Converter;
use PhpOffice\PhpWord\Shared\Html;
use PhpOffice\PhpWord\SimpleType\Jc;
use PhpOffice\PhpWord\SimpleType\LineSpacingRule;
use PhpOffice\PhpWord\SimpleType\TblWidth;
use PhpOffice\PhpWord\Style\Paragraph;
use PhpOffice\PhpWordTests\AbstractWebServerEmbeddedTest;
use PhpOffice\PhpWordTests\TestHelperDOCX;
Expand Down Expand Up @@ -249,6 +251,66 @@ public function testParseLineHeight(): void
self::assertEquals(LineSpacingRule::AUTO, $doc->getElementAttribute('/w:document/w:body/w:p[5]/w:pPr/w:spacing', 'w:lineRule'));
}

public function testParseCellPaddingStyle(): void
{
$phpWord = new PhpWord();
$section = $phpWord->addSection();

$top = 10;
$right = 11;
$bottom = 12;
$left = 13;

$testValTop = Converter::pixelToTwip($top);
$testValRight = Converter::pixelToTwip($right);
$testValBottom = Converter::pixelToTwip($bottom);
$testValLeft = Converter::pixelToTwip($left);

$html = '<table>
<tbody>
<tr>
<td style="padding:' . $top . 'px ' . $right . 'px ' . $bottom . 'px ' . $left . 'px;">full</td>
<td style="padding:' . $top . 'px 0px ' . $bottom . 'px ' . $left . 'px;padding-right:' . $right . 'px;">mix</td>
<td style="padding-top:' . $top . 'px;">top</td>
<td style="padding-bottom:' . $bottom . 'px;">bottom</td>
<td style="padding-left:' . $left . 'px;">left</td>
</tr>
</tbody>
</table>';
Html::addHtml($section, $html);

$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');

$path = '/w:document/w:body/w:tbl/w:tr/w:tc[1]/w:tcPr/w:tcMar/w:top';
self::assertTrue($doc->elementExists($path));
$path = '/w:document/w:body/w:tbl/w:tr/w:tc[1]/w:tcPr/w:tcMar/w:bottom';
self::assertTrue($doc->elementExists($path));
$path = '/w:document/w:body/w:tbl/w:tr/w:tc[1]/w:tcPr/w:tcMar/w:end';
self::assertTrue($doc->elementExists($path));
$path = '/w:document/w:body/w:tbl/w:tr/w:tc[1]/w:tcPr/w:tcMar/w:start';
self::assertTrue($doc->elementExists($path));

$path = '/w:document/w:body/w:tbl/w:tr/w:tc[2]/w:tcPr/w:tcMar/w:end';
self::assertTrue($doc->elementExists($path));
self::assertEquals($testValRight, $doc->getElementAttribute($path, 'w:w'));
self::assertEquals(TblWidth::TWIP, $doc->getElementAttribute($path, 'w:type'));

$path = '/w:document/w:body/w:tbl/w:tr/w:tc[3]/w:tcPr/w:tcMar/w:top';
self::assertTrue($doc->elementExists($path));
self::assertEquals($testValTop, $doc->getElementAttribute($path, 'w:w'));
self::assertEquals(TblWidth::TWIP, $doc->getElementAttribute($path, 'w:type'));

$path = '/w:document/w:body/w:tbl/w:tr/w:tc[4]/w:tcPr/w:tcMar/w:bottom';
self::assertTrue($doc->elementExists($path));
self::assertEquals($testValBottom, $doc->getElementAttribute($path, 'w:w'));
self::assertEquals(TblWidth::TWIP, $doc->getElementAttribute($path, 'w:type'));

$path = '/w:document/w:body/w:tbl/w:tr/w:tc[5]/w:tcPr/w:tcMar/w:start';
self::assertTrue($doc->elementExists($path));
self::assertEquals($testValLeft, $doc->getElementAttribute($path, 'w:w'));
self::assertEquals(TblWidth::TWIP, $doc->getElementAttribute($path, 'w:type'));
}

/**
* Test text-indent style.
*/
Expand Down
85 changes: 85 additions & 0 deletions tests/PhpWordTests/Writer/Word2007/Style/TableCellTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @see https://github.com/PHPOffice/PHPWord
*
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/

namespace PhpOffice\PhpWordTests\Writer\Word2007\Style;

use PhpOffice\PhpWord\Shared\Converter;
use PhpOffice\PhpWord\SimpleType\TblWidth;
use PhpOffice\PhpWordTests\TestHelperDOCX;

/**
* Test class for PhpOffice\PhpWord\Writer\Word2007\Style\Table.
*
* @coversDefaultClass \PhpOffice\PhpWord\Writer\Word2007\Style\Table
*
* @runTestsInSeparateProcesses
*/
class TableCellTest extends \PHPUnit\Framework\TestCase
{
/**
* Executed before each method of the class.
*/
protected function tearDown(): void
{
TestHelperDOCX::clear();
}

/**
* Test write styles.
*/
public function testCellPadding(): void
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$table = $section->addTable();
$table->addRow();

$testValTop = Converter::pixelToTwip(10);
$testValRight = Converter::pixelToTwip(11);
$testValBottom = Converter::pixelToTwip(12);
$testValLeft = Converter::pixelToTwip(13);

$cellStyle = [
'paddingTop' => $testValTop,
'paddingRight' => $testValRight,
'paddingBottom' => $testValBottom,
'paddingLeft' => $testValLeft,
];
$table->addCell(null, $cellStyle)->addText('Some text');
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');

$path = '/w:document/w:body/w:tbl/w:tr/w:tc/w:tcPr/w:tcMar/w:top';
self::assertTrue($doc->elementExists($path));
self::assertEquals($testValTop, $doc->getElementAttribute($path, 'w:w'));
self::assertEquals(TblWidth::TWIP, $doc->getElementAttribute($path, 'w:type'));

$path = '/w:document/w:body/w:tbl/w:tr/w:tc/w:tcPr/w:tcMar/w:start';
self::assertTrue($doc->elementExists($path));
self::assertEquals($testValLeft, $doc->getElementAttribute($path, 'w:w'));
self::assertEquals(TblWidth::TWIP, $doc->getElementAttribute($path, 'w:type'));

$path = '/w:document/w:body/w:tbl/w:tr/w:tc/w:tcPr/w:tcMar/w:bottom';
self::assertTrue($doc->elementExists($path));
self::assertEquals($testValBottom, $doc->getElementAttribute($path, 'w:w'));
self::assertEquals(TblWidth::TWIP, $doc->getElementAttribute($path, 'w:type'));

$path = '/w:document/w:body/w:tbl/w:tr/w:tc/w:tcPr/w:tcMar/w:end';
self::assertTrue($doc->elementExists($path));
self::assertEquals($testValRight, $doc->getElementAttribute($path, 'w:w'));
self::assertEquals(TblWidth::TWIP, $doc->getElementAttribute($path, 'w:type'));
}
}
Loading