| 1 | /* |
| 2 | * Copyright (C) 2012 Google Inc. All rights reserved. |
| 3 | * Copyright (C) 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 "PseudoElement.h" |
| 30 | |
| 31 | #include "CSSAnimationController.h" |
| 32 | #include "ContentData.h" |
| 33 | #include "DocumentTimeline.h" |
| 34 | #include "InspectorInstrumentation.h" |
| 35 | #include "RenderElement.h" |
| 36 | #include "RenderImage.h" |
| 37 | #include "RenderQuote.h" |
| 38 | #include "RuntimeEnabledFeatures.h" |
| 39 | #include "StyleResolver.h" |
| 40 | #include <wtf/IsoMallocInlines.h> |
| 41 | |
| 42 | namespace WebCore { |
| 43 | |
| 44 | WTF_MAKE_ISO_ALLOCATED_IMPL(PseudoElement); |
| 45 | |
| 46 | const QualifiedName& pseudoElementTagName() |
| 47 | { |
| 48 | static NeverDestroyed<QualifiedName> name(nullAtom(), "<pseudo>" , nullAtom()); |
| 49 | return name; |
| 50 | } |
| 51 | |
| 52 | String PseudoElement::pseudoElementNameForEvents(PseudoId pseudoId) |
| 53 | { |
| 54 | static NeverDestroyed<const String> after(MAKE_STATIC_STRING_IMPL("::after" )); |
| 55 | static NeverDestroyed<const String> before(MAKE_STATIC_STRING_IMPL("::before" )); |
| 56 | switch (pseudoId) { |
| 57 | case PseudoId::After: |
| 58 | return after; |
| 59 | case PseudoId::Before: |
| 60 | return before; |
| 61 | default: |
| 62 | return emptyString(); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | PseudoElement::PseudoElement(Element& host, PseudoId pseudoId) |
| 67 | : Element(pseudoElementTagName(), host.document(), CreatePseudoElement) |
| 68 | , m_hostElement(&host) |
| 69 | , m_pseudoId(pseudoId) |
| 70 | { |
| 71 | ASSERT(pseudoId == PseudoId::Before || pseudoId == PseudoId::After); |
| 72 | setHasCustomStyleResolveCallbacks(); |
| 73 | } |
| 74 | |
| 75 | PseudoElement::~PseudoElement() |
| 76 | { |
| 77 | ASSERT(!m_hostElement); |
| 78 | } |
| 79 | |
| 80 | Ref<PseudoElement> PseudoElement::create(Element& host, PseudoId pseudoId) |
| 81 | { |
| 82 | auto pseudoElement = adoptRef(*new PseudoElement(host, pseudoId)); |
| 83 | |
| 84 | InspectorInstrumentation::pseudoElementCreated(host.document().page(), pseudoElement.get()); |
| 85 | |
| 86 | return pseudoElement; |
| 87 | } |
| 88 | |
| 89 | void PseudoElement::clearHostElement() |
| 90 | { |
| 91 | InspectorInstrumentation::pseudoElementDestroyed(document().page(), *this); |
| 92 | |
| 93 | if (auto* timeline = document().existingTimeline()) |
| 94 | timeline->removeAnimationsForElement(*this); |
| 95 | if (auto* frame = document().frame()) |
| 96 | frame->animation().cancelAnimations(*this); |
| 97 | |
| 98 | m_hostElement = nullptr; |
| 99 | } |
| 100 | |
| 101 | bool PseudoElement::rendererIsNeeded(const RenderStyle& style) |
| 102 | { |
| 103 | return pseudoElementRendererIsNeeded(&style); |
| 104 | } |
| 105 | |
| 106 | } // namespace |
| 107 | |