/** * Sets the text of an option element. If the direction of the text is opposite to the page's * direction, also wraps it with Unicode bidi formatting characters to prevent garbling, and * indicates that this was done by setting the option's <code>BIDI_ATTR_NAME</code> custom * attribute. * * @param option an option element * @param text text to be set to the element * @param dir the text's direction. If {@code null} and direction estimation is turned off, * direction is ignored. */ protected void setOptionText(OptionElement option, String text, Direction dir) { if (dir == null && estimator != null) { dir = estimator.estimateDirection(text); } if (dir == null) { option.setText(text); option.removeAttribute(BIDI_ATTR_NAME); } else { String formattedText = BidiFormatter.getInstanceForCurrentLocale() .unicodeWrapWithKnownDir(dir, text, false /* isHtml */, false /* dirReset */); option.setText(formattedText); if (formattedText.length() > text.length()) { option.setAttribute(BIDI_ATTR_NAME, ""); } else { option.removeAttribute(BIDI_ATTR_NAME); } } }
/** * Creates an <code><option></code> element and inserts it as a child of the specified * <code><select></code> element. If the index is less than zero, or greater than or equal * to the length of the list, then the option element will be appended to the end of the list. * * @param selectElem the <code><select></code> element * @param item the text of the new item; cannot be <code>null</code> * @param value the <code>value</code> attribute for the new <code><option></code>; cannot * be <code>null</code> * @param index the index at which to insert the child */ public static void insertListItem(Element selectElem, String item, String value, int index) { assert !PotentialElement.isPotential(selectElem) : "Cannot insert into a PotentialElement"; SelectElement select = selectElem.<SelectElement>cast(); OptionElement option = Document.get().createOptionElement(); option.setText(item); option.setValue(value); if ((index == -1) || (index == select.getLength())) { select.add(option, null); } else { OptionElement before = select.getOptions().getItem(index); select.add(option, before); } }