Skip to content

Renderer: translate Stringable descriptions and group labels - #136

Open
f3l1x wants to merge 2 commits into
masterfrom
claude/amazing-bardeen-74yvzl
Open

f3l1x wants to merge 2 commits into
masterfrom
claude/amazing-bardeen-74yvzl

Conversation

@f3l1x

@f3l1x f3l1x commented Sep 29, 2026 •

Copy link
Copy Markdown
Member

Summary

Closes #48.

BootstrapRenderer::renderDescription() translated a description only when it was a plain string. Any other value that was not Html, for example a Contributte\Translation\Wrappers\Message or another Stringable, became '', so the description disappeared without an error. Group labels had the same problem. The same code works in Nette's DefaultFormRenderer, as the reporter says.

Changes

  • src/BootstrapRenderer.php, renderDescription():
    • HtmlStringable (including Html) → rendered raw, as trusted HTML (the same as Nette).
    • Plain string → unchanged: setHtml($control->translate(...)), kept for BC.
    • Other Stringable → setText($control->translate(...)), which escapes it. If the translator returns an HtmlStringable, it stays raw, like in Nette.
    • Anything else, or an empty result → no description (no empty <small>).
  • Group labels: a Stringable label is now translated and rendered, escaped with setText() as before, instead of being dropped.
  • Tests (tests/Rendering/VerticalTest.php, tests/Rendering/GroupRenderingTest.php, new fixture tests/data/vertical/text_description_translated.html):
    • a Stringable description, with and without a translator;
    • escaping of <b> and of a translated user parameter such as <img onerror>;
    • an empty Stringable is not rendered;
    • a raw HtmlStringable;
    • a translated and an escaped Stringable group label.

Security note

The first version of this change sent Stringable descriptions to setHtml() without escaping. The independent review found that this was an XSS path: a translation Message with a user-supplied parameter rendered as raw HTML. This is fixed in the second commit, and tests cover it. The reviewer checked the fix again with an XSS script and approved.

Test evidence

  • CI: all green.
    • Tests on PHP 8.3, 8.4 and 8.5, and PHP 8.3 with --prefer-lowest.
    • Nette Tester, static analysis, quality assurance.
    • Codecov: all changed lines covered.
  • Local QA (PHP 8.4.19, nette/form 3.3, PHPUnit 12.5): 189 tests, 287 assertions, OK.
  • The new tests catch the bugs:
    • with master src, the description and group-label tests fail (the output has no <small class="form-text text-muted">);
    • with the first commit's src, the 3 escaping/empty tests fail.

Reviewer notes

  • Existing behaviour, left unchanged for BC, maintainer decision: plain string descriptions are still inserted with setHtml(), so they are not escaped. Nette escapes them. Some users may use HTML in string descriptions. A change here should be a separate issue.
  • Also unchanged: int or other scalar descriptions still render nothing. Nette renders them.

🤖 Generated with Claude Code

https://claude.ai/code/session_012jmXrhsCDpW1PebtBnDxhy

f3l1x and others added 2 commits September 29, 2026 20:52
Objects like Contributte\Translation\Wrappers\Message were dropped from
the output. They now go through the translator like plain strings, as in
Nette's DefaultFormRenderer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012jmXrhsCDpW1PebtBnDxhy
Stringable descriptions such as translation messages may carry user input
in their parameters, so render them with setText() like Nette's
DefaultFormRenderer does. Plain strings keep the raw HTML behaviour and an
empty result renders no description element.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012jmXrhsCDpW1PebtBnDxhy
@codecov

codecov Bot commented Sep 29, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.64%. Comparing base (f6650b4) to head (b68d99d).

Additional details and impacted files
@@             Coverage Diff              @@
##             master     #136      +/-   ##
============================================
+ Coverage     97.54%   97.64%   +0.09%     
- Complexity      340      344       +4     
============================================
  Files            25       25              
  Lines          1019     1019              
============================================
+ Hits            994      995       +1     
+ Misses           25       24       -1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Translation issue with wrappers

1 participant