1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
/*
* Copyright (C) 2010, 2011 Nokia Corporation and/or its subsidiary(-ies)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#include "core/html/HTMLDetailsElement.h"
#include "bindings/core/v8/ExceptionState.h"
#include "core/CSSPropertyNames.h"
#include "core/CSSValueKeywords.h"
#include "core/HTMLNames.h"
#include "core/dom/ElementTraversal.h"
#include "core/dom/TaskRunnerHelper.h"
#include "core/dom/Text.h"
#include "core/dom/shadow/ShadowRoot.h"
#include "core/events/Event.h"
#include "core/frame/UseCounter.h"
#include "core/html/HTMLContentElement.h"
#include "core/html/HTMLDivElement.h"
#include "core/html/HTMLSummaryElement.h"
#include "core/html/shadow/DetailsMarkerControl.h"
#include "core/html/shadow/ShadowElementNames.h"
#include "core/layout/LayoutBlockFlow.h"
#include "platform/text/PlatformLocale.h"
namespace blink {
using namespace HTMLNames;
class FirstSummarySelectFilter final : public HTMLContentSelectFilter {
public:
virtual ~FirstSummarySelectFilter() {}
static FirstSummarySelectFilter* create() {
return new FirstSummarySelectFilter();
}
bool canSelectNode(const HeapVector<Member<Node>, 32>& siblings,
int nth) const override {
if (!siblings[nth]->hasTagName(HTMLNames::summaryTag))
return false;
for (int i = nth - 1; i >= 0; --i) {
if (siblings[i]->hasTagName(HTMLNames::summaryTag))
return false;
}
return true;
}
DEFINE_INLINE_VIRTUAL_TRACE() { HTMLContentSelectFilter::trace(visitor); }
private:
FirstSummarySelectFilter() {}
};
HTMLDetailsElement* HTMLDetailsElement::create(Document& document) {
HTMLDetailsElement* details = new HTMLDetailsElement(document);
details->ensureUserAgentShadowRoot();
return details;
}
HTMLDetailsElement::HTMLDetailsElement(Document& document)
: HTMLElement(detailsTag, document), m_isOpen(false) {
UseCounter::count(document, UseCounter::DetailsElement);
}
HTMLDetailsElement::~HTMLDetailsElement() {}
void HTMLDetailsElement::dispatchPendingEvent() {
dispatchEvent(Event::create(EventTypeNames::toggle));
}
LayoutObject* HTMLDetailsElement::createLayoutObject(const ComputedStyle&) {
return new LayoutBlockFlow(this);
}
void HTMLDetailsElement::didAddUserAgentShadowRoot(ShadowRoot& root) {
HTMLSummaryElement* defaultSummary = HTMLSummaryElement::create(document());
defaultSummary->appendChild(Text::create(
document(), locale().queryString(WebLocalizedString::DetailsLabel)));
HTMLContentElement* summary = HTMLContentElement::create(
document(), FirstSummarySelectFilter::create());
summary->setIdAttribute(ShadowElementNames::detailsSummary());
summary->appendChild(defaultSummary);
root.appendChild(summary);
HTMLDivElement* content = HTMLDivElement::create(document());
content->setIdAttribute(ShadowElementNames::detailsContent());
content->appendChild(HTMLContentElement::create(document()));
content->setInlineStyleProperty(CSSPropertyDisplay, CSSValueNone);
root.appendChild(content);
}
Element* HTMLDetailsElement::findMainSummary() const {
if (HTMLSummaryElement* summary =
Traversal<HTMLSummaryElement>::firstChild(*this))
return summary;
HTMLContentElement* content =
toHTMLContentElementOrDie(userAgentShadowRoot()->firstChild());
DCHECK(content->firstChild());
CHECK(isHTMLSummaryElement(*content->firstChild()));
return toElement(content->firstChild());
}
void HTMLDetailsElement::parseAttribute(
const AttributeModificationParams& params) {
if (params.name == openAttr) {
bool oldValue = m_isOpen;
m_isOpen = !params.newValue.isNull();
if (m_isOpen == oldValue)
return;
// Dispatch toggle event asynchronously.
m_pendingEvent =
TaskRunnerHelper::get(TaskType::DOMManipulation, &document())
->postCancellableTask(
BLINK_FROM_HERE,
WTF::bind(&HTMLDetailsElement::dispatchPendingEvent,
wrapPersistent(this)));
Element* content = ensureUserAgentShadowRoot().getElementById(
ShadowElementNames::detailsContent());
DCHECK(content);
if (m_isOpen)
content->removeInlineStyleProperty(CSSPropertyDisplay);
else
content->setInlineStyleProperty(CSSPropertyDisplay, CSSValueNone);
// Invalidate the LayoutDetailsMarker in order to turn the arrow signifying
// if the details element is open or closed.
Element* summary = findMainSummary();
DCHECK(summary);
Element* control = toHTMLSummaryElement(summary)->markerControl();
if (control && control->layoutObject())
control->layoutObject()->setShouldDoFullPaintInvalidation();
return;
}
HTMLElement::parseAttribute(params);
}
void HTMLDetailsElement::toggleOpen() {
setAttribute(openAttr, m_isOpen ? nullAtom : emptyAtom);
}
bool HTMLDetailsElement::isInteractiveContent() const {
return true;
}
} // namespace blink
|