Skip to content

fix(jsx): preserve self-closing void elements in cloneElement - #5314

Merged
yusukebe merged 3 commits into
honojs:mainfrom
adityajha77:fix/clone-element-empty-tags
Oct 16, 2026
Merged

yusukebe merged 3 commits into
honojs:mainfrom
adityajha77:fix/clone-element-empty-tags

Conversation

@adityajha77

@adityajha77 adityajha77 commented Aug 28, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes an issue where cloneElement() on elements with undefined children (such as HTML void elements <img />, <input />, <br />, <hr />, <link />) wrapped undefined into [undefined], causing void elements to lose self-closing tag rendering in SSR.

Problem

In src/jsx/base.ts, when cloneElement was called without new children, it extracted the existing element's props.children:

const c = (element as JSXNode).props.children
childrenToClone = Array.isArray(c) ? c : [c]

When an element has no children (e.g. <img src="foo.png" />), props.children is undefined. Because c was undefined, Array.isArray(c) ? c : [c] evaluated to [undefined], passing an array of length 1 containing undefined to jsx().

During SSR stringification (JSXNode.prototype.toStringToBuffer), the self-closing check emptyTags.includes(tag) && children.length === 0 failed because children.length was 1. This resulted in invalid non-self-closing HTML tags like <img src="foo.png"></img> instead of <img src="foo.png"/>.

###Solution

Check if c !== undefined before wrapping it into an array:

childrenToClone = c !== undefined ? (Array.isArray(c) ? c : [c]) : []

###Verification
Added a unit test in src/jsx/base.test.tsx verifying that cloning a void element (e.g. cloneElement(<img src="foo.png" />, { alt: 'bar' })) preserves <img src="foo.png" alt="bar"/> self-closing HTML output.

@usualoma

usualoma commented Sep 5, 2026

Copy link
Copy Markdown
Member

Looks good to me. The fix makes sense and the test covers the issue.

@adityajha77

Copy link
Copy Markdown
Contributor Author

@usualoma thanks for reviewing!

@codecov

codecov Bot commented Sep 8, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 80.01%. Comparing base (e2740d5) to head (fd09553).
⚠️ Report is 5 commits behind head on main.

Files with missing lines Patch % Lines
src/jsx/base.ts 0.00% 1 Missing ⚠️

❌ Your patch check has failed because the patch coverage (0.00%) is below the target coverage (80.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5314      +/-   ##
==========================================
+ Coverage   79.91%   80.01%   +0.10%     
==========================================
  Files         156      156              
  Lines       10973    11018      +45     
  Branches     2316     2340      +24     
==========================================
+ Hits         8769     8816      +47     
+ Misses       2204     2202       -2     

☔ 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.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@adityajha77
adityajha77 force-pushed the fix/clone-element-empty-tags branch from fd09553 to da43399 Compare September 9, 2026 18:27
@adityajha77

Copy link
Copy Markdown
Contributor Author

Rebased on latest main and added unit test coverage for array children in cloneElement to satisfy Codecov.

@yusukebe yusukebe left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@yusukebe

Copy link
Copy Markdown
Member

@adityajha77 @usualoma

Sorry for being late. Thanks.

@yusukebe
yusukebe merged commit 43379d9 into honojs:main Oct 16, 2026
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.

3 participants