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 "Element.h"
32
33namespace WebCore {
34
35void UserActionElementSet::didDetach(Element* element)
36{
37    ASSERT(element->isUserActionElement());
38    clearFlags(element, IsActiveFlag | InActiveChainFlag | IsHoveredFlag);
39}
40
41void UserActionElementSet::documentDidRemoveLastRef()
42{
43    m_elements.clear();
44}
45
46bool UserActionElementSet::hasFlags(const Element* element, unsigned flags) const
47{
48    ASSERT(element->isUserActionElement());
49    ElementFlagMap::const_iterator found = m_elements.find(const_cast<Element*>(element));
50    if (found == m_elements.end())
51        return false;
52    return found->value & flags;
53}
54
55void UserActionElementSet::clearFlags(Element* element, unsigned flags)
56{
57    if (!element->isUserActionElement()) {
58        ASSERT(m_elements.end() == m_elements.find(element));
59        return;
60    }
61
62    ElementFlagMap::iterator found = m_elements.find(element);
63    if (found == m_elements.end()) {
64        element->setUserActionElement(false);
65        return;
66    }
67
68    unsigned updated = found->value & ~flags;
69    if (!updated) {
70        element->setUserActionElement(false);
71        m_elements.remove(found);
72        return;
73    }
74
75    found->value = updated;
76}
77
78void UserActionElementSet::setFlags(Element* element, unsigned flags)
79{
80    ElementFlagMap::iterator result = m_elements.find(element);
81    if (result != m_elements.end()) {
82        ASSERT(element->isUserActionElement());
83        result->value |= flags;
84        return;
85    }
86
87    element->setUserActionElement(true);
88    m_elements.add(element, flags);
89}
90
91}
92