Skip to content

[BUGFIX] Prevent Twig from swallowing whitespace between inline tags #931

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
wants to merge 1 commit into from

Conversation

linawolf
Copy link
Contributor

resolves #922

I can only solve this by outputting &32; which equals a space. @wouterj is there any solution to force Twig to preserve a real space char?

@linawolf linawolf requested a review from jaapio March 16, 2024 09:11
@wouterj
Copy link
Contributor

wouterj commented Mar 16, 2024

I can only solve this by outputting &32; which equals a space. @wouterj is there any solution to force Twig to preserve a real space char?

It does not appear to be Twig who swallows the whitespace. If I try out this minimal example, the output is · (i.e. Twig renders it as a single space):

<?php

require_once 'vendor/autoload.php';

$loader = new \Twig\Loader\ArrayLoader([
    'test.twig' => '{{- value -}}',
]);
$twig = new \Twig\Environment($loader);

/** Small function to make whitespace visible */
function show_whitespace(string $text) {
    return preg_replace(['/\v/', '/\h/'], ["\n", '·'], $text);
}

echo show_whitespace(
  $twig->render('test.twig', ['value' => ' '])
);

I've always been a bit confused by how whitespace is handled in this project, the need for {% apply spaceless %} in some templates is also strange to me. I think we must identify which part of the NodeRenderer process is responsible for swallowing the whitespace.

@linawolf
Copy link
Contributor Author

I can render any sign but a space. I have no idea how twig works. Chatgbt told me it tries to get rid of superfluous spaces...

@linawolf
Copy link
Contributor Author

applyspaceless is needed in inline templates, because our id adds some such as an empty line at the end of each document. These would then get output, however bold: then becomes bold :

@wouterj
Copy link
Contributor

wouterj commented Mar 16, 2024

I'll find some time this weekend to see if I can find the troubling spot using the test case you added in this PR.

@wouterj
Copy link
Contributor

wouterj commented Mar 17, 2024

I've tracked down this issue to be a result of using the spaceless filter in paragraph.html.twig. I've refactored whitespace control to avoid using spaceless anywhere and taken your test from here in #944. We can close this as being replaced by the new PR.

@linawolf linawolf closed this Mar 17, 2024
@linawolf linawolf deleted the bugfix/whitespace branch May 6, 2024 19:17
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: Space missing after bold followed by a reference
2 participants