Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Unsupported HTML character references #28820

Open
valtlai opened this issue Apr 10, 2024 · 6 comments
Open

Bug: Unsupported HTML character references #28820

valtlai opened this issue Apr 10, 2024 · 6 comments
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug

Comments

@valtlai
Copy link

valtlai commented Apr 10, 2024

React does not support all the named character references, “entities”, defined in the HTML specification:

React version: 18.2.0

Steps To Reproduce

Render the following demo component and compare the exepcteds character and the actual rendering results.

Code demo
const HTMLCharRefDemo = () => (
  <>
    <h1>Test: HTML character references in React</h1>
    <table>
      <thead>
        <tr>
          <th>Reference</th>
          <th>Expected character</th>
          <th>Actual rendering</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>&amp;copy;</td>
          <td>©</td>
          <td>&copy;</td>
        </tr>
        <tr>
          <td>&amp;bemptyv;</td>
          <td></td>
          <td>&bemptyv;</td>
        </tr>
        <tr>
          <td>&amp;check;</td>
          <td></td>
          <td>&check;</td>
        </tr>
        <tr>
          <td>&amp;bigstar;</td>
          <td></td>
          <td>&bigstar;</td>
        </tr>
        <tr>
          <td>&amp;NoBreak;</td>
          <td>{'\u2060'}</td>
          <td>&NoBreak;</td>
        </tr>
      </tbody>
    </table>
  </>
);

Link to code example: https://codepen.io/valtlai/pen/qBwYezG?editors=1010

The current behavior

React does not recognize some character references but renders them as is.

The expected behavior

React should recognize all the specced character references that end with a semicolon and render the characters they represent.

@valtlai valtlai added the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label Apr 10, 2024
@XudayfiIbra
Copy link

i could not see that bug

@Yobro7292
Copy link

There are two approaches

  1. Convert entities to their corresponding Unicode characters before rendering. You can create a utility function or use a library that handles these conversions
  2. Use dangerouslySetInnerHTML
function MyComponent() {
  return (
    <div>
      Copy: ©, é: é
    </div>
  );
}

Or using dangerouslySetInnerHTML:

function MyComponent() {
  return (
    <div dangerouslySetInnerHTML={{__html: "Copy: &copy;, é: &eacute;"}}></div>
  );
}

@valtlai
Copy link
Author

valtlai commented Apr 12, 2024

@Yobro7292 Yeah, I know these workarounds, but I still think React should support all the standard character references.

i could not see that bug

@XudayfiIbra The first row of the table in the demo is an example of a supported character reference: &copy; gets printed as ©. The other rows represent unsupported character references: each reference, like &bemptyv;, gets printed as is.

@XudayfiIbra
Copy link

@valtlai Oh i didn't notice that!

@pankajd24
Copy link

pankajd24 commented Apr 16, 2024

@check below code .

use html-entities for unsupported symbols .
refer website - https://tools.w3cub.com/html-entities

	```

Test: HTML character references in React

Reference Expected character Actual rendering
&copy; © ©
&bemptyv;
&check;
&bigstar;
&NoBreak; {'\u2060'}
	

@nmain
Copy link

nmain commented Apr 16, 2024

This is working as intended, see facebook/jsx#136

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug
Projects
None yet
Development

No branches or pull requests

5 participants