diff options
author | Konstantin Tokarev <annulen@yandex.ru> | 2016-08-25 19:20:41 +0300 |
---|---|---|
committer | Konstantin Tokarev <annulen@yandex.ru> | 2017-02-02 12:30:55 +0000 |
commit | 6882a04fb36642862b11efe514251d32070c3d65 (patch) | |
tree | b7959826000b061fd5ccc7512035c7478742f7b0 /Source/WebCore/html/HTMLOptionElement.cpp | |
parent | ab6df191029eeeb0b0f16f127d553265659f739e (diff) | |
download | qtwebkit-6882a04fb36642862b11efe514251d32070c3d65.tar.gz |
Imported QtWebKit TP3 (git b57bc6801f1876c3220d5a4bfea33d620d477443)
Change-Id: I3b1d8a2808782c9f34d50240000e20cb38d3680f
Reviewed-by: Konstantin Tokarev <annulen@yandex.ru>
Diffstat (limited to 'Source/WebCore/html/HTMLOptionElement.cpp')
-rw-r--r-- | Source/WebCore/html/HTMLOptionElement.cpp | 152 |
1 files changed, 52 insertions, 100 deletions
diff --git a/Source/WebCore/html/HTMLOptionElement.cpp b/Source/WebCore/html/HTMLOptionElement.cpp index bfa9a9f00..baa49d213 100644 --- a/Source/WebCore/html/HTMLOptionElement.cpp +++ b/Source/WebCore/html/HTMLOptionElement.cpp @@ -27,7 +27,6 @@ #include "config.h" #include "HTMLOptionElement.h" -#include "Attribute.h" #include "Document.h" #include "ExceptionCode.h" #include "HTMLDataListElement.h" @@ -36,50 +35,48 @@ #include "HTMLParserIdioms.h" #include "HTMLSelectElement.h" #include "NodeRenderStyle.h" -#include "NodeRenderingContext.h" #include "NodeTraversal.h" #include "RenderMenuList.h" #include "RenderTheme.h" #include "ScriptElement.h" #include "StyleResolver.h" #include "Text.h" -#include <wtf/Vector.h> -#include <wtf/text/StringBuilder.h> +#include <wtf/Ref.h> namespace WebCore { using namespace HTMLNames; -HTMLOptionElement::HTMLOptionElement(const QualifiedName& tagName, Document* document) +HTMLOptionElement::HTMLOptionElement(const QualifiedName& tagName, Document& document) : HTMLElement(tagName, document) , m_disabled(false) , m_isSelected(false) { ASSERT(hasTagName(optionTag)); - setHasCustomStyleCallbacks(); + setHasCustomStyleResolveCallbacks(); } -PassRefPtr<HTMLOptionElement> HTMLOptionElement::create(Document* document) +Ref<HTMLOptionElement> HTMLOptionElement::create(Document& document) { - return adoptRef(new HTMLOptionElement(optionTag, document)); + return adoptRef(*new HTMLOptionElement(optionTag, document)); } -PassRefPtr<HTMLOptionElement> HTMLOptionElement::create(const QualifiedName& tagName, Document* document) +Ref<HTMLOptionElement> HTMLOptionElement::create(const QualifiedName& tagName, Document& document) { - return adoptRef(new HTMLOptionElement(tagName, document)); + return adoptRef(*new HTMLOptionElement(tagName, document)); } -PassRefPtr<HTMLOptionElement> HTMLOptionElement::createForJSConstructor(Document* document, const String& data, const String& value, +RefPtr<HTMLOptionElement> HTMLOptionElement::createForJSConstructor(Document& document, const String& data, const String& value, bool defaultSelected, bool selected, ExceptionCode& ec) { RefPtr<HTMLOptionElement> element = adoptRef(new HTMLOptionElement(optionTag, document)); - RefPtr<Text> text = Text::create(document, data.isNull() ? "" : data); + Ref<Text> text = Text::create(document, data.isNull() ? "" : data); ec = 0; - element->appendChild(text.release(), ec); + element->appendChild(WTFMove(text), ec); if (ec) - return 0; + return nullptr; if (!value.isNull()) element->setValue(value); @@ -87,54 +84,30 @@ PassRefPtr<HTMLOptionElement> HTMLOptionElement::createForJSConstructor(Document element->setAttribute(selectedAttr, emptyAtom); element->setSelected(selected); - return element.release(); -} - -void HTMLOptionElement::attach(const AttachContext& context) -{ - HTMLElement::attach(context); - // If after attaching nothing called styleForRenderer() on this node we - // manually cache the value. This happens if our parent doesn't have a - // renderer like <optgroup> or if it doesn't allow children like <select>. - if (!m_style && parentNode()->renderStyle()) - updateNonRenderStyle(); -} - -void HTMLOptionElement::detach(const AttachContext& context) -{ - m_style.clear(); - HTMLElement::detach(context); + return element; } bool HTMLOptionElement::isFocusable() const { - // Option elements do not have a renderer so we check the renderStyle instead. - return supportsFocus() && renderStyle() && renderStyle()->display() != NONE; + if (!supportsFocus()) + return false; + // Option elements do not have a renderer. + auto* style = const_cast<HTMLOptionElement&>(*this).computedStyle(); + return style && style->display() != NONE; } String HTMLOptionElement::text() const { - Document* document = this->document(); - String text; - - // WinIE does not use the label attribute, so as a quirk, we ignore it. - if (!document->inQuirksMode()) - text = fastGetAttribute(labelAttr); - - // FIXME: The following treats an element with the label attribute set to - // the empty string the same as an element with no label attribute at all. - // Is that correct? If it is, then should the label function work the same way? - if (text.isEmpty()) - text = collectOptionInnerText(); + String text = collectOptionInnerText(); // FIXME: Is displayStringModifiedByEncoding helpful here? // If it's correct here, then isn't it needed in the value and label functions too? - return document->displayStringModifiedByEncoding(text).stripWhiteSpace(isHTMLSpace).simplifyWhiteSpace(isHTMLSpace); + return document().displayStringModifiedByEncoding(text).stripWhiteSpace(isHTMLSpace).simplifyWhiteSpace(isHTMLSpace); } void HTMLOptionElement::setText(const String &text, ExceptionCode& ec) { - RefPtr<Node> protectFromMutationEvents(this); + Ref<HTMLOptionElement> protectFromMutationEvents(*this); // Changing the text causes a recalc of a select's items, which will reset the selected // index to the first item if the select is single selection with a menu list. We attempt to @@ -145,8 +118,8 @@ void HTMLOptionElement::setText(const String &text, ExceptionCode& ec) // Handle the common special case where there's exactly 1 child node, and it's a text node. Node* child = firstChild(); - if (child && child->isTextNode() && !child->nextSibling()) - toText(child)->setData(text, ec); + if (is<Text>(child) && !child->nextSibling()) + downcast<Text>(*child).setData(text); else { removeChildren(); appendChild(Text::create(document(), text), ec); @@ -173,12 +146,10 @@ int HTMLOptionElement::index() const int optionIndex = 0; - const Vector<HTMLElement*>& items = selectElement->listItems(); - size_t length = items.size(); - for (size_t i = 0; i < length; ++i) { - if (!isHTMLOptionElement(items[i])) + for (auto& item : selectElement->listItems()) { + if (!is<HTMLOptionElement>(*item)) continue; - if (items[i] == this) + if (item == this) return optionIndex; ++optionIndex; } @@ -198,9 +169,9 @@ void HTMLOptionElement::parseAttribute(const QualifiedName& name, const AtomicSt bool oldDisabled = m_disabled; m_disabled = !value.isNull(); if (oldDisabled != m_disabled) { - didAffectSelector(AffectedSelectorDisabled | AffectedSelectorEnabled); - if (renderer() && renderer()->style()->hasAppearance()) - renderer()->theme()->stateChanged(renderer(), EnabledState); + setNeedsStyleRecalc(); + if (renderer() && renderer()->style().hasAppearance()) + renderer()->theme().stateChanged(*renderer(), ControlStates::EnabledState); } } else if (name == selectedAttr) { // FIXME: This doesn't match what the HTML specification says. @@ -251,13 +222,13 @@ void HTMLOptionElement::setSelectedState(bool selected) return; m_isSelected = selected; - didAffectSelector(AffectedSelectorChecked); + setNeedsStyleRecalc(); if (HTMLSelectElement* select = ownerSelectElement()) select->invalidateSelectedItems(); } -void HTMLOptionElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta) +void HTMLOptionElement::childrenChanged(const ChildChange& change) { #if ENABLE(DATALIST_ELEMENT) if (HTMLDataListElement* dataList = ownerDataListElement()) @@ -266,37 +237,37 @@ void HTMLOptionElement::childrenChanged(bool changedByParser, Node* beforeChange #endif if (HTMLSelectElement* select = ownerSelectElement()) select->optionElementChildrenChanged(); - HTMLElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta); + HTMLElement::childrenChanged(change); } #if ENABLE(DATALIST_ELEMENT) HTMLDataListElement* HTMLOptionElement::ownerDataListElement() const { for (ContainerNode* parent = parentNode(); parent ; parent = parent->parentNode()) { - if (parent->hasTagName(datalistTag)) - return static_cast<HTMLDataListElement*>(parent); + if (is<HTMLDataListElement>(*parent)) + return downcast<HTMLDataListElement>(parent); } - return 0; + return nullptr; } #endif HTMLSelectElement* HTMLOptionElement::ownerSelectElement() const { ContainerNode* select = parentNode(); - while (select && !select->hasTagName(selectTag)) + while (select && !is<HTMLSelectElement>(*select)) select = select->parentNode(); if (!select) - return 0; + return nullptr; - return toHTMLSelectElement(select); + return downcast<HTMLSelectElement>(select); } String HTMLOptionElement::label() const { - const AtomicString& label = fastGetAttribute(labelAttr); + String label = fastGetAttribute(labelAttr); if (!label.isNull()) - return label; + return label.stripWhiteSpace(isHTMLSpace); return collectOptionInnerText().stripWhiteSpace(isHTMLSpace).simplifyWhiteSpace(isHTMLSpace); } @@ -305,30 +276,12 @@ void HTMLOptionElement::setLabel(const String& label) setAttribute(labelAttr, label); } -void HTMLOptionElement::updateNonRenderStyle() -{ - m_style = document()->ensureStyleResolver()->styleForElement(this); -} - -RenderStyle* HTMLOptionElement::nonRendererStyle() const -{ - return m_style.get(); -} - -PassRefPtr<RenderStyle> HTMLOptionElement::customStyleForRenderer() -{ - // styleForRenderer is called whenever a new style should be associated - // with an Element so now is a good time to update our cached style. - updateNonRenderStyle(); - return m_style; -} - -void HTMLOptionElement::didRecalcStyle(StyleChange) +void HTMLOptionElement::willResetComputedStyle() { // FIXME: This is nasty, we ask our owner select to repaint even if the new // style is exactly the same. - if (HTMLSelectElement* select = ownerSelectElement()) { - if (RenderObject* renderer = select->renderer()) + if (auto select = ownerSelectElement()) { + if (auto renderer = select->renderer()) renderer->repaint(); } } @@ -336,9 +289,9 @@ void HTMLOptionElement::didRecalcStyle(StyleChange) String HTMLOptionElement::textIndentedToRespectGroupLabel() const { ContainerNode* parent = parentNode(); - if (parent && isHTMLOptGroupElement(parent)) - return " " + text(); - return text(); + if (is<HTMLOptGroupElement>(parent)) + return " " + label(); + return label(); } bool HTMLOptionElement::isDisabledFormControl() const @@ -346,14 +299,13 @@ bool HTMLOptionElement::isDisabledFormControl() const if (ownElementDisabled()) return true; - if (!parentNode() || !parentNode()->isHTMLElement()) + if (!is<HTMLOptGroupElement>(parentNode())) return false; - HTMLElement* parentElement = static_cast<HTMLElement*>(parentNode()); - return isHTMLOptGroupElement(parentElement) && parentElement->isDisabledFormControl(); + return downcast<HTMLOptGroupElement>(*parentNode()).isDisabledFormControl(); } -Node::InsertionNotificationRequest HTMLOptionElement::insertedInto(ContainerNode* insertionPoint) +Node::InsertionNotificationRequest HTMLOptionElement::insertedInto(ContainerNode& insertionPoint) { if (HTMLSelectElement* select = ownerSelectElement()) { select->setRecalcListItems(); @@ -373,13 +325,13 @@ String HTMLOptionElement::collectOptionInnerText() const { StringBuilder text; for (Node* node = firstChild(); node; ) { - if (node->isTextNode()) + if (is<Text>(*node)) text.append(node->nodeValue()); // Text nodes inside script elements are not part of the option text. - if (node->isElementNode() && toScriptElementIfPossible(toElement(node))) - node = NodeTraversal::nextSkippingChildren(node, this); + if (is<Element>(*node) && toScriptElementIfPossible(downcast<Element>(node))) + node = NodeTraversal::nextSkippingChildren(*node, this); else - node = NodeTraversal::next(node, this); + node = NodeTraversal::next(*node, this); } return text.toString(); } |