void LayoutMenuList::setTextFromOption(int optionIndex)
{
    HTMLSelectElement* select = selectElement();
    const HeapVector<Member<HTMLElement>>& listItems = select->listItems();
    const int size = listItems.size();

    String text = emptyString();
    m_optionStyle.clear();

    if (selectElement()->multiple()) {
        unsigned selectedCount = 0;
        int firstSelectedIndex = -1;
        for (int i = 0; i < size; ++i) {
            Element* element = listItems[i];
            if (!isHTMLOptionElement(*element))
                continue;

            if (toHTMLOptionElement(element)->selected()) {
                if (++selectedCount == 1)
                    firstSelectedIndex = i;
            }
        }

        if (selectedCount == 1) {
            ASSERT(0 <= firstSelectedIndex);
            ASSERT(firstSelectedIndex < size);
            HTMLOptionElement* selectedOptionElement = toHTMLOptionElement(listItems[firstSelectedIndex]);
            ASSERT(selectedOptionElement->selected());
            text = selectedOptionElement->textIndentedToRespectGroupLabel();
            m_optionStyle = selectedOptionElement->mutableComputedStyle();
        } else {
            Locale& locale = select->locale();
            String localizedNumberString = locale.convertToLocalizedNumber(String::number(selectedCount));
            text = locale.queryString(WebLocalizedString::SelectMenuListText, localizedNumberString);
            ASSERT(!m_optionStyle);
        }
    } else {
        const int i = select->optionToListIndex(optionIndex);
        if (i >= 0 && i < size) {
            Element* element = listItems[i];
            if (isHTMLOptionElement(*element)) {
                text = toHTMLOptionElement(element)->textIndentedToRespectGroupLabel();
                m_optionStyle = element->mutableComputedStyle();
            }
        }
    }

    setText(text.stripWhiteSpace());

    didUpdateActiveOption(optionIndex);
}
Exemplo n.º 2
0
void LayoutMenuList::updateFromElement() {
  HTMLSelectElement* select = selectElement();
  HTMLOptionElement* option = select->optionToBeShown();
  String text = emptyString();
  m_optionStyle.clear();

  if (select->isMultiple()) {
    unsigned selectedCount = 0;
    HTMLOptionElement* selectedOptionElement = nullptr;
    for (const auto& option : select->optionList()) {
      if (option->selected()) {
        if (++selectedCount == 1)
          selectedOptionElement = option;
      }
    }

    if (selectedCount == 1) {
      text = selectedOptionElement->textIndentedToRespectGroupLabel();
      m_optionStyle = selectedOptionElement->mutableComputedStyle();
    } else {
      Locale& locale = select->locale();
      String localizedNumberString =
          locale.convertToLocalizedNumber(String::number(selectedCount));
      text = locale.queryString(WebLocalizedString::SelectMenuListText,
                                localizedNumberString);
      ASSERT(!m_optionStyle);
    }
  } else {
    if (option) {
      text = option->textIndentedToRespectGroupLabel();
      m_optionStyle = option->mutableComputedStyle();
    }
  }

  setText(text.stripWhiteSpace());

  didUpdateActiveOption(option);
}