HTMLOptionElement.cpp [plain text]
#include "config.h"
#include "HTMLOptionElement.h"
#include "Document.h"
#include "ExceptionCode.h"
#include "HTMLNames.h"
#include "HTMLSelectElement.h"
#include "RenderMenuList.h"
#include "Text.h"
#include "cssstyleselector.h"
#include <wtf/Vector.h>
namespace WebCore {
using namespace HTMLNames;
HTMLOptionElement::HTMLOptionElement(Document* doc, HTMLFormElement* f)
: HTMLGenericFormElement(optionTag, doc, f)
, m_selected(false)
, m_style(0)
{
}
bool HTMLOptionElement::checkDTD(const Node* newChild)
{
return newChild->isTextNode() || newChild->hasTagName(scriptTag);
}
void HTMLOptionElement::attach()
{
RenderStyle* style = styleForRenderer(0);
setRenderStyle(style);
style->deref(document()->renderArena());
HTMLGenericFormElement::attach();
}
void HTMLOptionElement::detach()
{
if (m_style) {
m_style->deref(document()->renderArena());
m_style = 0;
}
HTMLGenericFormElement::detach();
}
bool HTMLOptionElement::isFocusable() const
{
return false;
}
const AtomicString& HTMLOptionElement::type() const
{
static const AtomicString option("option");
return option;
}
String HTMLOptionElement::text() const
{
String text;
if (!document()->inCompatMode()) {
String text = getAttribute(labelAttr);
if (!text.isEmpty())
return text;
}
const Node* n = firstChild();
while (n) {
if (n->nodeType() == TEXT_NODE || n->nodeType() == CDATA_SECTION_NODE)
text += n->nodeValue();
if (n->isElementNode() && n->hasTagName(HTMLNames::scriptTag))
n = n->traverseNextSibling(this);
else
n = n->traverseNextNode(this);
}
return text;
}
void HTMLOptionElement::setText(const String &text, ExceptionCode& ec)
{
Node* child = firstChild();
if (child && child->isTextNode() && !child->nextSibling()) {
static_cast<Text *>(child)->setData(text, ec);
return;
}
removeChildren();
appendChild(new Text(document(), text), ec);
}
int HTMLOptionElement::index() const
{
HTMLSelectElement *select = getSelect();
if (select) {
const Vector<HTMLElement*>& items = select->listItems();
int l = items.size();
int optionIndex = 0;
for(int i = 0; i < l; i++) {
if (items[i]->hasLocalName(optionTag)) {
if (static_cast<HTMLOptionElement*>(items[i]) == this)
return optionIndex;
optionIndex++;
}
}
}
return 0;
}
void HTMLOptionElement::setIndex(int, ExceptionCode& ec)
{
ec = NO_MODIFICATION_ALLOWED_ERR;
}
void HTMLOptionElement::parseMappedAttribute(MappedAttribute *attr)
{
if (attr->name() == selectedAttr)
m_selected = (!attr->isNull());
else if (attr->name() == valueAttr)
m_value = attr->value();
else
HTMLGenericFormElement::parseMappedAttribute(attr);
}
String HTMLOptionElement::value() const
{
if ( !m_value.isNull() )
return m_value;
return text().deprecatedString().stripWhiteSpace();
}
void HTMLOptionElement::setValue(const String& value)
{
setAttribute(valueAttr, value);
}
void HTMLOptionElement::setSelected(bool selected)
{
if (m_selected == selected)
return;
m_selected = selected;
if (HTMLSelectElement* select = getSelect())
select->notifyOptionSelected(this, selected);
}
void HTMLOptionElement::childrenChanged()
{
HTMLSelectElement *select = getSelect();
if (select)
select->childrenChanged();
}
HTMLSelectElement* HTMLOptionElement::getSelect() const
{
Node* select = parentNode();
while (select && !select->hasTagName(selectTag))
select = select->parentNode();
return static_cast<HTMLSelectElement*>(select);
}
bool HTMLOptionElement::defaultSelected() const
{
return !getAttribute(selectedAttr).isNull();
}
void HTMLOptionElement::setDefaultSelected(bool b)
{
setAttribute(selectedAttr, b ? "" : 0);
}
String HTMLOptionElement::label() const
{
return getAttribute(labelAttr);
}
void HTMLOptionElement::setLabel(const String& value)
{
setAttribute(labelAttr, value);
}
void HTMLOptionElement::setRenderStyle(RenderStyle* newStyle)
{
RenderStyle* oldStyle = m_style;
m_style = newStyle;
if (newStyle)
newStyle->ref();
if (oldStyle)
oldStyle->deref(document()->renderArena());
}
String HTMLOptionElement::optionText()
{
DeprecatedString itemText = text().deprecatedString();
if (itemText.isEmpty())
itemText = getAttribute(labelAttr).deprecatedString();
itemText.replace('\\', document()->backslashAsCurrencySymbol());
itemText = itemText.stripWhiteSpace();
itemText = itemText.simplifyWhiteSpace();
if (parentNode() && parentNode()->hasTagName(optgroupTag))
itemText.prepend(" ");
return itemText;
}
}