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