是否可以在内部使用CDATA< pre>标签 [英] Is it possible to use CDATA inside <pre> tag

查看:139
本文介绍了是否可以在内部使用CDATA< pre>标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在HTML页面中显示异常跟踪。

执行此操作的一种方法是在异常跟踪中转义HTML特殊字符并将其转储到< pre> 标记。



尽管它有效,但效率非常低。我认为,一种方法是用CDATA包装跟踪。我试过了,但没有显示出来。



我的问题,可以这样做吗?



是我的微弱尝试。

 < pre><![CDATA [blah,blah,blah with< 
和等等等等等等等等。
and more blah,blah]]>< / pre>


解决方案

只有当您提供页面作为XML( application / xhtml + xml );纯HTML中没有CDATA部分。 HTML模式下的大多数浏览器都会忽略示例CDATA部分。



并抛出<![CDATA [。 .. ]]> 围绕一个字符串是不足以包装它的,无论如何。如果您的跟踪信息中包含序列]]> ,则会终止CDATA部分,并且您将回到相同的问题。所以你必须至少做一次转义才能应对这种情况,如果你要做一个转义过程,你也可以做一个正确的HTML转义。



这就是为什么CDATA部分在很大程度上毫无意义的原因。很多人似乎认为它可以免除他们对转义问题的思考,但它确实没有。

无论如何,HTML转义并不是低效的。这是几个字符串替换。任何Web应用程序(*)将在每个页面上执行一百次HTML转义。再加一个 - 尤其对于效率完全不重要的调试案例! - 没有很大的负担。

(*:好吧,除了PHP教程中写得不好的应用程序,其作者从未听说过 htmlspecialchars ,显然。)


I want to display an exception trace in the HTML page.

One way to do this is to escape HTML special characters in the exception trace and dump it inside the <pre> tag.

Although it works, it's terribly inefficient. I thought that one approach would be to wrap the trace with CDATA. I've tried it, but nothing get's displayed.

My question, can this be done?

Here is my feeble attempt.

  <pre><![CDATA[blah, blah, blah with <>
  and blah blah blah with &
    and more blah, blah]]></pre>

解决方案

It would only work if you're serving the page as XML (application/xhtml+xml); there are no CDATA sections in plain HTML. Most browsers in HTML mode would just ignore the example CDATA section.

And throwing <![CDATA[...]]> around a string is not sufficient to wrap it, anyway. If your trace information had the sequence ]]> in it, that'd end the CDATA section and you'd be back at the same problem. So you have to do at least one escape to cope with that case, and if you're going to be doing an escaping process anyway you might as well do a proper HTML-escape.

This is why CDATA sections are largely pointless. A lot of people seem to think it somehow absolves them of thinking about escaping issues, but it really doesn't.

Anyway, HTML-escaping isn't inefficient. It's a couple of string replaces. Any web app(*) will be doing a hundred HTML-escapes every page. Adding one more — especially for a debugging case where efficiency doesn't matter at all! — is no great burden.

(*: well, except for poorly-written apps from PHP tutorials, whose authors have never even heard of htmlspecialchars, obviously.)

这篇关于是否可以在内部使用CDATA&lt; pre&gt;标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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