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(const QualifiedName& tagName, Document* document) 39 : HTMLElement(tagName, document) 40 { 41 ASSERT(hasTagName(optgroupTag)); 42 setHasCustomStyleCallbacks(); 43 ScriptWrappable::init(this); 44 } 45 46 PassRefPtr<HTMLOptGroupElement> HTMLOptGroupElement::create(const QualifiedName& tagName, Document* document) 47 { 48 return adoptRef(new HTMLOptGroupElement(tagName, document)); 49 } 50 51 bool HTMLOptGroupElement::isDisabledFormControl() const 52 { 53 return fastHasAttribute(disabledAttr); 54 } 55 56 bool HTMLOptGroupElement::rendererIsFocusable() const 57 { 58 // Optgroup elements do not have a renderer so we check the renderStyle instead. 59 return renderStyle() && renderStyle()->display() != NONE; 60 } 61 62 const AtomicString& HTMLOptGroupElement::formControlType() const 63 { 64 DEFINE_STATIC_LOCAL(const AtomicString, optgroup, ("optgroup", AtomicString::ConstructFromLiteral)); 65 return optgroup; 66 } 67 68 void HTMLOptGroupElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta) 69 { 70 recalcSelectOptions(); 71 HTMLElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta); 72 } 73 74 void HTMLOptGroupElement::parseAttribute(const QualifiedName& name, const AtomicString& value) 75 { 76 HTMLElement::parseAttribute(name, value); 77 recalcSelectOptions(); 78 79 if (name == disabledAttr) 80 didAffectSelector(AffectedSelectorDisabled | AffectedSelectorEnabled); 81 } 82 83 void HTMLOptGroupElement::recalcSelectOptions() 84 { 85 ContainerNode* select = parentNode(); 86 while (select && !select->hasTagName(selectTag)) 87 select = select->parentNode(); 88 if (select) 89 toHTMLSelectElement(select)->setRecalcListItems(); 90 } 91 92 void HTMLOptGroupElement::attach(const AttachContext& context) 93 { 94 HTMLElement::attach(context); 95 // If after attaching nothing called styleForRenderer() on this node we 96 // manually cache the value. This happens if our parent doesn't have a 97 // renderer like <optgroup> or if it doesn't allow children like <select>. 98 if (!m_style && parentNode()->renderStyle()) 99 updateNonRenderStyle(); 100 } 101 102 void HTMLOptGroupElement::detach(const AttachContext& context) 103 { 104 m_style.clear(); 105 HTMLElement::detach(context); 106 } 107 108 void HTMLOptGroupElement::updateNonRenderStyle() 109 { 110 m_style = originalStyleForRenderer(); 111 } 112 113 RenderStyle* HTMLOptGroupElement::nonRendererStyle() const 114 { 115 return m_style.get(); 116 } 117 118 PassRefPtr<RenderStyle> HTMLOptGroupElement::customStyleForRenderer() 119 { 120 // styleForRenderer is called whenever a new style should be associated 121 // with an Element so now is a good time to update our cached style. 122 updateNonRenderStyle(); 123 return m_style; 124 } 125 126 String HTMLOptGroupElement::groupLabelText() const 127 { 128 String itemText = document()->displayStringModifiedByEncoding(getAttribute(labelAttr)); 129 130 // In WinIE, leading and trailing whitespace is ignored in options and optgroups. We match this behavior. 131 itemText = itemText.stripWhiteSpace(); 132 // We want to collapse our whitespace too. This will match other browsers. 133 itemText = itemText.simplifyWhiteSpace(); 134 135 return itemText; 136 } 137 138 HTMLSelectElement* HTMLOptGroupElement::ownerSelectElement() const 139 { 140 ContainerNode* select = parentNode(); 141 while (select && !select->hasTagName(selectTag)) 142 select = select->parentNode(); 143 144 if (!select) 145 return 0; 146 147 return toHTMLSelectElement(select); 148 } 149 150 void HTMLOptGroupElement::accessKeyAction(bool) 151 { 152 HTMLSelectElement* select = ownerSelectElement(); 153 // send to the parent to bring focus to the list box 154 if (select && !select->focused()) 155 select->accessKeyAction(false); 156 } 157 158 } // namespace 159