-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Parse formatting inside HTML lists #1239
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
Parse formatting inside HTML lists #1239
Conversation
@samimussbach wouldn't the following do the trick? private static function parseListItem($node, $element, &$styles, $data)
{
$cNodes = $node->childNodes;
if (!empty($cNodes)) {
$text = trim($node->textContent);
$element->addListItem($text, $data['listdepth'], $styles['font'], $styles['list'], $styles['paragraph']);
}
} |
other solution private static function getTextRecursively(\DOMNode $node)
{
$text = '';
$cNodes = $node->childNodes;
if (!empty($cNodes)) {
foreach ($cNodes as $cNode) {
if ($cNode->nodeName == '#text') {
$text .= ' ' . trim($cNode->nodeValue);
} else {
$text .= ' ' . self::getTextRecursively($cNode);
}
}
}
return trim($text);
} |
The latter would work, indeed. Your first suggestion has some problems with whitespacing but the second works with our test cases. When we go that way: wouldn't it be possible to parse styles as well in getTextRecursively and get #1215 tackled as well? |
@samimussbach actually, here is even better, it parses the styling inside the items: private static function parseListItem($node, $element, &$styles, $data)
{
$cNodes = $node->childNodes;
if (!empty($cNodes)) {
$listRun = $element->addListItemRun($data['listdepth'], $styles['list'], $styles['paragraph']);
foreach ($cNodes as $cNode) {
Html::parseNode($cNode, $listRun, $styles['list'], $data['listdepth']);
}
}
} |
Yes, very nice! This is what I did not manage to do. This works with our real cases. Test is failing but this is due to the crude structure of docx, test should be updated to match the real case. Bravo, #1215 is solved as well as far as I see. |
There is still an issue though when you have multiple lists, the numbering is not restarting. |
@troosan would you push your solution to this PR? You should get the credit but if you want to I could commit it as well. |
yes, I will merge it. I was just waiting to have it properly coved by some unit tests, almost done. |
I just found a case to consider:
The markup is <p>Test</p>
<ul>
<li>
<p>Item 1</p>
</li>
</ul> Which is (sad but true) reallife markup generated by TinyMCE |
https://github.com/samimussbach/PHPWord into parse_list_item_style Conflicts: src/PhpWord/Shared/Html.php
very sad indeed :-S |
Description
As a follow up of #1215 and #945: The current fix swallows texts in formatted lists when list item does not contain solely text formatted all the same.
This fix renders all texts.
Fixes # (issue)
#945
Checklist:
composer check
and no errors were reportedEdit: I modified tests because it shows another issue that is not part of this fix
Running
compooser check
leads toScript ./vendor/bin/php-cs-fixer fix --ansi --dry-run --diff handling the check event returned with error code 8