Skip to content

addHtml方法添加HTML过程中产生的BUG #2384

@NEETLee

Description

@NEETLee

Describe the Bug

我尝试将富文本转换为wrod文档。如果HTML代码中有任何标签的width属性值为auto就会引发一个异常:Unsupported operand types: string / int
比如这样的HTML片段:

<table style="width: auto;">
    <tbody>
    <tr>
        <td colspan="1" rowspan="1" width="auto">td</td>
    </tr>
    </tbody>
</table>

我通过跟踪源代码发现是 PhpOffice\PhpWord\Shared\Html::parseInlineStyle();方法中对于width的处理没有考虑到值为auto的情况
,我认为这是一个小问题,于是我尝试修复它。

我创建了一个 PhpOffice\PhpWord\Shared\Html的子类,希望重写parseInlineStyle方法,但是因为方法内的静态调用(就像self::)和固定了命名空间的函数调用(就像call_user_func_array(array('PhpOffice\PhpWord\Shared\Html', $method), array_values($arguments));),使我不得不在子类里重写了所有调用了parseInlineStyle的方法,也包括addHtml方法。这些方法几乎没有任何改动,仅仅是为了让代码在执行中能正确的调用我修复过的parseInlineStyle方法。

因此我有一个建议:是否可以在 PhpOffice\PhpWord\Shared\Html类中使用static::代替self::,同时call_user_func_array(array('PhpOffice\PhpWord\Shared\Html', $method), array_values($arguments));这样的代码可以替换成call_user_func_array(array(static::class, $method), array_values($arguments));

Steps to Reproduce

<?php
require __DIR__ . '/vendor/autoload.php';

$html=<<<html
<table style="width: auto;">
    <tbody>
    <tr>
        <td colspan="1" rowspan="1" width="auto">td</td>
    </tr>
    </tbody>
</table>
html;
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
PhpOffice\PhpWord\Shared\Html::addHtml($section, $html);

Expected Behavior

我希望它能成功转换

Current Behavior

抛出异常:

Unsupported operand types: string / int at /home/vagrant/code/proposition/vendor/phpoffice/phpword/src/PhpWord/Shared/Converter.php:151)
[stacktrace]
#0 /home/vagrant/code/proposition/vendor/phpoffice/phpword/src/PhpWord/Shared/Html.php(122): PhpOffice\\PhpWord\\Shared\\Converter::pixelToTwip()
#1 /home/vagrant/code/proposition/vendor/phpoffice/phpword/src/PhpWord/Shared/Html.php(469): PhpOffice\\PhpWord\\Shared\\Html::parseInlineStyle()
#2 /home/vagrant/code/proposition/vendor/phpoffice/phpword/src/PhpWord/Shared/Html.php(416): PhpOffice\\PhpWord\\Shared\\Html::recursiveParseStylesInHierarchy()
#3 [internal function]: PhpOffice\\PhpWord\\Shared\\Html::parseCell()
#4 /home/vagrant/code/proposition/vendor/phpoffice/phpword/src/PhpWord/Shared/Html.php(215): call_user_func_array()
#5 /home/vagrant/code/proposition/vendor/phpoffice/phpword/src/PhpWord/Shared/Html.php(247): PhpOffice\\PhpWord\\Shared\\Html::parseNode()
#6 /home/vagrant/code/proposition/vendor/phpoffice/phpword/src/PhpWord/Shared/Html.php(229): PhpOffice\\PhpWord\\Shared\\Html::parseChildNodes()
#7 /home/vagrant/code/proposition/vendor/phpoffice/phpword/src/PhpWord/Shared/Html.php(247): PhpOffice\\PhpWord\\Shared\\Html::parseNode()
#8 /home/vagrant/code/proposition/vendor/phpoffice/phpword/src/PhpWord/Shared/Html.php(229): PhpOffice\\PhpWord\\Shared\\Html::parseChildNodes()
#9 /home/vagrant/code/proposition/vendor/phpoffice/phpword/src/PhpWord/Shared/Html.php(247): PhpOffice\\PhpWord\\Shared\\Html::parseNode()
#10 /home/vagrant/code/proposition/vendor/phpoffice/phpword/src/PhpWord/Shared/Html.php(229): PhpOffice\\PhpWord\\Shared\\Html::parseChildNodes()
#11 /home/vagrant/code/proposition/vendor/phpoffice/phpword/src/PhpWord/Shared/Html.php(247): PhpOffice\\PhpWord\\Shared\\Html::parseNode()
#12 /home/vagrant/code/proposition/vendor/phpoffice/phpword/src/PhpWord/Shared/Html.php(229): PhpOffice\\PhpWord\\Shared\\Html::parseChildNodes()
#13 /home/vagrant/code/proposition/vendor/phpoffice/phpword/src/PhpWord/Shared/Html.php(84): PhpOffice\\PhpWord\\Shared\\Html::parseNode()
#14 /home/vagrant/code/proposition/app/Http/Controllers/WordController.php(32): PhpOffice\\PhpWord\\Shared\\Html::addHtml()
...

Context

  • PHP Version: PHP 8.2.3 (cli) (built: Feb 14 2023 16:57:50) (NTS)
  • PHPWord Version:0.18.3

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions