package votorola.g.web.gwt; // Copyright 2012, Michael Allan. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Votorola Software"), to deal in the Votorola Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Votorola Software, and to permit persons to whom the Votorola Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Votorola Software. THE VOTOROLA SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE VOTOROLA SOFTWARE OR THE USE OR OTHER DEALINGS IN THE VOTOROLA SOFTWARE. import com.google.gwt.dom.client.Element; /** {@linkplain Element Element} utilities. */ public final class ElementX { private ElementX() {} /** Returns true if the value of the 'class' attribute (allNames) contains the * specified class name. * * @param allNames the value of the 'class' attribute, a space-separated list of * class names. */ public static boolean hasClassName( final String allNames, final String className ) { // copied verbatim from removeClassName(), for consistency final String oldStyle = allNames; int idx = oldStyle.indexOf(className); // Calculate matching index. while (idx != -1) { if (idx == 0 || oldStyle.charAt(idx - 1) == ' ') { int last = idx + className.length(); int lastPos = oldStyle.length(); if ((last == lastPos) || ((last < lastPos) && (oldStyle.charAt(last) == ' '))) { break; } } idx = oldStyle.indexOf(className, idx + 1); } return idx != -1; } /** Does nothing if oldN and newN are equal; otherwise removes oldN if not null, and * adds newN if not null. This is similar to e.{@linkplain * Element#replaceClassName(String,String) replaceClassName}(oldN,newN) except that * it allows for null values. * * @return true if the class name was replaced as a result, false otherwise. */ public static boolean replaceNullClassName( final String oldN, final String newN, final Element e ) { final boolean isChanged; if( oldN == null ) { if( newN == null ) isChanged = false; else { e.addClassName( newN ); isChanged = true; } } else if( newN == null ) { e.removeClassName( oldN ); isChanged = true; } else if( !oldN.equals( newN )) { e.removeClassName( oldN ); e.addClassName( newN ); isChanged = true; } else isChanged = false; return isChanged; } }