HTMLOptionElement.cpp [plain text]
#include "config.h"
#include "HTMLOptionElement.h"
#include "Document.h"
#include "ExceptionCode.h"
#include "HTMLDataListElement.h"
#include "HTMLNames.h"
#include "HTMLOptGroupElement.h"
#include "HTMLParserIdioms.h"
#include "HTMLSelectElement.h"
#include "NodeRenderStyle.h"
#include "NodeTraversal.h"
#include "RenderMenuList.h"
#include "RenderTheme.h"
#include "ScriptElement.h"
#include "StyleResolver.h"
#include "Text.h"
#include <wtf/Ref.h>
namespace WebCore {
using namespace HTMLNames;
HTMLOptionElement::HTMLOptionElement(const QualifiedName& tagName, Document& document)
: HTMLElement(tagName, document)
, m_disabled(false)
, m_isSelected(false)
{
ASSERT(hasTagName(optionTag));
setHasCustomStyleResolveCallbacks();
}
Ref<HTMLOptionElement> HTMLOptionElement::create(Document& document)
{
return adoptRef(*new HTMLOptionElement(optionTag, document));
}
Ref<HTMLOptionElement> HTMLOptionElement::create(const QualifiedName& tagName, Document& document)
{
return adoptRef(*new HTMLOptionElement(tagName, document));
}
RefPtr<HTMLOptionElement> HTMLOptionElement::createForJSConstructor(Document& document, const String& data, const String& value,
bool defaultSelected, bool selected, ExceptionCode& ec)
{
Ref<HTMLOptionElement> element = adoptRef(*new HTMLOptionElement(optionTag, document));
auto text = Text::create(document, data.isNull() ? emptyString() : data);
ec = 0;
element->appendChild(text, ec);
if (ec)
return nullptr;
if (!value.isNull())
element->setValue(value);
if (defaultSelected)
element->setAttributeWithoutSynchronization(selectedAttr, emptyAtom);
element->setSelected(selected);
return WTFMove(element);
}
bool HTMLOptionElement::isFocusable() const
{
if (!supportsFocus())
return false;
auto* style = const_cast<HTMLOptionElement&>(*this).computedStyle();
return style && style->display() != NONE;
}
bool HTMLOptionElement::matchesDefaultPseudoClass() const
{
return hasAttributeWithoutSynchronization(selectedAttr);
}
String HTMLOptionElement::text() const
{
String text = collectOptionInnerText();
return document().displayStringModifiedByEncoding(text).stripWhiteSpace(isHTMLSpace).simplifyWhiteSpace(isHTMLSpace);
}
void HTMLOptionElement::setText(const String &text, ExceptionCode& ec)
{
Ref<HTMLOptionElement> protectedThis(*this);
RefPtr<HTMLSelectElement> select = ownerSelectElement();
bool selectIsMenuList = select && select->usesMenuList();
int oldSelectedIndex = selectIsMenuList ? select->selectedIndex() : -1;
Node* child = firstChild();
if (is<Text>(child) && !child->nextSibling())
downcast<Text>(*child).setData(text);
else {
removeChildren();
appendChild(Text::create(document(), text), ec);
}
if (selectIsMenuList && select->selectedIndex() != oldSelectedIndex)
select->setSelectedIndex(oldSelectedIndex);
}
void HTMLOptionElement::accessKeyAction(bool)
{
HTMLSelectElement* select = ownerSelectElement();
if (select)
select->accessKeySetSelectedIndex(index());
}
int HTMLOptionElement::index() const
{
HTMLSelectElement* selectElement = ownerSelectElement();
if (!selectElement)
return 0;
int optionIndex = 0;
for (auto& item : selectElement->listItems()) {
if (!is<HTMLOptionElement>(*item))
continue;
if (item == this)
return optionIndex;
++optionIndex;
}
return 0;
}
void HTMLOptionElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
{
#if ENABLE(DATALIST_ELEMENT)
if (name == valueAttr) {
if (HTMLDataListElement* dataList = ownerDataListElement())
dataList->optionElementChildrenChanged();
} else
#endif
if (name == disabledAttr) {
bool oldDisabled = m_disabled;
m_disabled = !value.isNull();
if (oldDisabled != m_disabled) {
setNeedsStyleRecalc();
if (renderer() && renderer()->style().hasAppearance())
renderer()->theme().stateChanged(*renderer(), ControlStates::EnabledState);
}
} else if (name == selectedAttr) {
setNeedsStyleRecalc();
m_isSelected = !value.isNull();
} else
HTMLElement::parseAttribute(name, value);
}
String HTMLOptionElement::value() const
{
const AtomicString& value = attributeWithoutSynchronization(valueAttr);
if (!value.isNull())
return value;
return collectOptionInnerText().stripWhiteSpace(isHTMLSpace).simplifyWhiteSpace(isHTMLSpace);
}
void HTMLOptionElement::setValue(const String& value)
{
setAttributeWithoutSynchronization(valueAttr, value);
}
bool HTMLOptionElement::selected()
{
if (HTMLSelectElement* select = ownerSelectElement())
select->updateListItemSelectedStates();
return m_isSelected;
}
void HTMLOptionElement::setSelected(bool selected)
{
if (m_isSelected == selected)
return;
setSelectedState(selected);
if (HTMLSelectElement* select = ownerSelectElement())
select->optionSelectionStateChanged(*this, selected);
}
void HTMLOptionElement::setSelectedState(bool selected)
{
if (m_isSelected == selected)
return;
m_isSelected = selected;
setNeedsStyleRecalc();
if (HTMLSelectElement* select = ownerSelectElement())
select->invalidateSelectedItems();
}
void HTMLOptionElement::childrenChanged(const ChildChange& change)
{
#if ENABLE(DATALIST_ELEMENT)
if (HTMLDataListElement* dataList = ownerDataListElement())
dataList->optionElementChildrenChanged();
else
#endif
if (HTMLSelectElement* select = ownerSelectElement())
select->optionElementChildrenChanged();
HTMLElement::childrenChanged(change);
}
#if ENABLE(DATALIST_ELEMENT)
HTMLDataListElement* HTMLOptionElement::ownerDataListElement() const
{
for (ContainerNode* parent = parentNode(); parent ; parent = parent->parentNode()) {
if (is<HTMLDataListElement>(*parent))
return downcast<HTMLDataListElement>(parent);
}
return nullptr;
}
#endif
HTMLSelectElement* HTMLOptionElement::ownerSelectElement() const
{
ContainerNode* select = parentNode();
while (select && !is<HTMLSelectElement>(*select))
select = select->parentNode();
if (!select)
return nullptr;
return downcast<HTMLSelectElement>(select);
}
String HTMLOptionElement::label() const
{
String label = attributeWithoutSynchronization(labelAttr);
if (!label.isNull())
return label.stripWhiteSpace(isHTMLSpace);
return collectOptionInnerText().stripWhiteSpace(isHTMLSpace).simplifyWhiteSpace(isHTMLSpace);
}
void HTMLOptionElement::setLabel(const String& label)
{
setAttributeWithoutSynchronization(labelAttr, label);
}
void HTMLOptionElement::willResetComputedStyle()
{
if (auto select = ownerSelectElement()) {
if (auto renderer = select->renderer())
renderer->repaint();
}
}
String HTMLOptionElement::textIndentedToRespectGroupLabel() const
{
ContainerNode* parent = parentNode();
if (is<HTMLOptGroupElement>(parent))
return " " + label();
return label();
}
bool HTMLOptionElement::isDisabledFormControl() const
{
if (ownElementDisabled())
return true;
if (!is<HTMLOptGroupElement>(parentNode()))
return false;
return downcast<HTMLOptGroupElement>(*parentNode()).isDisabledFormControl();
}
Node::InsertionNotificationRequest HTMLOptionElement::insertedInto(ContainerNode& insertionPoint)
{
if (HTMLSelectElement* select = ownerSelectElement()) {
select->setRecalcListItems();
if (m_isSelected)
select->optionSelectionStateChanged(*this, true);
select->scrollToSelection();
}
return HTMLElement::insertedInto(insertionPoint);
}
String HTMLOptionElement::collectOptionInnerText() const
{
StringBuilder text;
for (Node* node = firstChild(); node; ) {
if (is<Text>(*node))
text.append(node->nodeValue());
if (is<Element>(*node) && toScriptElementIfPossible(downcast<Element>(node)))
node = NodeTraversal::nextSkippingChildren(*node, this);
else
node = NodeTraversal::next(*node, this);
}
return text.toString();
}
}