1/*
2 * Copyright (C) 2012 Google Inc. All rights reserved.
3 * Copyright (C) 2013 Apple Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 *     * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *     * Neither the name of Google Inc. nor the names of its
12 * contributors may be used to endorse or promote products derived from
13 * this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "config.h"
29#include "UserActionElementSet.h"
30
31#include "Document.h"
32#include "Element.h"
33
34namespace WebCore {
35
36UserActionElementSet::UserActionElementSet()
37{
38}
39
40UserActionElementSet::~UserActionElementSet()
41{
42}
43
44void UserActionElementSet::didDetach(Element* element)
45{
46    ASSERT(element->isUserActionElement());
47    clearFlags(element, IsActiveFlag | InActiveChainFlag | IsHoveredFlag);
48}
49
50void UserActionElementSet::documentDidRemoveLastRef()
51{
52    m_elements.clear();
53}
54
55bool UserActionElementSet::hasFlags(const Element* element, unsigned flags) const
56{
57    ASSERT(element->isUserActionElement());
58    ElementFlagMap::const_iterator found = m_elements.find(const_cast<Element*>(element));
59    if (found == m_elements.end())
60        return false;
61    return found->value & flags;
62}
63
64void UserActionElementSet::clearFlags(Element* element, unsigned flags)
65{
66    if (!element->isUserActionElement()) {
67        ASSERT(m_elements.end() == m_elements.find(element));
68        return;
69    }
70
71    ElementFlagMap::iterator found = m_elements.find(element);
72    if (found == m_elements.end()) {
73        element->setUserActionElement(false);
74        return;
75    }
76
77    unsigned updated = found->value & ~flags;
78    if (!updated) {
79        element->setUserActionElement(false);
80        m_elements.remove(found);
81        return;
82    }
83
84    found->value = updated;
85}
86
87void UserActionElementSet::setFlags(Element* element, unsigned flags)
88{
89    ElementFlagMap::iterator result = m_elements.find(element);
90    if (result != m_elements.end()) {
91        ASSERT(element->isUserActionElement());
92        result->value |= flags;
93        return;
94    }
95
96    element->setUserActionElement(true);
97    m_elements.add(element, flags);
98}
99
100}
101