INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK. Changes the type of the current HtmlInput. Because there are several subclasses of HtmlInput, changing the type attribute is not sufficient, this will replace the HtmlInput element in the DOM tre
(String newType, final boolean setThroughAttribute)
| 1104 | * @return the new or the old HtmlInput element |
| 1105 | */ |
| 1106 | public HtmlInput changeType(String newType, final boolean setThroughAttribute) { |
| 1107 | final String currentType = getAttributeDirect(TYPE_ATTRIBUTE); |
| 1108 | |
| 1109 | final SgmlPage page = getPage(); |
| 1110 | final WebClient webClient = page.getWebClient(); |
| 1111 | final BrowserVersion browser = webClient.getBrowserVersion(); |
| 1112 | if (!currentType.equalsIgnoreCase(newType)) { |
| 1113 | if (!isSupported(StringUtils.toRootLowerCase(newType), browser)) { |
| 1114 | if (setThroughAttribute) { |
| 1115 | newType = "text"; |
| 1116 | } |
| 1117 | } |
| 1118 | |
| 1119 | final AttributesImpl attributes = new AttributesImpl(); |
| 1120 | boolean typeFound = false; |
| 1121 | for (final DomAttr entry : getAttributesMap().values()) { |
| 1122 | final String name = entry.getName(); |
| 1123 | final String value = entry.getValue(); |
| 1124 | |
| 1125 | if (TYPE_ATTRIBUTE.equals(name)) { |
| 1126 | attributes.addAttribute(null, name, name, null, newType); |
| 1127 | typeFound = true; |
| 1128 | } |
| 1129 | else { |
| 1130 | attributes.addAttribute(null, name, name, null, value); |
| 1131 | } |
| 1132 | } |
| 1133 | |
| 1134 | if (!typeFound) { |
| 1135 | attributes.addAttribute(null, TYPE_ATTRIBUTE, TYPE_ATTRIBUTE, null, newType); |
| 1136 | } |
| 1137 | |
| 1138 | // create a new one only if we have a new type |
| 1139 | if (ATTRIBUTE_NOT_DEFINED != currentType || !"text".equalsIgnoreCase(newType)) { |
| 1140 | final HtmlInput newInput = (HtmlInput) webClient.getPageCreator().getHtmlParser() |
| 1141 | .getFactory(TAG_NAME) |
| 1142 | .createElement(page, TAG_NAME, attributes); |
| 1143 | |
| 1144 | newInput.adjustValueAfterTypeChange(this, browser); |
| 1145 | |
| 1146 | // the input hasn't yet been inserted into the DOM tree (likely has been |
| 1147 | // created via document.createElement()), so simply replace it with the |
| 1148 | // new Input instance created in the code above |
| 1149 | if (getParentNode() != null) { |
| 1150 | getParentNode().replaceChild(newInput, this); |
| 1151 | } |
| 1152 | |
| 1153 | final WebClient client = page.getWebClient(); |
| 1154 | if (client.isJavaScriptEngineEnabled()) { |
| 1155 | final HTMLInputElement scriptable = getScriptableObject(); |
| 1156 | setScriptableObject(null); |
| 1157 | scriptable.setDomNode(newInput, true); |
| 1158 | } |
| 1159 | |
| 1160 | return newInput; |
| 1161 | } |
| 1162 | super.setAttributeNS(null, TYPE_ATTRIBUTE, newType, true, true); |
| 1163 | } |