Skip to content

Commit 10ae499

Browse files
Azamat8405OlisaevAG
andauthored
Writer Word2007: Support for padding in Table Cell (#2697)
* Support for padding in a table cell * Support for padding in a table cell. fix code style * feat: Support for padding in a table cell. Addition for changelog * feat: Support for padding in a table cell. Test file * feat: Support for padding in a table cell. Test file fix code style * feat: Support for padding in a table cell. More test file * Update composer.json azamat8405/phpword * Update composer.json azamat8405/phpword * Update composer.json fix renaming * feat: Сhanges for the code review * feat: Сhanges for the code review 2 * feat: feat: Сhanges for the code review 3 * feat: feat: Сhanges for the code review 4 * feat: feat: Сhanges for the code review 5 --------- Co-authored-by: OlisaevAG <[email protected]>
1 parent 21ac083 commit 10ae499

File tree

7 files changed

+355
-0
lines changed

7 files changed

+355
-0
lines changed

docs/changes/1.x/1.4.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
- 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)
1010
- Writer HTML: Support for vAlign in Tables by [@SpraxDev](https://github.com/SpraxDev) in [#2675](https://github.com/PHPOffice/PHPWord/pull/2675)
11+
- Writer Word2007: Support for padding in Table Cell by [@Azamat8405](https://github.com/Azamat8405) in [#2697](https://github.com/PHPOffice/PHPWord/pull/2697)
1112
- Added support for PHP 8.4 by [@Progi1984](https://github.com/Progi1984) in [#2660](https://github.com/PHPOffice/PHPWord/pull/2660)
1213
- Autoload : Allow to use PHPWord without Composer fixing [#2543](https://github.com/PHPOffice/PHPWord/issues/2543), [#2552](https://github.com/PHPOffice/PHPWord/issues/2552), [#2716](https://github.com/PHPOffice/PHPWord/issues/2716), [#2717](https://github.com/PHPOffice/PHPWord/issues/2717) in [#2722](https://github.com/PHPOffice/PHPWord/pull/2722)
1314

src/PhpWord/Shared/Html.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,58 @@ protected static function parseStyleDeclarations(array $selectors, array $styles
804804
$styles['spaceAfter'] = Converter::cssToTwip($value);
805805

806806
break;
807+
808+
case 'padding':
809+
$valueTop = $valueRight = $valueBottom = $valueLeft = null;
810+
$cValue = preg_replace('# +#', ' ', trim($value));
811+
$paddingArr = explode(' ', $cValue);
812+
$countParams = count($paddingArr);
813+
if ($countParams == 1) {
814+
$valueTop = $valueRight = $valueBottom = $valueLeft = $paddingArr[0];
815+
} elseif ($countParams == 2) {
816+
$valueTop = $valueBottom = $paddingArr[0];
817+
$valueRight = $valueLeft = $paddingArr[1];
818+
} elseif ($countParams == 3) {
819+
$valueTop = $paddingArr[0];
820+
$valueRight = $valueLeft = $paddingArr[1];
821+
$valueBottom = $paddingArr[2];
822+
} elseif ($countParams == 4) {
823+
$valueTop = $paddingArr[0];
824+
$valueRight = $paddingArr[1];
825+
$valueBottom = $paddingArr[2];
826+
$valueLeft = $paddingArr[3];
827+
}
828+
if ($valueTop !== null) {
829+
$styles['paddingTop'] = Converter::cssToTwip($valueTop);
830+
}
831+
if ($valueRight !== null) {
832+
$styles['paddingRight'] = Converter::cssToTwip($valueRight);
833+
}
834+
if ($valueBottom !== null) {
835+
$styles['paddingBottom'] = Converter::cssToTwip($valueBottom);
836+
}
837+
if ($valueLeft !== null) {
838+
$styles['paddingLeft'] = Converter::cssToTwip($valueLeft);
839+
}
840+
841+
break;
842+
case 'padding-top':
843+
$styles['paddingTop'] = Converter::cssToTwip($value);
844+
845+
break;
846+
case 'padding-right':
847+
$styles['paddingRight'] = Converter::cssToTwip($value);
848+
849+
break;
850+
case 'padding-bottom':
851+
$styles['paddingBottom'] = Converter::cssToTwip($value);
852+
853+
break;
854+
case 'padding-left':
855+
$styles['paddingLeft'] = Converter::cssToTwip($value);
856+
857+
break;
858+
807859
case 'border-color':
808860
self::mapBorderColor($styles, $value);
809861

src/PhpWord/Style/Cell.php

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,26 @@ class Cell extends Border
7474
*/
7575
private $vAlign;
7676

77+
/**
78+
* @var null|int
79+
*/
80+
private $paddingTop;
81+
82+
/**
83+
* @var null|int
84+
*/
85+
private $paddingBottom;
86+
87+
/**
88+
* @var null|int
89+
*/
90+
private $paddingLeft;
91+
92+
/**
93+
* @var null|int
94+
*/
95+
private $paddingRight;
96+
7797
/**
7898
* Text Direction.
7999
*
@@ -357,4 +377,84 @@ public function getNoWrap(): bool
357377
{
358378
return $this->noWrap;
359379
}
380+
381+
/**
382+
* Get style padding-top.
383+
*/
384+
public function getPaddingTop(): ?int
385+
{
386+
return $this->paddingTop;
387+
}
388+
389+
/**
390+
* Set style padding-top.
391+
*
392+
* @return $this
393+
*/
394+
public function setPaddingTop(int $value): self
395+
{
396+
$this->paddingTop = $value;
397+
398+
return $this;
399+
}
400+
401+
/**
402+
* Get style padding-bottom.
403+
*/
404+
public function getPaddingBottom(): ?int
405+
{
406+
return $this->paddingBottom;
407+
}
408+
409+
/**
410+
* Set style padding-bottom.
411+
*
412+
* @return $this
413+
*/
414+
public function setPaddingBottom(int $value): self
415+
{
416+
$this->paddingBottom = $value;
417+
418+
return $this;
419+
}
420+
421+
/**
422+
* Get style padding-left.
423+
*/
424+
public function getPaddingLeft(): ?int
425+
{
426+
return $this->paddingLeft;
427+
}
428+
429+
/**
430+
* Set style padding-left.
431+
*
432+
* @return $this
433+
*/
434+
public function setPaddingLeft(int $value): self
435+
{
436+
$this->paddingLeft = $value;
437+
438+
return $this;
439+
}
440+
441+
/**
442+
* Get style padding-right.
443+
*/
444+
public function getPaddingRight(): ?int
445+
{
446+
return $this->paddingRight;
447+
}
448+
449+
/**
450+
* Set style padding-right.
451+
*
452+
* @return $this
453+
*/
454+
public function setPaddingRight(int $value): self
455+
{
456+
$this->paddingRight = $value;
457+
458+
return $this;
459+
}
360460
}

src/PhpWord/Writer/Word2007/Style/Cell.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,40 @@ public function write(): void
5555
$xmlWriter->endElement(); // w:tcW
5656
}
5757

58+
$paddingTop = $style->getPaddingTop();
59+
$paddingLeft = $style->getPaddingLeft();
60+
$paddingBottom = $style->getPaddingBottom();
61+
$paddingRight = $style->getPaddingRight();
62+
63+
if ($paddingTop !== null || $paddingLeft !== null || $paddingBottom !== null || $paddingRight !== null) {
64+
$xmlWriter->startElement('w:tcMar');
65+
if ($paddingTop !== null) {
66+
$xmlWriter->startElement('w:top');
67+
$xmlWriter->writeAttribute('w:w', $paddingTop);
68+
$xmlWriter->writeAttribute('w:type', \PhpOffice\PhpWord\SimpleType\TblWidth::TWIP);
69+
$xmlWriter->endElement(); // w:top
70+
}
71+
if ($paddingLeft !== null) {
72+
$xmlWriter->startElement('w:start');
73+
$xmlWriter->writeAttribute('w:w', $paddingLeft);
74+
$xmlWriter->writeAttribute('w:type', \PhpOffice\PhpWord\SimpleType\TblWidth::TWIP);
75+
$xmlWriter->endElement(); // w:start
76+
}
77+
if ($paddingBottom !== null) {
78+
$xmlWriter->startElement('w:bottom');
79+
$xmlWriter->writeAttribute('w:w', $paddingBottom);
80+
$xmlWriter->writeAttribute('w:type', \PhpOffice\PhpWord\SimpleType\TblWidth::TWIP);
81+
$xmlWriter->endElement(); // w:bottom
82+
}
83+
if ($paddingRight !== null) {
84+
$xmlWriter->startElement('w:end');
85+
$xmlWriter->writeAttribute('w:w', $paddingRight);
86+
$xmlWriter->writeAttribute('w:type', \PhpOffice\PhpWord\SimpleType\TblWidth::TWIP);
87+
$xmlWriter->endElement(); // w:end
88+
}
89+
$xmlWriter->endElement(); // w:tcMar
90+
}
91+
5892
// Text direction
5993
$textDir = $style->getTextDirection();
6094
$xmlWriter->writeElementIf(null !== $textDir, 'w:textDirection', 'w:val', $textDir);

tests/PhpWordTests/Shared/HtmlTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use PhpOffice\PhpWord\Element\Section;
2323
use PhpOffice\PhpWord\Element\Table;
2424
use PhpOffice\PhpWord\PhpWord;
25+
use PhpOffice\PhpWord\Shared\Converter;
2526
use PhpOffice\PhpWord\Shared\Html;
2627
use PhpOffice\PhpWord\SimpleType\Jc;
2728
use PhpOffice\PhpWord\SimpleType\LineSpacingRule;
@@ -270,6 +271,66 @@ public function testParseLineHeight(): void
270271
self::assertEquals(LineSpacingRule::AUTO, $doc->getElementAttribute('/w:document/w:body/w:p[5]/w:pPr/w:spacing', 'w:lineRule'));
271272
}
272273

274+
public function testParseCellPaddingStyle(): void
275+
{
276+
$phpWord = new PhpWord();
277+
$section = $phpWord->addSection();
278+
279+
$top = 10;
280+
$right = 11;
281+
$bottom = 12;
282+
$left = 13;
283+
284+
$testValTop = Converter::pixelToTwip($top);
285+
$testValRight = Converter::pixelToTwip($right);
286+
$testValBottom = Converter::pixelToTwip($bottom);
287+
$testValLeft = Converter::pixelToTwip($left);
288+
289+
$html = '<table>
290+
<tbody>
291+
<tr>
292+
<td style="padding:' . $top . 'px ' . $right . 'px ' . $bottom . 'px ' . $left . 'px;">full</td>
293+
<td style="padding:' . $top . 'px 0px ' . $bottom . 'px ' . $left . 'px;padding-right:' . $right . 'px;">mix</td>
294+
<td style="padding-top:' . $top . 'px;">top</td>
295+
<td style="padding-bottom:' . $bottom . 'px;">bottom</td>
296+
<td style="padding-left:' . $left . 'px;">left</td>
297+
</tr>
298+
</tbody>
299+
</table>';
300+
Html::addHtml($section, $html);
301+
302+
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
303+
304+
$path = '/w:document/w:body/w:tbl/w:tr/w:tc[1]/w:tcPr/w:tcMar/w:top';
305+
self::assertTrue($doc->elementExists($path));
306+
$path = '/w:document/w:body/w:tbl/w:tr/w:tc[1]/w:tcPr/w:tcMar/w:bottom';
307+
self::assertTrue($doc->elementExists($path));
308+
$path = '/w:document/w:body/w:tbl/w:tr/w:tc[1]/w:tcPr/w:tcMar/w:end';
309+
self::assertTrue($doc->elementExists($path));
310+
$path = '/w:document/w:body/w:tbl/w:tr/w:tc[1]/w:tcPr/w:tcMar/w:start';
311+
self::assertTrue($doc->elementExists($path));
312+
313+
$path = '/w:document/w:body/w:tbl/w:tr/w:tc[2]/w:tcPr/w:tcMar/w:end';
314+
self::assertTrue($doc->elementExists($path));
315+
self::assertEquals($testValRight, $doc->getElementAttribute($path, 'w:w'));
316+
self::assertEquals(TblWidth::TWIP, $doc->getElementAttribute($path, 'w:type'));
317+
318+
$path = '/w:document/w:body/w:tbl/w:tr/w:tc[3]/w:tcPr/w:tcMar/w:top';
319+
self::assertTrue($doc->elementExists($path));
320+
self::assertEquals($testValTop, $doc->getElementAttribute($path, 'w:w'));
321+
self::assertEquals(TblWidth::TWIP, $doc->getElementAttribute($path, 'w:type'));
322+
323+
$path = '/w:document/w:body/w:tbl/w:tr/w:tc[4]/w:tcPr/w:tcMar/w:bottom';
324+
self::assertTrue($doc->elementExists($path));
325+
self::assertEquals($testValBottom, $doc->getElementAttribute($path, 'w:w'));
326+
self::assertEquals(TblWidth::TWIP, $doc->getElementAttribute($path, 'w:type'));
327+
328+
$path = '/w:document/w:body/w:tbl/w:tr/w:tc[5]/w:tcPr/w:tcMar/w:start';
329+
self::assertTrue($doc->elementExists($path));
330+
self::assertEquals($testValLeft, $doc->getElementAttribute($path, 'w:w'));
331+
self::assertEquals(TblWidth::TWIP, $doc->getElementAttribute($path, 'w:type'));
332+
}
333+
273334
/**
274335
* Test text-indent style.
275336
*/

tests/PhpWordTests/Style/CellTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,25 @@ public function testBorderSize(): void
9090
$object->setStyleValue('borderSize', $value);
9191
self::assertEquals($expected, $object->getBorderSize());
9292
}
93+
94+
/**
95+
* Test cell padding.
96+
*/
97+
public function testPadding(): void
98+
{
99+
$object = new Cell();
100+
$methods = [
101+
'paddingTop' => 10,
102+
'paddingBottom' => 20,
103+
'paddingLeft' => 30,
104+
'paddingRight' => 40,
105+
];
106+
107+
foreach ($methods as $methodName => $methodValue) {
108+
$object->setStyleValue($methodName, $methodValue);
109+
$getterName = 'get' . ucfirst($methodName);
110+
111+
self::assertEquals($methodValue, $object->$getterName());
112+
}
113+
}
93114
}

0 commit comments

Comments
 (0)