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

Nested lists only works with invalid HTML #1279

Closed
rkorebrits opened this issue Feb 13, 2018 · 3 comments
Closed

Nested lists only works with invalid HTML #1279

rkorebrits opened this issue Feb 13, 2018 · 3 comments

Comments

@rkorebrits
Copy link

This is a bug report

Expected Behavior

In HTML when nesting lists, the nested <ul> needs to be inside, at the end of the parent <li> node.

Current Behavior

E.g. Sample 26 uses invalid HTML to build HTML for the list.

The error message that I receive:

Type: BadMethodCallException
Message: Cannot add ListItemRun in ListItemRun.
Filename: /www/_ditto/ditto/vendor/phpoffice/phpword/src/PhpWord/Element/AbstractContainer.php
Line Number: 230

How to Reproduce

<?php
    public function runTest(){
        $phpWord = new \PhpOffice\PhpWord\PhpWord();
        $section = $phpWord->addSection();
        $html = '<h1>Adding element via HTML</h1>';

        // WRONG - but works
        $html .= '<ul><li>Item 1</li><li>Item 2</li><ul><li>Item 2.1</li><li>Item 2.1</li></ul></ul>';
        // RIGHT - But throws an error
        $html .= '<ul><li>Item 1</li><li>Item 2<ul><li>Item 2.1</li><li>Item 2.1</li></ul></li></ul>';

        \PhpOffice\PhpWord\Shared\Html::addHtml($section, $html, false, false);

        $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter(
            $phpWord, 'Word2007'
        );

        $file_path_docx = 'test_bullets.docx';
        $objWriter->save($file_path_docx);
    }

To solve this for now, I've added a regex to extract the HTML to make it work:

    // First replace new lines by spaces
    $html = str_replace(["\r\n", "\n", "\r"], '', $html);
    // Extract UL from LI an place after
    $html = preg_replace('/(<li.*?>.*?)(<ul>.*?<\/ul>)\s?(<\/li>)/i', '$1$3$2', $html);

Not sure if it's the best regex, but for our solution it works.

Context

  • PHP version: 7.1
  • PHPWord version: latest dev
@troosan
Copy link
Contributor

troosan commented Feb 13, 2018

@rkorebrits I think this is a duplicate of #1265, it was fixed in #1273. It was merged here
Are you sure you are using the dev-develop branch?

@rkorebrits
Copy link
Author

rkorebrits commented Feb 13, 2018

EDIT - Looks like composer wasn't updated properly
p.s. this doesn't resolve my other issue with <li> spacing

Apologies for duplicates!

Composer.json: "phpoffice/phpword": "dev-develop"

richard@ubuntu:/var/www/ditto$ composer update phpoffice/phpword
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Generating autoload files

composer.json inside package:

"extra": {
    "branch-alias": {
        "dev-master": "0.15-dev"
    }
}

Am I missing something?

@vladimirvalakh
Copy link

vladimirvalakh commented Aug 21, 2020

Some issue.
I'm using develop branch.

$section = $phpWord->addSection([
'marginLeft' => 600,
'marginRight' => 600,
'marginTop' => 600,
'marginBottom' => 600
]);

$html="<div><p>fdf</p><ul><li>123</li></ul></div>";

$section->addListItem(
    \PhpOffice\PhpWord\Shared\Html::addHtml($section, insertHtmlToList($html), false, false), 1, null);

have error:
PHP Fatal error: Uncaught BadMethodCallException: Cannot add ListItemRun in TextRun

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants