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