| 1 | /* |
| 2 | * Copyright (C) 2016 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 "StyleUpdate.h" |
| 28 | |
| 29 | #include "ComposedTreeAncestorIterator.h" |
| 30 | #include "Document.h" |
| 31 | #include "Element.h" |
| 32 | #include "NodeRenderStyle.h" |
| 33 | #include "RenderElement.h" |
| 34 | #include "Text.h" |
| 35 | |
| 36 | namespace WebCore { |
| 37 | namespace Style { |
| 38 | |
| 39 | Update::Update(Document& document) |
| 40 | : m_document(document) |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | const ElementUpdates* Update::elementUpdates(const Element& element) const |
| 45 | { |
| 46 | auto it = m_elements.find(&element); |
| 47 | if (it == m_elements.end()) |
| 48 | return nullptr; |
| 49 | return &it->value; |
| 50 | } |
| 51 | |
| 52 | ElementUpdates* Update::elementUpdates(const Element& element) |
| 53 | { |
| 54 | auto it = m_elements.find(&element); |
| 55 | if (it == m_elements.end()) |
| 56 | return nullptr; |
| 57 | return &it->value; |
| 58 | } |
| 59 | |
| 60 | const TextUpdate* Update::textUpdate(const Text& text) const |
| 61 | { |
| 62 | auto it = m_texts.find(&text); |
| 63 | if (it == m_texts.end()) |
| 64 | return nullptr; |
| 65 | return &it->value; |
| 66 | } |
| 67 | |
| 68 | const RenderStyle* Update::elementStyle(const Element& element) const |
| 69 | { |
| 70 | if (auto* updates = elementUpdates(element)) |
| 71 | return updates->update.style.get(); |
| 72 | auto* renderer = element.renderer(); |
| 73 | if (!renderer) |
| 74 | return nullptr; |
| 75 | return &renderer->style(); |
| 76 | } |
| 77 | |
| 78 | RenderStyle* Update::elementStyle(const Element& element) |
| 79 | { |
| 80 | if (auto* updates = elementUpdates(element)) |
| 81 | return updates->update.style.get(); |
| 82 | auto* renderer = element.renderer(); |
| 83 | if (!renderer) |
| 84 | return nullptr; |
| 85 | return &renderer->mutableStyle(); |
| 86 | } |
| 87 | |
| 88 | void Update::addElement(Element& element, Element* parent, ElementUpdates&& elementUpdates) |
| 89 | { |
| 90 | ASSERT(!m_elements.contains(&element)); |
| 91 | ASSERT(composedTreeAncestors(element).first() == parent); |
| 92 | |
| 93 | addPossibleRoot(parent); |
| 94 | m_elements.add(&element, WTFMove(elementUpdates)); |
| 95 | } |
| 96 | |
| 97 | void Update::addText(Text& text, Element* parent, TextUpdate&& textUpdate) |
| 98 | { |
| 99 | ASSERT(!m_texts.contains(&text)); |
| 100 | ASSERT(composedTreeAncestors(text).first() == parent); |
| 101 | |
| 102 | addPossibleRoot(parent); |
| 103 | m_texts.add(&text, WTFMove(textUpdate)); |
| 104 | } |
| 105 | |
| 106 | void Update::addText(Text& text, TextUpdate&& textUpdate) |
| 107 | { |
| 108 | addText(text, composedTreeAncestors(text).first(), WTFMove(textUpdate)); |
| 109 | } |
| 110 | |
| 111 | void Update::addPossibleRoot(Element* element) |
| 112 | { |
| 113 | if (!element) { |
| 114 | m_roots.add(&m_document); |
| 115 | return; |
| 116 | } |
| 117 | if (m_elements.contains(element)) |
| 118 | return; |
| 119 | m_roots.add(element); |
| 120 | } |
| 121 | |
| 122 | } |
| 123 | } |
| 124 | |