Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
332 views
in Technique[技术] by (71.8m points)

css - <!--[if IE]> conditional comments are rendered HTML-escaped in Facelets

I'm trying to use an IE conditional comment to declare a CSS resource:

<h:outputStylesheet name="common.css" library="css" />
<!--[if IE]>
    <link rel="stylesheet" type="text/css" href="#{resource['css:ie.css']}" />   
<![endif]-->    

However, that doesn't seem to work. I'm seeing this in my generated HTML output:

<link type="text/css" rel="stylesheet" href="/context/faces/javax.faces.resource/common.css?ln=css" />        
<!--[if IE]&gt;
    &lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;/context/faces/javax.faces.resource/ie.css?ln=css&quot;/&gt;
&lt;![endif]-->

It works fine without the conditional comment. I'm not using the context parameter javax.faces.FACELETS_SKIP_COMMENTS. How is this caused and how can I solve it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This indeed won't work as Facelets implicitly HTML-escapes the comment's contents. Your best bet is to put it in a <h:outputText escape="false"> as follows:

<h:outputText value="&lt;!--[if IE]&gt;&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;/#{resource['css:ie.css']}&quot; /&gt;&lt;![endif]--&gt;" escape="false" />

This is however a line of ugliness. The OmniFaces JSF utility library has a <o:conditionalComment> for exactly this purpose:

<o:conditionalComment if="IE">
    <link rel="stylesheet" type="text/css" href="#{resource['css:ie.css']}" />   
</o:conditionalComment>

Unrelated to the concrete problem, you are not really using the library attribute the right way. It should identify a common "theme", not the subfolder where the files are placed in, just put that subfolder in the name attribute instead. See also What is the JSF resource library for and how should it be used?

<h:outputStylesheet name="css/common.css" />
<o:conditionalComment if="IE">
    <link rel="stylesheet" type="text/css" href="#{resource['css/ie.css']}" />   
</o:conditionalComment>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...