Skip to content

Commit

Permalink
fix issue on justify text with an inline html tag at the end of a lin…
Browse files Browse the repository at this point in the history
…e - issue #258
  • Loading branch information
spipu committed Jul 31, 2018
1 parent 4a1f503 commit a75e1f0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ All notable changes to this project will be documented in this file.
* fix issue on line number when using style tag - issue #338
* fix issue on svg draw path - relative move was not implemented
* fix issue on svg draw path when Z directive is not separate from the next directive - issue #308
* fix issue on justify text with an inline html tag at the end of a line - issue #258
* fix better doc

## [5.1.0](https://github.com/spipu/html2pdf/compare/v5.0.1...v5.1.0) - 2018-01-23
Expand Down
43 changes: 36 additions & 7 deletions src/Parsing/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,26 +111,55 @@ public function parse($tokens)
'option'
);

// list of the tags to move space
$tagsToSpace = array(
'span', 'font', 'label',
'strong', 'b',
'address', 'cite', 'em', 'i', 'samp',
'cite', 's',
'ins', 'u',
'big', 'small', 'sub', 'sup'
);

// foreach action
$nb = count($actions);
for ($k = 0; $k < $nb; $k++) {
// if it is a Text
if ($actions[$k]->getName() === 'write') {
// if the tag before the text is a tag to clean => ltrim on the text
if ($k>0 && in_array($actions[$k - 1]->getName(), $tagsToClean)) {
if ($actions[$k]->getName() !== 'write') {
continue;
}

// if the tag before the text is a tag to clean => ltrim on the text
if ($k>0) {
if (in_array($actions[$k - 1]->getName(), $tagsToClean)) {
$actions[$k]->setParam('txt', ltrim($actions[$k]->getParam('txt')));
}
}

if ($k < $nb - 1) {
// if the tag after the text is a tag to clean => rtrim on the text
if ($k < $nb - 1 && in_array($actions[$k + 1]->getName(), $tagsToClean)) {
if (in_array($actions[$k + 1]->getName(), $tagsToClean)) {
$actions[$k]->setParam('txt', rtrim($actions[$k]->getParam('txt')));
}

// if the text is empty => remove the action
if (!strlen($actions[$k]->getParam('txt'))) {
unset($actions[$k]);
// if the tag after the text is a tag with space to move => move the space to the next write
if (in_array($actions[$k + 1]->getName(), $tagsToSpace)) {
if (substr($actions[$k]->getParam('txt'), -1) == ' ') {
$actions[$k]->setParam('txt', rtrim($actions[$k]->getParam('txt')));
for ($subK = $k+2; $subK < $nb; $subK++) {
if ($actions[$subK]->getName() === 'write') {
$actions[$subK]->setParam('txt', ' '.ltrim($actions[$subK]->getParam('txt')));
break;
}
}
}
}
}

// if the text is empty => remove the action
if (!strlen($actions[$k]->getParam('txt'))) {
unset($actions[$k]);
}
}

// if we are not on the level 0 => HTML validator ERROR
Expand Down

0 comments on commit a75e1f0

Please sign in to comment.