Skip to content

refactor: extract rendering logic into dedicated HtmlRenderer class#80

Merged
usernane merged 1 commit into
devfrom
refactor/extract-html-renderer
Jun 10, 2026
Merged

refactor: extract rendering logic into dedicated HtmlRenderer class#80
usernane merged 1 commit into
devfrom
refactor/extract-html-renderer

Conversation

@usernane

Copy link
Copy Markdown
Member

Summary

Extract HTML/XML rendering from HTMLNode into a standalone HtmlRenderer class with per-instance configuration and no shared mutable state.

Motivation

HTMLNode uses static variables ($IsFormatted, $IsQuoted, $UseForwardSlash) as global state shared across all instances. Calling setIsQuotedAttribute(true) on any node changes behavior for every other node. This is unsafe in async contexts. Fixes #64 and #61.

Changes

  • New WebFiori\Ui\HtmlRenderer class with local rendering state
  • render(HTMLNode $node, int $initTab = 0): string — produces HTML
  • renderXML(HTMLNode $node, bool $formatted = false): string — produces XML
  • Constructor accepts formatted, quoted, useForwardSlash options
  • Existing toHTML()/toXML() on HTMLNode remain unchanged (backward compatible)
  • 31 tests with 100% line and method coverage

How to Test / Verify

cd tests && php ../vendor/bin/phpunit WebFiori/Tests/Ui/HtmlRendererTest.php

31 tests, 45 assertions, 100% coverage. Full suite: 352 tests pass.

Breaking Changes and Migration Steps

None. This is additive — HtmlRenderer is a new class. Existing API is untouched.

Checklist

  • I reviewed my own diff before requesting review
  • My commits follow Conventional Commits
  • I added/updated tests (or explained why not) — 100% coverage
  • I updated docs (if needed) Docs Repo — N/A, new class with PHPDoc
  • I ran lint/cs-fixer (if applicable) (composer fix-cs)
  • I considered backward compatibility
  • I considered security

Related issues

Closes #64, Closes #61

Add HtmlRenderer class with per-instance rendering configuration
(formatted, quoted, useForwardSlash). This eliminates shared mutable
static state in the rendering path, making it safe for concurrent use
in async contexts (Swoole, ReactPHP, RoadRunner).

The renderer can be used standalone:
  $renderer = new HtmlRenderer(formatted: true, quoted: true);
  echo $renderer->render($node);

Existing toHTML()/toXML() methods on HTMLNode remain unchanged for
backward compatibility.

100% test coverage on HtmlRenderer (92 lines, 11 methods).

Closes #64
Closes #61
@usernane usernane merged commit e047352 into dev Jun 10, 2026
2 checks passed
@usernane usernane deleted the refactor/extract-html-renderer branch June 10, 2026 14:24
@codecov

codecov Bot commented Jun 10, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.40%. Comparing base (7c8ebe2) to head (83e3e6c).
⚠️ Report is 2 commits behind head on dev.

Additional details and impacted files
@@             Coverage Diff              @@
##                dev      #80      +/-   ##
============================================
+ Coverage     98.31%   98.40%   +0.08%     
- Complexity      917      967      +50     
============================================
  Files             6        7       +1     
  Lines          1843     1946     +103     
============================================
+ Hits           1812     1915     +103     
  Misses           31       31              
Flag Coverage Δ
php-8.3 98.40% <100.00%> (+0.08%) ⬆️
php-8.5 97.63% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ 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.

@sonarqubecloud

Copy link
Copy Markdown

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.

1 participant