| 1 | /* |
| 2 | * Copyright (C) 2012 Google Inc. All rights reserved. |
| 3 | * Copyright (C) 2013-2017 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 | |
| 33 | namespace WebCore { |
| 34 | |
| 35 | void UserActionElementSet::clear() |
| 36 | { |
| 37 | m_elements.clear(); |
| 38 | } |
| 39 | |
| 40 | bool UserActionElementSet::hasFlag(const Element& element, Flag flag) const |
| 41 | { |
| 42 | // Caller has the responsibility to check isUserActionElement before calling this. |
| 43 | ASSERT(element.isUserActionElement()); |
| 44 | |
| 45 | return m_elements.get(&const_cast<Element&>(element)).contains(flag); |
| 46 | } |
| 47 | |
| 48 | void UserActionElementSet::clearFlags(Element& element, OptionSet<Flag> flags) |
| 49 | { |
| 50 | ASSERT(!flags.isEmpty()); |
| 51 | |
| 52 | if (!element.isUserActionElement()) |
| 53 | return; |
| 54 | |
| 55 | auto iterator = m_elements.find(&element); |
| 56 | ASSERT(iterator != m_elements.end()); |
| 57 | auto updatedFlags = iterator->value - flags; |
| 58 | if (updatedFlags.isEmpty()) { |
| 59 | element.setUserActionElement(false); |
| 60 | m_elements.remove(iterator); |
| 61 | } else |
| 62 | iterator->value = updatedFlags; |
| 63 | } |
| 64 | |
| 65 | void UserActionElementSet::setFlags(Element& element, OptionSet<Flag> flags) |
| 66 | { |
| 67 | ASSERT(!flags.isEmpty()); |
| 68 | |
| 69 | m_elements.ensure(&element, [] { |
| 70 | return OptionSet<Flag>(); |
| 71 | }).iterator->value.add(flags); |
| 72 | |
| 73 | element.setUserActionElement(true); |
| 74 | } |
| 75 | |
| 76 | } |
| 77 | |