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

feat: add support for rowspan in addHTML() #2643

Open
wants to merge 6 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/2.x/2.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- IOFactory : Added extractVariables method to extract variables from a document [@sibalonat](https://github.com/sibalonat) in [#2515](https://github.com/PHPOffice/PHPWord/pull/2515)
- PDF Writer : Documented how to specify a PDF renderer, when working with the PDF writer, as well as the three available choices by [@settermjd](https://github.com/settermjd) in [#2642](https://github.com/PHPOffice/PHPWord/pull/2642)
- HTML Parser : Added support for rowspan in add HTML [#1643](https://github.com/PHPOffice/PHPWord/issues/1643)
Copy link
Member

Choose a reason for hiding this comment

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

@jnlin Could you add you as author ?


### Bug fixes

Expand Down
2 changes: 1 addition & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ parameters:

-
message: "#^Call to an undefined method DOMNode\\:\\:getAttribute\\(\\)\\.$#"
count: 1
count: 6
path: src/PhpWord/Shared/Html.php

-
Expand Down
42 changes: 42 additions & 0 deletions src/PhpWord/Shared/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -472,11 +472,53 @@ protected static function parseCell($node, $element, &$styles)
$cellStyles['gridSpan'] = $colspan - 0;
}

$rowspan = $node->getAttribute('rowspan');
if (!empty($rowspan)) {
$cellStyles['vMerge'] = 'restart';
}
$beforespan = $node->getAttribute('beforespan');
Copy link
Member

Choose a reason for hiding this comment

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

This attribute doesn't exist. Can you use a Real attribute or an another method ?

if (!empty($beforespan)) {
$cellRowContinue = ['vMerge' => 'continue'];
$beforecolspan = $node->getAttribute('beforecolspan');

for ($s = 1; $s <= $beforespan; ++$s) {
if (!empty($beforecolspan)) {
if (is_numeric($beforecolspan)) {
$beforecolspan = (int) $beforecolspan;
} else {
$beforecolspans = json_decode($beforecolspan, true);
$beforecolspan = $beforecolspans[$s - 1];
}
$cellRowContinue['gridSpan'] = $beforecolspan;
}
$element->addCell(null, $cellRowContinue);
}
}

// set cell width to control column widths
$width = $cellStyles['width'] ?? null;
unset($cellStyles['width']); // would not apply
$cell = $element->addCell($width, $cellStyles);

$afterspan = $node->getAttribute('afterspan');
if (!empty($afterspan)) {
$cellRowContinue = ['vMerge' => 'continue'];
$aftercolspan = $node->getAttribute('aftercolspan');
Copy link
Member

Choose a reason for hiding this comment

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

This attribute doesn't exist. Can you use a Real attribute or an another method ?


for ($s = 1; $s <= $afterspan; ++$s) {
if (!empty($aftercolspan)) {
if (is_numeric($aftercolspan)) {
$aftercolspan = (int) $aftercolspan;
} else {
$aftercolspans = json_decode($aftercolspan, true);
$aftercolspan = $aftercolspans[$s - 1];
}
$cellRowContinue['gridSpan'] = $aftercolspan;
}
$element->addCell(null, $cellRowContinue);
}
}

if (self::shouldAddTextRun($node)) {
return $cell->addTextRun(self::filterOutNonInheritedStyles(self::parseInlineStyle($node, $styles['paragraph'])));
}
Expand Down
34 changes: 33 additions & 1 deletion tests/PhpWordTests/Shared/HtmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public function testParseStyle(): void
background-color:red;
}
</style>

<p class="pStyle">Calculator</p>';
$phpWord = new PhpWord();
$section = $phpWord->addSection();
Expand Down Expand Up @@ -418,6 +418,38 @@ public function testParseTable(): void
self::assertFalse($doc->elementExists('/w:document/w:body/w:tbl/w:tr[1]/w:tc[2]/w:p/w:pPr/w:pBdr'));
}

public function testParseTableWithRowSpan(): void
{
$phpWord = new PhpWord();
$section = $phpWord->addSection();
$html = '<table style="width: 100%; border: 1px #000000 solid;" cellspacing="0" collpadding="0">
<thead>
<tr style="background-color: #FF0000; text-align: center; color: #FFFFFF; font-weight: bold; ">
<th style="text-align:center;">A</th>
<th style="text-align:center;">B</th>
<th style="text-align:center;">C</th>
<th style="text-align:center;">D</th>
</tr>
</thead>
<tbody>
<tr><td > A1 </td><td colspan="2"> BC1 </td><td> D1 </td></tr>
<tr><td rowspan="2" colspan="2"> AB23 </td><td> C2 </td><td> D2 </td></tr>
<tr><td beforespan="1" beforecolspan="2" > C3 </td><td> D3 </td></tr>
<tr><td rowspan="3" > A456 </td><td> B4 </td><td rowspan="2" colspan="2"> CD45 </td></tr>
<tr><td rowspan="2" beforespan="1" afterspan="1" aftercolspan="2">B5</td></tr>
<tr><td beforespan="2">C6</td><td> D6 </td></tr>
<tr><td> A7 </td><td> B7 </td><td> C7 </td><td> D7 </td></tr>
<tr><td > A8 </td><td colspan="2"> BC8 </td><td > D8 </td></tr>
<tr><td colspan="3"> ABC9 </td><td rowspan="2"> D9 </td></tr>
<tr><td > A9 </td><td > B9 </td><td afterspan="1"> C9 </td></tr>
</tbody>
</table>';
Html::addHtml($section, $html);

$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
self::assertTrue($doc->elementExists('/w:document/w:body/w:tbl'));
Copy link
Member

Choose a reason for hiding this comment

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

@jnlin Could you add more assertions for checking the colspan and the rowspan ?

}

/**
* Parse widths in tables and cells, which also allows for controlling column width.
*/
Expand Down
Loading