Skip to content

fix: decode html entities in widget titles - #409

Merged
AllTerrainDeveloper merged 2 commits into
WordPress:trunkfrom
Pranjal1423:fix/widget-html-entities
Jul 24, 2026
Merged

fix: decode html entities in widget titles#409
AllTerrainDeveloper merged 2 commits into
WordPress:trunkfrom
Pranjal1423:fix/widget-html-entities

Conversation

@Pranjal1423

@Pranjal1423 Pranjal1423 commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

Closes #393

Description

This PR fixes a visual bug where HTML entities in post titles (e.g. & or ’) were rendered raw inside built-in desktop widgets (such as Recent Comments and the Starter Widget).

Root Causes & Fixes

Missing Entity Decoding in Widgets:
Issue: The REST API returns titles with HTML-encoded entities in the title.rendered payload. The widgets were setting this raw payload directly to textContent, causing the browser to render the raw entity codes (like Q&A: User’s Guide).

Fix: Created a shared utility decodeHTML inside utils.ts that safely decodes HTML entities by parsing them through a temporary DOM textarea element. Both recent-comments-widget/index.ts and starter-widget/index.ts now import and apply decodeHTML to titles before updating the UI.

Code Duplication:

Issue: The Posts window already had a local decodeTitle function doing identical DOM-based decoding, creating redundant code.
Fix: Refactored posts-window/index.ts to import and reuse the shared decodeHTML utility from utils.ts.

Verification

Build: npm run build completed successfully.
Typecheck: npm run typecheck passed with no errors.
Unit Tests: npm run test:js and npm run test:php both completed successfully with all tests passing (1601/1601 JS tests and 1017/1017 PHP tests).

Before fix

  • Raw HTML entities rendered under commenter name: ↳ Q&A: User’s Guide
Screenshot 2026-07-22 at 8 43 14 PM

After fix

  • Clean, decoded titles: ↳ Q&A: User’s Guide
Screenshot 2026-07-23 at 1 10 58 PM

@AllTerrainDeveloper AllTerrainDeveloper left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank you very much!

Comment thread src/utils.ts
Comment on lines +416 to +425

/**
* Decode HTML entities from a string cleanly using a temporary textarea.
*/
export function decodeHTML( raw: string ): string {
const ta = document.createElement( 'textarea' );
ta.innerHTML = raw;
return ta.value;
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nice move!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thankyou!!

@AllTerrainDeveloper
AllTerrainDeveloper merged commit 807efc1 into WordPress:trunk Jul 24, 2026
7 checks passed
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.

HTML entities in post titles are rendered raw in built-in widgets

2 participants