Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / html / HTMLDialogElement.cpp
1 /*
2  * Copyright (C) 2012 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "core/html/HTMLDialogElement.h"
28
29 #include "bindings/v8/ExceptionState.h"
30 #include "core/accessibility/AXObjectCache.h"
31 #include "core/dom/ExceptionCode.h"
32 #include "core/dom/NodeTraversal.h"
33 #include "core/html/HTMLFormControlElement.h"
34 #include "core/frame/FrameView.h"
35 #include "core/rendering/RenderBlock.h"
36 #include "core/rendering/style/RenderStyle.h"
37
38 namespace WebCore {
39
40 using namespace HTMLNames;
41
42 // This function chooses the focused element when showModal() is invoked, as described in the spec for showModal().
43 static void setFocusForModalDialog(HTMLDialogElement* dialog)
44 {
45     Element* focusableDescendant = 0;
46     Node* next = 0;
47     for (Node* node = dialog->firstChild(); node; node = next) {
48         if (node->hasTagName(dialogTag))
49             next = NodeTraversal::nextSkippingChildren(*node, dialog);
50         else
51             next = NodeTraversal::next(*node, dialog);
52
53         if (!node->isElementNode())
54             continue;
55         Element* element = toElement(node);
56         if (element->isFormControlElement()) {
57             HTMLFormControlElement* control = toHTMLFormControlElement(node);
58             if (control->isAutofocusable()) {
59                 control->focus();
60                 return;
61             }
62         }
63         if (!focusableDescendant && element->isFocusable())
64             focusableDescendant = element;
65     }
66
67     if (focusableDescendant) {
68         focusableDescendant->focus();
69         return;
70     }
71
72     if (dialog->isFocusable()) {
73         dialog->focus();
74         return;
75     }
76
77     dialog->document().setFocusedElement(0);
78 }
79
80 static void inertSubtreesChanged(Document& document)
81 {
82     // When a modal dialog opens or closes, nodes all over the accessibility
83     // tree can change inertness which means they must be added or removed from
84     // the tree. The most foolproof way is to clear the entire tree and rebuild
85     // it, though a more clever way is probably possible.
86     Document* topDocument = document.topDocument();
87     topDocument->clearAXObjectCache();
88     if (AXObjectCache* cache = topDocument->axObjectCache())
89         cache->childrenChanged(cache->getOrCreate(topDocument));
90 }
91
92 HTMLDialogElement::HTMLDialogElement(Document& document)
93     : HTMLElement(dialogTag, document)
94     , m_centeringMode(NotCentered)
95     , m_centeredPosition(0)
96     , m_returnValue("")
97 {
98     ScriptWrappable::init(this);
99 }
100
101 PassRefPtr<HTMLDialogElement> HTMLDialogElement::create(Document& document)
102 {
103     return adoptRef(new HTMLDialogElement(document));
104 }
105
106 void HTMLDialogElement::close(const String& returnValue, ExceptionState& exceptionState)
107 {
108     if (!fastHasAttribute(openAttr)) {
109         exceptionState.throwDOMException(InvalidStateError, "The element does not have an 'open' attribute, and therefore cannot be closed.");
110         return;
111     }
112     closeDialog(returnValue);
113 }
114
115 void HTMLDialogElement::closeDialog(const String& returnValue)
116 {
117     if (!fastHasAttribute(openAttr))
118         return;
119     setBooleanAttribute(openAttr, false);
120
121     HTMLDialogElement* activeModalDialog = document().activeModalDialog();
122     document().removeFromTopLayer(this);
123     if (activeModalDialog == this)
124         inertSubtreesChanged(document());
125
126     if (!returnValue.isNull())
127         m_returnValue = returnValue;
128
129     dispatchScopedEvent(Event::create(EventTypeNames::close));
130 }
131
132 void HTMLDialogElement::forceLayoutForCentering()
133 {
134     m_centeringMode = NeedsCentering;
135     document().updateLayoutIgnorePendingStylesheets();
136     if (m_centeringMode == NeedsCentering)
137         setNotCentered();
138 }
139
140 void HTMLDialogElement::show()
141 {
142     if (fastHasAttribute(openAttr))
143         return;
144     setBooleanAttribute(openAttr, true);
145 }
146
147 void HTMLDialogElement::showModal(ExceptionState& exceptionState)
148 {
149     if (fastHasAttribute(openAttr)) {
150         exceptionState.throwDOMException(InvalidStateError, "The element already has an 'open' attribute, and therefore cannot be opened modally.");
151         return;
152     }
153     if (!inDocument()) {
154         exceptionState.throwDOMException(InvalidStateError, "The element is not in a Document.");
155         return;
156     }
157
158     document().addToTopLayer(this);
159     setBooleanAttribute(openAttr, true);
160
161     // Throw away the AX cache first, so the subsequent steps don't have a chance of queuing up
162     // AX events on objects that would be invalidated when the cache is thrown away.
163     inertSubtreesChanged(document());
164
165     forceLayoutForCentering();
166     setFocusForModalDialog(this);
167 }
168
169 void HTMLDialogElement::removedFrom(ContainerNode* insertionPoint)
170 {
171     HTMLElement::removedFrom(insertionPoint);
172     setNotCentered();
173     // FIXME: We should call inertSubtreesChanged() here.
174 }
175
176 void HTMLDialogElement::setCentered(LayoutUnit centeredPosition)
177 {
178     ASSERT(m_centeringMode == NeedsCentering);
179     m_centeredPosition = centeredPosition;
180     m_centeringMode = Centered;
181 }
182
183 void HTMLDialogElement::setNotCentered()
184 {
185     m_centeringMode = NotCentered;
186 }
187
188 bool HTMLDialogElement::isPresentationAttribute(const QualifiedName& name) const
189 {
190     // FIXME: Workaround for <https://bugs.webkit.org/show_bug.cgi?id=91058>: modifying an attribute for which there is an attribute selector
191     // in html.css sometimes does not trigger a style recalc.
192     if (name == openAttr)
193         return true;
194
195     return HTMLElement::isPresentationAttribute(name);
196 }
197
198 void HTMLDialogElement::defaultEventHandler(Event* event)
199 {
200     if (event->type() == EventTypeNames::cancel) {
201         closeDialog();
202         event->setDefaultHandled();
203         return;
204     }
205     HTMLElement::defaultEventHandler(event);
206 }
207
208 } // namespace WebCore