~ubuntu-branches/ubuntu/raring/qtwebkit-source/raring-proposed

« back to all changes in this revision

Viewing changes to Source/WebCore/html/HTMLDetailsElement.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-02-18 14:24:18 UTC
  • Revision ID: package-import@ubuntu.com-20130218142418-eon0jmjg3nj438uy
Tags: upstream-2.3
ImportĀ upstreamĀ versionĀ 2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2010, 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 "HTMLDetailsElement.h"
 
23
 
 
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"
 
34
#include "Text.h"
 
35
 
 
36
namespace WebCore {
 
37
 
 
38
using namespace HTMLNames;
 
39
 
 
40
static const AtomicString& summaryQuerySelector()
 
41
{
 
42
    DEFINE_STATIC_LOCAL(AtomicString, selector, ("summary:first-of-type", AtomicString::ConstructFromLiteral));
 
43
    return selector;
 
44
};
 
45
 
 
46
class DetailsContentElement : public HTMLContentElement {
 
47
public:
 
48
    static PassRefPtr<DetailsContentElement> create(Document*);
 
49
 
 
50
private:
 
51
    DetailsContentElement(Document* document)
 
52
        : HTMLContentElement(HTMLNames::webkitShadowContentTag, document)
 
53
    {
 
54
    }
 
55
};
 
56
 
 
57
PassRefPtr<DetailsContentElement> DetailsContentElement::create(Document* document)
 
58
{
 
59
    return adoptRef(new DetailsContentElement(document));
 
60
}
 
61
 
 
62
class DetailsSummaryElement : public HTMLContentElement {
 
63
public:
 
64
    static PassRefPtr<DetailsSummaryElement> create(Document*);
 
65
 
 
66
    Element* fallbackSummary()
 
67
    {
 
68
        ASSERT(firstChild() && firstChild()->hasTagName(summaryTag));
 
69
        return toElement(firstChild());
 
70
    }
 
71
 
 
72
private:
 
73
    DetailsSummaryElement(Document* document)
 
74
        : HTMLContentElement(HTMLNames::webkitShadowContentTag, document)
 
75
    {
 
76
        setSelect(summaryQuerySelector());
 
77
    }
 
78
};
 
79
 
 
80
PassRefPtr<DetailsSummaryElement> DetailsSummaryElement::create(Document* document)
 
81
{
 
82
    RefPtr<HTMLSummaryElement> defaultSummary = HTMLSummaryElement::create(summaryTag, document);
 
83
    defaultSummary->appendChild(Text::create(document, defaultDetailsSummaryText()), ASSERT_NO_EXCEPTION);
 
84
 
 
85
    RefPtr<DetailsSummaryElement> elem = adoptRef(new DetailsSummaryElement(document));
 
86
    elem->appendChild(defaultSummary);
 
87
    return elem.release();
 
88
}
 
89
 
 
90
PassRefPtr<HTMLDetailsElement> HTMLDetailsElement::create(const QualifiedName& tagName, Document* document)
 
91
{
 
92
    RefPtr<HTMLDetailsElement> elem = adoptRef(new HTMLDetailsElement(tagName, document));
 
93
    elem->createShadowSubtree();
 
94
 
 
95
    return elem.release();
 
96
}
 
97
 
 
98
HTMLDetailsElement::HTMLDetailsElement(const QualifiedName& tagName, Document* document)
 
99
    : HTMLElement(tagName, document)
 
100
    , m_isOpen(false)
 
101
{
 
102
    ASSERT(hasTagName(detailsTag));
 
103
}
 
104
 
 
105
RenderObject* HTMLDetailsElement::createRenderer(RenderArena* arena, RenderStyle*)
 
106
{
 
107
    return new (arena) RenderBlock(this);
 
108
}
 
109
 
 
110
void HTMLDetailsElement::createShadowSubtree()
 
111
{
 
112
    ASSERT(!shadow());
 
113
 
 
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);
 
117
}
 
118
 
 
119
Element* HTMLDetailsElement::findMainSummary() const
 
120
{
 
121
    for (Node* child = firstChild(); child; child = child->nextSibling()) {
 
122
        if (child->hasTagName(summaryTag))
 
123
            return toElement(child);
 
124
    }
 
125
 
 
126
    return static_cast<DetailsSummaryElement*>(userAgentShadowRoot()->firstChild())->fallbackSummary();
 
127
}
 
128
 
 
129
void HTMLDetailsElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
 
130
{
 
131
    if (name == openAttr) {
 
132
        bool oldValue = m_isOpen;
 
133
        m_isOpen = !value.isNull();
 
134
        if (oldValue != m_isOpen)
 
135
            reattachIfAttached();
 
136
    } else
 
137
        HTMLElement::parseAttribute(name, value);
 
138
}
 
139
 
 
140
bool HTMLDetailsElement::childShouldCreateRenderer(const NodeRenderingContext& childContext) const
 
141
{
 
142
    if (!childContext.isOnEncapsulationBoundary())
 
143
        return false;
 
144
 
 
145
    if (m_isOpen)
 
146
        return HTMLElement::childShouldCreateRenderer(childContext);
 
147
 
 
148
    if (!childContext.node()->hasTagName(summaryTag))
 
149
        return false;
 
150
 
 
151
    return childContext.node() == findMainSummary() && HTMLElement::childShouldCreateRenderer(childContext);
 
152
}
 
153
 
 
154
void HTMLDetailsElement::toggleOpen()
 
155
{
 
156
    setAttribute(openAttr, m_isOpen ? nullAtom : emptyAtom);
 
157
}
 
158
 
 
159
}
 
160
 
 
161
#endif