让 React 渲染输入标签的闭合标签?

huangapple go评论139阅读模式
英文:

Make react render the closing tag for inputs?

问题

我有一个特殊的要求。我的网站是用React构建的,我希望人们能够复制公共网站的HTML到他们自己的React组件中。

我遇到的唯一问题是,当由React渲染时,输入框没有闭合标签。

这两种都是有效的HTML

<input id="input-1" value="foo">
<input id="input-2" value="foo" />

然而,只有第二种在直接粘贴到React组件中时起作用。有办法强制React在我的网站上为输入框呈现闭合标签吗?

英文:

I have an unusual requirement. My site is build with React and I want people to be able to copy the public website's HTML into their own React components.

The only issue that I'm having is inputs do not have a closing tag when rendered by React.

Both of these are valid HTML

&lt;input id=&quot;input-1&quot; value=&quot;foo&quot;&gt;
&lt;input id=&quot;input-2&quot; value=&quot;foo&quot; /&gt;

However only the 2nd one will work if pasted directly into a React component. Is there a way to force React to render the closing tag for inputs on my site?

答案1

得分: 1

考虑通过类似 html-tidy 的工具处理复制的 HTML 源代码字符串。如果想在浏览器中进行格式化,请考虑使用 prettier。查看下面的示例。祝好运!

<!-- 开始代码片段: js 隐藏: false 控制台: true babel: false -->

<!-- 语言: lang-js -->

  const output = prettier.format(&quot;&lt;input type=&#39;text&#39; name=&#39;username&#39;&gt;&quot;, {
    parser: &quot;html&quot;,
    plugins: prettierPlugins,
  });

  // 发送到服务器或更新剪贴板。
  console.log(output)

<!-- 语言: lang-html -->

&lt;script src=&quot;https://unpkg.com/prettier@2.8.7/standalone.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://unpkg.com/prettier@2.8.7/parser-html.js&quot;&gt;&lt;/script&gt;

<!-- 结束代码片段 -->

英文:

Consider running the copied html source string through a tool like html tidy. If you'd like to do the formatting in the browser, consider prettier. See example below. Good Luck!

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  const output = prettier.format(&quot;&lt;input type=&#39;text&#39; name=&#39;username&#39;&gt;&quot;, {
    parser: &quot;html&quot;,
    plugins: prettierPlugins,
  });

  // Send to server or update your clipboard.
  console.log(output)

<!-- language: lang-html -->

&lt;script src=&quot;https://unpkg.com/prettier@2.8.7/standalone.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://unpkg.com/prettier@2.8.7/parser-html.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

答案2

得分: 0

要正确理解这一点,用户可以将HTML贴到您的应用中,它需要渲染为JSX?您面临的问题是用户可以添加非自闭合的HTML输入标签,但在渲染JSX时会有问题(因为它们需要是自闭合的)?

您可以进行检查,看看输入的标签是否是输入,并且如果它不是自闭合的,然后自行关闭它。这看起来可能是这样的(假设您将字段存储在某个变量中):

if(var.substring(1,6) == &quot;input&quot; &amp;&amp; var[var.length-2]!=&quot;/&quot;){
    var= var.slice(0, var.length-1)+&quot;/&quot;+var[var.length-1]
}
英文:

To understand this correctly, users can post the HTML into your app, and it needs to be rendered as JSX? And the issue you're facing is that users can add non self-closing HTML input tags, but that'll be an issue when rendering JSX (as they need to be self closing)?

You could perform a check to see if the tag being entered is an input, and if it isn't self-closing, and then close it yourself. That would look something like this (assuming you have the field in some var):

if(var.substring(1,6) == &quot;input&quot; &amp;&amp; var[var.length-2]!=&quot;/&quot;){
    var= var.slice(0, var.length-1)+&quot;/&quot;+var[var.length-1]
    }

huangapple
  • 本文由 发表于 2023年4月11日 15:12:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75983291.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定