Changeset 40130 in webkit
- Timestamp:
- Jan 22, 2009 12:40:18 PM (15 years ago)
- Location:
- trunk/WebCore
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/WebCore/ChangeLog
r40129 r40130 1 2009-01-22 Nikolas Zimmermann <nikolas.zimmermann@torchmobile.com> 2 3 Reviewed by Eric Seidel. 4 5 Fixes: https://bugs.webkit.org/show_bug.cgi?id=23465 6 7 Further enhancments to share code between HTMLOptionElement and the upcoming WMLOptionElement. 8 9 Rename optionText() to textIndentedToRespectGroupLabel() in (HTML)OptionElement, as it fits better. 10 optionText() returns the options text prefixed with some spaces, in case it got an optgroup parent. 11 12 Add two more pure-virtual functions to OptionElement: setSelectedState(bool) & value(). 13 These aren't used outside of html/ at the moment (unlike the other pure-virtual functions 14 used by RenderMenuList/RenderListBox) - but they will be used by SelectElement, once it exists. 15 16 * dom/OptionElement.cpp: 17 (WebCore::OptionElement::setSelectedState): 18 (WebCore::OptionElement::collectOptionText): 19 (WebCore::OptionElement::collectOptionTextRespectingGroupLabel): 20 (WebCore::OptionElement::collectOptionValue): 21 (WebCore::OptionElementData::OptionElementData): 22 (WebCore::OptionElementData::~OptionElementData): 23 * dom/OptionElement.h: 24 (WebCore::OptionElementData::element): 25 (WebCore::OptionElementData::value): 26 (WebCore::OptionElementData::setValue): 27 (WebCore::OptionElementData::label): 28 (WebCore::OptionElementData::setLabel): 29 (WebCore::OptionElementData::selected): 30 (WebCore::OptionElementData::setSelected): 31 * html/HTMLOptionElement.cpp: 32 (WebCore::HTMLOptionElement::HTMLOptionElement): 33 (WebCore::HTMLOptionElement::text): 34 (WebCore::HTMLOptionElement::parseMappedAttribute): 35 (WebCore::HTMLOptionElement::value): 36 (WebCore::HTMLOptionElement::selected): 37 (WebCore::HTMLOptionElement::setSelected): 38 (WebCore::HTMLOptionElement::setSelectedState): 39 (WebCore::HTMLOptionElement::label): 40 (WebCore::HTMLOptionElement::textIndentedToRespectGroupLabel): 41 * html/HTMLOptionElement.h: 42 * html/HTMLSelectElement.cpp: 43 (WebCore::HTMLSelectElement::typeAheadFind): 44 * rendering/RenderListBox.cpp: 45 (WebCore::RenderListBox::updateFromElement): 46 (WebCore::RenderListBox::paintItemForeground): 47 * rendering/RenderMenuList.cpp: 48 (WebCore::RenderMenuList::updateOptionsWidth): 49 (WebCore::RenderMenuList::setTextFromOption): 50 (WebCore::RenderMenuList::itemText): 51 1 52 2009-01-22 Chris Fleizach <cfleizach@apple.com> 2 53 -
trunk/WebCore/dom/OptionElement.cpp
r40085 r40130 22 22 #include "OptionElement.h" 23 23 24 #include "Document.h" 24 25 #include "Element.h" 25 26 #include "HTMLNames.h" 26 27 #include "HTMLOptionElement.h" 28 #include "OptionGroupElement.h" 29 #include "ScriptElement.h" 27 30 #include <wtf/Assertions.h> 28 31 … … 34 37 35 38 namespace WebCore { 39 40 void OptionElement::setSelectedState(OptionElementData& data, bool selected) 41 { 42 if (data.selected() == selected) 43 return; 44 45 data.setSelected(selected); 46 data.element()->setChanged(); 47 } 48 49 String OptionElement::collectOptionText(const OptionElementData& data, Document* document) 50 { 51 String text; 52 53 // WinIE does not use the label attribute, so as a quirk, we ignore it. 54 if (!document->inCompatMode()) 55 text = data.label(); 56 57 if (text.isEmpty()) { 58 Node* n = data.element()->firstChild(); 59 while (n) { 60 if (n->nodeType() == Node::TEXT_NODE || n->nodeType() == Node::CDATA_SECTION_NODE) 61 text += n->nodeValue(); 62 63 // skip script content 64 if (n->isElementNode() && toScriptElement(static_cast<Element*>(n))) 65 n = n->traverseNextSibling(data.element()); 66 else 67 n = n->traverseNextNode(data.element()); 68 } 69 } 70 71 text = document->displayStringModifiedByEncoding(text); 72 73 // In WinIE, leading and trailing whitespace is ignored in options and optgroups. We match this behavior. 74 text = text.stripWhiteSpace(); 75 76 // We want to collapse our whitespace too. This will match other browsers. 77 text = text.simplifyWhiteSpace(); 78 return text; 79 } 80 81 String OptionElement::collectOptionTextRespectingGroupLabel(const OptionElementData& data, Document* document) 82 { 83 Element* parentElement = static_cast<Element*>(data.element()->parentNode()); 84 if (parentElement && optionGroupElementForElement(parentElement)) 85 return " " + collectOptionText(data, document); 86 87 return collectOptionText(data, document); 88 } 89 90 String OptionElement::collectOptionValue(const OptionElementData& data, Document* document) 91 { 92 String value = data.value(); 93 if (!value.isNull()) 94 return value; 95 96 // Use the text if the value wasn't set. 97 return collectOptionText(data, document).stripWhiteSpace(); 98 } 99 100 // OptionElementData 101 OptionElementData::OptionElementData(Element* element) 102 : m_element(element) 103 , m_selected(false) 104 { 105 ASSERT(m_element); 106 } 107 108 OptionElementData::~OptionElementData() 109 { 110 } 36 111 37 112 OptionElement* optionElementForElement(Element* element) -
trunk/WebCore/dom/OptionElement.h
r40085 r40130 22 22 #define OptionElement_h 23 23 24 #include "PlatformString.h" 25 24 26 namespace WebCore { 25 27 26 28 class Element; 27 class String; 29 class Document; 30 class OptionElementData; 28 31 29 32 class OptionElement { … … 32 35 33 36 virtual bool selected() const = 0; 34 virtual String optionText() const = 0; 37 virtual void setSelectedState(bool) = 0; 38 39 virtual String textIndentedToRespectGroupLabel() const = 0; 40 virtual String value() const = 0; 35 41 36 42 protected: 37 43 OptionElement() { } 44 45 static void setSelectedState(OptionElementData&, bool selected); 46 static String collectOptionText(const OptionElementData&, Document*); 47 static String collectOptionTextRespectingGroupLabel(const OptionElementData&, Document*); 48 static String collectOptionValue(const OptionElementData&, Document*); 49 }; 50 51 // HTML/WMLOptionElement hold this struct as member variable 52 // and pass it to the static helper functions in OptionElement 53 class OptionElementData { 54 public: 55 OptionElementData(Element*); 56 ~OptionElementData(); 57 58 Element* element() const { return m_element; } 59 60 String value() const { return m_value; } 61 void setValue(const String& value) { m_value = value; } 62 63 String label() const { return m_label; } 64 void setLabel(const String& label) { m_label = label; } 65 66 bool selected() const { return m_selected; } 67 void setSelected(bool selected) { m_selected = selected; } 68 69 private: 70 Element* m_element; 71 String m_value; 72 String m_label; 73 bool m_selected; 38 74 }; 39 75 -
trunk/WebCore/html/HTMLOptionElement.cpp
r40085 r40130 43 43 HTMLOptionElement::HTMLOptionElement(const QualifiedName& tagName, Document* doc, HTMLFormElement* f) 44 44 : HTMLFormControlElement(tagName, doc, f) 45 , m_ selected(false)45 , m_data(this) 46 46 , m_style(0) 47 47 { … … 80 80 String HTMLOptionElement::text() const 81 81 { 82 String text; 83 84 // WinIE does not use the label attribute, so as a quirk, we ignore it. 85 if (!document()->inCompatMode()) 86 text = getAttribute(labelAttr); 87 88 if (text.isEmpty()) { 89 const Node* n = firstChild(); 90 while (n) { 91 if (n->nodeType() == TEXT_NODE || n->nodeType() == CDATA_SECTION_NODE) 92 text += n->nodeValue(); 93 // skip script content 94 if (n->isElementNode() && n->hasTagName(HTMLNames::scriptTag)) 95 n = n->traverseNextSibling(this); 96 else 97 n = n->traverseNextNode(this); 98 } 99 } 100 101 text = document()->displayStringModifiedByEncoding(text); 102 // In WinIE, leading and trailing whitespace is ignored in options and optgroups. We match this behavior. 103 text = text.stripWhiteSpace(); 104 // We want to collapse our whitespace too. This will match other browsers. 105 text = text.simplifyWhiteSpace(); 106 107 return text; 82 return OptionElement::collectOptionText(m_data, document()); 108 83 } 109 84 … … 151 126 { 152 127 if (attr->name() == selectedAttr) 153 m_ selected =(!attr->isNull());128 m_data.setSelected(!attr->isNull()); 154 129 else if (attr->name() == valueAttr) 155 m_value = attr->value(); 130 m_data.setValue(attr->value()); 131 else if (attr->name() == labelAttr) 132 m_data.setLabel(attr->value()); 156 133 else 157 134 HTMLFormControlElement::parseMappedAttribute(attr); … … 160 137 String HTMLOptionElement::value() const 161 138 { 162 if ( !m_value.isNull() ) 163 return m_value; 164 // Use the text if the value wasn't set. 165 return text().stripWhiteSpace(); 139 return OptionElement::collectOptionValue(m_data, document()); 166 140 } 167 141 … … 171 145 } 172 146 147 bool HTMLOptionElement::selected() const 148 { 149 return m_data.selected(); 150 } 151 173 152 void HTMLOptionElement::setSelected(bool selected) 174 153 { 175 if (m_ selected== selected)154 if (m_data.selected() == selected) 176 155 return; 156 157 OptionElement::setSelectedState(m_data, selected); 158 177 159 if (HTMLSelectElement* select = ownerSelectElement()) 178 160 select->setSelectedIndex(selected ? index() : -1, false); 179 m_selected = selected;180 161 } 181 162 182 163 void HTMLOptionElement::setSelectedState(bool selected) 183 164 { 184 if (m_selected == selected) 185 return; 186 m_selected = selected; 187 setChanged(); 165 OptionElement::setSelectedState(m_data, selected); 188 166 } 189 167 … … 220 198 String HTMLOptionElement::label() const 221 199 { 222 return getAttribute(labelAttr);200 return m_data.label(); 223 201 } 224 202 … … 238 216 } 239 217 240 String HTMLOptionElement::optionText() const 241 { 242 if (parentNode() && parentNode()->hasTagName(optgroupTag)) 243 return " " + text(); 244 245 return text(); 218 String HTMLOptionElement::textIndentedToRespectGroupLabel() const 219 { 220 return OptionElement::collectOptionTextRespectingGroupLabel(m_data, document()); 246 221 } 247 222 -
trunk/WebCore/html/HTMLOptionElement.h
r40085 r40130 57 57 virtual void parseMappedAttribute(MappedAttribute*); 58 58 59 String value() const;59 virtual String value() const; 60 60 void setValue(const String&); 61 61 62 virtual bool selected() const { return m_selected; }62 virtual bool selected() const; 63 63 void setSelected(bool); 64 v oid setSelectedState(bool);64 virtual void setSelectedState(bool); 65 65 66 66 HTMLSelectElement* ownerSelectElement() const; … … 74 74 void setLabel(const String&); 75 75 76 virtual String optionText() const;76 virtual String textIndentedToRespectGroupLabel() const; 77 77 78 78 virtual bool disabled() const; … … 83 83 private: 84 84 virtual RenderStyle* nonRendererRenderStyle() const; 85 86 String m_value; 87 bool m_selected; 85 86 OptionElementData m_data; 88 87 RefPtr<RenderStyle> m_style; 89 88 }; -
trunk/WebCore/html/HTMLSelectElement.cpp
r39603 r40130 980 980 continue; 981 981 982 if (stripLeadingWhiteSpace(static_cast<HTMLOptionElement*>(items[index])->optionText()).startsWith(prefix, false)) { 982 String text = static_cast<HTMLOptionElement*>(items[index])->textIndentedToRespectGroupLabel(); 983 if (stripLeadingWhiteSpace(text).startsWith(prefix, false)) { 983 984 setSelectedIndex(listToOptionIndex(index)); 984 985 if(!usesMenuList()) -
trunk/WebCore/rendering/RenderListBox.cpp
r40107 r40130 106 106 Font itemFont = style()->font(); 107 107 if (OptionElement* optionElement = optionElementForElement(element)) 108 text = optionElement-> optionText();108 text = optionElement->textIndentedToRespectGroupLabel(); 109 109 else if (OptionGroupElement* optionGroupElement = optionGroupElementForElement(element)) { 110 110 text = optionGroupElement->groupLabelText(); … … 301 301 String itemText; 302 302 if (optionElement) 303 itemText = optionElement-> optionText();303 itemText = optionElement->textIndentedToRespectGroupLabel(); 304 304 else if (OptionGroupElement* optionGroupElement = optionGroupElementForElement(element)) 305 305 itemText = optionGroupElement->groupLabelText(); -
trunk/WebCore/rendering/RenderMenuList.cpp
r40107 r40130 147 147 continue; 148 148 149 String text = optionElement-> optionText();149 String text = optionElement->textIndentedToRespectGroupLabel(); 150 150 if (!text.isEmpty()) 151 151 maxOptionWidth = max(maxOptionWidth, style()->font().floatWidth(text)); … … 183 183 if (i >= 0 && i < size) { 184 184 if (OptionElement* optionElement = optionElementForElement(listItems[i])) 185 text = optionElement-> optionText();185 text = optionElement->textIndentedToRespectGroupLabel(); 186 186 } 187 187 … … 309 309 return optionGroupElement->groupLabelText(); 310 310 else if (OptionElement* optionElement = optionElementForElement(element)) 311 return optionElement-> optionText();311 return optionElement->textIndentedToRespectGroupLabel(); 312 312 return String(); 313 313 }
Note: See TracChangeset
for help on using the changeset viewer.