- Issue created by @prudloff
- Merge request !4315Issue #3372446: PlainTextOutput::renderFromHtml() could better handle spaces → (Open) created by prudloff
- last update
almost 2 years ago 29,727 pass, 101 fail - Status changed to Needs review
almost 2 years ago 9:00am 5 July 2023 - Status changed to Needs work
almost 2 years ago 11:34am 5 July 2023 - 🇮🇳India keshavv India
I have tested the MR with this code snippet.
print_r(\Drupal\Component\Render\PlainTextOutput::renderFromHtml('<div class="test">Some content</div><p>Foo</p><p>Bar</p>'));
It results
- Status changed to Needs review
about 2 months ago 9:28pm 26 February 2025 - 🇺🇸United States smustgrave
Fixed up summary just slightly.
Left 1 comment on MR
If you are another contributor eager to jump in, please allow the previous poster at least 48 hours to respond to feedback first, so they have the opportunity to finish what they started!
- 🇺🇸United States smustgrave
You are absolutely correct. That was the only feedback I had
- 🇫🇷France nod_ Lille
I'll put that at least to NW, it's possibly a won't fix situation depending on how much exists out there that could help with the situation.
So the fix here is to add a space between all tags, this breaks pretty fast, for example:
<strong>test</strong><sup>nospace!</sup>
Here we would not expect a space to be added. Whitespace in HTML is very complex, see https://blog.dwac.dev/posts/html-whitespace/ so hand crafting rules by hand is not a reasonable solution. We could parse the string as HTML and use
textContent
property fromDOMNode
and hope it does things correctly with html5. - 🇫🇷France prudloff Lille
We could parse the string as HTML and use textContent property from DOMNode and hope it does things correctly with html5.
I did a quick test like this:
$dom = \Drupal\Component\Utility\Html::load('<p>Giraffes.</p><p>Wombats.</p>'); echo $dom->textContent;
But it seems it never adds any space.
A more robust solution would be to use the html2text library but it would be a bit overkill to add a new dependency just for this.