initial import
[vuplus_webkit] / Source / WebCore / html / HTMLSummaryElement.cpp
1 /*
2  * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies)
3  *
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.
8  *
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.
13  *
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.
18  *
19  */
20
21 #include "config.h"
22 #include "HTMLSummaryElement.h"
23
24 #if ENABLE(DETAILS)
25
26 #include "DetailsMarkerControl.h"
27 #include "HTMLDetailsElement.h"
28 #include "HTMLNames.h"
29 #include "MouseEvent.h"
30 #include "PlatformMouseEvent.h"
31 #include "RenderSummary.h"
32 #include "ShadowContentElement.h"
33
34 #include "ShadowRoot.h"
35
36 namespace WebCore {
37
38 using namespace HTMLNames;
39
40 class SummaryContentElement : public ShadowContentElement {
41 public:
42     static PassRefPtr<SummaryContentElement> create(Document*);
43
44 private:
45     SummaryContentElement(Document* document)
46         : ShadowContentElement(HTMLNames::divTag, document)
47     {
48     }
49 };
50
51 PassRefPtr<SummaryContentElement> SummaryContentElement::create(Document* document)
52 {
53     return adoptRef(new SummaryContentElement(document));
54 }
55
56 PassRefPtr<HTMLSummaryElement> HTMLSummaryElement::create(const QualifiedName& tagName, Document* document)
57 {
58     RefPtr<HTMLSummaryElement> result = adoptRef(new HTMLSummaryElement(tagName, document));
59     result->createShadowSubtree();
60     return result;
61 }
62
63 HTMLSummaryElement::HTMLSummaryElement(const QualifiedName& tagName, Document* document)
64     : HTMLElement(tagName, document)
65 {
66     ASSERT(hasTagName(summaryTag));
67 }
68
69 RenderObject* HTMLSummaryElement::createRenderer(RenderArena* arena, RenderStyle*)
70 {
71     return new (arena) RenderSummary(this);
72 }
73
74 void HTMLSummaryElement::createShadowSubtree()
75 {
76     ExceptionCode ec = 0;
77     ensureShadowRoot()->appendChild(DetailsMarkerControl::create(document()), ec, true);
78     ensureShadowRoot()->appendChild(SummaryContentElement::create(document()), ec, true);
79 }
80
81 HTMLDetailsElement* HTMLSummaryElement::detailsElement() const
82 {
83     Node* mayDetails = const_cast<HTMLSummaryElement*>(this)->parentNodeForRenderingAndStyle();
84     if (!mayDetails || !mayDetails->hasTagName(detailsTag))
85         return 0;
86     return static_cast<HTMLDetailsElement*>(mayDetails);
87 }
88
89 bool HTMLSummaryElement::isMainSummary() const
90 {
91     if (HTMLDetailsElement* details = detailsElement())
92         return details->mainSummary() == this;
93     return 0;
94 }
95
96 void HTMLSummaryElement::defaultEventHandler(Event* event)
97 {
98     HTMLElement::defaultEventHandler(event);
99     if (!isMainSummary() || !renderer() || !renderer()->isSummary() || !event->isMouseEvent() || event->type() != eventNames().clickEvent || event->defaultHandled())
100         return;
101
102     MouseEvent* mouseEvent = static_cast<MouseEvent*>(event);
103     if (mouseEvent->button() != LeftButton)
104         return;
105
106     if (HTMLDetailsElement* details = detailsElement())
107         details->toggleOpen();
108     event->setDefaultHandled();
109 }
110
111 }
112
113 #endif