| 1 | /* |
| 2 | * Copyright (C) 2017 Apple 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 |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | #include "RenderTreeUpdaterGeneratedContent.h" |
| 28 | |
| 29 | #include "ContentData.h" |
| 30 | #include "InspectorInstrumentation.h" |
| 31 | #include "PseudoElement.h" |
| 32 | #include "RenderDescendantIterator.h" |
| 33 | #include "RenderElement.h" |
| 34 | #include "RenderImage.h" |
| 35 | #include "RenderQuote.h" |
| 36 | #include "RenderTreeUpdater.h" |
| 37 | #include "StyleTreeResolver.h" |
| 38 | |
| 39 | namespace WebCore { |
| 40 | |
| 41 | RenderTreeUpdater::GeneratedContent::GeneratedContent(RenderTreeUpdater& updater) |
| 42 | : m_updater(updater) |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | void RenderTreeUpdater::GeneratedContent::updateRemainingQuotes() |
| 47 | { |
| 48 | if (!m_updater.renderView().hasQuotesNeedingUpdate()) |
| 49 | return; |
| 50 | updateQuotesUpTo(nullptr); |
| 51 | m_previousUpdatedQuote = nullptr; |
| 52 | m_updater.renderView().setHasQuotesNeedingUpdate(false); |
| 53 | } |
| 54 | |
| 55 | void RenderTreeUpdater::GeneratedContent::updateQuotesUpTo(RenderQuote* lastQuote) |
| 56 | { |
| 57 | auto quoteRenderers = descendantsOfType<RenderQuote>(m_updater.renderView()); |
| 58 | auto it = m_previousUpdatedQuote ? ++quoteRenderers.at(*m_previousUpdatedQuote) : quoteRenderers.begin(); |
| 59 | auto end = quoteRenderers.end(); |
| 60 | for (; it != end; ++it) { |
| 61 | auto& quote = *it; |
| 62 | // Quote character depends on quote depth so we chain the updates. |
| 63 | quote.updateRenderer(m_updater.m_builder, m_previousUpdatedQuote.get()); |
| 64 | m_previousUpdatedQuote = makeWeakPtr(quote); |
| 65 | if ("e == lastQuote) |
| 66 | return; |
| 67 | } |
| 68 | ASSERT(!lastQuote); |
| 69 | } |
| 70 | |
| 71 | static void createContentRenderers(RenderTreeBuilder& builder, RenderElement& pseudoRenderer, const RenderStyle& style) |
| 72 | { |
| 73 | ASSERT(style.contentData()); |
| 74 | |
| 75 | for (const ContentData* content = style.contentData(); content; content = content->next()) { |
| 76 | auto child = content->createContentRenderer(pseudoRenderer.document(), style); |
| 77 | if (pseudoRenderer.isChildAllowed(*child, style)) |
| 78 | builder.attach(pseudoRenderer, WTFMove(child)); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | static void updateStyleForContentRenderers(RenderElement& pseudoRenderer, const RenderStyle& style) |
| 83 | { |
| 84 | for (auto& contentRenderer : descendantsOfType<RenderElement>(pseudoRenderer)) { |
| 85 | // We only manage the style for the generated content which must be images or text. |
| 86 | if (!is<RenderImage>(contentRenderer) && !is<RenderQuote>(contentRenderer)) |
| 87 | continue; |
| 88 | contentRenderer.setStyle(RenderStyle::createStyleInheritingFromPseudoStyle(style)); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | void RenderTreeUpdater::GeneratedContent::updatePseudoElement(Element& current, const Optional<Style::ElementUpdate>& update, PseudoId pseudoId) |
| 93 | { |
| 94 | PseudoElement* pseudoElement = pseudoId == PseudoId::Before ? current.beforePseudoElement() : current.afterPseudoElement(); |
| 95 | |
| 96 | if (auto* renderer = pseudoElement ? pseudoElement->renderer() : nullptr) |
| 97 | m_updater.renderTreePosition().invalidateNextSibling(*renderer); |
| 98 | |
| 99 | if (!needsPseudoElement(update)) { |
| 100 | if (pseudoElement) { |
| 101 | if (pseudoId == PseudoId::Before) |
| 102 | removeBeforePseudoElement(current, m_updater.m_builder); |
| 103 | else |
| 104 | removeAfterPseudoElement(current, m_updater.m_builder); |
| 105 | } |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | RefPtr<PseudoElement> newPseudoElement; |
| 110 | if (!pseudoElement) { |
| 111 | newPseudoElement = PseudoElement::create(current, pseudoId); |
| 112 | pseudoElement = newPseudoElement.get(); |
| 113 | } |
| 114 | |
| 115 | if (update->change == Style::NoChange) |
| 116 | return; |
| 117 | |
| 118 | if (newPseudoElement) { |
| 119 | if (pseudoId == PseudoId::Before) |
| 120 | current.setBeforePseudoElement(newPseudoElement.releaseNonNull()); |
| 121 | else |
| 122 | current.setAfterPseudoElement(newPseudoElement.releaseNonNull()); |
| 123 | } |
| 124 | |
| 125 | if (update->style->display() == DisplayType::Contents) { |
| 126 | // For display:contents we create an inline wrapper that inherits its |
| 127 | // style from the display:contents style. |
| 128 | auto contentsStyle = RenderStyle::createPtr(); |
| 129 | contentsStyle->setStyleType(pseudoId); |
| 130 | contentsStyle->inheritFrom(*update->style); |
| 131 | contentsStyle->copyContentFrom(*update->style); |
| 132 | |
| 133 | Style::ElementUpdate contentsUpdate { WTFMove(contentsStyle), update->change, update->recompositeLayer }; |
| 134 | m_updater.updateElementRenderer(*pseudoElement, contentsUpdate); |
| 135 | pseudoElement->storeDisplayContentsStyle(RenderStyle::clonePtr(*update->style)); |
| 136 | } else { |
| 137 | m_updater.updateElementRenderer(*pseudoElement, *update); |
| 138 | ASSERT(!pseudoElement->hasDisplayContents()); |
| 139 | } |
| 140 | |
| 141 | auto* pseudoElementRenderer = pseudoElement->renderer(); |
| 142 | if (!pseudoElementRenderer) |
| 143 | return; |
| 144 | |
| 145 | if (update->change == Style::Detach) |
| 146 | createContentRenderers(m_updater.m_builder, *pseudoElementRenderer, *update->style); |
| 147 | else |
| 148 | updateStyleForContentRenderers(*pseudoElementRenderer, *update->style); |
| 149 | |
| 150 | if (m_updater.renderView().hasQuotesNeedingUpdate()) { |
| 151 | for (auto& child : descendantsOfType<RenderQuote>(*pseudoElementRenderer)) |
| 152 | updateQuotesUpTo(&child); |
| 153 | } |
| 154 | m_updater.m_builder.updateAfterDescendants(*pseudoElementRenderer); |
| 155 | } |
| 156 | |
| 157 | bool RenderTreeUpdater::GeneratedContent::needsPseudoElement(const Optional<Style::ElementUpdate>& update) |
| 158 | { |
| 159 | if (!update) |
| 160 | return false; |
| 161 | if (!m_updater.renderTreePosition().parent().canHaveGeneratedChildren()) |
| 162 | return false; |
| 163 | if (!pseudoElementRendererIsNeeded(update->style.get())) |
| 164 | return false; |
| 165 | return true; |
| 166 | } |
| 167 | |
| 168 | void RenderTreeUpdater::GeneratedContent::removeBeforePseudoElement(Element& element, RenderTreeBuilder& builder) |
| 169 | { |
| 170 | auto* pseudoElement = element.beforePseudoElement(); |
| 171 | if (!pseudoElement) |
| 172 | return; |
| 173 | tearDownRenderers(*pseudoElement, TeardownType::Full, builder); |
| 174 | element.clearBeforePseudoElement(); |
| 175 | } |
| 176 | |
| 177 | void RenderTreeUpdater::GeneratedContent::removeAfterPseudoElement(Element& element, RenderTreeBuilder& builder) |
| 178 | { |
| 179 | auto* pseudoElement = element.afterPseudoElement(); |
| 180 | if (!pseudoElement) |
| 181 | return; |
| 182 | tearDownRenderers(*pseudoElement, TeardownType::Full, builder); |
| 183 | element.clearAfterPseudoElement(); |
| 184 | } |
| 185 | |
| 186 | } |
| 187 | |