webengine/osswebengine/WebCore/html/HTMLOptGroupElement.cpp
changeset 0 dd21522fd290
child 68 92a765b5b3e7
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 /*
       
     2  * This file is part of the DOM implementation for KDE.
       
     3  *
       
     4  * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
       
     5  *           (C) 1999 Antti Koivisto (koivisto@kde.org)
       
     6  *           (C) 2001 Dirk Mueller (mueller@kde.org)
       
     7  * Copyright (C) 2004, 2005, 2006 Apple Computer, Inc.
       
     8  *           (C) 2006 Alexey Proskuryakov (ap@nypop.com)
       
     9  *
       
    10  * This library is free software; you can redistribute it and/or
       
    11  * modify it under the terms of the GNU Library General Public
       
    12  * License as published by the Free Software Foundation; either
       
    13  * version 2 of the License, or (at your option) any later version.
       
    14  *
       
    15  * This library is distributed in the hope that it will be useful,
       
    16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    18  * Library General Public License for more details.
       
    19  *
       
    20  * You should have received a copy of the GNU Library General Public License
       
    21  * along with this library; see the file COPYING.LIB.  If not, write to
       
    22  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
       
    23  * Boston, MA 02110-1301, USA.
       
    24  *
       
    25  */
       
    26 
       
    27 #include "config.h"
       
    28 #include "HTMLOptGroupElement.h"
       
    29 
       
    30 #include "CSSStyleSelector.h"
       
    31 #include "Document.h"
       
    32 #include "HTMLNames.h"
       
    33 #include "HTMLSelectElement.h"
       
    34 #include "RenderMenuList.h"
       
    35 
       
    36 namespace WebCore {
       
    37 
       
    38 using namespace HTMLNames;
       
    39 
       
    40 HTMLOptGroupElement::HTMLOptGroupElement(Document* doc, HTMLFormElement* f)
       
    41     : HTMLGenericFormElement(optgroupTag, doc, f)
       
    42     , m_style(0)
       
    43 {
       
    44 }
       
    45 
       
    46 bool HTMLOptGroupElement::isFocusable() const
       
    47 {
       
    48     return false;
       
    49 }
       
    50 
       
    51 const AtomicString& HTMLOptGroupElement::type() const
       
    52 {
       
    53     static const AtomicString optgroup("optgroup");
       
    54     return optgroup;
       
    55 }
       
    56 
       
    57 bool HTMLOptGroupElement::insertBefore(PassRefPtr<Node> newChild, Node* refChild, ExceptionCode& ec)
       
    58 {
       
    59     bool result = HTMLGenericFormElement::insertBefore(newChild, refChild, ec);
       
    60     if (result)
       
    61         recalcSelectOptions();
       
    62     return result;
       
    63 }
       
    64 
       
    65 bool HTMLOptGroupElement::replaceChild(PassRefPtr<Node> newChild, Node* oldChild, ExceptionCode& ec)
       
    66 {
       
    67     bool result = HTMLGenericFormElement::replaceChild(newChild, oldChild, ec);
       
    68     if (result)
       
    69         recalcSelectOptions();
       
    70     return result;
       
    71 }
       
    72 
       
    73 bool HTMLOptGroupElement::removeChild(Node* oldChild, ExceptionCode& ec)
       
    74 {
       
    75     bool result = HTMLGenericFormElement::removeChild(oldChild, ec);
       
    76     if (result)
       
    77         recalcSelectOptions();
       
    78     return result;
       
    79 }
       
    80 
       
    81 bool HTMLOptGroupElement::appendChild(PassRefPtr<Node> newChild, ExceptionCode& ec)
       
    82 {
       
    83     bool result = HTMLGenericFormElement::appendChild(newChild, ec);
       
    84     if (result)
       
    85         recalcSelectOptions();
       
    86     return result;
       
    87 }
       
    88 
       
    89 ContainerNode* HTMLOptGroupElement::addChild(PassRefPtr<Node> newChild)
       
    90 {
       
    91     ContainerNode* result = HTMLGenericFormElement::addChild(newChild);
       
    92     if (result)
       
    93         recalcSelectOptions();
       
    94     return result;
       
    95 }
       
    96 
       
    97 void HTMLOptGroupElement::parseMappedAttribute(MappedAttribute *attr)
       
    98 {
       
    99     HTMLGenericFormElement::parseMappedAttribute(attr);
       
   100     recalcSelectOptions();
       
   101 }
       
   102 
       
   103 void HTMLOptGroupElement::recalcSelectOptions()
       
   104 {
       
   105     Node *select = parentNode();
       
   106     while (select && !select->hasTagName(selectTag))
       
   107         select = select->parentNode();
       
   108     if (select)
       
   109         static_cast<HTMLSelectElement*>(select)->setRecalcListItems();
       
   110 }
       
   111 
       
   112 String HTMLOptGroupElement::label() const
       
   113 {
       
   114     return getAttribute(labelAttr);
       
   115 }
       
   116 
       
   117 void HTMLOptGroupElement::setLabel(const String &value)
       
   118 {
       
   119     setAttribute(labelAttr, value);
       
   120 }
       
   121 
       
   122 bool HTMLOptGroupElement::checkDTD(const Node* newChild)
       
   123 {
       
   124     // Make sure to keep this in sync with <select> (other than not allowing an optgroup).
       
   125     return newChild->isTextNode() || newChild->hasTagName(HTMLNames::optionTag) || newChild->hasTagName(HTMLNames::hrTag) || newChild->hasTagName(HTMLNames::scriptTag);
       
   126 }
       
   127 
       
   128 void HTMLOptGroupElement::attach()
       
   129 {
       
   130     if (parentNode()->renderStyle()) {
       
   131         RenderStyle* style = styleForRenderer(0);
       
   132         setRenderStyle(style);
       
   133         style->deref(document()->renderArena());
       
   134     }
       
   135     HTMLGenericFormElement::attach();
       
   136 }
       
   137 
       
   138 void HTMLOptGroupElement::detach()
       
   139 {
       
   140     if (m_style) {
       
   141         m_style->deref(document()->renderArena());
       
   142         m_style = 0;
       
   143     }
       
   144     HTMLGenericFormElement::detach();
       
   145 }
       
   146 
       
   147 void HTMLOptGroupElement::setRenderStyle(RenderStyle* newStyle)
       
   148 {
       
   149     RenderStyle* oldStyle = m_style;
       
   150     m_style = newStyle;
       
   151     if (newStyle)
       
   152         newStyle->ref();
       
   153     if (oldStyle)
       
   154         oldStyle->deref(document()->renderArena());
       
   155 }
       
   156 
       
   157 String HTMLOptGroupElement::groupLabelText()
       
   158 {
       
   159     DeprecatedString itemText = getAttribute(labelAttr).deprecatedString();
       
   160     
       
   161     itemText.replace('\\', document()->backslashAsCurrencySymbol());
       
   162     // In WinIE, leading and trailing whitespace is ignored in options and optgroups. We match this behavior.
       
   163     itemText = itemText.stripWhiteSpace();
       
   164     // We want to collapse our whitespace too.  This will match other browsers.
       
   165     itemText = itemText.simplifyWhiteSpace();
       
   166         
       
   167     return itemText;
       
   168 }
       
   169  
       
   170 } // namespace