Home | History | Annotate | Download | only in html
      1 /*
      2  * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies)
      3  *
      4  * This library is free software; you can redistribute it and/or
      5  * modify it under the terms of the GNU Library General Public
      6  * License as published by the Free Software Foundation; either
      7  * version 2 of the License, or (at your option) any later version.
      8  *
      9  * This library is distributed in the hope that it will be useful,
     10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     12  * Library General Public License for more details.
     13  *
     14  * You should have received a copy of the GNU Library General Public License
     15  * along with this library; see the file COPYING.LIB.  If not, write to
     16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     17  * Boston, MA 02110-1301, USA.
     18  *
     19  */
     20 
     21 #include "config.h"
     22 #include "core/html/HTMLSummaryElement.h"
     23 
     24 #include "bindings/core/v8/ExceptionStatePlaceholder.h"
     25 #include "core/HTMLNames.h"
     26 #include "core/events/KeyboardEvent.h"
     27 #include "core/dom/NodeRenderingTraversal.h"
     28 #include "core/dom/shadow/ShadowRoot.h"
     29 #include "core/html/HTMLContentElement.h"
     30 #include "core/html/HTMLDetailsElement.h"
     31 #include "core/html/shadow/DetailsMarkerControl.h"
     32 #include "core/rendering/RenderBlockFlow.h"
     33 
     34 namespace blink {
     35 
     36 using namespace HTMLNames;
     37 
     38 PassRefPtrWillBeRawPtr<HTMLSummaryElement> HTMLSummaryElement::create(Document& document)
     39 {
     40     RefPtrWillBeRawPtr<HTMLSummaryElement> summary = adoptRefWillBeNoop(new HTMLSummaryElement(document));
     41     summary->ensureUserAgentShadowRoot();
     42     return summary.release();
     43 }
     44 
     45 HTMLSummaryElement::HTMLSummaryElement(Document& document)
     46     : HTMLElement(summaryTag, document)
     47 {
     48 }
     49 
     50 RenderObject* HTMLSummaryElement::createRenderer(RenderStyle*)
     51 {
     52     return new RenderBlockFlow(this);
     53 }
     54 
     55 void HTMLSummaryElement::didAddUserAgentShadowRoot(ShadowRoot& root)
     56 {
     57     root.appendChild(DetailsMarkerControl::create(document()));
     58     root.appendChild(HTMLContentElement::create(document()));
     59 }
     60 
     61 HTMLDetailsElement* HTMLSummaryElement::detailsElement() const
     62 {
     63     Node* parent = NodeRenderingTraversal::parent(this);
     64     if (isHTMLDetailsElement(parent))
     65         return toHTMLDetailsElement(parent);
     66     return 0;
     67 }
     68 
     69 bool HTMLSummaryElement::isMainSummary() const
     70 {
     71     if (HTMLDetailsElement* details = detailsElement())
     72         return details->findMainSummary() == this;
     73 
     74     return false;
     75 }
     76 
     77 static bool isClickableControl(Node* node)
     78 {
     79     if (!node->isElementNode())
     80         return false;
     81     Element* element = toElement(node);
     82     if (element->isFormControlElement())
     83         return true;
     84     Element* host = element->shadowHost();
     85     return host && host->isFormControlElement();
     86 }
     87 
     88 bool HTMLSummaryElement::supportsFocus() const
     89 {
     90     return isMainSummary();
     91 }
     92 
     93 void HTMLSummaryElement::defaultEventHandler(Event* event)
     94 {
     95     if (isMainSummary() && renderer()) {
     96         if (event->type() == EventTypeNames::DOMActivate && !isClickableControl(event->target()->toNode())) {
     97             if (HTMLDetailsElement* details = detailsElement())
     98                 details->toggleOpen();
     99             event->setDefaultHandled();
    100             return;
    101         }
    102 
    103         if (event->isKeyboardEvent()) {
    104             if (event->type() == EventTypeNames::keydown && toKeyboardEvent(event)->keyIdentifier() == "U+0020") {
    105                 setActive(true);
    106                 // No setDefaultHandled() - IE dispatches a keypress in this case.
    107                 return;
    108             }
    109             if (event->type() == EventTypeNames::keypress) {
    110                 switch (toKeyboardEvent(event)->charCode()) {
    111                 case '\r':
    112                     dispatchSimulatedClick(event);
    113                     event->setDefaultHandled();
    114                     return;
    115                 case ' ':
    116                     // Prevent scrolling down the page.
    117                     event->setDefaultHandled();
    118                     return;
    119                 }
    120             }
    121             if (event->type() == EventTypeNames::keyup && toKeyboardEvent(event)->keyIdentifier() == "U+0020") {
    122                 if (active())
    123                     dispatchSimulatedClick(event);
    124                 event->setDefaultHandled();
    125                 return;
    126             }
    127         }
    128     }
    129 
    130     HTMLElement::defaultEventHandler(event);
    131 }
    132 
    133 bool HTMLSummaryElement::willRespondToMouseClickEvents()
    134 {
    135     if (isMainSummary() && renderer())
    136         return true;
    137 
    138     return HTMLElement::willRespondToMouseClickEvents();
    139 }
    140 
    141 }
    142