Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
License Agreement. JBoss RichFaces - Ajax4jsf Component Library Copyright (C) 2007 Exadel, Inc. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 package org.richfaces.component.util;
 import  javax.faces.component.UIComponent;
 import  javax.faces.component.UIViewRoot;
 import  javax.faces.context.FacesContext;
 import  org.ajax4jsf.javascript.ScriptUtils;
 import  org.richfaces.renderkit.util.HtmlDimensions;

Nick Belaevski - created 09.02.2007
Brian Leathem
 public final class HtmlUtil {
     public static final Pattern ID_SELECTOR_PATTERN = Pattern
     private static final String ORG_AJAX4JSF = "org.ajax4jsf.";
     private static final String ORG_RICHFACES = "org.richfaces.";
     private static final RendererUtils RENDERER_UTILS = RendererUtils.getInstance();
     private HtmlUtil() {
     public static String addToSize(String declarationString delta) {
         Double doubleDelta = HtmlDimensions.decode(delta);
         Double decoded = HtmlDimensions.decode(declaration);
         return HtmlDimensions.formatPx(new Double(decoded.doubleValue() + doubleDelta.doubleValue()));
     private static String escapeReplacement(String s) {
         return s.replaceAll("(\\\\|\\$)""\\\\$1");


Expands ID selectors in given CSS selector from componentId to clientId.

e.g. #componentId turns out into #form:componentId

For expansion, RendererUtils.findComponentFor(FacesContext, UIComponent, String) is used.

selector the CSS selector
component the root component for finding other components in tree
context the FacesContext
The expanded CSS selector where ID selectors are expanded from componentIds to clientIds
     public static String expandIdSelector(String selector, UIComponent component, FacesContext context) {
         Matcher matcher = .matcher(selector);
         StringBuffer sb = new StringBuffer();
         while (matcher.find()) {
             // make new id selector here using
             String unescaped ="\\\\:"":");
             UIComponent target = .findComponentFor(contextcomponentunescaped);
             if (target != null) {
                         escapeReplacement("#" + ScriptUtils.escapeCSSMetachars(target.getClientId(context))));
         return sb.toString();
    public static String idsToIdSelector(String ids) {
        StringBuffer buffer = new StringBuffer();
        if (ids != null) {
            String[] idString = ids.split("\\s*,\\s*");
            for (int i = 0; i < idString.lengthi++) {
                if (i > 0) {
                idString[i] = idString[i].replaceAll(":""\\\\:");
        return buffer.toString();
    public static boolean shouldWriteId(UIComponent component) {
        String rendererType = component.getRendererType();
        String id = component.getId();
        if ((id != null) && !id.startsWith(UIViewRoot.UNIQUE_ID_PREFIX)) {
            return true;
        if ((rendererType != null) && (rendererType.startsWith() || rendererType.startsWith())) {
            return true;
        return false;
    private static String concat(char separatorObject... objects) {
        StringBuilder result = new StringBuilder();
        for (Object o : objects) {
            String s = (Stringo;
            if (!Strings.isNullOrEmpty(s)) {
                if (result.length() != 0) {
        return result.toString();
    public static String concatClasses(Object... classes) {
        return concat(' 'classes);
    public static String concatStyles(Object... styles) {
        return concat(';'styles);
    public static String escapeHtml(String html) {
        return html.replaceAll("&""&amp;")
New to GrepCode? Check out our FAQ X