1 /* 2 * Copyright (C) 2010, 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 "HTMLDetailsElement.h" 23 24 #include "HTMLNames.h" 25 #include "HTMLSummaryElement.h" 26 #include "LocalizedStrings.h" 27 #include "MouseEvent.h" 28 #include "RenderDetails.h" 29 #include "Text.h" 30 31 namespace WebCore { 32 33 using namespace HTMLNames; 34 35 PassRefPtr<HTMLDetailsElement> HTMLDetailsElement::create(const QualifiedName& tagName, Document* document) 36 { 37 return adoptRef(new HTMLDetailsElement(tagName, document)); 38 } 39 40 HTMLDetailsElement::HTMLDetailsElement(const QualifiedName& tagName, Document* document) 41 : HTMLElement(tagName, document) 42 , m_mainSummary(0) 43 , m_isOpen(false) 44 { 45 ASSERT(hasTagName(detailsTag)); 46 } 47 48 RenderObject* HTMLDetailsElement::createRenderer(RenderArena* arena, RenderStyle*) 49 { 50 return new (arena) RenderDetails(this); 51 } 52 53 Node* HTMLDetailsElement::findSummaryFor(ContainerNode* container) 54 { 55 for (Node* child = container->firstChild(); child; child = child->nextSibling()) { 56 if (child->hasTagName(summaryTag)) 57 return child; 58 } 59 60 return 0; 61 } 62 63 Node* HTMLDetailsElement::findMainSummary() 64 { 65 Node* found = findSummaryFor(this); 66 if (found) { 67 removeShadowRoot(); 68 return found; 69 } 70 71 createShadowSubtree(); 72 found = findSummaryFor(shadowRoot()); 73 ASSERT(found); 74 return found; 75 } 76 77 void HTMLDetailsElement::refreshMainSummary(RefreshRenderer refreshRenderer) 78 { 79 RefPtr<Node> oldSummary = m_mainSummary; 80 m_mainSummary = findMainSummary(); 81 82 if (oldSummary == m_mainSummary || !attached()) 83 return; 84 85 if (oldSummary && oldSummary->parentNodeForRenderingAndStyle()) { 86 oldSummary->detach(); 87 oldSummary->attach(); 88 } 89 90 if (refreshRenderer == RefreshRendererAllowed) { 91 m_mainSummary->detach(); 92 m_mainSummary->attach(); 93 } 94 } 95 96 void HTMLDetailsElement::createShadowSubtree() 97 { 98 if (shadowRoot()) 99 return; 100 101 RefPtr<HTMLSummaryElement> defaultSummary = HTMLSummaryElement::create(summaryTag, document()); 102 ExceptionCode ec = 0; 103 defaultSummary->appendChild(Text::create(document(), defaultDetailsSummaryText()), ec); 104 ensureShadowRoot()->appendChild(defaultSummary, ec, true); 105 } 106 107 108 void HTMLDetailsElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta) 109 { 110 HTMLElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta); 111 // If childCountDelta is less then zero and the main summary has changed it must be because previous main 112 // summary was removed. The new main summary was then inside the unrevealed content and needs to be 113 // reattached to create its renderer. If childCountDelta is not less then zero then a new <summary> element 114 // has been added and it will be attached without our help. 115 if (!changedByParser) 116 refreshMainSummary(childCountDelta < 0 ? RefreshRendererAllowed : RefreshRendererSupressed); 117 } 118 119 void HTMLDetailsElement::finishParsingChildren() 120 { 121 HTMLElement::finishParsingChildren(); 122 refreshMainSummary(RefreshRendererAllowed); 123 } 124 125 void HTMLDetailsElement::parseMappedAttribute(Attribute* attr) 126 { 127 if (attr->name() == openAttr) { 128 bool oldValue = m_isOpen; 129 m_isOpen = !attr->value().isNull(); 130 if (attached() && oldValue != m_isOpen) { 131 detach(); 132 attach(); 133 } 134 } else 135 HTMLElement::parseMappedAttribute(attr); 136 } 137 138 bool HTMLDetailsElement::childShouldCreateRenderer(Node* child) const 139 { 140 return m_isOpen || child == m_mainSummary; 141 } 142 143 void HTMLDetailsElement::toggleOpen() 144 { 145 setAttribute(openAttr, m_isOpen ? nullAtom : emptyAtom); 146 } 147 148 } 149