Why does React only allow a subset of HTML entitie

2019-07-16 08:32发布

The only relevant documentation I found on this issue simply says that I can use HTML entities in React.

However, this doesn't entirely seem to be the case. For example, & and ∑ work, while | and # don't. Was that a conscious design decision? If yes, what is the reason?

Example:

const App = () => (
  <div>
    <h2>&amp;, &sum; - this works. &vert;, &num; - this doesn't.</h2>
  </div>
);

Example in CodeSandbox

3条回答
甜甜的少女心
2楼-- · 2019-07-16 09:04

ReactJS only supports HTML4 special entities.

This might be intentional, although the documentation is unclear about it. My guess would be that HTML entities are commonly used to mask characters that are used in code as well, but since ReactJS has Unicode support, it's not as needed to make use of the new entities that can be represented in Unicode.

You can find a list of HTML4 entities here.

查看更多
倾城 Initia
3楼-- · 2019-07-16 09:08

Sorry, but only HTML4 elements are supported unfortunatelly

查看更多
仙女界的扛把子
4楼-- · 2019-07-16 09:19

&vert; and &num; are new character entities from HTML5 specification.
&amp; and &sum; are character entities from HTML4 specification (better visualised here).

JSX Rendering results show that character entities from only HTML4 specification are allowed.

查看更多
登录 后发表回答