Skip to content

Commit

Permalink
Fix a little issue with sizing table columns
Browse files Browse the repository at this point in the history
  • Loading branch information
otsch committed Jan 26, 2024
1 parent f286f5e commit 90ae05e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/NodeConverters/Table/TableCell.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ public function __construct(
public readonly int $colspan,
public readonly string $text
) {
$this->length = strlen($this->text);
$this->length = mb_strlen($this->text);
}
}
2 changes: 1 addition & 1 deletion src/NodeConverters/TableConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,6 @@ protected function getTableCell(DOMElement|DOMNode $td): TableCell

protected function removeLineBreaksFromTdText(string $text): string
{
return preg_replace('/\s+/', ' ', $text) ?? $text;
return trim(preg_replace('/\s+/', ' ', $text) ?? $text);
}
}
69 changes: 56 additions & 13 deletions tests/NodeConverters/TableConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@
namespace tests\NodeConverters;

use Crwlr\Html2Text\Aggregates\DomNodeAndPrecedingText;
use Crwlr\Html2Text\Html2Text;
use Crwlr\Html2Text\NodeConverters\TableConverter;

it('correctly converts a table', function () {
$nodeConverter = new TableConverter();

$nodeConverter->setConverter(new Html2Text());

$html = <<<HTML
<table id="a">
<tr><th>Column1</th><th>Column2</th><th>Column3</th><th>Column4</th><th>Column5</th></tr>
Expand All @@ -36,8 +33,6 @@
it('correctly converts a table with thead and tbody sections', function () {
$nodeConverter = new TableConverter();

$nodeConverter->setConverter(new Html2Text());

$html = <<<HTML
<table id="a">
<thead>
Expand Down Expand Up @@ -68,8 +63,6 @@
it('correctly sizes the columns', function () {
$nodeConverter = new TableConverter();

$nodeConverter->setConverter(new Html2Text());

$html = <<<HTML
<table id="a">
<thead>
Expand Down Expand Up @@ -100,8 +93,6 @@
it('correctly handles cells with a colspan attribute', function () {
$nodeConverter = new TableConverter();

$nodeConverter->setConverter(new Html2Text());

$html = <<<HTML
<table id="a">
<thead>
Expand Down Expand Up @@ -132,8 +123,6 @@
it('correctly handles colspan columns, when their content is longer than the separate columns combined', function () {
$nodeConverter = new TableConverter();

$nodeConverter->setConverter(new Html2Text());

$html = <<<HTML
<table id="a">
<thead>
Expand Down Expand Up @@ -166,8 +155,6 @@
it('handles columns containing line breaks, by removing the line breaks', function () {
$nodeConverter = new TableConverter();

$nodeConverter->setConverter(new Html2Text());

$html = <<<HTML
<table id="a">
<tr><th>Column1</th><th>Column2</th><th>Column3</th><th>Column4</th><th>Column5</th></tr>
Expand All @@ -187,3 +174,59 @@
TEXT);
});

test('sizing table columns works correctly with € character in the table data', function () {
$nodeConverter = new TableConverter();

$html = <<<HTML
<table id="a">
<thead>
<tr><th></th><th>XS</th><th>S</th><th>M</th><th>L</th></tr>
</thead>
<tbody>
<tr>
<td>Requests/Tag</td>
<td>5.000</td>
<td>15.000</td>
<td>60.000</td>
<td>250.000</td>
</tr>
<tr>
<td>Speicherplatz</td><td>1 GB</td><td>5 GB</td><td>20 GB</td><td>50 GB</td>
</tr>
<tr data-period="monthly">
<td>Preis inkl. USt.</td>
<td>
€ 36<br>
<span>pro Monat</span>
</td>
<td>
€ 72<br>
<span>pro Monat</span>
</td>
<td>
€ 240<br>
<span>pro Monat</span>
</td>
<td>
€ 720<br>
<span>pro Monat</span>
</td>
</tr>
</tbody>
</table>
HTML;

$node = new DomNodeAndPrecedingText(helper_getElementById($html, 'a'), 'hi');

expect($nodeConverter->convert($node))
->toBe(<<<TEXT
| | XS | S | M | L |
| ---------------- | -------------- | -------------- | --------------- | --------------- |
| Requests/Tag | 5.000 | 15.000 | 60.000 | 250.000 |
| Speicherplatz | 1 GB | 5 GB | 20 GB | 50 GB |
| Preis inkl. USt. | € 36 pro Monat | € 72 pro Monat | € 240 pro Monat | € 720 pro Monat |
TEXT);
});

0 comments on commit 90ae05e

Please sign in to comment.