/
ConditionalComment.java
128 lines (107 loc) · 4.71 KB
/
ConditionalComment.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* Copyright OmniFaces
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.omnifaces.component.output;
import static org.omnifaces.util.Utils.isEmpty;
import java.io.IOException;
import jakarta.faces.component.FacesComponent;
import jakarta.faces.context.FacesContext;
import jakarta.faces.context.ResponseWriter;
import org.omnifaces.util.State;
/**
* <p>
* The <code><o:conditionalComment></code> component renders a conditional comment. Conditional
* comments are an IE specific feature which enables the developer to (out)comment blocks of HTML depending on whether
* the client is using IE and if so even which version. They are often seen in combination with CSS stylesheets like so:
* <pre>
* <!--[if lte IE 7]>
* <link rel="stylesheet" href="ie6-ie7.css" />
* <![endif]-->
* </pre>
* <p>
* However, Facelets renders the comment's contents HTML-escaped which makes it unusable.
* <pre>
* <!--[if lte IE 7]&gt;
* &lt;link rel=&quot;stylesheet&quot; href=&quot;ie6-ie7.css&quot; /&gt;
* &lt;![endif]-->
* </pre>
* <p>
* Also, if <code>jakarta.faces.FACELETS_SKIP_COMMENTS</code> context param is
* set to <code>true</code> then it will even not be rendered at all. You would need to workaround this with an ugly
* <code><h:outputText escape="false"></code>.
* <pre>
* <h:outputText
* value="&lt;!--[if lte IE 7]&gt;&lt;link rel=&quot;stylesheet&quot; href=&quot;ie6-ie7.css&quot; /&gt;&lt;![endif]--&gt;"
* escape="false" />
* </pre>
* <p>This component is designed to solve this problem.
* <pre>
* <o:conditionalComment if="lte IE 7">
* <link rel="stylesheet" href="ie6-ie7.css" />
* </o:conditionalComment>
* </pre>
* <p>Note that you cannot use this with <code><h:outputStylesheet></code> as it would implicitly be relocated as
* direct child of <code><h:head></code>.
*
* @author Bauke Scholtz
* @see OutputFamily
*/
@FacesComponent(ConditionalComment.COMPONENT_TYPE)
public class ConditionalComment extends OutputFamily {
// Public constants -----------------------------------------------------------------------------------------------
/** The component type, which is {@value org.omnifaces.component.output.ConditionalComment#COMPONENT_TYPE}. */
public static final String COMPONENT_TYPE = "org.omnifaces.component.output.ConditionalComment";
// Private constants ----------------------------------------------------------------------------------------------
private static final String ERROR_MISSING_IF =
"ConditionalComment attribute 'if' must be specified.";
private enum PropertyKeys {
IF;
@Override public String toString() { return name().toLowerCase(); }
}
// Variables ------------------------------------------------------------------------------------------------------
private final State state = new State(getStateHelper());
// UIComponent overrides ------------------------------------------------------------------------------------------
/**
* @throws IllegalArgumentException When <code>if</code> attribute is not specified.
*/
@Override
public void encodeBegin(FacesContext context) throws IOException {
String condition = getIf();
if (isEmpty(condition)) {
throw new IllegalArgumentException(ERROR_MISSING_IF);
}
ResponseWriter writer = context.getResponseWriter();
writer.write("<!--[if ");
writer.write(condition);
writer.write("]>");
}
@Override
public void encodeEnd(FacesContext context) throws IOException {
ResponseWriter writer = context.getResponseWriter();
writer.write("<![endif]-->");
}
// Attribute getters/setters --------------------------------------------------------------------------------------
/**
* Returns the if condition.
* @return The if condition.
*/
public String getIf() {
return state.get(PropertyKeys.IF);
}
/**
* Sets the if condition.
* @param condition The if condition.
*/
public void setIf(String condition) {
state.put(PropertyKeys.IF, condition);
}
}