1/*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * Copyright (C) 2010 Apple Inc. All rights reserved.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 *
21 */
22
23#include "config.h"
24#include "HTMLMenuElement.h"
25
26#include "Chrome.h"
27#include "ChromeClient.h"
28#include "Document.h"
29#include "ElementChildIterator.h"
30#include "HTMLMenuItemElement.h"
31#include "HTMLNames.h"
32#include "Page.h"
33#include "RuntimeEnabledFeatures.h"
34#include <wtf/IsoMallocInlines.h>
35
36namespace WebCore {
37
38WTF_MAKE_ISO_ALLOCATED_IMPL(HTMLMenuElement);
39
40using namespace HTMLNames;
41
42inline HTMLMenuElement::HTMLMenuElement(const QualifiedName& tagName, Document& document)
43 : HTMLElement(tagName, document)
44{
45 ASSERT(hasTagName(menuTag));
46}
47
48Node::InsertedIntoAncestorResult HTMLMenuElement::insertedIntoAncestor(InsertionType type, ContainerNode& ancestor)
49{
50 auto result = HTMLElement::insertedIntoAncestor(type, ancestor);
51 if (type.connectedToDocument && RuntimeEnabledFeatures::sharedFeatures().menuItemElementEnabled() && m_isTouchBarMenu) {
52 if (auto* page = document().page())
53 page->chrome().client().didInsertMenuElement(*this);
54 }
55 return result;
56}
57
58void HTMLMenuElement::removedFromAncestor(RemovalType type, ContainerNode& ancestor)
59{
60 HTMLElement::removedFromAncestor(type, ancestor);
61 if (type.disconnectedFromDocument && RuntimeEnabledFeatures::sharedFeatures().menuItemElementEnabled() && m_isTouchBarMenu) {
62 if (auto* page = document().page())
63 page->chrome().client().didRemoveMenuElement(*this);
64 }
65}
66
67void HTMLMenuElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
68{
69 if (name != typeAttr || !RuntimeEnabledFeatures::sharedFeatures().menuItemElementEnabled()) {
70 HTMLElement::parseAttribute(name, value);
71 return;
72 }
73 bool wasTouchBarMenu = m_isTouchBarMenu;
74 m_isTouchBarMenu = equalLettersIgnoringASCIICase(value, "touchbar");
75 if (!wasTouchBarMenu && m_isTouchBarMenu) {
76 if (auto* page = document().page()) {
77 page->chrome().client().didInsertMenuElement(*this);
78 for (auto& child : childrenOfType<Element>(*this))
79 page->chrome().client().didInsertMenuItemElement(downcast<HTMLMenuItemElement>(child));
80 }
81 } else if (wasTouchBarMenu && !m_isTouchBarMenu) {
82 if (auto* page = document().page())
83 page->chrome().client().didRemoveMenuElement(*this);
84 }
85}
86
87Ref<HTMLMenuElement> HTMLMenuElement::create(const QualifiedName& tagName, Document& document)
88{
89 return adoptRef(*new HTMLMenuElement(tagName, document));
90}
91
92}
93