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

Sub/superscript HTML is rendered with line breaks #2476

Open
dsuurlant opened this issue Sep 19, 2023 · 3 comments
Open

Sub/superscript HTML is rendered with line breaks #2476

dsuurlant opened this issue Sep 19, 2023 · 3 comments

Comments

@dsuurlant
Copy link

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
@thomasb88
Copy link

I have a different environment than yours, but

I made it work with
$textWithTags = "

C8H10N4O2

";
instead of
$textWithTags = "C8H10N4O2";

I suppose it is because in the readParagraph function, there is the following part
"if (0 === $textRunContainers) {
$parent->addTextBreak(null, $paragraphStyle);
}"

And then when writing it, in HTML writer for example, it does

"if ($this->withoutP) {
$content = '
' . PHP_EOL;
} else {
$content = '

 

' . PHP_EOL;
}"

Note that adding a paragraph around your $textWithTags add also a text run

@oleibman
Copy link
Contributor

First, this seems to be a problem with all formatting tags (e.g. <b> will cause similar results). Section expects a line break between its constituent elements; TextRun, however, does not. So, the following minor modification of your third ("whitespace") attempt seems to work:

$textRun = $section->addTextRun();
Html::addHtml($textRun, $textWithTags);

Likewise for a table cell:

$textRun = $cell->addTextRun();
Html::addHtml($textRun, $textWithTags);

@thomasb88
Copy link

Perhaps because for OOXML, " A section is a grouping of paragraphs that have a specific set of properties used to define the pages on which the text will appear."

http:https://officeopenxml.com/WPsection.php

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

No branches or pull requests

3 participants