Upstream version 5.34.92.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(Uninitialized)
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 = Uninitialized;
135     document().updateLayoutIgnorePendingStylesheets();
136     if (m_centeringMode == Uninitialized)
137         m_centeringMode = NotCentered;
138 }
139
140 void HTMLDialogElement::show()
141 {
142     if (fastHasAttribute(openAttr))
143         return;
144     setBooleanAttribute(openAttr, true);
145     forceLayoutForCentering();
146 }
147
148 void HTMLDialogElement::showModal(ExceptionState& exceptionState)
149 {
150     if (fastHasAttribute(openAttr)) {
151         exceptionState.throwDOMException(InvalidStateError, "The element already has an 'open' attribute, and therefore cannot be opened modally.");
152         return;
153     }
154     if (!inDocument()) {
155         exceptionState.throwDOMException(InvalidStateError, "The element is not in a Document.");
156         return;
157     }
158
159     document().addToTopLayer(this);
160     setBooleanAttribute(openAttr, true);
161
162     // Throw away the AX cache first, so the subsequent steps don't have a chance of queuing up
163     // AX events on objects that would be invalidated when the cache is thrown away.
164     inertSubtreesChanged(document());
165
166     forceLayoutForCentering();
167     setFocusForModalDialog(this);
168 }
169
170 void HTMLDialogElement::setCentered(LayoutUnit centeredPosition)
171 {
172     ASSERT(m_centeringMode == Uninitialized);
173     m_centeredPosition = centeredPosition;
174     m_centeringMode = Centered;
175 }
176
177 void HTMLDialogElement::setNotCentered()
178 {
179     ASSERT(m_centeringMode == Uninitialized);
180     m_centeringMode = NotCentered;
181 }
182
183 bool HTMLDialogElement::isPresentationAttribute(const QualifiedName& name) const
184 {
185     // FIXME: Workaround for <https://bugs.webkit.org/show_bug.cgi?id=91058>: modifying an attribute for which there is an attribute selector
186     // in html.css sometimes does not trigger a style recalc.
187     if (name == openAttr)
188         return true;
189
190     return HTMLElement::isPresentationAttribute(name);
191 }
192
193 void HTMLDialogElement::defaultEventHandler(Event* event)
194 {
195     if (event->type() == EventTypeNames::cancel) {
196         closeDialog();
197         event->setDefaultHandled();
198         return;
199     }
200     HTMLElement::defaultEventHandler(event);
201 }
202
203 } // namespace WebCore