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"
26 #include "HTMLNames.h"
27 #include "HTMLSummaryElement.h"
28 #include "LocalizedStrings.h"
29 #include "MouseEvent.h"
30 #include "RenderDetails.h"
31 #include "ShadowContentElement.h"
32 #include "ShadowRoot.h"
37 using namespace HTMLNames;
39 class DetailsContentElement : public ShadowContentElement {
41 static PassRefPtr<DetailsContentElement> create(Document*);
44 DetailsContentElement(Document* document)
45 : ShadowContentElement(HTMLNames::divTag, document)
49 virtual bool shouldInclude(Node*);
52 PassRefPtr<DetailsContentElement> DetailsContentElement::create(Document* document)
54 return adoptRef(new DetailsContentElement(document));
57 bool DetailsContentElement::shouldInclude(Node* node)
59 HTMLDetailsElement* details = static_cast<HTMLDetailsElement*>(shadowAncestorNode());
60 return details->mainSummary() != node;
64 class DetailsSummaryElement : public ShadowContentElement {
66 static PassRefPtr<DetailsSummaryElement> create(Document*);
69 DetailsSummaryElement(Document* document)
70 : ShadowContentElement(HTMLNames::divTag, document)
74 virtual bool shouldInclude(Node*);
77 PassRefPtr<DetailsSummaryElement> DetailsSummaryElement::create(Document* document)
79 return adoptRef(new DetailsSummaryElement(document));
82 bool DetailsSummaryElement::shouldInclude(Node* node)
84 HTMLDetailsElement* details = static_cast<HTMLDetailsElement*>(shadowAncestorNode());
85 return details->mainSummary() == node;
89 PassRefPtr<HTMLDetailsElement> HTMLDetailsElement::create(const QualifiedName& tagName, Document* document)
91 RefPtr<HTMLDetailsElement> result = adoptRef(new HTMLDetailsElement(tagName, document));
92 result->ensureShadowSubtreeOf(ForwardingSummary);
96 HTMLDetailsElement::HTMLDetailsElement(const QualifiedName& tagName, Document* document)
97 : HTMLElement(tagName, document)
98 , m_summaryType(NoSummary)
102 ASSERT(hasTagName(detailsTag));
105 RenderObject* HTMLDetailsElement::createRenderer(RenderArena* arena, RenderStyle*)
107 return new (arena) RenderDetails(this);
110 void HTMLDetailsElement::ensureShadowSubtreeOf(SummaryType type)
112 if (type == m_summaryType)
114 m_summaryType = type;
116 createShadowSubtree();
119 static Node* findSummaryFor(PassRefPtr<ContainerNode> container)
121 for (Node* child = container->firstChild(); child; child = child->nextSibling()) {
122 if (child->hasTagName(summaryTag))
129 Node* HTMLDetailsElement::ensureMainSummary()
131 Node* summary = findSummaryFor(this);
133 ensureShadowSubtreeOf(ForwardingSummary);
137 ensureShadowSubtreeOf(DefaultSummary);
138 return findSummaryFor(shadowRoot());
141 void HTMLDetailsElement::refreshMainSummary(RefreshRenderer refreshRenderer)
143 RefPtr<Node> oldSummary = m_mainSummary;
144 m_mainSummary = ensureMainSummary();
146 if (oldSummary == m_mainSummary || !attached())
149 if (oldSummary && oldSummary->parentNodeForRenderingAndStyle())
150 oldSummary->reattach();
151 if (m_mainSummary && refreshRenderer == RefreshRendererAllowed)
152 m_mainSummary->reattach();
155 void HTMLDetailsElement::createShadowSubtree()
157 ASSERT(!shadowRoot());
158 ExceptionCode ec = 0;
159 if (m_summaryType == DefaultSummary) {
160 RefPtr<HTMLSummaryElement> defaultSummary = HTMLSummaryElement::create(summaryTag, document());
161 defaultSummary->appendChild(Text::create(document(), defaultDetailsSummaryText()), ec);
162 ensureShadowRoot()->appendChild(defaultSummary, ec, true);
163 ensureShadowRoot()->appendChild(DetailsContentElement::create(document()), ec, true);
165 ASSERT(m_summaryType == ForwardingSummary);
166 ensureShadowRoot()->appendChild(DetailsSummaryElement::create(document()), ec, true);
167 ensureShadowRoot()->appendChild(DetailsContentElement::create(document()), ec, true);
172 void HTMLDetailsElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
174 HTMLElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
175 // If childCountDelta is less then zero and the main summary has changed it must be because previous main
176 // summary was removed. The new main summary was then inside the unrevealed content and needs to be
177 // reattached to create its renderer. If childCountDelta is not less then zero then a new <summary> element
178 // has been added and it will be attached without our help.
179 if (!changedByParser)
180 refreshMainSummary(childCountDelta < 0 ? RefreshRendererAllowed : RefreshRendererSupressed);
183 void HTMLDetailsElement::finishParsingChildren()
185 HTMLElement::finishParsingChildren();
186 refreshMainSummary(RefreshRendererAllowed);
189 void HTMLDetailsElement::parseMappedAttribute(Attribute* attr)
191 if (attr->name() == openAttr) {
192 bool oldValue = m_isOpen;
193 m_isOpen = !attr->value().isNull();
194 if (oldValue != m_isOpen)
195 reattachIfAttached();
197 HTMLElement::parseMappedAttribute(attr);
200 bool HTMLDetailsElement::childShouldCreateRenderer(Node* child) const
202 return m_isOpen || child == m_mainSummary;
205 void HTMLDetailsElement::toggleOpen()
207 setAttribute(openAttr, m_isOpen ? nullAtom : emptyAtom);