Changeset 40130 in webkit


Ignore:
Timestamp:
Jan 22, 2009 12:40:18 PM (15 years ago)
Author:
Nikolas Zimmermann
Message:

Reviewed by Eric Seidel.

Fixes: https://bugs.webkit.org/show_bug.cgi?id=23465

Further enhancments to share code between HTMLOptionElement and the upcoming WMLOptionElement.

Rename optionText() to textIndentedToRespectGroupLabel() in (HTML)OptionElement, as it fits better.
optionText() returns the options text prefixed with some spaces, in case it got an optgroup parent.

Add two more pure-virtual functions to OptionElement: setSelectedState(bool) & value().
These aren't used outside of html/ at the moment (unlike the other pure-virtual functions
used by RenderMenuList/RenderListBox) - but they will be used by SelectElement, once it exists.

Location:
trunk/WebCore
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/WebCore/ChangeLog

    r40129 r40130  
     12009-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
    1522009-01-22  Chris Fleizach  <cfleizach@apple.com>
    253
  • trunk/WebCore/dom/OptionElement.cpp

    r40085 r40130  
    2222#include "OptionElement.h"
    2323
     24#include "Document.h"
    2425#include "Element.h"
    2526#include "HTMLNames.h"
    2627#include "HTMLOptionElement.h"
     28#include "OptionGroupElement.h"
     29#include "ScriptElement.h"
    2730#include <wtf/Assertions.h>
    2831
     
    3437
    3538namespace WebCore {
     39
     40void 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
     49String 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
     81String 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
     90String 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
     101OptionElementData::OptionElementData(Element* element)
     102    : m_element(element)
     103    , m_selected(false)
     104{
     105    ASSERT(m_element);
     106}
     107
     108OptionElementData::~OptionElementData()
     109{
     110}
    36111
    37112OptionElement* optionElementForElement(Element* element)
  • trunk/WebCore/dom/OptionElement.h

    r40085 r40130  
    2222#define OptionElement_h
    2323
     24#include "PlatformString.h"
     25
    2426namespace WebCore {
    2527
    2628class Element;
    27 class String;
     29class Document;
     30class OptionElementData;
    2831
    2932class OptionElement {
     
    3235
    3336    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;
    3541
    3642protected:
    3743    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
     53class OptionElementData {
     54public:
     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
     69private:
     70    Element* m_element;
     71    String m_value;
     72    String m_label;
     73    bool m_selected;
    3874};
    3975
  • trunk/WebCore/html/HTMLOptionElement.cpp

    r40085 r40130  
    4343HTMLOptionElement::HTMLOptionElement(const QualifiedName& tagName, Document* doc, HTMLFormElement* f)
    4444    : HTMLFormControlElement(tagName, doc, f)
    45     , m_selected(false)
     45    , m_data(this)
    4646    , m_style(0)
    4747{
     
    8080String HTMLOptionElement::text() const
    8181{
    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());
    10883}
    10984
     
    151126{
    152127    if (attr->name() == selectedAttr)
    153         m_selected = (!attr->isNull());
     128        m_data.setSelected(!attr->isNull());
    154129    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());
    156133    else
    157134        HTMLFormControlElement::parseMappedAttribute(attr);
     
    160137String HTMLOptionElement::value() const
    161138{
    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());
    166140}
    167141
     
    171145}
    172146
     147bool HTMLOptionElement::selected() const
     148{
     149    return m_data.selected();
     150}
     151
    173152void HTMLOptionElement::setSelected(bool selected)
    174153{
    175     if (m_selected == selected)
     154    if (m_data.selected() == selected)
    176155        return;
     156
     157    OptionElement::setSelectedState(m_data, selected);
     158
    177159    if (HTMLSelectElement* select = ownerSelectElement())
    178160        select->setSelectedIndex(selected ? index() : -1, false);
    179     m_selected = selected;
    180161}
    181162
    182163void HTMLOptionElement::setSelectedState(bool selected)
    183164{
    184     if (m_selected == selected)
    185         return;
    186     m_selected = selected;
    187     setChanged();
     165    OptionElement::setSelectedState(m_data, selected);
    188166}
    189167
     
    220198String HTMLOptionElement::label() const
    221199{
    222     return getAttribute(labelAttr);
     200    return m_data.label();
    223201}
    224202
     
    238216}
    239217
    240 String HTMLOptionElement::optionText() const
    241 {
    242     if (parentNode() && parentNode()->hasTagName(optgroupTag))
    243         return "    " + text();
    244 
    245     return text();
     218String HTMLOptionElement::textIndentedToRespectGroupLabel() const
     219{
     220    return OptionElement::collectOptionTextRespectingGroupLabel(m_data, document());
    246221}
    247222
  • trunk/WebCore/html/HTMLOptionElement.h

    r40085 r40130  
    5757    virtual void parseMappedAttribute(MappedAttribute*);
    5858
    59     String value() const;
     59    virtual String value() const;
    6060    void setValue(const String&);
    6161
    62     virtual bool selected() const { return m_selected; }
     62    virtual bool selected() const;
    6363    void setSelected(bool);
    64     void setSelectedState(bool);
     64    virtual void setSelectedState(bool);
    6565
    6666    HTMLSelectElement* ownerSelectElement() const;
     
    7474    void setLabel(const String&);
    7575
    76     virtual String optionText() const;
     76    virtual String textIndentedToRespectGroupLabel() const;
    7777
    7878    virtual bool disabled() const;
     
    8383private:
    8484    virtual RenderStyle* nonRendererRenderStyle() const;
    85    
    86     String m_value;
    87     bool m_selected;
     85
     86    OptionElementData m_data;
    8887    RefPtr<RenderStyle> m_style;
    8988};
  • trunk/WebCore/html/HTMLSelectElement.cpp

    r39603 r40130  
    980980            continue;
    981981
    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)) {
    983984            setSelectedIndex(listToOptionIndex(index));
    984985            if(!usesMenuList())
  • trunk/WebCore/rendering/RenderListBox.cpp

    r40107 r40130  
    106106            Font itemFont = style()->font();
    107107            if (OptionElement* optionElement = optionElementForElement(element))
    108                 text = optionElement->optionText();
     108                text = optionElement->textIndentedToRespectGroupLabel();
    109109            else if (OptionGroupElement* optionGroupElement = optionGroupElementForElement(element)) {
    110110                text = optionGroupElement->groupLabelText();
     
    301301    String itemText;
    302302    if (optionElement)
    303         itemText = optionElement->optionText();
     303        itemText = optionElement->textIndentedToRespectGroupLabel();
    304304    else if (OptionGroupElement* optionGroupElement = optionGroupElementForElement(element))
    305305        itemText = optionGroupElement->groupLabelText();     
  • trunk/WebCore/rendering/RenderMenuList.cpp

    r40107 r40130  
    147147            continue;
    148148
    149         String text = optionElement->optionText();
     149        String text = optionElement->textIndentedToRespectGroupLabel();
    150150        if (!text.isEmpty())
    151151            maxOptionWidth = max(maxOptionWidth, style()->font().floatWidth(text));
     
    183183    if (i >= 0 && i < size) {
    184184        if (OptionElement* optionElement = optionElementForElement(listItems[i]))
    185             text = optionElement->optionText();
     185            text = optionElement->textIndentedToRespectGroupLabel();
    186186    }
    187187
     
    309309        return optionGroupElement->groupLabelText();
    310310    else if (OptionElement* optionElement = optionElementForElement(element))
    311         return optionElement->optionText();
     311        return optionElement->textIndentedToRespectGroupLabel();
    312312    return String();
    313313}
Note: See TracChangeset for help on using the changeset viewer.