HTMLSummaryElement.cpp [plain text]
#include "config.h"
#include "HTMLSummaryElement.h"
#if ENABLE(DETAILS_ELEMENT)
#include "DetailsMarkerControl.h"
#include "HTMLDetailsElement.h"
#include "InsertionPoint.h"
#include "KeyboardEvent.h"
#include "MouseEvent.h"
#include "NodeRenderingTraversal.h"
#include "PlatformMouseEvent.h"
#include "RenderBlockFlow.h"
namespace WebCore {
using namespace HTMLNames;
class SummaryContentElement : public InsertionPoint {
public:
static PassRefPtr<SummaryContentElement> create(Document&);
private:
SummaryContentElement(Document& document)
: InsertionPoint(webkitShadowContentTag, document)
{
}
};
PassRefPtr<SummaryContentElement> SummaryContentElement::create(Document& document)
{
return adoptRef(new SummaryContentElement(document));
}
PassRefPtr<HTMLSummaryElement> HTMLSummaryElement::create(const QualifiedName& tagName, Document& document)
{
RefPtr<HTMLSummaryElement> summary = adoptRef(new HTMLSummaryElement(tagName, document));
summary->ensureUserAgentShadowRoot();
return summary.release();
}
HTMLSummaryElement::HTMLSummaryElement(const QualifiedName& tagName, Document& document)
: HTMLElement(tagName, document)
{
ASSERT(hasTagName(summaryTag));
}
RenderPtr<RenderElement> HTMLSummaryElement::createElementRenderer(PassRef<RenderStyle> style)
{
return createRenderer<RenderBlockFlow>(*this, WTF::move(style));
}
bool HTMLSummaryElement::childShouldCreateRenderer(const Node& child) const
{
if (child.isPseudoElement())
return HTMLElement::childShouldCreateRenderer(child);
return hasShadowRootOrActiveInsertionPointParent(child) && HTMLElement::childShouldCreateRenderer(child);
}
void HTMLSummaryElement::didAddUserAgentShadowRoot(ShadowRoot* root)
{
root->appendChild(DetailsMarkerControl::create(document()), ASSERT_NO_EXCEPTION);
root->appendChild(SummaryContentElement::create(document()), ASSERT_NO_EXCEPTION);
}
HTMLDetailsElement* HTMLSummaryElement::detailsElement() const
{
Node* mayDetails = NodeRenderingTraversal::parent(this);
if (!mayDetails || !mayDetails->hasTagName(detailsTag))
return 0;
return toHTMLDetailsElement(mayDetails);
}
bool HTMLSummaryElement::isMainSummary() const
{
if (HTMLDetailsElement* details = detailsElement())
return details->findMainSummary() == this;
return false;
}
static bool isClickableControl(Node* node)
{
if (!node->isElementNode())
return false;
Element* element = toElement(node);
if (element->isFormControlElement())
return true;
Element* host = element->shadowHost();
return host && host->isFormControlElement();
}
bool HTMLSummaryElement::supportsFocus() const
{
return isMainSummary();
}
void HTMLSummaryElement::defaultEventHandler(Event* event)
{
if (isMainSummary() && renderer()) {
if (event->type() == eventNames().DOMActivateEvent && !isClickableControl(event->target()->toNode())) {
if (HTMLDetailsElement* details = detailsElement())
details->toggleOpen();
event->setDefaultHandled();
return;
}
if (event->isKeyboardEvent()) {
if (event->type() == eventNames().keydownEvent && toKeyboardEvent(event)->keyIdentifier() == "U+0020") {
setActive(true, true);
return;
}
if (event->type() == eventNames().keypressEvent) {
switch (toKeyboardEvent(event)->charCode()) {
case '\r':
dispatchSimulatedClick(event);
event->setDefaultHandled();
return;
case ' ':
event->setDefaultHandled();
return;
}
}
if (event->type() == eventNames().keyupEvent && toKeyboardEvent(event)->keyIdentifier() == "U+0020") {
if (active())
dispatchSimulatedClick(event);
event->setDefaultHandled();
return;
}
}
}
HTMLElement::defaultEventHandler(event);
}
bool HTMLSummaryElement::willRespondToMouseClickEvents()
{
if (isMainSummary() && renderer())
return true;
return HTMLElement::willRespondToMouseClickEvents();
}
}
#endif