我可以在类中转义html标签(重新创建xmp标签)吗? [英] Can I escape html tags within a class (recreate the xmp tag)?

查看:157
本文介绍了我可以在类中转义html标签(重新创建xmp标签)吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想想这个问题的几种方法。决定自己对你最有用的...




  • 可以使用xmp类来重新创建不推荐使用的< xmp>标签? / li>
  • 我们可以模仿SO的标记处理器的内部代码中的转义标签吗?

  • (metaSO,我承认)SO的md处理器如何在内联代码中转义标签?

  • 我们可以逃避吗嵌入HTML的markdown代码块?



目标:转义< ,允许该类包含文本< html>,< body>< head>< script>< style>< body>以及不属于的任何其他标签



< xmp>实现了这一点(并且实际上继续被废弃了)但仍然是浏览器支持的):< xmp>< body>< / xmp> & lt; body& gt / code>。 SO的markdown处理器在内联代码中实现了这个:显示< body> 只需写入`< body>`(它本身就是\` & lt; body> \`和不能包含在一个SO代码块…我不是唯一可以使用这个的人;)



我的解决方案迄今为止,用& lt; & lt; lt 替换所有 照顾不太特殊的HTML标签(这些内部< body>静态内容标签?< div>,< code>< span>等)中的名称,但是特殊标签仍然必须从& lt而不是< / p>

  Javascript:
xmps = document.getElementsByClassName('xmp'); (var i = 0; i< xmps.length; i ++){
var xmp = xmp.item(i);

var newhtml = xmp.innerHTML.replace(/ \& gt; / g,\>)替换(/ \& lt; / g,\& ;
xmp.innerHTML = newhtml;
}

我可以写< div class = xmp>& lt; body>< / div>



什么将允许



< div class =xmp>< body>< / div>



或如何?

 < div class =xmp> 
< body>
& / div>
< / div>

我的项目的细节可能很重要: class =xmp所以我们需要注意行首先> s。没有用户输入,所以安全性不是(?)一个问题。我希望有一个不使用jQuery的解决方案。

解决方案

您不能创建 xmp 与JavaScript,因为功能是关于HTML解析。而且,当JavaScript可以让她的手上,元素已经被浏览器解析了。



另一方面,我看不到任何需要。据我所知,所有浏览器都支持 xmp ,而且,HTML5 CR要求保留支持,新的浏览器也可以实现。它也说作者不能使用它,但这并不意味着 xmp 将无法正常工作。


A couple ways to think of this question. Decide for yourself which is most useful for you…

  • Can javascript recreate the deprecated <xmp> tag with an "xmp" class?
  • Can we mimic SO's markdown processor's escaping tags within inline code?
  • (metaSO, I admit) How does SO's md processor escape tags in inline code?
  • Can we escape < in markdown code blocks embedded in HTML?

The goal: a class that escapes <, allowing that class to contain the text <html>, <body>, <head>, <script>, <style>, <body>, and any other tags that don't belong inside <body> or are processed specially, without processing them specially.

<xmp> achieved this (and actually continues to - deprecated but still browser-supported): <xmp><body></xmp> was like &lt;body&gt;. SO's markdown processor achieves this in inline code: to display <body> just write `<body>` (which itself is \`&lt;body>\` and cannot be included in an SO code block… I'm not the only one who could use this ;)

My solution so far —replacing all < with &lt;— takes care of the less-special HTML tags (is there a name for these within-<body> static content tags? <div>, <code>, <span>, etc), but the "special" tags still have to be started with &lt; instead of <

Javascript:
xmps = document.getElementsByClassName('xmp');
for (var i = 0; i < xmps.length; i++) {
    var xmp = xmp.item(i);
    var newhtml = xmp.innerHTML.replace(/\&gt;/g,"\>").replace(/\&lt;/g,"\<");
    xmp.innerHTML = newhtml;
    }

With that I can write <div class="xmp">&lt;body></div>.

What will allow

<div class="xmp"><body></div>?

or how about?

<div class="xmp">
    <body>
    &/div>
</div>

The details of my project might matter: There'll be markdown in class="xmp" so we need to be careful with line-initial >s. There is no user input, so security isn't(?) an issue. I'm hoping for a solution that doesn't use jQuery.

解决方案

You cannot create the functionality of the xmp with JavaScript, because the functionality is about HTML parsing. And the element has already been parsed by the browser when it JavaScript can get her hands on it.

On the other hand, I don’t see any need for that. As far as I know, xmp is supported by all browsers, and HTML5 CR requires that support be retained and that new browsers implement it too. It also says that authors must not use it, but this doesn’t mean xmp wouldn’t work.

这篇关于我可以在类中转义html标签(重新创建xmp标签)吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆