Skip to content

Fix innerHTML/innerText to coerce non-string values - #333

Closed
dyk1454683243-sudo wants to merge 1 commit into
WebReflection:mainfrom
dyk1454683243-sudo:cursor/fix-innerhtml-coerce-non-string-308-61d2
Closed

dyk1454683243-sudo wants to merge 1 commit into
WebReflection:mainfrom
dyk1454683243-sudo:cursor/fix-innerhtml-coerce-non-string-308-61d2

Conversation

@dyk1454683243-sudo

Copy link
Copy Markdown

Fixes #308

Setting innerHTML or innerText with a non-string (for example 1 or null) currently throws. Browsers treat these accessors as [LegacyNullToEmptyString] DOMString: null becomes '', and any other value is stringified.

This keeps the change on the setter path only:

  • setInnerHtml coerces with html === null ? '' : String(html) before parseFromString
  • Element now has a matching set innerText that uses the same coerce and replaces children like textContent (the existing get innerText whitespace rules are unchanged)
  • Regression tests cover innerHTML = 1, innerHTML = null, and innerText = 1
document.body.innerHTML = 1;    // '1', no throw
document.body.innerHTML = null; // ''
document.body.innerText = 1;    // visible text '1'

Browsers stringify assignments (null → ''). Setting a number no longer
throws; innerText now has a matching setter that replaces text content.

Co-authored-by: David <dyk1454683243-sudo@users.noreply.github.com>
@dyk1454683243-sudo

Copy link
Copy Markdown
Author

Withdrawing this PR while I clean up a high-volume open-PR backlog. Sorry for the noise — happy to come back later with a focused change if useful.

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.

Exceptions thrown when setting Element's innerHTML or innerText with non-string value

2 participants