This source file includes following definitions.
- m_popupIsVisible
- isChildAllowed
- createInnerBlock
- adjustInnerStyle
- selectElement
- addChild
- removeChild
- styleDidChange
- updateOptionsWidth
- updateFromElement
- setTextFromOption
- setText
- text
- controlClipRect
- computeIntrinsicLogicalWidths
- computePreferredLogicalWidths
- showPopup
- hidePopup
- valueChanged
- listBoxSelectItem
- multiple
- didSetSelectedIndex
- didUpdateActiveOption
- itemText
- itemAccessibilityText
- itemToolTip
- itemIsEnabled
- itemStyle
- getItemBackgroundColor
- menuStyle
- clientPaddingLeft
- clientPaddingRight
- listSize
- selectedIndex
- popupDidHide
- itemIsSeparator
- itemIsLabel
- itemIsSelected
- setTextFromItem
#include "config.h"
#include "core/rendering/RenderMenuList.h"
#include <math.h>
#include "HTMLNames.h"
#include "core/accessibility/AXMenuList.h"
#include "core/accessibility/AXObjectCache.h"
#include "core/css/CSSFontSelector.h"
#include "core/css/resolver/StyleResolver.h"
#include "core/dom/NodeRenderStyle.h"
#include "core/frame/FrameHost.h"
#include "core/frame/FrameView.h"
#include "core/frame/LocalFrame.h"
#include "core/html/HTMLOptGroupElement.h"
#include "core/html/HTMLOptionElement.h"
#include "core/html/HTMLSelectElement.h"
#include "core/page/Chrome.h"
#include "core/rendering/RenderBR.h"
#include "core/rendering/RenderScrollbar.h"
#include "core/rendering/RenderTheme.h"
#include "core/rendering/RenderView.h"
#include "platform/fonts/FontCache.h"
#include "platform/geometry/IntSize.h"
using namespace std;
namespace WebCore {
using namespace HTMLNames;
RenderMenuList::RenderMenuList(Element* element)
: RenderFlexibleBox(element)
, m_buttonText(0)
, m_innerBlock(0)
, m_optionsChanged(true)
, m_optionsWidth(0)
, m_lastActiveIndex(-1)
, m_popupIsVisible(false)
{
ASSERT(isHTMLSelectElement(element));
}
RenderMenuList::~RenderMenuList()
{
if (m_popup)
m_popup->disconnectClient();
m_popup = nullptr;
}
bool RenderMenuList::isChildAllowed(RenderObject* object, RenderStyle*) const
{
return object->isAnonymous() && !object->isRenderFullScreen();
}
void RenderMenuList::createInnerBlock()
{
if (m_innerBlock) {
ASSERT(firstChild() == m_innerBlock);
ASSERT(!m_innerBlock->nextSibling());
return;
}
ASSERT(!firstChild());
m_innerBlock = createAnonymousBlock();
adjustInnerStyle();
RenderFlexibleBox::addChild(m_innerBlock);
}
void RenderMenuList::adjustInnerStyle()
{
RenderStyle* innerStyle = m_innerBlock->style();
innerStyle->setFlexGrow(1);
innerStyle->setFlexShrink(1);
if (style()->alignItems() == ItemPositionCenter) {
innerStyle->setMarginTop(Length());
innerStyle->setMarginBottom(Length());
innerStyle->setAlignSelf(ItemPositionFlexStart);
}
innerStyle->setPaddingLeft(Length(RenderTheme::theme().popupInternalPaddingLeft(style()), Fixed));
innerStyle->setPaddingRight(Length(RenderTheme::theme().popupInternalPaddingRight(style()), Fixed));
innerStyle->setPaddingTop(Length(RenderTheme::theme().popupInternalPaddingTop(style()), Fixed));
innerStyle->setPaddingBottom(Length(RenderTheme::theme().popupInternalPaddingBottom(style()), Fixed));
if (m_optionStyle) {
if ((m_optionStyle->direction() != innerStyle->direction() || m_optionStyle->unicodeBidi() != innerStyle->unicodeBidi()))
m_innerBlock->setNeedsLayoutAndPrefWidthsRecalc();
innerStyle->setTextAlign(style()->isLeftToRightDirection() ? LEFT : RIGHT);
innerStyle->setDirection(m_optionStyle->direction());
innerStyle->setUnicodeBidi(m_optionStyle->unicodeBidi());
}
}
inline HTMLSelectElement* RenderMenuList::selectElement() const
{
return toHTMLSelectElement(node());
}
void RenderMenuList::addChild(RenderObject* newChild, RenderObject* beforeChild)
{
createInnerBlock();
m_innerBlock->addChild(newChild, beforeChild);
ASSERT(m_innerBlock == firstChild());
if (AXObjectCache* cache = document().existingAXObjectCache())
cache->childrenChanged(this);
}
void RenderMenuList::removeChild(RenderObject* oldChild)
{
if (oldChild == m_innerBlock || !m_innerBlock) {
RenderFlexibleBox::removeChild(oldChild);
m_innerBlock = 0;
} else
m_innerBlock->removeChild(oldChild);
}
void RenderMenuList::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
{
RenderBlock::styleDidChange(diff, oldStyle);
if (m_buttonText)
m_buttonText->setStyle(style());
if (m_innerBlock)
adjustInnerStyle();
bool fontChanged = !oldStyle || oldStyle->font() != style()->font();
if (fontChanged)
updateOptionsWidth();
}
void RenderMenuList::updateOptionsWidth()
{
float maxOptionWidth = 0;
const Vector<HTMLElement*>& listItems = selectElement()->listItems();
int size = listItems.size();
FontCachePurgePreventer fontCachePurgePreventer;
for (int i = 0; i < size; ++i) {
HTMLElement* element = listItems[i];
if (!isHTMLOptionElement(*element))
continue;
String text = toHTMLOptionElement(element)->textIndentedToRespectGroupLabel();
applyTextTransform(style(), text, ' ');
if (RenderTheme::theme().popupOptionSupportsTextIndent()) {
float optionWidth = 0;
if (RenderStyle* optionStyle = element->renderStyle())
optionWidth += minimumValueForLength(optionStyle->textIndent(), 0);
if (!text.isEmpty())
optionWidth += style()->font().width(text);
maxOptionWidth = max(maxOptionWidth, optionWidth);
} else if (!text.isEmpty())
maxOptionWidth = max(maxOptionWidth, style()->font().width(text));
}
int width = static_cast<int>(ceilf(maxOptionWidth));
if (m_optionsWidth == width)
return;
m_optionsWidth = width;
if (parent())
setNeedsLayoutAndPrefWidthsRecalc();
}
void RenderMenuList::updateFromElement()
{
if (m_optionsChanged) {
updateOptionsWidth();
m_optionsChanged = false;
}
if (m_popupIsVisible) {
m_popup->updateFromElement();
} else {
if (selectElement()->suggestedIndex() >= 0)
setTextFromOption(selectElement()->suggestedIndex());
else
setTextFromOption(selectElement()->selectedIndex());
}
}
void RenderMenuList::setTextFromOption(int optionIndex)
{
HTMLSelectElement* select = selectElement();
const Vector<HTMLElement*>& listItems = select->listItems();
int size = listItems.size();
int i = select->optionToListIndex(optionIndex);
String text = emptyString();
if (i >= 0 && i < size) {
Element* element = listItems[i];
if (isHTMLOptionElement(*element)) {
text = toHTMLOptionElement(element)->textIndentedToRespectGroupLabel();
m_optionStyle = element->renderStyle();
}
}
setText(text.stripWhiteSpace());
didUpdateActiveOption(optionIndex);
}
void RenderMenuList::setText(const String& s)
{
if (s.isEmpty()) {
if (!m_buttonText || !m_buttonText->isBR()) {
if (m_buttonText)
m_buttonText->destroy();
m_buttonText = new RenderBR(&document());
m_buttonText->setStyle(style());
addChild(m_buttonText);
}
} else {
if (m_buttonText && !m_buttonText->isBR())
m_buttonText->setText(s.impl(), true);
else {
if (m_buttonText)
m_buttonText->destroy();
m_buttonText = new RenderText(&document(), s.impl());
m_buttonText->setStyle(style());
m_buttonText->setText(s.impl(), true);
addChild(m_buttonText);
}
adjustInnerStyle();
}
}
String RenderMenuList::text() const
{
return m_buttonText ? m_buttonText->text() : String();
}
LayoutRect RenderMenuList::controlClipRect(const LayoutPoint& additionalOffset) const
{
LayoutRect outerBox(additionalOffset.x() + borderLeft() + paddingLeft(),
additionalOffset.y() + borderTop() + paddingTop(),
contentWidth(),
contentHeight());
LayoutRect innerBox(additionalOffset.x() + m_innerBlock->x() + m_innerBlock->paddingLeft(),
additionalOffset.y() + m_innerBlock->y() + m_innerBlock->paddingTop(),
m_innerBlock->contentWidth(),
m_innerBlock->contentHeight());
return intersection(outerBox, innerBox);
}
void RenderMenuList::computeIntrinsicLogicalWidths(LayoutUnit& minLogicalWidth, LayoutUnit& maxLogicalWidth) const
{
maxLogicalWidth = max(m_optionsWidth, RenderTheme::theme().minimumMenuListSize(style())) + m_innerBlock->paddingLeft() + m_innerBlock->paddingRight();
if (!style()->width().isPercent())
minLogicalWidth = maxLogicalWidth;
}
void RenderMenuList::computePreferredLogicalWidths()
{
m_minPreferredLogicalWidth = 0;
m_maxPreferredLogicalWidth = 0;
if (style()->width().isFixed() && style()->width().value() > 0)
m_minPreferredLogicalWidth = m_maxPreferredLogicalWidth = adjustContentBoxLogicalWidthForBoxSizing(style()->width().value());
else
computeIntrinsicLogicalWidths(m_minPreferredLogicalWidth, m_maxPreferredLogicalWidth);
if (style()->minWidth().isFixed() && style()->minWidth().value() > 0) {
m_maxPreferredLogicalWidth = max(m_maxPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(style()->minWidth().value()));
m_minPreferredLogicalWidth = max(m_minPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(style()->minWidth().value()));
}
if (style()->maxWidth().isFixed()) {
m_maxPreferredLogicalWidth = min(m_maxPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(style()->maxWidth().value()));
m_minPreferredLogicalWidth = min(m_minPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(style()->maxWidth().value()));
}
LayoutUnit toAdd = borderAndPaddingWidth();
m_minPreferredLogicalWidth += toAdd;
m_maxPreferredLogicalWidth += toAdd;
clearPreferredLogicalWidthsDirty();
}
void RenderMenuList::showPopup()
{
if (m_popupIsVisible)
return;
if (document().frameHost()->chrome().hasOpenedPopup())
return;
createInnerBlock();
if (!m_popup)
m_popup = document().frameHost()->chrome().createPopupMenu(*document().frame(), this);
m_popupIsVisible = true;
FloatQuad quad(localToAbsoluteQuad(FloatQuad(borderBoundingBox())));
IntSize size = pixelSnappedIntRect(frameRect()).size();
HTMLSelectElement* select = selectElement();
m_popup->show(quad, size, select->optionToListIndex(select->selectedIndex()));
}
void RenderMenuList::hidePopup()
{
if (m_popup)
m_popup->hide();
}
void RenderMenuList::valueChanged(unsigned listIndex, bool fireOnChange)
{
Document& doc = toElement(node())->document();
if (&doc != doc.frame()->document())
return;
HTMLSelectElement* select = selectElement();
select->optionSelectedByUser(select->listToOptionIndex(listIndex), fireOnChange);
}
void RenderMenuList::listBoxSelectItem(int listIndex, bool allowMultiplySelections, bool shift, bool fireOnChangeNow)
{
selectElement()->listBoxSelectItem(listIndex, allowMultiplySelections, shift, fireOnChangeNow);
}
bool RenderMenuList::multiple() const
{
return selectElement()->multiple();
}
void RenderMenuList::didSetSelectedIndex(int listIndex)
{
didUpdateActiveOption(selectElement()->listToOptionIndex(listIndex));
}
void RenderMenuList::didUpdateActiveOption(int optionIndex)
{
if (!AXObjectCache::accessibilityEnabled() || !document().existingAXObjectCache())
return;
if (m_lastActiveIndex == optionIndex)
return;
m_lastActiveIndex = optionIndex;
HTMLSelectElement* select = selectElement();
int listIndex = select->optionToListIndex(optionIndex);
if (listIndex < 0 || listIndex >= static_cast<int>(select->listItems().size()))
return;
if (AXMenuList* menuList = toAXMenuList(document().axObjectCache()->get(this)))
menuList->didUpdateActiveOption(optionIndex);
}
String RenderMenuList::itemText(unsigned listIndex) const
{
HTMLSelectElement* select = selectElement();
const Vector<HTMLElement*>& listItems = select->listItems();
if (listIndex >= listItems.size())
return String();
String itemString;
Element* element = listItems[listIndex];
if (isHTMLOptGroupElement(*element))
itemString = toHTMLOptGroupElement(*element).groupLabelText();
else if (isHTMLOptionElement(*element))
itemString = toHTMLOptionElement(*element).textIndentedToRespectGroupLabel();
applyTextTransform(style(), itemString, ' ');
return itemString;
}
String RenderMenuList::itemAccessibilityText(unsigned listIndex) const
{
const Vector<HTMLElement*>& listItems = selectElement()->listItems();
if (listIndex >= listItems.size())
return String();
return listItems[listIndex]->fastGetAttribute(aria_labelAttr);
}
String RenderMenuList::itemToolTip(unsigned listIndex) const
{
const Vector<HTMLElement*>& listItems = selectElement()->listItems();
if (listIndex >= listItems.size())
return String();
return listItems[listIndex]->title();
}
bool RenderMenuList::itemIsEnabled(unsigned listIndex) const
{
const Vector<HTMLElement*>& listItems = selectElement()->listItems();
if (listIndex >= listItems.size())
return false;
HTMLElement* element = listItems[listIndex];
if (!isHTMLOptionElement(*element))
return false;
bool groupEnabled = true;
if (Element* parentElement = element->parentElement()) {
if (isHTMLOptGroupElement(*parentElement))
groupEnabled = !parentElement->isDisabledFormControl();
}
if (!groupEnabled)
return false;
return !element->isDisabledFormControl();
}
PopupMenuStyle RenderMenuList::itemStyle(unsigned listIndex) const
{
const Vector<HTMLElement*>& listItems = selectElement()->listItems();
if (listIndex >= listItems.size()) {
if (!listIndex)
return menuStyle();
listIndex = 0;
}
HTMLElement* element = listItems[listIndex];
Color itemBackgroundColor;
bool itemHasCustomBackgroundColor;
getItemBackgroundColor(listIndex, itemBackgroundColor, itemHasCustomBackgroundColor);
RenderStyle* style = element->renderStyle() ? element->renderStyle() : element->computedStyle();
return style ? PopupMenuStyle(resolveColor(style, CSSPropertyColor), itemBackgroundColor, style->font(), style->visibility() == VISIBLE,
style->display() == NONE, style->textIndent(), style->direction(), isOverride(style->unicodeBidi()),
itemHasCustomBackgroundColor ? PopupMenuStyle::CustomBackgroundColor : PopupMenuStyle::DefaultBackgroundColor) : menuStyle();
}
void RenderMenuList::getItemBackgroundColor(unsigned listIndex, Color& itemBackgroundColor, bool& itemHasCustomBackgroundColor) const
{
const Vector<HTMLElement*>& listItems = selectElement()->listItems();
if (listIndex >= listItems.size()) {
itemBackgroundColor = resolveColor(CSSPropertyBackgroundColor);
itemHasCustomBackgroundColor = false;
return;
}
HTMLElement* element = listItems[listIndex];
Color backgroundColor;
if (element->renderStyle())
backgroundColor = resolveColor(element->renderStyle(), CSSPropertyBackgroundColor);
itemHasCustomBackgroundColor = backgroundColor.alpha();
if (!backgroundColor.hasAlpha()) {
itemBackgroundColor = backgroundColor;
return;
}
backgroundColor = resolveColor(CSSPropertyBackgroundColor).blend(backgroundColor);
if (!backgroundColor.hasAlpha()) {
itemBackgroundColor = backgroundColor;
return;
}
itemBackgroundColor = Color(Color::white).blend(backgroundColor);
}
PopupMenuStyle RenderMenuList::menuStyle() const
{
const RenderObject* o = m_innerBlock ? m_innerBlock : this;
const RenderStyle* s = o->style();
return PopupMenuStyle(o->resolveColor(CSSPropertyColor), o->resolveColor(CSSPropertyBackgroundColor), s->font(), s->visibility() == VISIBLE,
s->display() == NONE, s->textIndent(), style()->direction(), isOverride(style()->unicodeBidi()));
}
LayoutUnit RenderMenuList::clientPaddingLeft() const
{
return paddingLeft() + m_innerBlock->paddingLeft();
}
const int endOfLinePadding = 2;
LayoutUnit RenderMenuList::clientPaddingRight() const
{
if (style()->appearance() == MenulistPart || style()->appearance() == MenulistButtonPart) {
return endOfLinePadding;
}
return paddingRight() + m_innerBlock->paddingRight();
}
int RenderMenuList::listSize() const
{
return selectElement()->listItems().size();
}
int RenderMenuList::selectedIndex() const
{
HTMLSelectElement* select = selectElement();
return select->optionToListIndex(select->selectedIndex());
}
void RenderMenuList::popupDidHide()
{
m_popupIsVisible = false;
}
bool RenderMenuList::itemIsSeparator(unsigned listIndex) const
{
const Vector<HTMLElement*>& listItems = selectElement()->listItems();
return listIndex < listItems.size() && isHTMLHRElement(*listItems[listIndex]);
}
bool RenderMenuList::itemIsLabel(unsigned listIndex) const
{
const Vector<HTMLElement*>& listItems = selectElement()->listItems();
return listIndex < listItems.size() && isHTMLOptGroupElement(*listItems[listIndex]);
}
bool RenderMenuList::itemIsSelected(unsigned listIndex) const
{
const Vector<HTMLElement*>& listItems = selectElement()->listItems();
if (listIndex >= listItems.size())
return false;
HTMLElement* element = listItems[listIndex];
return isHTMLOptionElement(*element) && toHTMLOptionElement(*element).selected();
}
void RenderMenuList::setTextFromItem(unsigned listIndex)
{
setTextFromOption(selectElement()->listToOptionIndex(listIndex));
}
}