2 * Copyright (C) 2012 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "core/html/HTMLDialogElement.h"
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"
40 using namespace HTMLNames;
42 // This function chooses the focused element when showModal() is invoked, as described in the spec for showModal().
43 static void setFocusForModalDialog(HTMLDialogElement* dialog)
45 Element* focusableDescendant = 0;
47 for (Node* node = dialog->firstChild(); node; node = next) {
48 if (node->hasTagName(dialogTag))
49 next = NodeTraversal::nextSkippingChildren(*node, dialog);
51 next = NodeTraversal::next(*node, dialog);
53 if (!node->isElementNode())
55 Element* element = toElement(node);
56 if (element->isFormControlElement()) {
57 HTMLFormControlElement* control = toHTMLFormControlElement(node);
58 if (control->isAutofocusable()) {
63 if (!focusableDescendant && element->isFocusable())
64 focusableDescendant = element;
67 if (focusableDescendant) {
68 focusableDescendant->focus();
72 if (dialog->isFocusable()) {
77 dialog->document().setFocusedElement(0);
80 static void inertSubtreesChanged(Document& document)
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));
92 HTMLDialogElement::HTMLDialogElement(Document& document)
93 : HTMLElement(dialogTag, document)
94 , m_centeringMode(Uninitialized)
95 , m_centeredPosition(0)
98 ScriptWrappable::init(this);
101 PassRefPtr<HTMLDialogElement> HTMLDialogElement::create(Document& document)
103 return adoptRef(new HTMLDialogElement(document));
106 void HTMLDialogElement::close(const String& returnValue, ExceptionState& exceptionState)
108 if (!fastHasAttribute(openAttr)) {
109 exceptionState.throwDOMException(InvalidStateError, "The element does not have an 'open' attribute, and therefore cannot be closed.");
112 closeDialog(returnValue);
115 void HTMLDialogElement::closeDialog(const String& returnValue)
117 if (!fastHasAttribute(openAttr))
119 setBooleanAttribute(openAttr, false);
121 HTMLDialogElement* activeModalDialog = document().activeModalDialog();
122 document().removeFromTopLayer(this);
123 if (activeModalDialog == this)
124 inertSubtreesChanged(document());
126 if (!returnValue.isNull())
127 m_returnValue = returnValue;
129 dispatchScopedEvent(Event::create(EventTypeNames::close));
132 void HTMLDialogElement::forceLayoutForCentering()
134 m_centeringMode = Uninitialized;
135 document().updateLayoutIgnorePendingStylesheets();
136 if (m_centeringMode == Uninitialized)
137 m_centeringMode = NotCentered;
140 void HTMLDialogElement::show()
142 if (fastHasAttribute(openAttr))
144 setBooleanAttribute(openAttr, true);
145 forceLayoutForCentering();
148 void HTMLDialogElement::showModal(ExceptionState& exceptionState)
150 if (fastHasAttribute(openAttr)) {
151 exceptionState.throwDOMException(InvalidStateError, "The element already has an 'open' attribute, and therefore cannot be opened modally.");
155 exceptionState.throwDOMException(InvalidStateError, "The element is not in a Document.");
159 document().addToTopLayer(this);
160 setBooleanAttribute(openAttr, true);
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());
166 forceLayoutForCentering();
167 setFocusForModalDialog(this);
170 void HTMLDialogElement::setCentered(LayoutUnit centeredPosition)
172 ASSERT(m_centeringMode == Uninitialized);
173 m_centeredPosition = centeredPosition;
174 m_centeringMode = Centered;
177 void HTMLDialogElement::setNotCentered()
179 ASSERT(m_centeringMode == Uninitialized);
180 m_centeringMode = NotCentered;
183 bool HTMLDialogElement::isPresentationAttribute(const QualifiedName& name) const
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)
190 return HTMLElement::isPresentationAttribute(name);
193 void HTMLDialogElement::defaultEventHandler(Event* event)
195 if (event->type() == EventTypeNames::cancel) {
197 event->setDefaultHandled();
200 HTMLElement::defaultEventHandler(event);
203 } // namespace WebCore