2
* Copyright (C) 2010, 2011 Nokia Corporation and/or its subsidiary(-ies)
4
* This library is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU Library General Public
6
* License as published by the Free Software Foundation; either
7
* version 2 of the License, or (at your option) any later version.
9
* This library is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
* Library General Public License for more details.
14
* You should have received a copy of the GNU Library General Public License
15
* along with this library; see the file COPYING.LIB. If not, write to
16
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17
* Boston, MA 02110-1301, USA.
22
#include "HTMLDetailsElement.h"
24
#if ENABLE(DETAILS_ELEMENT)
25
#include "ElementShadow.h"
26
#include "HTMLContentElement.h"
27
#include "HTMLNames.h"
28
#include "HTMLSummaryElement.h"
29
#include "LocalizedStrings.h"
30
#include "MouseEvent.h"
31
#include "NodeRenderingContext.h"
32
#include "RenderBlock.h"
33
#include "ShadowRoot.h"
38
using namespace HTMLNames;
40
static const AtomicString& summaryQuerySelector()
42
DEFINE_STATIC_LOCAL(AtomicString, selector, ("summary:first-of-type", AtomicString::ConstructFromLiteral));
46
class DetailsContentElement : public HTMLContentElement {
48
static PassRefPtr<DetailsContentElement> create(Document*);
51
DetailsContentElement(Document* document)
52
: HTMLContentElement(HTMLNames::webkitShadowContentTag, document)
57
PassRefPtr<DetailsContentElement> DetailsContentElement::create(Document* document)
59
return adoptRef(new DetailsContentElement(document));
62
class DetailsSummaryElement : public HTMLContentElement {
64
static PassRefPtr<DetailsSummaryElement> create(Document*);
66
Element* fallbackSummary()
68
ASSERT(firstChild() && firstChild()->hasTagName(summaryTag));
69
return toElement(firstChild());
73
DetailsSummaryElement(Document* document)
74
: HTMLContentElement(HTMLNames::webkitShadowContentTag, document)
76
setSelect(summaryQuerySelector());
80
PassRefPtr<DetailsSummaryElement> DetailsSummaryElement::create(Document* document)
82
RefPtr<HTMLSummaryElement> defaultSummary = HTMLSummaryElement::create(summaryTag, document);
83
defaultSummary->appendChild(Text::create(document, defaultDetailsSummaryText()), ASSERT_NO_EXCEPTION);
85
RefPtr<DetailsSummaryElement> elem = adoptRef(new DetailsSummaryElement(document));
86
elem->appendChild(defaultSummary);
87
return elem.release();
90
PassRefPtr<HTMLDetailsElement> HTMLDetailsElement::create(const QualifiedName& tagName, Document* document)
92
RefPtr<HTMLDetailsElement> elem = adoptRef(new HTMLDetailsElement(tagName, document));
93
elem->createShadowSubtree();
95
return elem.release();
98
HTMLDetailsElement::HTMLDetailsElement(const QualifiedName& tagName, Document* document)
99
: HTMLElement(tagName, document)
102
ASSERT(hasTagName(detailsTag));
105
RenderObject* HTMLDetailsElement::createRenderer(RenderArena* arena, RenderStyle*)
107
return new (arena) RenderBlock(this);
110
void HTMLDetailsElement::createShadowSubtree()
114
RefPtr<ShadowRoot> root = ShadowRoot::create(this, ShadowRoot::UserAgentShadowRoot);
115
root->appendChild(DetailsSummaryElement::create(document()), ASSERT_NO_EXCEPTION, true);
116
root->appendChild(DetailsContentElement::create(document()), ASSERT_NO_EXCEPTION, true);
119
Element* HTMLDetailsElement::findMainSummary() const
121
for (Node* child = firstChild(); child; child = child->nextSibling()) {
122
if (child->hasTagName(summaryTag))
123
return toElement(child);
126
return static_cast<DetailsSummaryElement*>(userAgentShadowRoot()->firstChild())->fallbackSummary();
129
void HTMLDetailsElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
131
if (name == openAttr) {
132
bool oldValue = m_isOpen;
133
m_isOpen = !value.isNull();
134
if (oldValue != m_isOpen)
135
reattachIfAttached();
137
HTMLElement::parseAttribute(name, value);
140
bool HTMLDetailsElement::childShouldCreateRenderer(const NodeRenderingContext& childContext) const
142
if (!childContext.isOnEncapsulationBoundary())
146
return HTMLElement::childShouldCreateRenderer(childContext);
148
if (!childContext.node()->hasTagName(summaryTag))
151
return childContext.node() == findMainSummary() && HTMLElement::childShouldCreateRenderer(childContext);
154
void HTMLDetailsElement::toggleOpen()
156
setAttribute(openAttr, m_isOpen ? nullAtom : emptyAtom);