PHPWord icon indicating copy to clipboard operation
PHPWord copied to clipboard

Sub/superscript HTML is rendered with line breaks

Open dsuurlant opened this issue 1 year ago • 3 comments

Describe the Bug

When attempting to add text with <sup>...</sup> or <sub>...</sub> tags using Html::addHtml and placing it into a Section or Table Cell, the resulting text has unwanted line breaks. When placing it in a TextRun, the text isn't rendered at all.

Steps to Reproduce

Please provide a code sample that reproduces the issue.

$textWithTags = "C<sup>8</sup>H<sup>10</sup>N<sub>4</sub>O<sub>2</sub>";

$phpWord = new PhpWord();
$section = $phpWord->addSection();
Html::addHtml($section, $textWithTags); // Rendered with line breaks

$table = $section->addTable();
$row = $table->addRow();
$cell = $row->addCell();
Html::addHtml($cell, $textWithTags); // Rendered with line breaks

$textRun = new TextRun();
Html::addHtml($textRun, $textWithTags); 
$section->addTextRun($textRun); // Doesn't work at all, just whitespace

$tmpFile = '/tmp/PhpWordTest.docx';
$objWriter = IOFactory::createWriter($phpWord);
$objWriter->save($tmpFile);

Our target behavior is actually to render the text with sub/sup tags inside a table that is used with the template processor:

// Create template
$template = new PhpWord();
$section = $template->addSection();
$section->addText('${replaceThis}');

$templateFile = '/tmp/PhpWordTemplate.docx';
$objWriter = IOFactory::createWriter($template);
$objWriter->save($templateFile);

// Create table to render in template
$table = new Table();
$row = $table->addRow();
$cell = $row->addCell();
Html::addHtml($cell, $textWithTags);

$processor = new TemplateProcessor($templateFile);
$templateProcessor->setComplexValue('replaceThis', $table);
$renderedTemplateFile = '/tmp/PhpWordRenderedTemplate.docx';
$processor->saveAs($renderedTemplateFile);

In the above example, the text inside the table cell has line breaks as well.

Working example

We know this behaviour is possible as this does work in the TemplateProcessor with a TextRun:

$processor = new TemplateProcessor($templateFile);
$textRun = new TextRun();
Html::addHtml($textRun, $textWithTags);
$processor->setComplexValue('replaceThis', $textRun);
$renderedTemplateFile = '/tmp/PhpWordRenderedTemplate.docx';
$processor->saveAs($renderedTemplateFile);

Now the sub/sup tags are rendered correctly on a single line.

Expected Behavior

I would expect the text to be rendered without line breaks whether in a table cell, a textrun, or any other element; with or without the TemplateProcessor.

Current Behavior

Text with sub or sup HTML tags is rendered with line breaks.

Context

Please fill in your environment information:

  • PHP 8.1.18
  • PHPWord Version: 1.1.0

dsuurlant avatar Sep 19 '23 10:09 dsuurlant