View Javadoc
1   /*
2    * Copyright (c) 2011-2024 PrimeFaces Extensions
3    *
4    *  Permission is hereby granted, free of charge, to any person obtaining a copy
5    *  of this software and associated documentation files (the "Software"), to deal
6    *  in the Software without restriction, including without limitation the rights
7    *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    *  copies of the Software, and to permit persons to whom the Software is
9    *  furnished to do so, subject to the following conditions:
10   *
11   *  The above copyright notice and this permission notice shall be included in
12   *  all copies or substantial portions of the Software.
13   *
14   *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20   *  THE SOFTWARE.
21   */
22  package org.primefaces.extensions.component.suneditor;
23  
24  import java.io.IOException;
25  import java.util.Map;
26  
27  import javax.faces.component.UIComponent;
28  import javax.faces.context.FacesContext;
29  import javax.faces.context.ResponseWriter;
30  import javax.faces.convert.Converter;
31  
32  import org.primefaces.renderkit.InputRenderer;
33  import org.primefaces.util.ComponentUtils;
34  import org.primefaces.util.HTML;
35  import org.primefaces.util.WidgetBuilder;
36  
37  /**
38   * <code>SunEditor</code> component.
39   *
40   * @author Matthieu Valente
41   * @since 12.0.6
42   */
43  public class SunEditorRenderer extends InputRenderer {
44  
45      @Override
46      public void decode(final FacesContext context, final UIComponent component) {
47          final SunEditor editor = (SunEditor) component;
48  
49          if (!shouldDecode(editor)) {
50              return;
51          }
52  
53          // set value
54          final String clientId = editor.getClientId(context);
55          final Map<String, String> params = context.getExternalContext().getRequestParameterMap();
56          if (params.containsKey(clientId)) {
57              String value = editor.sanitizeHtml(context, params.get(clientId));
58              editor.setSubmittedValue(value);
59          }
60  
61          // decode behaviors
62          decodeBehaviors(context, component);
63      }
64  
65      @Override
66      public void encodeEnd(final FacesContext context, final UIComponent component) throws IOException {
67          final SunEditor editor = (SunEditor) component;
68          editor.checkSecurity(context);
69          encodeMarkup(context, editor);
70          encodeScript(context, editor);
71      }
72  
73      protected void encodeMarkup(final FacesContext context, final SunEditor editor) throws IOException {
74          final ResponseWriter writer = context.getResponseWriter();
75          final String clientId = editor.getClientId(context);
76          String style = editor.getStyle();
77          String styleClass = createStyleClass(editor, SunEditor.EDITOR_CLASS);
78  
79          writer.startElement("textarea", editor);
80          writer.writeAttribute("id", clientId, null);
81          writer.writeAttribute("name", clientId, null);
82          writer.writeAttribute("class", styleClass, null);
83          if (style != null) {
84              writer.writeAttribute("style", style, null);
85          }
86  
87          renderAccessibilityAttributes(context, editor);
88          renderPassThruAttributes(context, editor, HTML.TEXTAREA_ATTRS_WITHOUT_EVENTS);
89          renderDomEvents(context, editor, HTML.INPUT_TEXT_EVENTS);
90          renderValidationMetadata(context, editor);
91  
92          String valueToRender = editor.sanitizeHtml(context, ComponentUtils.getValueToRender(context, editor));
93          if (valueToRender != null) {
94              // #1639 do not escape as its already been sanitized above, and we want to keep exact formatting
95              writer.writeText(valueToRender, "value");
96          }
97  
98          writer.endElement("textarea");
99      }
100 
101     protected void encodeScript(final FacesContext context, final SunEditor editor) throws IOException {
102         final WidgetBuilder wb = getWidgetBuilder(context);
103         wb.init("ExtSunEditor", editor)
104                     .attr("width", editor.getWidth())
105                     .attr("height", editor.getHeight())
106                     .attr("mode", editor.getMode(), "classic")
107                     .attr("rtl", ComponentUtils.isRTL(context, editor), false)
108                     .attr("locale", editor.calculateLocale().toString())
109                     .attr("readOnly", editor.isReadonly(), false)
110                     .attr("disabled", editor.isDisabled(), false)
111                     .attr("toolbar", editor.getToolbar())
112                     .nativeAttr("extender", editor.getExtender());
113 
114         encodeClientBehaviors(context, editor);
115         wb.finish();
116     }
117 
118     @Override
119     public Object getConvertedValue(final FacesContext context, final UIComponent component,
120                 final Object submittedValue) {
121         final SunEditor editor = (SunEditor) component;
122         final String value = (String) submittedValue;
123         final Converter converter = ComponentUtils.getConverter(context, component);
124 
125         if (converter != null) {
126             return converter.getAsObject(context, editor, value);
127         }
128 
129         return value;
130     }
131 }