Skip to content

fix: resolve 10 bugs in HTMLNode, HTMLTable, and TemplateCompiler#78

Merged
usernane merged 12 commits into
devfrom
fix/bug-fixes-batch
Jun 10, 2026
Merged

fix: resolve 10 bugs in HTMLNode, HTMLTable, and TemplateCompiler#78
usernane merged 12 commits into
devfrom
fix/bug-fixes-batch

Conversation

@usernane

@usernane usernane commented Jun 10, 2026

Copy link
Copy Markdown
Member

Summary

Fix 10 reported bugs across HTMLNode, HTMLTable, and TemplateCompiler classes.

Motivation

Multiple bugs were reported affecting attribute rendering, template parsing, and DOM state consistency. Fixes #55, #56, #57, #58, #59, #60, #62, #63, #71, #74, #75, #76, #77.

Changes

How to Test / Verify

All fixes include dedicated PHPUnit tests. Run composer test or:

cd tests && php ../vendor/bin/phpunit

321 tests pass, 1016 assertions, zero failures.

Breaking Changes and Migration Steps

  • setAttribute() now throws InvalidArgumentException for invalid attribute names (previously silently discarded)
  • Attribute values containing & are now rendered as & and " as " (correct per HTML spec)
  • HTMLTable::rows() and cols() now derive from DOM state, not internal counters

Code relying on silent attribute rejection should add try-catch. Code parsing open() output expecting unescaped & in attributes needs updating.

Checklist

  • I reviewed my own diff before requesting review
  • My commits follow Conventional Commits
  • I added/updated tests (or explained why not)
  • I updated docs (if needed) Docs Repo — N/A, bug fixes only
  • I ran lint/cs-fixer (if applicable) (composer fix-cs) — fixed config and applied
  • I considered backward compatibility
  • I considered security

Related issues

Closes #55, #56, #57, #58, #59, #60, #62, #63, #71, #74, #75, #76, #77

Ibrahim BinAlshikh added 11 commits June 10, 2026 15:16
…63, #71, #74, #75, #76, #77

Add tests that verify the existence of bugs reported in the listed
issues. Tests for #57, #74, and #75 already pass as those bugs were
fixed alongside the test additions.
Replace str_replace('"', '\"', $val) with proper encoding of & and
" characters in quoted attribute values. This produces valid HTML output
where & is rendered as & and " as ".

Update existing test expectations to match the corrected output.

Closes #77
…ames

setAttribute() now throws InvalidArgumentException when a structurally
invalid attribute name is passed (e.g., names with spaces, quotes, =).
Previously these were silently discarded.

The TemplateCompiler uses a safeSetAttribute() helper to gracefully
skip invalid names encountered during HTML parsing.

Closes #76
Wrap the ob_start/require/ob_get_clean sequence in a try-catch block.
If the template throws, ob_end_clean() is called before re-throwing
the exception, preventing a permanently leaked output buffer.

Closes #59
Use a static closure to execute the template require, preventing
templates from accessing the TemplateCompiler instance via $this.
Also eliminates variable name collisions by using __ prefixed names
for internal variables.

Closes #62
Replace manual $rows and $cols counters with methods that read from
the actual DOM state. rows() returns childrenCount() and cols() returns
the first row's child count. This eliminates desync when rows are
added or removed via addRow() or inherited methods.

Closes #71
Extend the existing <?php escaping to also handle <?= (short echo)
and <? (short open tag) before splitting on <. This prevents
InvalidNodeNameException when these sequences appear in parsed text.

Closes #60
Copy attributes from parsed <html> and <body> elements to the
HTMLDoc instance in parseHTMLDoc(). Previously these attributes
(lang, dir, class, id, etc.) were silently discarded.

Closes #56
Change the script content extraction regex from a fixed lookbehind
(?<=<script>) to a capturing group <script[^>]*>(.*?)</script>.
This correctly handles script tags with attributes like type or src,
preventing their content from being corrupted by the HTML parser.

Closes #55
Replace restricted ASCII character class [\t-!#-~] in attribute value
regex with [^"]* (and [^']* for single quotes). This correctly
matches UTF-8 multibyte characters and prevents > inside quoted
attribute values from being interpreted as the tag closing delimiter.

Closes #58
Extract comment bodies that contain < into placeholders before the
explode('<', ...) split, preserving the <!-- --> wrapper so the parser
still recognizes them as comment nodes. Only comments with < are
replaced to avoid disrupting normal comment parsing.

Closes #63
@codecov

codecov Bot commented Jun 10, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.15254% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 98.42%. Comparing base (cf3dfe1) to head (7eba4ae).
⚠️ Report is 13 commits behind head on dev.

Files with missing lines Patch % Lines
WebFiori/Ui/TemplateCompiler.php 98.46% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##                dev      #78      +/-   ##
============================================
- Coverage     98.44%   98.42%   -0.03%     
- Complexity      898      913      +15     
============================================
  Files             6        6              
  Lines          1799     1839      +40     
============================================
+ Hits           1771     1810      +39     
- Misses           28       29       +1     
Flag Coverage Δ
php-8.1 ?
php-8.2 ?
php-8.3 98.42% <99.15%> (+0.09%) ⬆️
php-8.4 ?
php-8.5 97.60% <99.15%> (+0.10%) ⬆️

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.

Fix array_indentation rule in php_cs.php.dist (was passed empty array
instead of true). Run php-cs-fixer on all source files.
@usernane usernane merged commit 1da4977 into dev Jun 10, 2026
2 checks passed
@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