Posts Tagged ‘xml’
Firefox与disable-output-escaping之间的纠结。。。
(最近在琢磨Google Appengine,难免有所脑残,谁让我一向喜欢淫荡架构,去碰XSLT的结果就是。。。)
话说这个世界只有IE的时候还是美好的,程序员可以写出一段有爱的XML
<?xml version=”1.0″ encoding=”utf-8″?>
<?xml-stylesheet type=’text/xsl’ href=’t.xsl’ media=”all” version=’1.0′?>
<channel>
<item>
<title>《哆啦A梦剧场版全集》(Doraemon movies collection)[1980-2007剧场版][清晰版][6.10重发1994][RMVB]</title>
<link>http://www.verycd.com/topics/270597/</link>
<description><![CDATA[<img src="http://image-6.verycd.com/6d8078dfa512365af6ad24ba08407d0f73190(280x)/thumb.jpg" alt="《哆啦A梦剧场版全集》(Doraemon movies collection)[1980-2007剧场版][清晰版][6.10重发1994][RMVB]” style=”float:right;padding:0 0 10px 10px;” />中文名称:哆啦A梦剧场版全集<br />英文名称:Doraemon movies collection<br />别名:ドラえもん<br />资源类型:RMVB<br />版本:[1980-2007剧场版][清晰版][6.10重发1994]<br />导演:藤子·F·不二雄<br />演员:哆啦A梦<br /> 野比大雄<br /> 源静香<br /> 刚田武<br /> 骨川哲夫<br /> 哆啦美<br />地区:日本<br />语言:日语<br />简介:<br />【片名】 哆啦A梦剧场版合集<br />【字幕】 中文内嵌<br />【格式】 RMVB<br />说明:94的出来和前阵91一样的问题,已重发,请重下,95的也出了问题,先别下<br />【内容介绍】<br />继卡通之后,哆啦A梦电影的诞生,再度把哆啦A梦系列推向一个新境界。本作题材非常新颖,设计冒险舞台的那想象力真是叹为观止,不失为一部优秀作品。<br /><a href=’http://www.verycd.com/topics/270597/’>查看完整资源页</a><br />]]></description>
</item>
</channel>
然后轻松的使用这样一段xslt让访问者浏览的时候哦看上去High一点
<?xml version=”1.0″ encoding=”utf-8″?>
<xsl:stylesheet xmlns=”http://www.w3.org/1999/xhtml” xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” xmlns:dc=”http://purl.org/dc/elements/1.1/” version=”1.0″>
<xsl:output method=”html” doctype-system=”http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd” doctype-public=”-//W3C//DTD XHTML 1.0 Strict//EN”/>
<xsl:template match=”/channel”>
<html>
<head>
<script type=”text/javascript” src=”mop-up.js”></script>
</head>
<body”>
<div id=”page-contents”>
<h3><a href=”{item/link}”><xsl:value-of select=”item/title”/></a></h3>
<xsl:value-of select=”item/description” disable-output-escaping=”yes” />
<hr />
</div>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
可是该死的Firefox搅局了,你发现当使用Fx浏览的时候,那段description仍然被escaping了,HTML代码性感的裸露在外
于是乎,你去mozilla.org报告这个disable-output-escaping不工作的bug,发现早在2001年就有人提交这个问题了
https://bugzilla.mozilla.org/show_bug.cgi?id=98168#c11
(看懂了,然后绝望了,你咒骂Firefox,甚至对Opera加深了好感,因为它正常)
然后,你决定寻求别的解决方法
比如。。。<xsl:copy-of select=”item/description” />取代<xsl:value-of select=”item/description” disable-output-escaping=”yes” /> (参照http://carey.geek.nz/doc/xslt-cdata-escaping/)
为此你必须剥离<![CDATA[]],但别高兴的太早,很快你会发现去掉<![CDATA[]]是非常SB的决定,因为这意味着你必须在<description></description>中使用严格的XML格式。如果打算放在其中的HTML代码还有诸如<br>之类的化石级东西,你就绝望吧,就算你可以生成完美的XHTML,一个 也足以让一切都SB了
崩溃。。。。。。。。。。。。
最终解决方法:(参考的VeryCD,但是应该不是它原创,sburke@cpan.org, Sean M. Burke.这个人是原作)
一段Js
// A workaround for XSL-to-XHTML systems that don’t
// implement XSL ‘disable-output-escaping=”yes”‘.
//
// sburke@cpan.org, Sean M. Burke.
// – I hereby release this JavaScript code into the public domain.
var is_decoding;
var DEBUG = 0;
function complaining (s) { alert(s); return s; }
if(!(document.getElementById && document.getElementsByName))
throw complaining(“Your browser is too old to render this page properly.”
+ ” Consider going to getfirefox.com to upgrade.”);
function check_decoding () {
var d = document.getElementById(‘comeTestMe’);
if(!d) {
throw complaining(“Can’t find an id=’comeTestMe’ element?”);
} else if(!(‘textContent’ in d)) {
// It’s a browser with a halfassed DOM implementation (like IE6)
// that doesn’t implement textContent! Assume that if it’s that
// dumb, it probably doesn’t implement disable-content-encoding.
} else {
ampy = d.textContent;
if(DEBUG > 1) { alert(“Got ” + ampy); }
if(ampy == undefined) throw complaining(“‘comeTestMe’ element has undefined text content?!”);
if(ampy == ” ) throw complaining(“‘comeTestMe’ element has empty text content?!” );
if (ampy == “\x26″ ) { is_decoding = true; }
else if (ampy == “\x26amp;” ) { is_decoding = false; }
else { throw complaining(‘Insane value: “‘ + ampy + ‘”!’); }
}
var msg =
(is_decoding == undefined) ? “I can’t tell whether the XSL processor supports disable-content-encoding!D”
: is_decoding ? “The XSL processor DOES support disable-content-encoding”
: “The XSL processor does NOT support disable-content-encoding”
;
if(DEBUG) alert(msg);
return msg;
}
function go_decoding () {
check_decoding();
if(is_decoding) {
DEBUG && alert(“No work needs doing — already decoded!”);
return;
}
var to_decode = document.getElementsByName(‘decodeable’);
if(!( to_decode && to_decode.length )) {
DEBUG && alert(“No work needs doing — no elements to decode!”);
return;
}
var s;
for(var i = to_decode.length – 1; i >= 0; i–) {
s = to_decode[i].textContent;
if(
s == undefined ||
(s.indexOf(‘&’) == -1 && s.indexOf(‘<’) == -1)
) {
// the null or markupless element needs no reworking
} else {
to_decode[i].innerHTML = s; // that’s the magic
}
}
return;
}
//End
再修改一下那个xslt
<?xml version=”1.0″ encoding=”utf-8″?>
<xsl:stylesheet xmlns=”http://www.w3.org/1999/xhtml” xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” xmlns:dc=”http://purl.org/dc/elements/1.1/” version=”1.0″>
<xsl:output method=”html” doctype-system=”http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd” doctype-public=”-//W3C//DTD XHTML 1.0 Strict//EN”/>
<xsl:template match=”/channel”>
<html>
<head>
<script type=”text/javascript” src=”mop-up.js”></script>
</head>
<body onload=”go_decoding();”>
<div id=”comeTestMe” style=”display: none;”><xsl:text disable-output-escaping=”yes” >&</xsl:text></div>
<div id=”page-contents”>
<h3><a href=”{item/link}”><xsl:value-of select=”item/title”/></a></h3>
<p name=”decodeable”>
<xsl:value-of select=”item/description” disable-output-escaping=”yes” />
</p>
</div>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Firefox下正常了,Opera和IE就来就正常。。。
不纠结了。。。
继续玩Google Appengine去
PS:无论什么方法,KHTML内核的Konqueror都是不鸟你的,而WebKit内核则一直表现很好,可见一切都是Fx的错。。。哼。。。