1 38 package com.gargoylesoftware.htmlunit.javascript.host; 39 40 import com.gargoylesoftware.htmlunit.Assert; 41 import com.gargoylesoftware.htmlunit.javascript.SimpleScriptable; 42 43 import java.text.MessageFormat ; 44 import java.text.ParseException ; 45 import java.util.Iterator ; 46 import java.util.Map ; 47 import java.util.SortedMap ; 48 import java.util.StringTokenizer ; 49 import java.util.TreeMap ; 50 import org.mozilla.javascript.Scriptable; 51 52 61 public class Style extends SimpleScriptable { 62 private static final long serialVersionUID = -1976370264911039311L; 63 private static final MessageFormat URL_FORMAT = new MessageFormat ("url({0})"); 64 private HTMLElement jsElement_; 65 66 69 public Style() { 70 } 71 72 73 77 public void initialize( final HTMLElement htmlElement ) { 78 Assert.notNull("htmlElement", htmlElement); 80 jsElement_ = htmlElement; 81 82 if (htmlElement.getHtmlElementOrDie().getPage().getWebClient().getBrowserVersion().isIE()) { 83 for (Iterator i = getStyleMap().entrySet().iterator(); i.hasNext();) { 85 final Map.Entry entry = (Map.Entry ) i.next(); 86 final String key = (String ) entry.getKey(); 87 if ("behavior".equals(key)) { 88 final String value = (String ) entry.getValue(); 89 try { 90 final Object [] url = URL_FORMAT.parse(value); 91 if (url.length > 0) { 92 jsElement_.jsxFunction_addBehavior((String ) url[0]); 93 break; 94 } 95 } 96 catch (final ParseException e) { 97 getLog().warn("Invalid behavior: '" + value + "'."); 98 } 99 } 100 } 101 } 102 } 103 104 105 111 public Object get( final String name, final Scriptable start ) { 112 final Object result = super.get(name, start); 113 114 if( jsElement_ == null || result != NOT_FOUND ) { 117 return result; 118 } 119 120 final Object value = getStyleMap().get(name); 121 if( value == null ) { 122 return ""; 123 } 124 else { 125 return value; 126 } 127 } 128 129 130 136 public void put( final String name, final Scriptable start, final Object newValue ) { 137 if( jsElement_ == null ) { 141 super.put(name, start, newValue); 142 return; 143 } 144 145 final Map styleMap = getStyleMap(); 146 styleMap.put( name, newValue ); 147 148 final StringBuffer buffer = new StringBuffer (); 149 150 final Iterator iterator = styleMap.entrySet().iterator(); 151 while( iterator.hasNext() ) { 152 final Map.Entry entry = (Map.Entry )iterator.next(); 153 buffer.append( entry.getKey() ); 154 buffer.append( ": " ); 155 buffer.append( entry.getValue() ); 156 buffer.append( "; " ); 157 } 158 jsElement_.getHtmlElementOrDie().setAttributeValue("style", buffer.toString()); 159 } 160 161 162 private Map getStyleMap() { 163 final SortedMap styleMap = new TreeMap (); 165 166 final String styleAttribute = jsElement_.getHtmlElementOrDie().getAttributeValue("style"); 167 final StringTokenizer tokenizer = new StringTokenizer (styleAttribute, ";"); 168 while( tokenizer.hasMoreTokens() ) { 169 final String token = tokenizer.nextToken(); 170 final int index = token.indexOf(":"); 171 if( index != -1 ) { 172 final String key = token.substring(0,index).trim(); 173 final String value = token.substring(index+1).trim(); 174 175 styleMap.put( key, value ); 176 } 177 } 178 179 return styleMap; 180 } 181 } 182 | Popular Tags |