1 /* 2 * Copyright (C) 1999 Lars Knoll (knoll (at) kde.org) 3 * (C) 1999 Antti Koivisto (koivisto (at) kde.org) 4 * (C) 2001 Dirk Mueller (mueller (at) kde.org) 5 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2010 Apple Inc. All rights reserved. 6 * (C) 2006 Alexey Proskuryakov (ap (at) nypop.com) 7 * 8 * This library is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU Library General Public 10 * License as published by the Free Software Foundation; either 11 * version 2 of the License, or (at your option) any later version. 12 * 13 * This library is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 * Library General Public License for more details. 17 * 18 * You should have received a copy of the GNU Library General Public License 19 * along with this library; see the file COPYING.LIB. If not, write to 20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 21 * Boston, MA 02110-1301, USA. 22 * 23 */ 24 25 #include "config.h" 26 #include "core/html/HTMLOptGroupElement.h" 27 28 #include "HTMLNames.h" 29 #include "core/dom/Document.h" 30 #include "core/dom/NodeRenderStyle.h" 31 #include "core/html/HTMLSelectElement.h" 32 #include "wtf/StdLibExtras.h" 33 34 namespace WebCore { 35 36 using namespace HTMLNames; 37 38 inline HTMLOptGroupElement::HTMLOptGroupElement(Document& document) 39 : HTMLElement(optgroupTag, document) 40 { 41 setHasCustomStyleCallbacks(); 42 ScriptWrappable::init(this); 43 } 44 45 PassRefPtr<HTMLOptGroupElement> HTMLOptGroupElement::create(Document& document) 46 { 47 return adoptRef(new HTMLOptGroupElement(document)); 48 } 49 50 bool HTMLOptGroupElement::isDisabledFormControl() const 51 { 52 return fastHasAttribute(disabledAttr); 53 } 54 55 bool HTMLOptGroupElement::rendererIsFocusable() const 56 { 57 // Optgroup elements do not have a renderer so we check the renderStyle instead. 58 return renderStyle() && renderStyle()->display() != NONE; 59 } 60 61 const AtomicString& HTMLOptGroupElement::formControlType() const 62 { 63 DEFINE_STATIC_LOCAL(const AtomicString, optgroup, ("optgroup", AtomicString::ConstructFromLiteral)); 64 return optgroup; 65 } 66 67 void HTMLOptGroupElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta) 68 { 69 recalcSelectOptions(); 70 HTMLElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta); 71 } 72 73 void HTMLOptGroupElement::parseAttribute(const QualifiedName& name, const AtomicString& value) 74 { 75 HTMLElement::parseAttribute(name, value); 76 recalcSelectOptions(); 77 78 if (name == disabledAttr) 79 didAffectSelector(AffectedSelectorDisabled | AffectedSelectorEnabled); 80 } 81 82 void HTMLOptGroupElement::recalcSelectOptions() 83 { 84 ContainerNode* select = parentNode(); 85 while (select && !select->hasTagName(selectTag)) 86 select = select->parentNode(); 87 if (select) 88 toHTMLSelectElement(select)->setRecalcListItems(); 89 } 90 91 void HTMLOptGroupElement::attach(const AttachContext& context) 92 { 93 HTMLElement::attach(context); 94 // If after attaching nothing called styleForRenderer() on this node we 95 // manually cache the value. This happens if our parent doesn't have a 96 // renderer like <optgroup> or if it doesn't allow children like <select>. 97 if (!m_style && parentNode()->renderStyle()) 98 updateNonRenderStyle(); 99 } 100 101 void HTMLOptGroupElement::detach(const AttachContext& context) 102 { 103 m_style.clear(); 104 HTMLElement::detach(context); 105 } 106 107 void HTMLOptGroupElement::updateNonRenderStyle() 108 { 109 m_style = originalStyleForRenderer(); 110 } 111 112 RenderStyle* HTMLOptGroupElement::nonRendererStyle() const 113 { 114 return m_style.get(); 115 } 116 117 PassRefPtr<RenderStyle> HTMLOptGroupElement::customStyleForRenderer() 118 { 119 // styleForRenderer is called whenever a new style should be associated 120 // with an Element so now is a good time to update our cached style. 121 updateNonRenderStyle(); 122 return m_style; 123 } 124 125 String HTMLOptGroupElement::groupLabelText() const 126 { 127 String itemText = getAttribute(labelAttr); 128 129 // In WinIE, leading and trailing whitespace is ignored in options and optgroups. We match this behavior. 130 itemText = itemText.stripWhiteSpace(); 131 // We want to collapse our whitespace too. This will match other browsers. 132 itemText = itemText.simplifyWhiteSpace(); 133 134 return itemText; 135 } 136 137 HTMLSelectElement* HTMLOptGroupElement::ownerSelectElement() const 138 { 139 ContainerNode* select = parentNode(); 140 while (select && !select->hasTagName(selectTag)) 141 select = select->parentNode(); 142 143 if (!select) 144 return 0; 145 146 return toHTMLSelectElement(select); 147 } 148 149 void HTMLOptGroupElement::accessKeyAction(bool) 150 { 151 HTMLSelectElement* select = ownerSelectElement(); 152 // send to the parent to bring focus to the list box 153 if (select && !select->focused()) 154 select->accessKeyAction(false); 155 } 156 157 } // namespace 158